Project 6: HTTP/1.1 Server (From Scratch)

A web server that serves static files, handles multiple concurrent connections (using poll/select from Project 3), supports keep-alive, and returns proper HTTP status codes and headers.

Quick Reference

Attribute Value
Primary Language C++
Alternative Languages Rust, Go, C
Difficulty Level 3: Advanced
Time Estimate 2 weeks
Knowledge Area HTTP Protocol, Request Routing, Static File Serving
Tooling nginx/Apache alternative
Prerequisites Projects 3 and 5

What You Will Build

A web server that serves static files, handles multiple concurrent connections (using poll/select from Project 3), supports keep-alive, and returns proper HTTP status codes and headers.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Parsing HTTP requests → maps to handling partial reads, buffer management
  • Routing and serving files → maps to path validation, preventing directory traversal
  • MIME type detection → maps to file extension mapping
  • Proper error responses → maps to HTTP status codes, error pages

Key Concepts

  • HTTP Request Parsing: RFC 7230 Section 3
  • Static File Serving: “HTTP: The Definitive Guide” Chapter 18 - Gourley
  • Security (Path Traversal): Prevent /../../../etc/passwd attacks
  • MIME Types: “HTTP: The Definitive Guide” Chapter 17 - Gourley

Real-World Outcome

$ ./httpserver -p 8080 -d ./www
HTTP server listening on port 8080
Serving files from ./www

# In browser: http://localhost:8080/
# Or with curl:
$ curl -v http://localhost:8080/index.html
> GET /index.html HTTP/1.1
> Host: localhost:8080
>
< HTTP/1.1 200 OK
< Content-Type: text/html
< Content-Length: 1234
< Connection: keep-alive
<
<!DOCTYPE html>...

$ curl http://localhost:8080/notfound.html
HTTP/1.1 404 Not Found
Content-Type: text/html

<h1>404 Not Found</h1>

$ curl http://localhost:8080/../../../etc/passwd
HTTP/1.1 403 Forbidden

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: LEARN_CPP_NETWORK_PROGRAMMING.md
  • “HTTP: The Definitive Guide” by David Gourley