Response body length

The HTTP body response length can be determined a few different ways. The simplest is if the HTTP server includes a Content-Length header line in its response. In that case, the server simply states the body length directly.

If the server would like to begin sending data before the body's length is known, then it can't use the Content-Length header line. In this case, the server can send a Transfer-Encoding: chunked header line. This header line indicates to the client that the response body will be sent in separate chunks. Each chunk begins with its chunk length, encoded in base-16 (hexadecimal), followed by a newline, and then the chunk data. The entire HTTP body ends with a zero-length chunk.

Let's consider an HTTP response example that uses chunked encoding:

HTTP/1.1 200 OK
Content-Type: text/plain; charset=ascii
Transfer-Encoding: chunked

44
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eius
37
mod tempor incididunt ut labore et dolore magna aliqua.
0

In the preceding example, we see the HTTP body starts with 44 followed by a newline. This 44 should be interpreted as hexadecimal. We can use the built-in C strtol() function to interpret hexadecimal numbers.

Hexadecimal numbers are commonly written with a 0x prefix to disambiguate them from decimal. We identify them with this prefix here, but keep in mind that the HTTP protocol does not add this prefix.

The 0x44 hexadecimal number is equal to 68 in decimal. After the 44 and newline, we see 68 characters that are part of the requested resource. After the 68 character chunk, the server sends a newline.

The server then sent 37. 0x37 is 55 in decimal. After a newline, 55 characters are sent as chunk data. The server then sends a zero-length chunk to indicate that the response is finished.

The client should interpret the complete HTTP response after it has decoded the chunking as Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

There are a few other ways to indicate the HTTP response body length besides Content-Length and Transfer-Encoding: chunked. However, the server is limited to those two unless the client explicitly states support for additional encoding types in the HTTP request.

You may sometimes see a server that simply closes the TCP connection when it has finished transmitting a resource. That was a common way to indicate resource size in HTTP/1.0. However, this method shouldn't be used with HTTP/1.1. The issue with using a closed connection to indicate response length is that it's ambiguous as to why the connection was closed. It could be because all data has been sent, or it could be because of some other reason. Consider what would happen if a network cable is unplugged while data is being sent.

Now that we've seen the basics of HTTP requests and responses, let's look at how web resources are identified.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.204.216