Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending estimate HTTP Content-Length from Servlet etc

I often need to generate content dynamically from a servlet / restlet or whatever, and don't know the length ahead of time. If the client is a browser, the progress bar doesn't work properly, because I haven't set the Content-Length header. Is there any way of setting an estimated content length, so the progress bar works "more or less"?

like image 869
Jim Downing Avatar asked Oct 12 '09 08:10

Jim Downing


People also ask

How do I set the Content Length in an HTTP request?

In PHP you would use something like this. header("Content-Length: ". filesize($filename)); In case of "Content-Type: application/x-www-form-urlencoded" the encoded data is sent to the processing agent designated so you can set the length or size of the data you are going to post.

How is HTTP Content Length calculated?

As bytes are represented with two hexadecimal digits, one can divide the number of digits by two to obtain the content length (There are 12 hexadecimal digits in "48656c6c6f21" which equates to six bytes, as indicated in the header "Content-Length: 6" sent from the server).

How do I send Content header Length?

To manually pass the Content-Length header, you need to add the Content-Length: [length] and Content-Type: [mime type] headers to your request, which describe the size and type of data in the body of the POST request.

What is HTTP Content Length?

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.


1 Answers

No, the Content-Length value must be the exact content length:

When a Content-Length is given in a message where a message-body is allowed, its field value MUST exactly match the number of OCTETs in the message-body. HTTP/1.1 user agents MUST notify the user when an invalid length is received and detected.

So you cannot send just an estimated Content-Length value to get a progress bar.

like image 187
Gumbo Avatar answered Oct 23 '22 08:10

Gumbo