Content-Length and content-length in Http
In the HTTP protocol, a detailed explanation of Content-Length is provided. Content-Length is used to describe the transmission length of the HTTP message entity the transfer-Length of the message-body. In HTTP, the message entity length is different from the message entity transmission length. For example, in gzip compression, the message entity length is the length before compression, the transmission length of the message entity is the length after gzip compression.
In specific HTTP interactions, how does the client obtain the message length based on the following rules:
If the response is 1xx, 204,304, or head request, the message entity content is ignored directly.
If Transfer-Encoding exists, the corresponding length is obtained first using the method in Transfer-Encoding. For example, the Chunked mode.
"If the header contains Content-Length, the Content-Length indicates both the object Length and the transmission Length. If the object Length is not the same as the transmission Length (for example, Transfer-Encoding), the Content-Length cannot be set. If Transfer-Encoding is set, Content-Length will be ignored ". The key aspect of the translation is that with Transfer-Encoding, Content-Length is not allowed.
Range Transmission. Don't pay attention to it. I haven't read it in detail :)
You can determine the message transmission length by closing the connection on the server. (The requester cannot end the Request Message Body by closing the connection, because this prevents the server from continuing to respond ). In this case, short connections are used, that is, non-keep-alive mode.
HTTP1.1 must support the chunk mode. When the message length is not determined, the chunk mechanism can be used to handle this situation.
In the header containing the message content, if the content-length field exists, the corresponding value of this field must completely match the length in the message topic.
"The entity-length of a message is the length of the message-body before any transfer-codings have been applied"
That is, content-length is not allowed if a chunk is used.
In fact, the following several items can be ignored. The summary is as follows:
1. If Content-Length exists and is valid, it must be exactly the same as the transmission Length of the message Content. (After testing, if it is too short, it will be truncated. If it is too long, it will lead to timeout .)
2. If Transfer-Encoding exists (the key is chunked), Content-Length cannot be included in the header, and Content-Length cannot be ignored.
3. If short connections are used, you can directly close the connection on the server to determine the message transmission length. (This is easy to understand)
Based on other features of HTTP, for example, keep alive is not supported before Http1.1. The following conclusions can be drawn:
1. In Http 1.0 and earlier versions, the content-length field is optional.
2. http1.1 and later versions. If it is keep alive, the content-length and chunk must be two. If it is not keep alive, it is the same as http1.0. Content-length is optional.