A small static-file HTTP/1.1 server written in C, supporting both POSIX (Linux/macOS) and Windows (Winsock).
- Serves static files from a configurable web root (
www/by default) - Handles
GETrequests with correctContent-TypeandContent-Length - Common MIME types (HTML, CSS, JS, JSON, images, plain text)
- Basic error responses:
400,403,404,405 - Directory-traversal protection (rejects paths containing
..)
http-server-c/
├── src/
│ ├── main.c # entry point, argument parsing
│ ├── server.c # socket setup and the accept loop
│ ├── server.h
│ ├── request.c # parses the HTTP request line
│ ├── request.h
│ ├── response.c # builds responses, serves files
│ └── response.h
├── www/
│ └── index.html # files the server will serve
├── Makefile
└── README.md
Requires gcc and make.
makeOn Windows, use a toolchain that provides gcc and make (e.g. MinGW-w64
or MSYS2). The Makefile links Winsock (-lws2_32) automatically.
make run # default: port 8080, root ./www
./http-server # same as above
./http-server 3000 # custom port
./http-server 3000 site # custom port and web rootThen open http://localhost:8080/ in your browser.
make cleanThis is an educational project. It handles one connection at a time
(no threading), supports only GET, and reads just the request line.
It is not intended for production use.