daneelsan/toy-httpserver
Implementation of a toy HTTP server in lua, python, wolfram language and zig.
Implementation of a toy HTTP server in lua, python, wolfram language and zig. The server is built on top of simple socket functionality, using TCP for the connection protocol and using localhost as the socket address. The server currently only handles the GET method, but it could be easily be expanded.
Start the HTTP server in various languages:
Tested on:
$ lua -v
Lua 5.4.6 Copyright (C) 1994-2023 Lua.org, PUC-Rio
Install the luasocket package (I chose luarocks
for this):
$ luarocks install luasocket
Also install the mimetypes package:
$ luarocks install mimetypes
Start the server:
$ lua httpserver.lua
NOTE: Testing against Google Chrome works fine. There seems to be an issue with Safari which needs more study.
Tested on:
$ python3 --version
Python 3.9.6
Start the server:
$ python3 httpserver.py
Tested on:
$ wolframscript --version
WolframScript 1.8.0 for Mac OS X ARM (64-bit)
Start the server:
wolframscript -f httpserver.wl
A write-up of this example is in the Wolfram Example Repository: A Toy HTTP Server.
Start the server:
zig run httpserver.zig
Send a HTTP request to the toy HTTP server using curl:
$ curl -i 127.0.0.1:8888/index.html
HTTP/1.1 200 OK
content-type: text/html;charset=UTF-8
<html>
<head>
<title>Index page</title>
</head>
<body>
<h1>Index page</h1>
<p>This is the index page.</p>
<img src="./images/random.jpeg">
</body>
</html>
Or do it via a web browser:
A request for a non-existant file returns the following:
$ curl -i --http0.9 127.0.0.1:8888/notafile.txt
HTTP/1.1 404 Not Found
Server: ToyServer (python)
Content-Type: text/html
<h1>404 Not Found</h1>