cybersecurity · beginner · ~10 min
Tiny allow-list check, an easy primitive but extremely defensive.
Implement int is_valid_http_method(const char *m) returning 1 if m is exactly one of:
GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCHOtherwise return 0. The comparison is case-sensitive (HTTP methods are uppercase by convention).
is_valid_http_method("GET") -> 1
is_valid_http_method("POST") -> 1
is_valid_http_method("PATCH") -> 1
is_valid_http_method("TRACE") -> 0
is_valid_http_method("get") -> 0 // case sensitive
is_valid_http_method("CONNECT") -> 0 // intentionally excluded
is_valid_http_method("") -> 0
is_valid_http_method(NULL) -> 0
The first byte of every HTTP request is attacker-controlled. Refusing weird methods (TRACE, CONNECT, the legendary GETT) is a one-line defence that blocks an entire class of cache-pollution and request-smuggling tricks.
Null-terminated string (or NULL).
0/1.
Pure comparison, no allocations.
int is_valid_http_method(const char *m) { /* TODO */ return 0; }
Including TRACE (information-leak prone) or CONNECT (proxy tunneling). Doing a case-insensitive compare.
NULL; empty; lowercase; near-matches like GETT.
O(1) — constant work over a fixed table.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.