The HTTP/1.0 specs state that the server can indicate the length of a response containing an entity body by
Content-Length
header, orSo the Content-Length
header, for normal responses like an image, Html page or Ajax data is optional.
As that header has been provided there are cases in which using it give some benefits.
So the question is: what are those cases and what are the benefits?
(For example: if the response is a file that is being downloaded the browser displays a progress bar if Content-Length
is specified.)
When the content length is predictable, communicating it to the client helps, because then the required memory and disk space can be allocated at once, allowing for a more efficient transfer, and also an early abortion if the required resources are not available to the client.
This optimizes the transfer, which benefits both the server and the client.
When the content length cannot be predicted (e.g. because it is dynamically generated) these optimizations are obviously not possible.