Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP response headers valid with no Transfer-Encoding and Content-Length?

Is a HTTP response Header (like the one below) legal even if if does not contain Content-Length or Transfer-Encoding?

- Http: Response, HTTP/1.1, Status: Ok, URL: /AAA/B.json
  ProtocolVersion: HTTP/1.1
  StatusCode: 200, Ok
  Reason: OK
  Expires:  Fri, 05 Oct 2012 01:41:30 GMT
  Date:  Fri, 05 Oct 2012 01:40:46 GMT
  Vary:  Accept-Encoding
  Accept-Ranges:  bytes
  Cache-Control:  public, max-age=43
  Server:  Noelios-Restlet-Engine/1.1.10
  ContentType:  application/json;charset=UTF-8
  ContentEncoding:  gzip
  Connection:  close
  X-Served-By:  85.111
  HeaderEnd: CRLF

I expected to see either Content-Length or Transfer-Encoding, but none of them exist.

I read the HTTP-RFC but am still unsure.

@CodeCaster, I did read RFC section 4.4, but am still not clear, as you can see, the response header is used to return a json stream, so:

  • section 4.4, rule 1 defines MUST NOT include a message-body, does not seem to apply to my case.
  • section 4.4, rule 4, not sure about this, but since I do not see "multipart/byteranges" in the response header - does it mean this rule is not applicable for me?
  • section 4.4 rule 5, this is mostly unclear to me since the header actual contain "Connection : close", is it related?

So, any further comments?

like image 568
user1721757 Avatar asked Oct 05 '12 02:10

user1721757


People also ask

Is Content-Length required in HTTP response?

A valid Content-Length field value is required on all HTTP/1.0 request messages containing an entity body.

Is the Content-Length header necessary in HTTP?

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.

Does HTTP 1.1 require Content-Length?

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.

Does HTTP response have headers?

An HTTP response header is a component of a network packet that is sent by a Web server to a Web browser or client machine in response to an HTTP request. It is used in Web communications to deliver webpage and other Web-based data from the server to the requesting end-user browsers.


1 Answers

Yes, it is valid. There are five ways to determine the message length:

RFC 2616 Section 4.4. Message 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. When a message-body is included with a message, the transfer-length of that body is determined by one of the following (in order of precedence):

  1. Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, and 304 responses and any response to a HEAD request) is always terminated by the first empty line after the header fields, regardless of the entity-header fields present in the message.
  1. If a Transfer-Encoding header field (section 14.41) is present and has any value other than "identity", then the transfer-length is defined by use of the "chunked" transfer-coding (section 3.6), unless the message is terminated by closing the connection.
  1. 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 (i.e., if a Transfer-Encoding header field is present). If a message is received with both a Transfer-Encoding header field and a Content-Length header field, the latter MUST be ignored.
  1. If the message uses the media type "multipart/byteranges", and the transfer-length is not otherwise specified, then this self- delimiting media type defines the transfer-length. This media type [M]UST NOT be used unless the sender knows that the recipient can parse it; the presence in a request of a Range header with multiple byte- range specifiers from a 1.1 client implies that the client can parse multipart/byteranges responses.
  A range header might be forwarded by a 1.0 proxy that does not
   understand multipart/byteranges; in this case the server MUST
   delimit the message using methods defined in items 1,3 or 5 of
   this section.
  1. By the server closing the connection. (Closing the connection cannot be used to indicate the end of a request body, since that would leave no possibility for the server to send back a response.)
like image 92
CodeCaster Avatar answered Nov 15 '22 08:11

CodeCaster