Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

content-length when using http compression

Tags:

http

The client is making a range request 0-1023 to the http server. It prefers gzip compression with Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0 in the request.

What would be the content-length in the response header? Will it be 1024 or the size of the compressed data.

Thanks,

like image 359
derekwei Avatar asked Sep 29 '10 06:09

derekwei


People also ask

Is Content Length compressed?

The Content-Length header indicates the size of the entity body in the message, in bytes. The size includes any content encodings (the Content-Length of a gzip-compressed text file will be the compressed size, not the original size).

Is Content Length required in HTTP?

For compatibility with HTTP/1.0 applications, HTTP/1.1 requests containing a message-body MUST include a valid Content-Length header field unless the server is known to be HTTP/1.1 compliant.

What is Content Length in HTTP response?

The Content-Length header indicates the size of the message body, in bytes, sent to the recipient.

How is HTTP Content Length calculated?

As bytes are represented with two hexadecimal digits, one can divide the number of digits by two to obtain the content length (There are 12 hexadecimal digits in "48656c6c6f21" which equates to six bytes, as indicated in the header "Content-Length: 6" sent from the server).


2 Answers

It depends on the Content-Encoding.

RFC 2616 has this to say (amongst other things) about Content-Length:

Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4.

So we have to figure out what transfer-length is; Section 4.4 (Message Length) says these two things about transfer-length:

The transfer-length of a message is the length of the message-body as it appears in the message; that is, after any transfer-codings have been applied.

If a Content-Length header field (section 14.13) is present, its decimal value in OCTETs represents both the entity-length and the transfer-length. The Content-Length header field MUST NOT be sent if these two lengths are different

Okay, so we know that in this case transfer-length, entity-length, and Content-Length all have the same value, and all refer to "the length of the message-body as it appears in the message", and so we have to determine what message-body is. Section 4.3 says this about message-body:

The message-body (if any) of an HTTP message is used to carry the entity-body associated with the request or response."

So what's an entity-body? For that you have to refer to basically all of Section 7. (Which also defines entity-length.) Most importantly, there this:

entity-body := Content-Encoding( Content-Type( data ) )

The length of the entity-body (and therefore our value for Content-Length per 4.4) is the length of the data after content-coding.

like image 57
4 revs, 2 users 89% Avatar answered Nov 09 '22 12:11

4 revs, 2 users 89%


The actual content length depends on the transfer encoding and data: If you use identity, no compression is applied and the content length is 1024; if you use gzip, the actual content length depends on the data that is to be compressed.

like image 39
Gumbo Avatar answered Nov 09 '22 13:11

Gumbo