Anyone who has implemented a simple HTTP server can tell you that it is a really simple protocol. Basically, it’s a text file that has some spec

How do HTTP servers figure out Content-Length?

submited by
Style Pass
2024-10-07 03:30:03

Anyone who has implemented a simple HTTP server can tell you that it is a really simple protocol. Basically, it’s a text file that has some specific rules to make parsing it easier.

The first line is the “request line”, and it has the requested method, path and HTTP version. The following lines are headers, each terminated with “carriage return” and “line feed” characters. There is also an extra CRLF at the end, to mark the end of the header section. After that, there is the message body which can whatever data you want to send.

One interesting thing about this is the header Content-Length, which has a value of 12. That’s exactly the length of "Hello world!" in UTF-8. In the above Go code, writing the response happens in two parts: first we write"Hello", then we write " world!". Notice that we didn’t need to call any function to write the headers, but they are still in the response. In Go’s http package, a status of 200 and the headers are automatically written if w.Write() is called before w.WriteHeader(). Any calls to w.WriteHeader() after that are useless and will output a warning.

Remember that in HTTP, the headers are always written before the body. How is it possible that before writing “Hello” to the connection, the server already knows how long the response will be? What if I wanted to write one “Hello”, and then a thousand exclamation marks after that? Or a million? Does the server need to know how long every response is before sending it? It would mean that every response needs to be kept in memory for the entire duration of the handler.

Leave a Comment