chunked transfer encoding (Chunked transfer encoding) is a data transfer mechanism in Hypertext Transfer Protocol (HTTP) that allows HTTP to be sent by the application server to the client application ( Data that is typically a Web browser) can be divided into multiple parts. chunked transfer encoding is only available in the HTTP Protocol version 1.1 (http/1.1).
Typically, the data sent in the HTTP reply message is sent throughout, and the Content-length message header field represents the length of the data. The length of the data is important because the client needs to know where the answer message ends and the start of the subsequent reply message. However, using chunked transfer encoding, the data is decomposed into a series of blocks of data and sent in one or more blocks, so that the server can send data without needing to know the total size of the sent content beforehand. The size of the data block is usually the same, but it is not always the case.
The introduction of chunked transfer encoding in HTTP 1.1 provides the following benefits:
- http chunked transfer encoding allows the server to maintain HTTP persistent connections for dynamically generated content. Typically, a persistent link requires the server to send the Content-length message header field before the body of the message is started, but for dynamically generated content, it is not known until the content is created. [dynamic content, content-length unpredictable]
- chunked transfer encoding allows the server to send the message header field at the end. It is important for situations where the header field value cannot be known until the content is generated, such as the content of the message is to be signed with a hash, and the result of the hash is transmitted over the HTTP message header field. When there is no chunked transfer encoding, the server must buffer the content until it is complete, calculate the value of the header field and send the values of those header fields before sending the content. [hash signature, need buffer complete to calculate]
- HTTP servers sometimes use compression (gzip or deflate) to shorten the time it takes to transfer. chunked transfer encoding can be used to separate multiple parts of a compressed object. In this case, the block is not compressed separately, but the entire load is compressed, and the compressed output is chunked-transferred using the scheme described in this article. In the case of compression, chunked encoding is advantageous for sending data while compressing, rather than completing the compression process first to know the size of the compressed data. [gzip compression, compression and transmission at the same time]
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.
In the chunked encoded transmission, the headers of the reply message has a transfer-coding domain value of chunked, indicating that the content will be transmitted in chunked encoding. Use the chunked encoded headers as follows (you can use Firefox's firebug plugin or httpwatch to view headers information):
encode in the following ways:
Chunked-body=*chunk
"0" CRLF
Footer
CRLF
Chunk=chunk-size[chunk-ext]crlf
Chunk-datacrlf
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 encoding consists of a number of chunk consisting of a chunk with a length of 0, each of which consists of two parts, the first part is the length and length of the chunk (generally not written), the second part is the content of the specified length, and each part is separated by CRLF. In the last chunk of length 0 is the content called footer, which is something that is not written in the header content. The following is a chunked decoding process (available in the RFC document)
length:=0
Readchunk-size,chunk-ext (ifany) Andcrlf
While (chunk-size>0) {
Readchunk-dataandcrlf
Appendchunk-datatoentity-body
length:=length+chunk-size
Readchunk-sizeandcrlf
}
Readentity-header
While (entity-headernotempty) {
Appendentity-headertoexistingheaderfields
Readentity-header
}
Content-length:=length
Remove "chunked" fromtransfer-encoding
Chunk encoding of HTTP protocol (chunked transfer encoding)