Transfer-encoding: Meaning of chunked

Source: Internet
Author: User

Transfer-encoding: chunked indicates that the length of the output content cannot be determined. This is basically not used for common static pages, images, and other such pages.

However, dynamic pages may be used, but I also noticed that most of ASP, PHP, and Asp.net dynamic page outputs use Content-Length instead of transfer-encoding: chunked.

However, if combined:Content-encoding: GzipIn use, transfer-encoding: chunked is useful.

Remember the previous implementation:Content-encoding: When Gzip is output, first write the entire compressed data to a large byte array (such as bytearrayoutputstream), and then get the array size-> Content-Length.

If it is used in combination with transfer-encoding: chunked, you do not have to apply for a large byte array. You can use a single piece of output, which is more scientific and consumes less resources.

This is also a common field in the HTTP protocol, which is used in the HTTP transmission process. The reason is that the length of the HTTP Server Response Message is often unpredictable, object Search Using Content-Length does not always work.

The block technology means that entities are divided into many blocks, that is, the data at the application layer. TCP does not explain them during transmission, instead, we understand all the data produced at the application layer as binary streams, and then cut them into one point based on the length of the MSS. One brain is inserted into the TCP protocol stack, the specific interpretation of these binary data requires the application layer. Therefore, before that, the data at the entire application layer needs to wait for all the TCP segments it divides to reach the other party, after re-assembly, the applicationProgramYou can use your own decoding method to restore them.

Http1.1 adopts a persistent connection, that is, a TCP connection is not immediately released, allowing many requests and responses to be sent over a TCP connection, therefore, the client and server need some way to indicate where a packet ends and where the next packet starts. The simple method is to use Content-Length, but this only works when the message length can be pre-determined, when dynamic content or the length cannot be determined before sending data, you can use the block method to transfer encoding.

The Web server sometimes generates httpresponse and cannot determine the Message Size in the header. In this case, the server generally does not provide the Content-Length header information, but uses chunked encoding to dynamically provide the length of the body content.

HTTP Response for chunked encoding transmission will be set in the message header:

Transfer-encoding: chunked

Indicates that the content body will be transmitted using chunked encoding.

Chunked encoding is composed of several chunks.The length is 0.. Each Chunk is divided into two parts: the header and the body. The header content specifies the total number of characters in the next body section (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 ).

There is only one meaningful chunke and one footer. The first chunk, with the header of 3134, represents the two ASCII characters 1 and 4. It is interpreted by the HTTP protocol as the hexadecimal number 14, that is, the decimal number 20. Followed by 0d0a, followed by the chunk body of 20 bytes (011e ~ in the figure ~ 0131 ).

Followed by 0d0a, followed by footer, 30 represents the ASCII character 0, HTTP is interpreted as the length is 0 (it also indicates that this is the last chunk), followed by 0d0a, then the body is blank, followed by 0d 0a to end

Related Article

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.