A minimal HTTP/1.1 server written from scratch in C.
-
Manual HTTP/1.1 Request Parsing
- a custom HTTP parser in C
- Supports request line parsing, header parsing, and body extraction
-
Response generation pipeline
- Dynamically builds and sends HTTP/1.1 responses with proper status lines, headers, and bodies
- Supports both text and binary payloads
-
Routing System
- Simple internal dispatcher for mapping routes to handlers
- Supports static and dynamic routes (e.g., /echo/:string)
-
Persistent Connections
- Reuses the same TCP connection for multiple HTTP requests when allowed by the client
- Correctly handles Connection: close request
-
Static File Serving
- Serves files directly from the filesystem with correct Content-Type detection
- Efficiently streams large files without loading them fully into memory
- Prevents directory traversal attacks by sanitizing and validating file paths
-
File Writing Support
- Allows safe writing of uploaded data to disk
- Enforces path restrictions to prevent overwriting or creating files outside allowed directories
-
Gzip Compression
- Uses zlib to compress responses when the client supports Content-Encoding: gzip
- Falls back to uncompressed content when unsupported
-
Error Handling & Status Codes
- Returns appropriate HTTP error responses (400, 404, 500, etc.)
- Handles malformed requests gracefully without crashing the server
-
Concurrent/Threaded Connection Handling
- Each connection processed in its own thread for concurrent request handling
- Language: C (C99 standard)
- Networking: BSD sockets API (
<sys/socket.h>,<netinet/in.h>) for low-level TCP/IP communication - Concurrency: POSIX threads (
pthreads) for multi-client connection handling - Platform: Developed and tested on Linux. Compatible with macOS (POSIX-compliant). Windows support would require adapting socket code to Winsock and replacing
pthreads - Build System: CMake (project configuration) with Ninja (fast incremental builds)
- Compiler: Clang (tested; compatible with GCC)
- Package Manager: vcpkg for dependency management
- Libraries: zlib (gzip compression support)
- Clone the repository
git clone [email protected]:raphico/http-server-c.git
cd http-server-c- Start server
./your_program.sh- Try
# Basic Request handling
curl -v http://localhost:4221
curl -v http://localhost:4221/echo/abc
curl -v --header "User-Agent: foobar/1.2.3" http://localhost:4221/user-agent
# Concurrent connections
(sleep 3 && printf "GET / HTTP/1.1\r\n\r\n") | nc localhost 4221 &
(sleep 3 && printf "GET / HTTP/1.1\r\n\r\n") | nc localhost 4221 &
(sleep 3 && printf "GET / HTTP/1.1\r\n\r\n") | nc localhost 4221 &
# File Reading/writing
echo -n 'Hello, World!' > /tmp/foo
curl -i http://localhost:4221/files/foo
curl -v --data "12345" -H "Content-Type: application/octet-stream" http://localhost:4221/files/file_123
# HTTP compression
curl -v -H "Accept-Encoding: gzip" http://localhost:4221/echo/abc | hexdump -C
# Persistent connections
curl --http1.1 -v http://localhost:4221/echo/banana --next http://localhost:4221/user-agent -H "User-Agent: blueberry/apple-blueberry"
curl --http1.1 -v http://localhost:4221/echo/orange --next http://localhost:4221/ -H "Connection: close"├── src/ # Core source code for the HTTP server
│ ├── main.c # Program entry point
│ ├── server.c # Creates socket, binds, listens, and accepts client connections
│ ├── request.c # Manual HTTP/1.1 request parsing
│ ├── response.c # Constructs and sends HTTP responses to clients
│ ├── dispatcher.c # Routes parsed requests to the appropriate handler
│ ├── compression.c # Implements gzip compression using zlib
│ ├── utils.c # General helper and utility functions
│ ├── status.c # Defines HTTP status codes and their corresponding messages
│ ├── headers.c # Handles HTTP header creation and management
│ └── handlers/ # Individual route handlers for specific endpoints
│
└── include/ # Header files for modularity, type definitions, and forward declarationsThis project was built as part of the Codecrafters "Build Your Own HTTP Server" Challenge.