networking · intermediate · ~15 min

Parse the request method

Read the method off the request line.

Challenge

Implement int request_method(const char *req, char *out, int outsz) copying the method (first token up to the first space) into out and returning its length, or -1 if there is no space.

Starter code

#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.