A lightweight, fully functional HTTP/1.1 server written from scratch in C for POSIX/Linux environments.
- Static File Serving: Serves static assets (
.html,.css,.js,.png,.jpg,.txt) with correct MIME types. - CGI (Common Gateway Interface): Executes CGI scripts and pipes their output directly to the HTTP response.
- File Uploads: Supports
multipart/form-dataparsing for uploading files directly to the server. - Configurable: Driven by a simple
server.conffile to configure ports and directory roots. - Non-blocking / Signal Handling: Ignores
SIGPIPEto prevent crashes on premature client disconnects.
src/- Contains the C source code.main.c- Entry point, configuration loading, and server initialization.server.c/server.h- Core socket programming, connection handling, and event loop.http.c/http.h- HTTP request parsing and response formatting.handlers.c/handlers.h- Logic for static files, CGI execution, and file uploads.
www/- The document root for serving static files.cgi-bin/- Directory for executable CGI scripts.uploads/- Destination directory for uploaded files.server.conf- Configuration file.
This project relies on POSIX-specific system calls (<sys/wait.h>, <arpa/inet.h>, fork(), pipe()).
It requires a Linux environment or Windows Subsystem for Linux (WSL) to compile and run.
- GCC or Clang compiler
- Make (optional, but a
Makefileis provided)
To build the project, simply use make in the root directory:
makeThis will compile the source files and generate an executable named http_server.
Start the server by executing the compiled binary:
./http_serverThe server will read server.conf to determine the listening port and root directories.
The server is configured using a simple key-value format in server.conf:
port=8080
document_root=./www
cgi_root=./cgi-bin
GET /-> Servesindex.htmlfrom the document root.GET /*-> Serves static files from the document root.GET /cgi-bin/*orPOST /cgi-bin/*-> Executes the specified script in the CGI root.POST /upload-> Handlesmultipart/form-datauploads and saves them to theuploads/directory.
- Buffer Size: The server reads incoming requests into a fixed-size
BUFFER_SIZEarray (64KB). Requests or uploads larger than this limit are not properly handled because the read loop expects the full request to fit into this initial buffer.
MIT