This project implements a small, iterative HTTP server in C, designed primarily as an educational exercise to deepen understanding of network programming concepts. It's built upon the principles discussed in chapters 11: Network Programming and 12: Concurrent Programming of the book Computer Systems: A Programmer's Perspective (CS:APP) .
It's a foundational server capable of handling basic HTTP requests and serving both static and dynamic content.
- HTTP/1.1 Support: Handles
GET
,HEAD
, andPOST
HTTP methods. - Static Content Serving: Serves HTML, images (JPG, PNG, GIF, MP4), and plain text files directly from a specified document root (
wwwroot
). - Dynamic Content (CGI): Executes CGI (Common Gateway Interface) programs for dynamic content generation. Includes example CGI scripts (
hello.c
andadder.c
) to demonstrate functionality like query string parsing and POST data handling. - Error Handling: Provides basic error responses (e.g., 400 Bad Request, 403 Forbidden, 404 Not Found, 500 Internal Server Error) and handles common network issues.
While master
branch is iterative (handling one client request at a time), this project also explored various concurrency models in dedicated branches:
concurrency-with-fork
: Process-based concurrency usingfork()
.concurrency-with-threads
: Thread-based concurrency usingpthreads
.concurrency-with-select
: Concurrency using I/O multiplexing withselect()
.pre-threading
: Pre-threaded (thread pool) model with a simple auto-scaling heuristic.
tiny.c
: The main server implementation. It accepts client connections, parses HTTP requests, and dispatches to appropriate handlers for static or dynamic content.mynetlib.c
/mynetlib.h
: A custom utility library providing wrappers around socket functions (clientConnect
,serverListen
,sendBytes
,receiveBytes
) and a buffered reader (buffered_reader_t
,bufReadLine
,bufReadBytes
) for efficient network I/O.cgi/hello.c
andcgi/adder.c
: Simple example CGI programs.Makefile
: Used to compile the server and the CGI programs.wwwroot/
: (Created by Makefile) The web document root directory for static files.wwwroot/cgi-bin/
: (Created by Makefile) The directory where compiled CGI executables are placed.
- Developed and tested on Linux
make
utilitygcc
C compiler
$ cd src
$ make clean
$ make
$ ./tiny 8080 # or another port number
Once the server is running, you can access it using a web browser or curl
.
curl http://localhost:8000/
curl http://localhost:8000/cgi-bin/hello
curl "http://localhost:8000/cgi-bin/adder?a=100&b=200"
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "a=50&b=75" http://localhost:8000/cgi-bin/adder
This project is an educational effort and, as such, has several intentional simplifications and limitations!!!
- Poor security
- Lacks robustness
- Inefficient large file serving
- Poor CGI input parsing
- No Persistent Connections
- No HTTPS/SSL/TLS
- No Advanced Features
- Etc.
For other HTTP server implementations with a different set of features, please check: