开发者

Sending estimate HTTP Content-Length from Servlet etc

开发者 https://www.devze.com 2022-12-08 11:49 出处:网络
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, becaus

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开发者_JS百科 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"?


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.


While I wouldn't recommend doing it you can set the content-length to an estimate of what the content is, and you will get a progress bar. As long as you can guarantee that your estimate is equal to or greater than the actual content this will generally work. If your actual content is greater than the estimate than the content will be truncated to the specified content-length.

I tested this in Firefox, IE, and Chrome without issue. The HTTP spec states that users agents MUST notify the user if the specified length doesn't match the actual length, but I have not observed this behavior with any browser I've tested.

I investigated this as an option but abandoned it due to the potential unforeseen conflicts for playing outside the bounds of the specification.


Not clear what you're asking. You can always set the Content-Length header yourself, though it needs to actually match the amount of data you're sending. The standard way to handle dynamic data where you don't know anything about the length ahead of time is to buffer the output, find the actual length, set the header, then dump the output. Not really answering what you appear to be asking, but I think what you're asking is impossible.


The only way to do this (as decribed in an RFC) is not to set the Content-Length header, ie. the response header does not contain a Content-Length line. In this case the browser does not know how long the body is, so the server "tells" the browser that all of the body has been sent by closing the connection.

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

To answer your question: I do not think that it is possible to give the browser an estimate.


If the length of your content is not known beforehand you can use the “chunked” content encoding (as per HTTP version 1.1). This will not solve your progress bar problem, though—and there’s just no way to make it work unless you know how much content you are going to send.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号