A small HTTP/1.1 server written in C from scratch, built to learn how HTTP works on top of raw sockets. It uses only the POSIX sockets API and the C standard library, with no external dependencies.
The socket setup follows Beej's Guide to Network Programming (see References).
- TCP listener built with
getaddrinfo,socket,bind,listenandaccept - Request line parsing (method, path, version)
- Header parsing and validation
- Valid HTTP responses with status line, headers and
Content-Length - Route handling for
/,/helloand static assets - Custom error pages:
400 Bad Requestand404 Not Found 405 Method Not Allowedfor unsupported methods- Path traversal protection
- Test harness for the request parser
- Handles one client at a time (no concurrency yet)
- Every response uses
Connection: close(no keep-alive) - No support for request bodies or chunked transfer encoding
make
./build/server
The server listens on port 3649.
The parser tests live in tests/test_request.c. They feed valid and malformed requests through the parser and print PASS or FAIL for each case.
.
├── assets/ HTML served by the server
│ ├── index.html
│ ├── hello.html
│ ├── 400.html error page for bad requests
│ └── 404.html error page for unknown paths
├── src/
│ ├── server.c socket setup and accept loop
│ ├── request.c/h request line and header parsing
│ ├── routes.c/h maps method and path to a handler
│ └── response.c/h builds and sends HTTP responses
├── tests/
│ └── test_request.c
├── Makefile
└── README.md
Home page served in the browser:
- Beej's Guide to Network Programming: https://beej.us/guide/bgnet/
- RFC 9112, HTTP/1.1 message syntax: https://www.rfc-editor.org/rfc/rfc9112
- RFC 9110, HTTP semantics: https://www.rfc-editor.org/rfc/rfc9110

