Skip to content

Server From Sockets

You'll speak HTTP from the spec over raw sockets, serve files to a real browser, hold a thousand connections open and shrug off garbage.

About

You've used a web server a thousand times, so now build one from RFC 9112 and the socket calls you learned in the Echo Server badge. Parse the request by hand, serve files, keep many clients alive at once, and stay up when someone sends junk. The first thing a browser asks your server for is favicon.ico, which you never mentioned, and that's HTTP for you. Building a website is the Web Building track; this is about knowing what every framework is hiding from you, and it takes a few weeks of evenings.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
7
Takes
A month
Skill level
Advanced (years of practice)
Cost
Free
Supplies
You need some
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Systems Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Answer a browser with a hard-coded page from a raw socket. Accept the connection, read the request bytes, print them to the terminal, and write back a status line, headers and a body by hand. Take a screenshot of the browser showing your page and the terminal showing the raw request. Photo
  2. 2
    Parse the request line and headers from the spec and return 404 for unknown paths and 400 for garbage. Read the method, the path and the version, then the headers until the blank line, with a cap on line length so a giant header can't overrun your buffer. Send it junk with netcat and make sure it answers 400 instead of hanging or crashing. Take a screenshot of a 404 and a 400 from curl -v. Photo
  3. 3
    Serve static files from a folder with correct content types and no path traversal. Map the path to a file under one directory and resolve it with realpath so ../../etc/passwd fails. Set the content type for html, css, png and js from the extension. Take a screenshot of a page with an image loading and the traversal attempt rejected. Photo
  4. 4
    Handle many clients at once with poll, epoll or kqueue, or a thread pool. A slow client should never block a fast one. Hold one connection open with netcat and show that a curl in another terminal still gets served. Film a short clip of the slow and the fast request side by side. Video
  5. 5
    Hold a load test of 1,000 concurrent connections without crashing or leaking. Use wrk, ab or hey from another terminal, against localhost only. Never point a load tool at a machine you don't own, because it's an attack whatever you meant by it. Raise your file descriptor limit with ulimit -n first, and watch memory in top while it runs. Take a screenshot of the load tool's output and the memory before and after. Photo
  6. 6
    Add keep-alive and Range requests, so a browser can seek in a video you serve. For keep-alive, reuse the connection for several requests and honor Connection: close. For Range, answer a Range header with 206 and the right Content-Range, which is what makes seeking work. Take a screenshot of curl -v getting a 206, or film the browser seeking in the video. Photo or video
  7. 7
    Fuzz it with 10,000 malformed requests and show it never crashes or hangs. Write a script that sends random bytes, giant headers, a missing blank line, and a request that stops halfway. Every one should get a 400 or a closed connection within a second. Run the server under the address sanitizer while you do it, since a fuzzer finds memory bugs faster than anything else. Take a screenshot of the fuzzer's summary and a normal request succeeding afterward. Photo