networking · intermediate · ~15 min
Robust string parsing, fail-fast on malformed input.
Pull the numeric status code out of an HTTP status line, rejecting anything malformed.
Implement int parse_status_line(const char *line, int *out_status) that parses a status line such as HTTP/1.1 200 OK\r\n and writes the 3-digit status code into *out_status. No main — the grader calls it. This is a pure parser; there is no network call.
line: a candidate HTTP status line (NUL-terminated).out_status: pointer that receives the parsed status code.Returns 0 and sets *out_status on success; returns -1 on malformed input (and leaves the status unset).
"HTTP/1.1 200 OK\r\n" -> 0, *out_status = 200
"HTTP/1.0 404 Not Found\r\n" -> 0, *out_status = 404
"FOO/1.1 200 OK\r\n" -> -1
"HTTP/1.1 abc OK\r\n" -> -1
-1.HTTP/ returns -1.HTTP/ prefix, then exactly three decimal digits after the first space.A candidate status line and an out_status pointer.
0 with *out_status set on success; -1 on malformed input.
Require the HTTP/ prefix and exactly three decimal digits; pure parser.
#include <stddef.h>
int parse_status_line(const char *line, int *out_status) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.