@@ -117,3 +117,45 @@ I kept the format string argument as a simple `char *` because the prevailing us
...
@@ -117,3 +117,45 @@ I kept the format string argument as a simple `char *` because the prevailing us
The formatting syntax is the same as the underlying `fmt` library. It's quite similar to [Python's Format Specification Mini Language](https://docs.python.org/3.8/library/string.html#formatspec), and in fact it's inspired by it.
The formatting syntax is the same as the underlying `fmt` library. It's quite similar to [Python's Format Specification Mini Language](https://docs.python.org/3.8/library/string.html#formatspec), and in fact it's inspired by it.
## A Simple HTTP/1.0 Echo Server
With all this in hand, I wrote a minimal HTTP/1.0 server. It's a kind of echo server that parses HTTP requests and responds with the same body as the message you send it.
I started by writing a simple HTTP/1.0 parser using basic string operations such as `find` and `substr`. While I was at it I wrote a URL parser as well.
The server waits until it has received all the HTTP headers (delimited by a double `\r\n`). If the headers specify a `Content-Length`, it waits again until the whole message body is received, and then responds with `200 OK` and the same message body. Otherwise it just sends a `200 OK` response. If it fails to parse the request, it answers with [`418 I'm a teapot`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/418).