HTTP protocol response Header transfer-encoding: chunked transfer Detail

Source: Internet
Author: User
Tags time limit

There are two ways to connect Http connection: Short connection and long connection, short connection is the process of establishing and destroying TCP connection once request, and long connection is multiple requests sharing the same connection. This can save a lot of connection settling time and improve communication efficiency. At present, the main browser will include the Connection:keep-alive field in the request header, the purpose of this field is to tell the HTTP server after the end of the response do not close the connection, the browser will set up the connection cache, When a request is sent again to the same server within a limited time limit, the connection is removed from the cache and communicated directly. Of course the cached connection closes the connection if the idle time exceeds the set value (for example, Firefox is 115s,ie to 60s).

When a short connection is used, the recipient can get the end position of the message body correctly by the server-side shutdown of the connection, but when the long connection recipient correctly know the boundary position of the response content of the adjacent two requests. The main use is to set the response header Content-length or transfer-encoding:chunked method to solve this problem.

Chunked transfer encoding is a data transmission mechanism, the message body is divided into several blocks from the server to the recipient (receiver), the current use of Chunked transmission method is more, why use Chunked below will say If you do not use chunked transport, you must set the Content-length field so that recipient can correctly know where the body of the message ends, and why chunked does not have to set the Content-length field. Because the chunked transmission mode-specific format allows recipient to correctly learn the end of the body of the message.

Chunked transfer is a chunked transmission: The response body is divided into several blocks, and the length of the block and a carriage return in front of each piece, so that recipient (such as a browser) can correctly receive each piece of data according to this length value, and finally a 0-length block as the end of the message body flag. With this transmission, sender does not need to know the length of the content before it starts transmitting the response content to recipient.

The chunked message has the following formatting:

Hex block Length +<cr> carriage return +<lf> newline

chunked data

The block length of the end block is 0

If the content to be sent (message body) is: 123456789 then the format of the message body is:

9<cr><lf>

123456789<cr><lf>

0<cr><lf>

Benefits of using a chunked transfer method:

(1) Because the server does not need to know the length of the data before sending the data to recipient, it can dynamically generate the response content without first caching all the data, because when the body of the message ends with a clear signal identification (0<cr><lf>), Therefore, subsequent requests to the same HTTP server can be reused for this connection.

(2) Allow the server to send additional response header fields behind the message body, which is very important when the value of a field waits until the response content is fully generated before it can be determined, such as the digital signature of the response content, If you do not use a chunked transport server in order to calculate the digital signature of the response content, you must first cache everything until the content has finished. (The response headers sent behind the message body cannot be recipient correctly if the chunked block transfer is not used)

(3) HTTP servers sometimes use the compression (gzip or deflate) method to optimize transmission, which compresses the bytes that are transferred, chunked and gzip encode each other in two phases of HTTP encoding The first stage response content byte stream is compressed by gzip, and the byte stream generated after compression is encoded by chunked, which means that chunked and compression can be used at the same time, but at different stages.

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.