Submitting an HTTP Content-Length estimate from a servlet, etc.

I often have to dynamically generate content from a servlet / reslit or something else, and don’t know the length ahead of time. If the client is a browser, the progress bar does not work correctly because I did not set the Content-Length header. Is there a way to set the estimated length of the content, so the progress bar works "more or less"?

+4
source share
5 answers

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

When a Content-Length is specified in a message where the 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.

Thus, you cannot send only an approximate value of Content-Length to get a progress bar.

+7
source

Until I recommend doing this, you can set the length of the content to evaluate what the content is, and you will get a progress bar. As long as you can guarantee that your rating is equal to or greater than the actual content that will usually work. If your actual content is more rated than the content will be truncated to the specified content length.

I tested this in Firefox, IE and Chrome without any problems. The HTTP specification indicates that user agents MUST notify the user if the specified length does not match the actual length, but I have not observed this behavior with any browser that I tested.

I explored this as an option, but abandoned it because of potential unforeseen conflicts for a game outside of the specification.

+3
source

It is not clear what you are asking. You can always set the Content-Length header yourself, although it should actually match the amount of data sent. The standard way to process dynamic data when you know nothing about the duration in advance is to buffer the output, search for the actual length, set the header, and then reset the output. Not quite answering what you ask, but I think that what you ask is impossible.

+1
source

The only way to do this ( as described in the RFC ) is not to set the Content-Length header, i.e. The response header does not contain the string Content-Length. In this case, the browser does not know how long the body takes, so the server "tells" the browser that the entire body has been sent to close the connection .

I'm not sure if the Java container will automatically close the connection in this case or if you can do it yourself through some kind of filter.

To answer your question: I don’t think you can give the browser a rating.

0
source

If the length of your content is not known in advance, you can use the "chunked" encoding of the content (according to HTTP version 1.1). However, this will not solve the problem of your progress bar, and theres simply will not be able to make it work if you do not know how much content you are going to send.

0
source

All Articles