在没有 Content-Length 或 Transfer-encoding 的情况下,HTTP 对 GET 请求的响应会发生什么?
What happens in HTTP response to a GET request without Content-Length or Transfer-encoding?
如果对 GET 请求的响应没有 Content-Length 或 Transfer-encoding: chunked 字段,会发生什么?客户端如何知道消息何时结束?
根据规范
The length of a message body is determined by one of the following (in
order of precedence):
- [...]
- Otherwise, this is a response message without a declared message body length, so the message body length is determined by the number of octets received prior to the server closing the connection.
客户端需要检测连接关闭
RFC 7230 section 3.3.3 包含一个很好的条件清单,用于查找消息 body 大小。本质上它说答案取决于状态代码是什么。相关条件是#1 和#7。
"1. Any response ... with a 1xx
(Informational), 204 (No Content), or 304 (Not Modified) status
code is always terminated by the first empty line after the
header fields, regardless of the header fields present in the
message, and thus cannot contain a message body."
"7. Otherwise, this is a response message without a declared message
body length, so the message body length is determined by the
number of octets received prior to the server closing the
connection."
还值得注意的是,消息可能会在 body 部分结束后继续。 RFC 7230 section 4.4 定义了一个 Trailers 功能,其中有效负载后面可能跟有第二组 mime headers。如果存在,则消息在它们所在的位置结束。
如果对 GET 请求的响应没有 Content-Length 或 Transfer-encoding: chunked 字段,会发生什么?客户端如何知道消息何时结束?
根据规范
The length of a message body is determined by one of the following (in order of precedence):
- [...]
- Otherwise, this is a response message without a declared message body length, so the message body length is determined by the number of octets received prior to the server closing the connection.
客户端需要检测连接关闭
RFC 7230 section 3.3.3 包含一个很好的条件清单,用于查找消息 body 大小。本质上它说答案取决于状态代码是什么。相关条件是#1 和#7。
"1. Any response ... with a 1xx (Informational), 204 (No Content), or 304 (Not Modified) status code is always terminated by the first empty line after the header fields, regardless of the header fields present in the message, and thus cannot contain a message body."
"7. Otherwise, this is a response message without a declared message body length, so the message body length is determined by the number of octets received prior to the server closing the connection."
还值得注意的是,消息可能会在 body 部分结束后继续。 RFC 7230 section 4.4 定义了一个 Trailers 功能,其中有效负载后面可能跟有第二组 mime headers。如果存在,则消息在它们所在的位置结束。