Master the fundamental concepts of protocol implementation through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksHTTP/1.1 defaults to keep-alive: one TCP connection on port 80 serves multiple requests. For example, a browser fetches /index.html, then /style.css, then /logo.png over the same socket without repeating the three-way handshake.
The client must know where each response ends:
Content-Length: 1234 tells the client to read exactly 1234 bytesTransfer-Encoding: chunked is the alternative for dynamic contentEither side can terminate with Connection: close.
192.168.1.10:80Connection: keep-aliveContent-LengthConnection: close to endPipelining (sending multiple requests before reading responses) was part of HTTP/1.1 but rarely used in practice because head-of-line blocking on TCP made it risky. Modern browsers open several parallel connections to the same host on port 443 instead of pipelining on one socket.
This task requires you to handle multiple requests on one connection. The Slowloris attack from 2009 abused this lifecycle by opening many keep-alive connections and trickling headers slowly to exhaust server connection pools. Production configs like nginx's keepalive_timeout and Apache's KeepAliveTimeout tune the same Connection header behavior you implement here on port 80.
Write a C program that simulates the connection-lifecycle state machine of an HTTP/1.1 keep-alive server, processing a pipelined stream of requests read from stdin.
Input: HTTP requests in wire format separated by blank lines: each request is a request line (e.g. GET /index.html HTTP/1.1) followed by header lines. The last request may end at EOF without a trailing blank line.
For each request, decide whether the connection stays open after the response, per RFC 9112 section 9.3:
Connection header containing the close token closes the connection after that responseConnection header contains the keep-alive token
Header names, values, and connection tokens are case-insensitive; a Connection value may be a comma-separated token list (e.g. keep-alive, upgrade)Output:
req=N keep-alive or req=N close (N is 1-based)requests=N: total requests served on the connectionExample: an HTTP/1.0 request with no Connection header is served and immediately closes, so requests=1 even if more requests follow in the stream.