This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9827|Reply: 0

HTTP protocol Transfer-Encoding (chunked)

[Copy link]
Posted on 9/24/2019 6:07:57 PM | | | |
1. Background:

  • Persistent connection issues: For non-persistent connections, the browser can define the boundaries of the requesting or response entity by whether the connection is down or not; For continuous connections, this approach obviously doesn't work. Sometimes, even though I've sent all the data, the browser doesn't know if there will be new data coming in on the open connection, so it just waits.
  • Solve with Content-length: Calculate the entity length and tell the other party through the head. The browser can tell that the response entity is over by the length information of the Content-Length
  • New problem with Content-length: Since the Content-Length field must truly reflect the entity length, the length is unknown for dynamically generated content until the content is created. At this time, to get the length accurately, you can only open a large enough buffer and wait for all the content to be generated before calculating. However, this requires more memory overhead on the one hand, and on the other hand, it will make the client wait longer.
  • We need a new mechanism that doesn't rely on the length of the header and knows the boundaries of entities - Transfer-Encoding: chunked.

2. Transfer-Encoding (chunked)

  • Transfer-Encoding is an HTTP header field (response header field), which literally means "transfer encoding". The latest HTTP specification defines only one type of encoding transport: chunked.
  • Chunked transfer encoding is a data transfer mechanism in the Hypertext Transfer Protocol (HTTP) that allows HTTP data sent by a web server to the client to be divided into multiple parts. Block transport encoding is only available in HTTP protocol version 1.1 (HTTP/1.1).
  • Data is broken down into a series of chunks and sent in one or more chunks so that the server can send data without knowing the total size of the sent content in advance.


Specific methods

  • After adding Transfer-Encoding: chunked to the header, it means that the packet is chunked. In this case, the entities in the packet need to be transmitted in a series of chunks instead.
  • Each chunk contains the hexadecimal length value and data, and the length value is exclusive to one line, and the length does not include the CRLF at the end of it, nor the CRLF at the end of the chunked data.
  • The last chunk length value must be 0, and the corresponding chunk data has no content, indicating the end of the entity.




asp.net Core test code:







Previous:Content-Length of the Http protocol
Next:Three factors that make me deprecate Chrome
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com