networking · beginner · ~15 min

Valid HTTP method?

Validate against the common HTTP methods.

Challenge

Check whether a string is one of the common HTTP methods a server accepts.

Task

Implement int is_http_method(const char *m).

Input

  • m: a NUL-terminated candidate method string.

Output

Return 1 if m is exactly one of GET, POST, PUT, DELETE, or HEAD (case-sensitive), else 0.

Example

is_http_method("GET")    ->  1
is_http_method("POST")   ->  1
is_http_method("FETCH")  ->  0

Input format

m: a NUL-terminated candidate method string.

Output format

1 if m is one of GET, POST, PUT, DELETE, HEAD; else 0.

Constraints

Exact, case-sensitive match against the five-method set.

Starter code

#include <string.h>

int is_http_method(const char *m) {
    /* TODO */
    return 0;
}

Common mistakes

Assuming HTTP request header fits in a single recv() buffer; not parsing \r\n line endings properly; failing to send valid HTTP response status.

Edge cases to handle

HTTP requests with headers split across packets; malformed headers lacking colon; empty lines; requests without Content-Length.

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.