Project 20: HTTP Web Server

Project 20: HTTP Web Server

Build a small but real HTTP server in C: parse requests, serve static files, and run simple CGI-style dynamic handlers.

Quick Reference

Attribute Value
Difficulty Intermediate
Time Estimate 1-2 weeks
Language C (Alternatives: Rust, Go)
Prerequisites Basic sockets, robust I/O habits (P15 recommended)
Key Topics TCP sockets, HTTP parsing, MIME types, processes, CGI
CS:APP Chapters 10, 11 (plus 8 for fork/exec)

1. Learning Objectives

By completing this project, you will:

  1. Use the sockets API to accept connections and implement a request/response loop
  2. Parse HTTP request lines + headers defensively
  3. Serve static files with correct status lines and MIME types
  4. Implement a minimal dynamic endpoint mechanism (CGI-style fork/exec or in-process handlers)

2. Project Specification

2.1 What You Will Build

A CLI called tiny:

  • ./tiny <port> starts the server
  • Supports GET for static content under a document root
  • Optional: a cgi-bin/ directory where executables are invoked with env vars

2.2 Example Output

$ ./tiny 8080
listening=8080 docroot=./www

$ curl -i http://localhost:8080/index.html
HTTP/1.1 200 OK
Content-Type: text/html
...

3. Architecture

accept() -> read request -> route -> serve file / run handler -> write response -> close()

Key modules:

  • net.c: listen/accept helpers
  • http.c: parse request, build response headers
  • static.c: safe path join + file I/O + MIME mapping
  • cgi.c (optional): fork/exec, env vars, stdout->socket

4. Testing Strategy

  • Use curl -v, ab/wrk, and a browser.
  • Fuzz the request parser with malformed headers and long lines.
  • Add a test corpus for MIME mappings and path traversal (../) protection.

5. Extensions

  • Add keep-alive for HTTP/1.1 and measure performance differences.
  • Add a thread pool (P21) and compare thread-per-conn vs pool.
  • Add request logging with rotation and structured logs.

6. Reference Reading

  • CS:APP 3e — Ch. 11 (Network Programming), Ch. 10 (System I/O)