Networking in C · intermediate · ~15 min
Walk an HTTP request line and pull out method, path, and version into a struct.
Three tokens, bounded copies into a struct, reject on overflow or bad terminator.
The request line is where every HTTP attack starts. A bounded, allow-list parser stops the easy ones at the door.
Every web proxy, every WAF, every reverse-proxy access log starts the
same way: read the first line of an HTTP request and pull out three
tokens — method, request-target, version. The line is plain ASCII
ending in \r\n. The whole protocol is text-driven so a C parser is
small and worth reading.
This is the parser side of Burp / mitmproxy / nginx's access log — we just write it ourselves.
GET /index.html HTTP/1.1\r\n
Host: example.com\r\n
\r\n
The request line is three space-separated tokens, then \r\n.
Implement int parse_request_line(const char *buf, http_req_t *out)
where http_req_t has bounded method[8], path[256], version[16]
char arrays. Return 0 on success, -1 on any malformed input.
strcpy without
bounds.\r\n./, alphanumerics, ?, &, =, ., -,
_. Reject anything else for this exercise.sscanf("%s %s %s", ...) without length specifiers. That's an
uncontrolled write.\r\n terminator check.parse-http-smuggling-defence.Walk the line, split on spaces, copy into fixed-size fields with explicit length checks.