Skip to content

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

Build an HTTP Server

progress-banner

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.

Features

  • Routing: GET / → 200, GET /echo/<str> echoes the path segment, GET /user-agent reflects 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 with Content-Encoding: gzip and matching Content-Length
  • Persistent connections: keep-alive by default with pipelining support; Connection: close is echoed and honored
  • Binary-safe bodies (Buffer end to end), path-traversal-safe file serving, per-socket buffers for concurrent clients

Architecture

  • app/main.ts — TCP server on localhost:4221. Each socket gets its own byte buffer; a while loop drains every complete request in it, so sequential and pipelined requests are all answered. Sockets stay open unless Connection: close or an error occurs.
  • app/parser.ts — splits the request line and headers into a case-insensitive map; bodies are sliced by Content-Length bytes.
  • 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 raw Buffer.

Install

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]

Test

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.

Progress

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

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages