Chunked resolution of HTTP protocol

Source: Internet
Author: User

After searching for the Chunked code on the Internet for a while, I have never found any article about Chunked encoding. Write one by myself.
There are not many websites that use Chunked encoding on the Internet, except for those websites that use GZip compression, such as google.com, and most PHP forums that open GZip compression.
According to my understanding, the main advantage of Chunked encoding is that some programs can dynamically output Content During the computation process.
For example, it takes an hour to process operations in the background, but you do not want to wait an hour to see the results. In this case, the Chunked encoding can be used to block the content and the user can receive the latest processing results at any time.
ASP disables the cache output mode, that is, Chunked encoding. (Response. Buffer = false)
Each Response. Write is a Chunked, so do not use it too frequently. Otherwise, the number of chunks is too large, and the extra data is too much space.
To understand the specific encoding structure of Chunked, it is quite convenient to disable cache debugging with ASP. :)

Let's take a look at the definition of Chunked in RFC2616:
Chunked-Body = * chunk
Last-chunk
Trailer
CRLF

Chunk = chunk-size [chunk-extension] CRLF
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)

Let's simulate the data structure:
[Chunk size] [Press enter] [Chunk data body] [Press enter] [Chunk size] [Press enter] [Chunk data body] [Press enter] [0] [Press enter]

Note that chunk-size is represented by hexadecimal ASCII Code. For example, 86AE (the actual hexadecimal format should be: 38366165) is calculated as length: 34478, it indicates that there are 34478 consecutive bytes of data after the carriage return.
After tracking the returned data of www.yahoo.com, it is found that there will be more spaces in chunk-size. It may be a fixed length of 7 bytes. If the length is less than 7 bytes, it is supplemented by space. The ASCII code of the space is 0x20.

The following is the pseudocode of the decoding process:
Length: = 0 // The length of the decoded data body.
Read chunk-size, chunk-extension (if any) and CRLF // size of the first read Block
While (chunk-size> 0) {// keep repeating until the size of the read block is 0
Read chunk-data and CRLF // read the block data body and press enter to finish
Append chunk-data to entity-body // Add the block data body to the decoded Object data
Length: = length + chunk-size // update the decoded object length
Read chunk-size and CRLF // read the new block size
}
Read entity-header // The following Code reads all header tags
While (entity-header not empty ){
Append entity-header to existing header fields
Read entity-header
}
Content-Length: = length // Add Content Length to the header
Remove "chunked" from Transfer-Encoding // Remove Transfer-Encoding from the header flag


If you have time, study how GZip + Chunked is encoded. It is estimated that each Chunk block is compressed independently by GZip.

When Chunked is used, it will naturally offer a slight discount on performance, because it consumes more than normal data bodies.
However, in some cases, you must use multipart output ~ Pai_^

In my last question, why does the home page of yahoo.com also use Chunked? Is it true that the homepage of yahoo.com is also dynamically output by the program? If it is a static HTML page, just output it directly? Hope someone else can give me some advice. Thank you ~ *_*

Reprinted please indicate the source: http://ayi.ck97.com

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.