Web Server in C
A lightweight HTTP web server implemented from scratch in C, designed to handle concurrent connections efficiently using multi-threading. This project demonstrates deep understanding of socket programming, HTTP protocol implementation, and systems-level programming concepts.
Overview
The web server is built using POSIX sockets and implements core HTTP/1.1 functionality including GET and POST requests, static file serving, and concurrent connection handling through thread pools. It serves as an educational project to understand how web servers work at the fundamental level.
Features
- HTTP/1.1 Support: Implements basic HTTP/1.1 protocol with GET and POST methods
- Concurrent Connections: Multi-threaded architecture using pthreads for handling multiple clients simultaneously
- Static File Serving: Serves HTML, CSS, JavaScript, images, and other static content
- Thread Pool: Efficient thread management to minimize thread creation overhead
- Error Handling: Comprehensive error handling for socket operations and HTTP errors
- Logging: Basic request logging for monitoring server activity
Architecture
The server follows a classic multi-threaded server architecture:
- Main Thread: Listens for incoming connections on a specified port
- Thread Pool: Pre-allocated worker threads ready to handle incoming requests
- Connection Queue: A bounded buffer that holds pending connections
- Worker Threads: Each worker thread processes HTTP requests from the queue
Socket Programming
The server uses the Berkeley sockets API:
socket(): Creates a new socketbind(): Associates the socket with a portlisten(): Marks the socket as passive, ready to accept incoming connectionsaccept(): Blocks until a client connects, returns a new socket for that connectionsend()/recv(): Sends and receives data over the connection
HTTP Request Parsing
The server parses HTTP requests manually:
- Extracts the request method (GET/POST)
- Parses the requested URL path
- Extracts HTTP headers
- Handles request body for POST requests
Response Generation
The server constructs HTTP responses:
- Generates appropriate status codes (200 OK, 404 Not Found, etc.)
- Sets proper Content-Type headers based on file extension
- Serves file content or error pages
- Includes Connection headers for keep-alive support
Usage
# Compile the server
gcc -o webserver webserver.c -lpthread
# Run the server on port 8080
./webserver 8080
# Access the server
curl http://localhost:8080/
Project Structure
webserver_in_C/
├── webserver.c # Main server implementation
├── Makefile # Build configuration
├── public/ # Static files to serve
│ ├── index.html
│ └── styles.css
└── README.md # Documentation
Technical Details
Thread Pool Implementation
The thread pool uses producer-consumer pattern:
- Main thread acts as producer, adding connections to the queue
- Worker threads act as consumers, processing connections
- Mutex and condition variables synchronize access to the queue
- Bounded queue prevents memory exhaustion under heavy load
File Serving
The server determines file types by extension:
.html→text/html.css→text/css.js→application/javascript.jpg,.jpeg→image/jpeg.png→image/png.gif→image/gif
Error Handling
The server handles various error conditions:
- Socket creation failures
- Bind failures (port already in use)
- Connection errors
- File not found (404 errors)
- Permission denied errors
Learning Outcomes
This project provided deep insights into:
- Low-level network programming and socket API
- HTTP protocol implementation details
- Concurrent programming with threads
- Systems-level resource management
- Performance considerations in server design
- Security basics in network applications
Future Enhancements
Potential improvements for future versions:
- Support for HTTP/2
- CGI script execution
- HTTPS/TLS support
- Configuration file support
- More sophisticated logging
- Request throttling and rate limiting
- Virtual host support
Dependencies
- GCC compiler
- POSIX threads library (
-lpthread) - Linux/Unix operating system
References
- Beej's Guide to Network Programming
- RFC 2616 (HTTP/1.1)
- POSIX Threads Programming
