Commit 59a4042c authored by Xavier Thompson's avatar Xavier Thompson

README: Add section on HTTP server

parent 94593fe6
...@@ -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).
To start the server:
```
$ make httpserver
>>> import httpserver
listening on http://0.0.0.0:3490
```
You can use `wget` to test it:
```
wget http://0.0.0.0:3490
```
To send data:
```
wget http://0.0.0.0:3490 --post-data="Hello!"
```
or
```
wget http://0.0.0.0:3490 --post-file=helloworld.pyx
```
To stop the server, you can send `quit`:
```
wget http://0.0.0.0:3490 --post-data="quit"
```
You can also send non-HTTP data using the simple client I wrote earlier, and stop the server just by sending `quit`.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment