The HTTP response for chunked encoded transmission is set at the message header:
Transfer-encoding:chunked
Indicates that the content body will transmit the contents using chunked encoding.
The chunked code is concatenated with several chunk, ending with a chunk indicating a length of 0. Each chunk is divided into the head and the body two parts, the head content to specify the next paragraph of the text of the total number of characters (16 binary numbers) and the number of units (generally do not write), the body part is the actual content of the specified length, between the two parts with a carriage return line (CRLF) separated. 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 specific chunk encoding format is as follows:
Copy CodeThe code is as follows:
Chunked-body = *chunk
"0" CRLF
Footer
CRLF
Chunk = chunk-size [Chunk-ext] CRLF
Chunk-data CRLF
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 chunked decoding process in the RFC documentation is as follows:
Copy CodeThe code is as follows:
Length: = 0
Read Chunk-size, Chunk-ext (if any) and CRLF
while (Chunk-size > 0) {
Read Chunk-data and CRLF
Append Chunk-data to Entity-body
Length: = length + chunk-size
Read Chunk-size and CRLF
}
Read Entity-header
while (Entity-header not empty) {
Append Entity-header to existing header fields
Read Entity-header
}
Content-length: = Length
Remove "chunked" from transfer-encoding
Finally, a PHP version of the chunked decoding code is provided:
Copy CodeThe code is as follows:
$chunk _size = (integer) hexdec (fgets ($socket _fd, 4096));
while (!feof ($socket _fd) && $chunk _size > 0) {
$bodyContent. = Fread ($socket _fd, $chunk _size);
Fread ($socket _fd, 2); Skip \ r \ n
$chunk _size = (integer) hexdec (fgets ($socket _fd, 4096));
}
http://www.bkjia.com/PHPjc/319767.html www.bkjia.com true http://www.bkjia.com/PHPjc/319767.html techarticle The HTTP response for chunked encoded transmission is set on the message header: Transfer-encoding:chunked indicates that the content body will transmit the contents with chunked encoding. Chunked encoding uses a number of Chun ...