Turn from: wonderful day girl--chunked transmission Code ~
Reference Links:
- HTTP Mdn--http Protocol
First, background:
- Persistent connection problem: for a non-persistent connection, the browser can define the boundary of the request or response entity by whether the connection is closed or not, and for persistent connections, this method is obviously ineffective. Sometimes, although I have sent all the data, but the browser does not know this, it can not tell whether the open connection will still have new data come in, can only be silly to wait.
- Solved with Content-length: Calculates the length of the body and tells the other through the head. The browser can determine the end of the response entity by content-length the length information
- New issues introduced by Content-length: Because the Content-length field must truly reflect the length of the entity, for dynamically generated content, the length is not known until the content is created. At this time to accurately obtain the length, can only open a large enough buffer, and so on all the content generated good recalculation. But doing so requires a lot more memory overhead and, on the other hand, makes the client wait longer.
- We need a new mechanism: not depending on the length of the header, but also the boundaries of the entity--chunked coding (transfer-encoding:chunked)
Two, Block code (transfer-encoding:chunked)
- Transfer-encoding, is an HTTP header field (response Header field), which literally means "transmission encoding". In the latest HTTP specification, only one encoding transmission is defined: chunked encoding (chunked).
- chunked transfer encoding (Chunked transfer encoding) is a data transfer mechanism in Hypertext Transfer Protocol (HTTP) that allows HTTP data sent by the Web server to the client to be divided into multiple parts. chunked transfer encoding is only available in the HTTP Protocol version 1.1 (http/1.1).
- The data is decomposed into a series of chunks 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.
- Specific methods
- After adding transfer-encoding:chunked to the head, the message is represented by a chunked code. At this point, the entity in the message needs to be transferred by a series of blocks.
- Each tile contains 16 binary length values and data, the length value is exclusive to one row, the length does not include the CRLF at its end (\ r \ n), or the CRLF at the end of the chunked data.
- The last chunk length value must be 0, the corresponding chunked data has no content, indicating the end of the entity.
- Cases:
- Content-encoding and transfer-encoding are often combined to use, in fact, the transfer-encoding for the sub-block and then content-encoding compression.
Chunked encoding in HTTP (transfer-encoding:chunked)