问题描述
即使if不包含Content-Length或Transfer-Encoding,HTTP响应标头(如下所示)也是合法的吗?
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
我希望看到Content-Length或Transfer-Encoding,但它们都不存在。
I expected to see either Content-Length or Transfer-Encoding, but none of them exist.
我读了HTTP-RFC但是仍然不确定。
I read the HTTP-RFC but am still unsure.
@CodeCaster,我确实阅读了RFC第4.4节,但我仍然不清楚,正如你所看到的,响应头用于返回一个js在流上,所以:
@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:
- 第4.4节,规则1定义不得包含消息体,似乎不适用于我的情况。
- 第4.4节,规则4,对此不确定,但由于我在响应标题中没有看到multipart / byteranges - 这是否意味着此规则不适用于我?
- 第4.4节规则5,这对我来说大部分都不清楚,因为标题实际包含连接:关闭,它是否相关?
那么,还有什么进一步的评论吗?
So, any further comments?
推荐答案
是的,它是有效的。有五种方法可以确定消息长度:
Yes, it is valid. There are five ways to determine the 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 *,204和304响应的
以及对HEAD $的任何响应b $ b请求)总是在
头字段之后的第一个空行终止,而不管消息中
中是否存在实体头字段。
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.
如果存在Transfer-Encoding标头字段(第14.41节)且
具有identity以外的任何值,则传输长度为
,使用chunked定义转移编码(第3.6节),
,除非通过关闭连接终止消息。
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.
如果是Content-Length标头字段(部分) 14.13)存在,其在OCTET中的
十进制值表示实体长度和
传输长度。如果这两个长度不同(即,如果存在Transfer-Encoding
头字段),则Content-Length头字段不得发送
。如果收到带有
Transfer-Encoding标头字段和Content-Length标头字段的消息,则必须忽略
后者。
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.
如果消息使用媒体类型multipart / byteranges,并且未指定
transfer-length,则此self-
分隔媒体类型定义传输长度。除非发件人知道收件人可以解析
,否则不会使用此媒体类型
[M] UST;来自1.1客户端的多个byte-
范围说明符的Range标头请求的存在意味着客户端可以解析
multipart / byteranges响应。
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.
范围标题可能由1.0代理转发,而不是
了解multipart / byteranges;在这种情况下,服务器必须使用
本节1,3或5中定义的方法来定界消息。
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.
By服务器关闭连接。 (关闭连接
不能用于表示请求正文的结束,因为
不会让服务器发回响应。)
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.)
这篇关于HTTP响应头有效,没有Transfer-Encoding和Content-Length?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!