HTTP Protocol
Theory: Transfer-Encoding
Sometimes, users pass large amounts of data to the server. Moreover, we might not know their final size. For example, if you need to stream a video or download an archive.
To solve this problem, you can load the data completely into the server's RAM, calculate the Content-Length and pass it. When the browser fully accepts the content, it'll display it straight away.
There is another solution. It allows us to reliably pass data when we don't know their final size. This link leads to an example of an image that is rendered gradually while data is transferred.
To do so, we use a technique for passing in small parts called chunks, and a special Transfer-Encoding header with the value chunked.
In a standard response, we get the whole body and then process it. We can't process it in parts because then we can't introduce unique rules inside the protocol. But when transmitting chunks, we can process the response until the body is fully received.
Let's make a request to the site httpwatch.com:
Note that the headers are separated from the request body by a line break, as we usually do. At the beginning of each chunk, we see the chunk's size. After it we see the data, a line break at the end of the chunk, then we have the next chunk, and so on.
By doing this, you can pass as many chunks as you want. The only limit is a timeout of a server.
To finish the process, you need to pass the last chunk, which must be of zero length. Then, two line breaks follow, and the request is considered fully passed.
Message format
To separate records of block sizes from their contents, we use a CRLF separator:
- As a string:
\r\n - As bytes in HEX format:
0x0D,0x0A
The block length is the size of the block content. The CRLF separators are not considered.
It can be represented like this:
The last block is built in the same way, so it looks like this due to the lack of content: 0<CRLF><CRLF>
The standard also allows you to use only CR or only LF as a separator.


