A simple HTTP server implementation in C, built from scratch to understand the fundamentals of web servers and the HTTP protocol.
- GCC compiler
- Make utility
- POSIX-compliant operating system (Linux, macOS, WSL)
# Clone and navigate to the project directory
cd http-server
# Build the server
make
# Or build with debug symbols
make debug
# Run on default port (8080)
./server
# Run on custom port
./server 3000
# Or use make targets
make run # Runs on port 8080
make run-port # Prompts for port number
- Open your browser and navigate to
http://localhost:8080
- Try different URLs:
http://localhost:8080/
- Homepagehttp://localhost:8080/nonexistent
- 404 error page
- Check the console and
logs/server.log
for request logs
http-server/
βββ src/ # Source code
β βββ server.c # Main server implementation
β βββ server.h # Header declarations
β βββ http_parser.c # HTTP request/response handling
β βββ file_handler.c # Static file serving
β βββ logger.c # Logging functionality
βββ include/
β βββ common.h # Common definitions and constants
βββ public/ # Static files directory
β βββ index.html # Default homepage
β βββ 404.html # 404 error page
β βββ 500.html # 500 error page
βββ logs/ # Server logs (created automatically)
βββ build/ # Build artifacts (created automatically)
βββ Makefile # Build configuration
βββ README.md # This file
- Single-threaded HTTP/1.1 server - Handles one request at a time
- GET request support - Serves static files and handles routing
- Static file serving - Serves HTML files from the
public/
directory - Basic routing - Routes
/
toindex.html
automatically - Error handling - Custom 404 and 500 error pages
- Request logging - Console output with colors and file logging
- Security features - Directory traversal protection
- Configurable port - Command-line port specification
- Graceful shutdown - Handles SIGINT and SIGTERM signals
Target | Description |
---|---|
make or make all |
Build the server executable |
make debug |
Build with debug symbols and debugging enabled |
make release |
Build optimized release version |
make clean |
Remove build artifacts |
make run |
Run server on port 8080 |
make run-port |
Run server on custom port (prompts user) |
make install |
Install to /usr/local/bin (requires sudo) |
make uninstall |
Remove from /usr/local/bin (requires sudo) |
make valgrind |
Run with memory leak detection (requires valgrind) |
make check |
Run static analysis (requires cppcheck) |
make format |
Format source code (requires clang-format) |
make help |
Show all available targets |
- Default Port: 8080
- Max Connections: 128 (listening queue)
- Buffer Size: 8KB for requests/responses
- File Size Limit: 10MB per file
- Public Directory:
./public/
- Log File:
./logs/server.log
- Methods: GET only (Phase 1)
- Protocol: HTTP/1.1
- Connection: Close (no keep-alive in Phase 1)
- Content Types: HTML, text, CSS, JS, JSON, images
# Start server on default port
./server
# Start server on port 3000
./server 3000
# Add HTML files to public directory
echo "<h1>Hello World</h1>" > public/hello.html
# Access via browser
# http://localhost:8080/hello.html
# Watch live logs
tail -f logs/server.log
# View colored console output
./server 8080
# Build debug version and run with valgrind
make debug
make valgrind
# Run cppcheck for code analysis
make check
All requests are logged with:
- Timestamp
- HTTP method and path
- Response status code
- Client IP address
- Color-coded console output
- Directory Traversal Protection - Prevents
../
attacks - File Size Limits - Prevents memory exhaustion
- Request Size Limits - Buffer overflow protection
- Input Validation - Basic HTTP request validation
Phase 1 is single-threaded and handles one request at a time. This is intentional for learning purposes. Performance improvements will be added in Phase 2 with multi-threading.
Current limitations:
- No concurrent request handling
- No connection keep-alive
- Basic file caching (OS-level only)
- Multi-threaded request handling
- POST request support
- Enhanced HTTP header parsing
- Configuration file support
- Multiple content types
- Improved error handling
This is a learning project, but suggestions and improvements are welcome!
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is for educational purposes. Feel free to use and modify as needed.
- β Socket programming in C
- β HTTP protocol implementation
- β Request parsing and response generation
- β File I/O operations
- β Error handling and logging
- β Signal handling
- β Memory management
- β Build system (Makefile)
- Multi-threaded request handling
- Support for POST requests
- Basic request parsing and validation
- Support for common HTTP headers
- Improved error handling and status codes
- Basic security features (request size limits)
- Support for serving different file types (HTML, CSS, images)
- Configuration file support
- Request logging with timestamps
- Connection pooling
- Keep-alive connections
- HTTP/1.1 pipelining support
- Basic caching mechanism
- Compression support (gzip)
- Basic authentication
- URL parameter parsing
- Custom error pages
- Performance monitoring
- Support for HTTP methods (PUT, DELETE, HEAD)
- Basic security features (request validation, basic DoS protection)
make
./server [port]
If no port is specified, the server will default to port 8080.
server.c
- Main server implementationserver.h
- Header file containing function declarations and constantsMakefile
- Build configuration
- C compiler (GCC recommended)
- Make
- POSIX-compliant operating system
- Understanding of socket programming
- HTTP protocol implementation
- Multi-threading and concurrency
- Network programming best practices
- Error handling and debugging
- Performance optimization
- Security considerations in web servers
Happy Coding! π