I. Background knowledge 1, chunked code
chunked transfer encoding (Chunked transfer encoding) is a data transfer mechanism that is provided only in the HTTP Protocol 1.1 version (http/1.1). The data in the previous HTTP response was sent together, and the Content-length field in the answer header identifies the length of the data so that the client knows the end of the reply message.
2. Benefits
For dynamically generated responses, the total length of content before the completion of the build is not known. Therefore, the generated content needs to be cached before the total length is populated to content-length, and then the entire data content is sent. This is not very flexible, and the use of block coding can be improved.
Chunked transfer encoding allows the server to send the message header field at the end. For example, add a hash signature to the header.
For compressed transport transmissions, you can compress one side of the transmission.
3. Format
If Transfer-encoding is chunked in the message header of HTTP, this is the way to encode it. Next, the number of unknown blocks is sent, and each block has a hexadecimal number at the beginning, indicating the size of the block and then CRLF ("\ r \ n"). Then there is the data itself, and after the data is finished, there will be a CRLF of two characters. In some implementations, there can be spaces between the block-size hexadecimal number and the CRLF.
The last chunk of the block size is 0, indicating the end of the data send. The last piece no longer contains any data, but you can send an optional trailer, including the message header field. The message ends with CRLF.
4. Case
Second, the code to send
Third, the code receives
HTTP protocol Literacy (IX) transfer-encoding=chunked mode of HTTP1.1 response