An HTTP server from the socket up
Stage 1 of 10v2 · 045d2696

Minimal HTTP response

Build an HTTP/1.1 200 response with exact length and CRLF separation.

Minimal HTTP response

Build an HTTP/1.1 200 response with exact length and CRLF separation. Keep behaviour accepted in earlier stages working.

Observable contract

The harness receives HTTP request bytes inside JSON and returns either the parsed result or response bytes. This tests the protocol without opening ports.

{
  "op": "respond",
  "status": 200,
  "body": "hello",
  "headers": { "Content-Type": "text/plain" }
}

produces exactly:

{
  "raw": "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nConnection: close\r\n\r\nhello"
}

The compact JSON response ends with a newline. If the harness request is invalid, leave stdout empty, include error on stderr, and exit non-zero.

Implementation

Keep framing, the HTTP parser, routing, and transport separate. Count bytes, not characters, for Content-Length; preserve CRLF where the protocol requires it and bound line, header, body, and time resources. Fixtures replace the socket, not HTTP logic.

Add manual cases for partial reads, repeated headers, incomplete bodies, and malicious targets. Never let one client's error contaminate another client's state.

Acceptance criteria

  • The representative case produces the exact output.
  • Incomplete input fails without hanging.
  • Results do not depend on language map ordering.