chunked encoding of the HTTP1.1 Protocol (chunked transfer encoding chunked transfer encoding)

Source: Internet
Author: User
Tags flush

In general, the header of HTTP contains the Content-length field to indicate the length of the newspaper style.

Sometimes the service generates HTTP response is unable to determine the size of the message, such as large file download, or the background requires complex logic to all the page request, when the need to generate a real-time message length, the server generally use chunked encoding. There are two options for using chunked encoding, one is to set the server's IO buffer length so that the server automatically flush the contents of buffer, and the other is to manually invoke the flush function in IO. The flush feature is available in different language IO:
-Php:ob_flush (); Flush ();
-Perl:stdout->autoflush (1);
-Java:out.flush ();
-Python:sys.stdout.flush ()
-Ruby:stdout.flush

Use chunked encoded headers as follows (you can use Firefox's firebug plugin or httpwatch to view headers information, HttpWatch can also see the number of chunked):



The chunked encoding is typically concatenated with several chunk, ending with a chunk marked with a length of 0. Each chunk is divided into the head and the body two parts, the head content specifies the total number of characters in the next paragraph of the body (not 0 hexadecimal digits) and the number of units (generally not written, representing bytes). The body part is the actual content of the specified length, separated by a carriage return line (CRLF) between the two parts. In the last chunk of length 0 is the content called footer, which is some additional header information (which can usually be ignored directly).


The above explanation is too official, in short, the basic method of chunked encoding is to break up chunks of data into chunks of small data, each of which can be of a specified length, in the following format (BNF grammar):

<pre name= "code" class= "java" >chunked-body   = *chunk            //0 up to chunk <span
style= "White-space:pre" >		</span>last-chunk         //Last chunk
<span style= "White-space:pre" >		</span> Trailer            //tail
<span style= "White-space:pre" >		</span>crlf               //end marker
Chunk          = chunk-size [chunk-extension] CRLF
<span style= "White-space:pre" >		</span>chunk-data CRLF
chunk-size     = 1*hex
last-chunk     = 1* ("0") [chunk-extension] CRLF
chunk-extension= * (";" chunk-ext-name ["=" chunk-ext-val])
chunk-ext-name = token
chunk-ext-val  = token | quoted-string
Chunk-data     = chunk-size (OCTET)
Trailer        = * (Entity-header CRLF)




Explain:
Chunked-body indicates the style of the newspaper after Chunked encoding. Newspaper style can be divided into chunk, Last-chunk,trailer and Terminator four parts. The number of chunk in the newspaper style can be at least 0, no limit;

The length of each chunk is self-specified, that is, the starting data must be a string of 16 decimal digits, representing the length (in bytes) of the subsequent chunk-data. The first character of this 16-binary string, if "0", indicates that Chunk-size is 0, the chunk is last-chunk, and there is no chunk-data part.

The optional chunk-extension is determined by the communication parties themselves, and can be ignored if the recipient does not understand its meaning.

Trailer is an additional header field attached at the tail, usually containing some metadata (metadata, meta means "about information"), which can be appended after decoding after the existing header field


Look at the results of a wireshark grab bag:


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

followed by 0d0a, and then footer, 30 means the ASCII character 0,http interpreted as the length is 0 (also explained that this is the last chunk), followed by 0d0a, and then the body part is empty, and then the 0d 0a means the end


Reprinted article:

1) http://blog.csdn.net/zhangboyj/article/details/6236780

2) http://www.cnblogs.com/jhxk/articles/2715848.html

3) http://www.cnblogs.com/jhxk/articles/2715848.html

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.