cybersecurity · intermediate · ~15 min · safe pentest lab

Extract the request path

Pull the path out of a request line.

Challenge

Pull the path out of an HTTP request line — the middle token of METHOD PATH VERSION — into a bounded buffer.

Task

Implement int request_path(const char *line, char *out, int outsz) that copies the second space-separated token (the path) of line into out, NUL-terminates it, and returns its length. Return -1 if line doesn't contain at least two spaces.

Input

  • line: a NUL-terminated request line the grader provides (e.g. "GET /admin/login HTTP/1.1").
  • out: the destination buffer.
  • outsz: the size of out in bytes.

Output

Returns the path length on success (with out holding the path), or -1 if the line is malformed (fewer than two spaces).

Example

request_path("GET /admin/login HTTP/1.1", out, 64)   ->   12, out = "/admin/login"
request_path("GET", out, 64)                         ->   -1   (no path token)

Edge cases

  • The path is the text between the first and second spaces.
  • If the path is longer than outsz - 1, copy what fits and NUL-terminate.

Input format

A request line, a destination out, and its size outsz.

Output format

The path length with out set; or -1 if line lacks two spaces.

Constraints

Copy the token between the first two spaces; NUL-terminate; -1 on malformed line.

Starter code

#include <string.h>

int request_path(const char *line, char *out, int outsz) {
    /* TODO */
    return -1;
}

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