Article 5: Content-Length and chunked encoding in HTTP

Source: Internet
Author: User
Reprinted please indicate the source http://blog.csdn.net/yankai0219/article/details/8269922
0. Sort 1. fields related to chunked encoding in HTTP/1.1 protocol
1) entity body2) Content-length3) Message length4) The role of the Content-Length Field
2. Chunked Encoding
1) Definition 2) Description: 3) Format: 4) Programmatic representation of Chunked Encoding
0. OrderWhen studying the response process of Baidu cloud disk, we found that its response adopts the chunked encoding format and there is no Content-Length field. Because of project requirements, we need to study the chunked encoding in HTTP/1.1 protocol. First, several concepts related to chunked encoding are introduced, which leads to chunked encoding. 1. fields related to chunked encoding in HTTP/1.1 1) entity body: Entity-body only appears when message-body appears. The entity-body is obtained by decoding the message-body. Transfer-encoding is used to ensure proper transmission of Security and Information. Entity-length: the length of the message-body before applying any transfer-encoding. That is, there is no length of the message-body Before encoding. 2) Content-Length: used to describe the transmission length of an HTTP message entity. (
Transfer-length of the message-Body)
Message Body Length: entity-length. The length of the message body before compression. The transmission length of the message body is Content-Length, and the length of the compressed message-body.
3) message length: This part of the explanation must look at Daniel's interpretation http://blog.xiuwz.com/tag/content-length/
The following content is from idea:
  • 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 ". Advantages of Translation,In fact, the key point is: with transfer-encoding, there cannot be Content-Length.
  • 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.

The important points are as follows: If transfer-encoding exists, Content-Length is not allowed in the header, and Content-Length is ignored. If Content-Length exists and is valid, it must be exactly the same as the transmission length of the message content. 4) function of the Content-Length FieldConent-Length indicates the object content length, The client (server) can determine whether the data is received based on this value.. But if no conent-length exists in the message, how can we determine it? Under what circumstances will there be no conent-length? When no Content-Length exists, how does the client determine whether the data is received successfully?1) static page or image: when the client requests a static page or image from the server, the server can clearly understand the content size, then, you can use the Content-Length message header field to tell the client how much data to receive. 2) dynamic page: if it is a dynamic page, the server cannot know the content size in advance. In this case, you can use the transfer-encoding: Chunk mode to transmit data. That is to say, if you want to generate data while sending data to the client, the server needs to replace Content-Length With "transfer-encoding: chunked. The purpose of using transfer-EncodingData is generated and sent to the client. 2. Chunked Encoding 1) DefinitionChunked Transfer Encoding is a data transmission mechanism in Hypertext Transfer Protocol (HTTP). It allows HTTP to be sent to client applications by Web servers (usually Web browsers) data can be divided into multiple parts. Multipart Transfer Encoding is only available in HTTP 1.1 (HTTP/1.1. 2) Description: Generally, the data sent in an HTTP response message is sent as a whole, and the Content-Length header field indicates the Data Length. The length of the data is very important, because the client needs to know where the end of the Response Message and the start of the subsequent response message. However, the data is divided into a series of data blocks and sent in one or more blocks, so that the server can send data without having to know the total size of the sent content in advance. 3) format:If the value of the transfer-encoding header of an HTTP message (request message or response message) is chunked, the message body consists of unfixed blocks, end with the last block of 0. Each non-empty block starts with the number of bytes (in hexadecimal notation) of the block containing data, followed by a CRLF (carriage return and line feed), followed by the data itself, the last CRLF ends. In some implementations, the block size and CRLF are filled with white spaces (0x20 ). The last one is a single row, with the block size (0), some optional white spaces, and CRLF. The last part no longer contains any data, but it can send an optional tail, including the message header field. The message ends with a CRLF. Chunk Encoding splits data into one block. Chunked encoding is concatenated by several chunks. The length is 0.. Each Chunk is divided into two parts: the header and the body. The content of the header specifies the total number of characters in the body ( Hexadecimal number) And the number unit (generally not written). The body part is the actual content of the specified length. Carriage return line feed (CRLF). In the last chunk with a length of 0, the content is called footer, which is some additional header information (which can be ignored directly ).

The Chunk encoding format is as follows:

Chunked-Body = *Chunk
"0" CRLF
Footer
CRLF
Chunk = chunk-size [chunk-ext] CRLF
Chunk-data CRLF

Hex-no-zero =

Chunk-size = hex-no-zero * hex
Chunk-ext = * (";" chunk-ext-name ["=" chunk-ext-value])
Chunk-ext-name = token
Chunk-ext-val = token | quoted-string
Chunk-Data = chunk-size (octet)

Footer = * entity-Header

The Chunk Encoding consists of four parts: 1,0 up to chunk Blocks, 2,"0" CRLF, 3,Footer, 4,CRLF.Each chunk consists of chunk-size, chunk-ext (optional), CRLF, chunk-data, and CRLF.

4) Programmatic representation of chunked encoding (1) c ++ see http://wuhua.iteye.com/blog/673841 (2) C Language

Char* Chunkpart1 = "42 \ r \ n ";Char* Chunkpart2 =
Tmpuchar_body_data;Char* Chunkpart3 = "\ r \ N0 \ r \ n ";IntChunklen = 0; chunklen =Strlen(Chunkpart1)
+Strlen(Chunkpart2) +Strlen(Chunkpart3 );Char* Chunk = (Char*) Ngx_pcalloc (R-> pool, chunklen + 1 );Strncpy(Chunk, chunkpart1,Strlen(Chunkpart1 ));Strncpy(Chunk +Strlen(Chunkpart1), chunkpart2,Strlen(Chunkpart2)
);Strncpy(Chunk +Strlen(Chunkpart1)
+Strlen(Chunkpart2), chunkpart3,Strlen(Chunkpart3 ));
Reference Article 1) http://zh.wikipedia.org/wiki/%E5%88%86%E5%9D%97%E4%BC%A0%E8%BE%93%E7%BC%96%E7%A0%812) http://blog.xiuwz.com/tag/content-length/3) http://wuhua.iteye.com/blog/673841

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.