Chunked encoding implementation method in HTTP response under PHP _php tutorial

Source: Internet
Author: User
Tags fread
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 ...

  • 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.