KatInterview

Build a Minimal HTTP/1.1 Server from Scratch

System DesignModel answer
HardModel answerSystem Designnetworkinghttpconcurrency

Last updated

Implement a bare-metal HTTP server using only the standard library: request parsing, routing, static files, concurrency, timeouts, and graceful shutdown.

The Task

Build a working HTTP/1.1 server in your language of choice using only the standard library — no frameworks, no third-party HTTP helpers.

Your server must satisfy all of the following requirements.

Configuration

  • The listening port is configurable (e.g. via a command-line flag or environment variable).

Protocol

  • Parse raw HTTP/1.1 requests: request line, headers, and optional body.
  • Support the GET and POST methods; respond with 405 Method Not Allowed for anything else on handled routes.

Routing

  • GET /health → 200 OK with a plain-text or JSON body indicating the server is alive.
  • POST /echo → 200 OK with the request body mirrored back verbatim, with the original Content-Type preserved.
  • Any unrecognised path → 404 Not Found.
  • Malformed requests that cannot be parsed → 400 Bad Request.

Static file serving

  • Serve files from a configurable root directory under a prefix such as /static/.
  • Set the Content-Type header based on file extension (e.g. .html, .css, .js, .png, .json).
  • Reject path-traversal attempts (e.g. ../../etc/passwd) with 400 or 403.

Concurrency

  • Handle multiple simultaneous connections without blocking.

Reliability

  • Enforce per-connection read/write timeouts so a slow client cannot hold a goroutine/thread indefinitely.
  • On SIGINT/SIGTERM, stop accepting new connections and allow in-flight requests to finish before exiting (graceful shutdown).

Observability

  • Emit a structured log line for every request: timestamp, method, path, status code, and response duration.

Testing

  • Include unit tests for at least: request parsing, routing logic, the path-traversal guard, and the echo handler.

Deliverable Provide the full source code and a short README section with instructions to build and run the server locally. The reviewer should be able to clone, run, and curl all endpoints in under two minutes.


What the interviewer is looking for

  • Correct, self-contained HTTP parsing (you understand what a raw request looks like).
  • Clean separation of concerns: listener, parser, router, handler, logger.
  • Concrete concurrency model — explain why you chose it, not just that you used it.
  • Edge cases handled explicitly: empty body on POST, unknown MIME type, file not found, path traversal.
  • Tests that target logic, not just happy-path integration.
  • Operational thinking: timeouts, shutdown, structured logs.

Hints

Hint 1

Start by sketching the lifecycle of a single request — accept socket → read bytes → parse → route → handle → write response → close. Implementing each stage as a discrete function keeps the code testable and easy to reason about.

Hint 2

For path traversal, clean the requested path with your standard library's path-normalisation function (e.g. filepath.Clean in Go, os.path.realpath in Python) and then verify the result still starts with the configured static root before opening any file.

Hint 3

Graceful shutdown is a two-step operation: first stop the listener so no new connections arrive, then wait for active handlers to finish. A WaitGroup (or equivalent) around your handler goroutines/threads is the simplest correct implementation.