An HTTP server from the socket up
Stage 5 of 10v2 · 3a636bb7

Body and errors

Read Content-Length exactly and generate well-formed 400, 404, and 405 responses.

Body and errors

Read Content-Length exactly and generate well-formed 400, 404, and 405 responses. 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": "split-body",
  "raw": "POST / HTTP/1.1\r\nContent-Length: 4\r\n\r\ntestNEXT"
}

produces exactly:

{ "body": "test", "remaining": "NEXT" }

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.