networking · intermediate · ~15 min
Read the method off the request line.
The first token of an HTTP request line is the method (GET, POST, ...). Extract it.
Implement int request_method(const char *req, char *out, int outsz) that copies the method — everything up to the first space — into out (bounded by outsz) and returns its length.
req: the request line (e.g. "GET /x HTTP/1.1").out, outsz: destination buffer for the method and its size.Returns the method length on success, or -1 if req contains no space.
request_method("GET /x HTTP/1.1", out, 16) -> 3, out="GET"
request_method("GET", out, 16) -> -1 (no space)
-1.outsz and NUL-terminate.The request line (req) and an output buffer (out) with size (outsz).
The method length on success, or -1 if there is no space.
Method is the first token up to the first space. Copy bounded by outsz and NUL-terminate; return -1 if no space.
#include <string.h>
int request_method(const char *req, char *out, int outsz) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.