HTTP response for chunked encoded transmissions are set at the message header:
Transfer-encoding:chunked
Indicates that the content body will use chunked encoding to transmit the contents.
The chunked code is concatenated with several chunk, ending with a chunk marked with a length of 0. Each chunk is divided into head and body two parts, the head content specifies the total number of characters of the next body (16 numbers) and the quantity unit (generally does not write), the body part is the actual content of the specified length, the two parts are separated by carriage return Line (CRLF). In the last chunk of length 0, the content is called footer, and is some additional header information (which can often be ignored directly). The specific chunk encoding format is as follows:
Copy Code code 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 an RFC document is as follows:
Copy Code code 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 provide a section of PHP version of the chunked decoding code:
Copy Code code 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));
}