Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chunked encoding and content-length header

Is it possible to set the content-length header and also use chunked transfer encoding? and does doing so solve the problem of not knowing the length of the response at the client side when using chunked?

the scenario I'm thinking about is when you have a large file to transfer and there's no problem in determining its size, but it's too large to be buffered completely. (If you're not using chunked, then the whole response must get buffered first? Right??)

thanks.

like image 994
p00ya00 Avatar asked Jul 21 '10 21:07

p00ya00


People also ask

What is content-Length in header?

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.

Is the content-Length header necessary?

The Content-Length header is mandatory for messages with entity bodies, unless the message is transported using chunked encoding. Content-Length is needed to detect premature message truncation when servers crash and to properly segment messages that share a persistent connection.

How do I change the content-Length in a header?

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.

What is a transfer encoding header?

The Transfer-Encoding header specifies the form of encoding used to safely transfer the payload body to the user. Note: HTTP/2 doesn't support HTTP 1.1's chunked transfer encoding mechanism, as it provides its own, more efficient, mechanisms for data streaming.


1 Answers

  1. No:

"Messages MUST NOT include both a Content-Length header field and a non-identity transfer-coding. If the message does include a non-identity transfer-coding, the Content-Length MUST be ignored." (RFC 2616, Section 4.4)

  1. And no, you can use Content-Length and stream; the protocol doesn't constrain how your implementation works.
like image 79
Julian Reschke Avatar answered Sep 28 '22 11:09

Julian Reschke