A minimal HTTP/1.1 server built from scratch in TypeScript (Bun) for the CodeCrafters "Build Your Own HTTP Server" challenge. No HTTP framework — just raw TCP via node:net, with parsing, routing, and response framing all handwritten.
- Routing:
GET /→200,GET /echo/<str>echoes the path segment,GET /user-agentreflects the header,GET /files/<name>serves bytes from--directory,POST /files/<name>writes the raw body →201, anything else →404 - Gzip negotiation:
Accept-Encoding: <list containing gzip>→gzipSync-compressed body withContent-Encoding: gzipand matchingContent-Length - Persistent connections: keep-alive by default with pipelining support;
Connection: closeis echoed and honored - Binary-safe bodies (
Bufferend to end), path-traversal-safe file serving, per-socket buffers for concurrent clients
app/main.ts— TCP server onlocalhost:4221. Each socket gets its own byte buffer; awhileloop drains every complete request in it, so sequential and pipelined requests are all answered. Sockets stay open unlessConnection: closeor an error occurs.app/parser.ts— splits the request line and headers into a case-insensitive map; bodies are sliced byContent-Lengthbytes.app/router.ts— route matching, file I/O, and gzip negotiation.app/response.ts— status line + headers built as text, concatenated with the body as a rawBuffer.
Requires Bun 1.3+ (see codecrafters.yml).
git clone git@github.com:usfmah/Build-an-HTTP-Server.git
cd Build-an-HTTP-Server
./your_program.sh [--directory /tmp/data]curl -v http://localhost:4221/echo/hello
curl -H "Accept-Encoding: gzip" --compressed http://localhost:4221/echo/hello
curl --http1.1 http://localhost:4221/echo/a --next http://localhost:4221/ -H "Connection: close"Submit to CodeCrafters with codecrafters submit.
14/14 stages complete.
- Bind to a port
- Respond with 200
- Extract URL path
- Respond with body
- Read header (
/user-agent) - Concurrent connections (per-socket buffer + event loop)
- Return a file (
GET /files/<filename>) - Read request body (
POST /files/<filename>→201) - HTTP Compression
- Compression headers
- Multiple compression schemes
- Gzip compression
- Persistent Connections
- Persistent connections
- Concurrent persistent connections
- Connection closure