user3890756 user3890756 - 2 years ago 115
HTTP Question

Serving large PDF, should I set content-length?

I create pdf document dynamically and want to serve them in my handler. I set the content-type to application/pdf and it works fine. I run my server through nginx proxy.

My problem is that some requests generate a lot of other requests for the same doc. I looked at the headers and seen that it want a Chunked transfer encoding.

My solution was to set the content-length and it seems to works fine.

I wonder if it's enough and why I never had to do it with simple html page.

Answer Source

A comment in the source code says:

If the handler didn't declare a Content-Length up front, we either go into chunking mode or, if the handler finishes running before the chunking buffer size, we compute a Content-Length and send that in the header instead.

If you want to avoid chunking, then set the content length. Setting the content length for a large response does reduce the amount of data transferred and can reduce copying within the HTTP server.

As a rule of thumb, set the content length if the length is known in advance of producing the response body.

Your simple HTML pages may be smaller than the chunking buffer size. If so, they were not chunked.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download