This is a small learning HTTP server written in C. It uses Unix sockets to
listen on addresses and port configured with environmental variables (with default valuses 127.0.0.1:8080), parse simple HTTP request lines, serve static files
from a fixed directory, build HTTP responses, log requests, and clean up
resources.
It is not intended to be a production web server.
-
IPv4 TCP server
-
Single-threaded blocking accept loop
-
Supports
GET -
Serves files from
files/ -
Returns basic
200,400,404,405, and500responses -
Sends
Content-Length,Content-Type, andConnection: close -
Logs method, path, status code, and response time
-
Docker compatibility
makeThis creates:
server.out
./server.outThe server listens on:
http://127.0.0.1:8080
Root request:
curl http://127.0.0.1:8080/Static file:
curl http://127.0.0.1:8080/test.htmlMissing file:
curl -v http://127.0.0.1:8080/missing.htmlUnsupported method:
curl -v -X POST http://127.0.0.1:8080/test.htmlBuild the image (a multi-stage build: the server is compiled in a gcc
image and only the binary and files/ are copied into a slim Debian image):
docker build -t http-server .Run it and publish the container port on the host:
docker run --rm -p 8080:8080 http-servercurl http://localhost:8080/healthz
curl http://localhost:8080/hostname # returns the container IDInside the container the server listens on 0.0.0.0 so that it is reachable
from outside the container. The image sets the defaults:
LISTEN_ADDR=0.0.0.0
PORT=8080
Override them with -e. The container side of -p must match PORT:
docker run --rm -e PORT=9090 -p 9090:9090 http-serverInvalid values (for example PORT=abc) make the server exit with an error
message, visible with docker logs.
On a user-defined network containers can reach each other by name, and no
-p is needed for container-to-container traffic:
docker network create lab-net
docker run -d --rm --network lab-net --name web1 http-server
docker run -d --rm --network lab-net --name web2 http-serverEach container answers /hostname with its own ID:
docker run --rm --network lab-net curlimages/curl -s http://web1:8080/hostname
docker run --rm --network lab-net curlimages/curl -s http://web2:8080/hostnameClean up (-t 1 shortens the wait: the server runs as PID 1 and does not
handle SIGTERM, so Docker would otherwise wait 10 seconds before killing it):
docker stop -t 1 web1 web2
docker network rm lab-netMakefile
Dockerfile
server.c socket setup, accept loop, request handling, file serving
http.c HTTP request parsing and response building
http.h public HTTP structs and function declarations
files/ static files served by the server
This server is intentionally minimal. It does not implement full HTTP behavior. Known limitations include:
- one request per connection
- no concurrency
- request reading assumes the request fits in one buffer
- basic path traversal protection only
- basic content type handling
- no URL decoding
- limited client disconnect handling
The goal is to understand the path from sockets to HTTP parsing, file I/O, response writing, logging, and cleanup.