cybersecurity · intermediate · ~15 min · safe pentest lab

Parse HTTP response headers

Read structured headers out of HTTP response text.

Challenge

Pull a named header value out of an HTTP response — reading structured headers from raw response text.

Task

Implement int find_header(const char *response, const char *name, char *out, size_t out_sz) that finds the header name (case-insensitive) and writes its trimmed value to out.

Input

  • response: a NUL-terminated HTTP response (status line, CRLF-separated headers, a blank line, then the body). The grader passes a fixed string.
  • name: the header name to look up (matched case-insensitively).
  • out, out_sz: the output buffer and its capacity.

Output

Returns int: 0 if the header exists (writing its trimmed value to out), -1 otherwise.

Example

find_header(resp, "Server", out, sz)         ->   0, out = "nginx"
find_header(resp, "content-length", out, sz) ->   0, out = "42"   (case-insensitive)
find_header(resp, "X-Missing", out, sz)      ->   -1

Edge cases

  • A missing header returns -1.
  • Stop scanning at the blank line that ends the header block.

Rules

  • Compare header names case-insensitively (e.g. strncasecmp).
  • Trim leading whitespace from the value before copying.

Input format

A NUL-terminated HTTP response, a header name, and an output buffer with its size.

Output format

An int: 0 with the trimmed value in out if found, else -1.

Constraints

Case-insensitive name match; trim the value; stop at the blank line.

Starter code

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stddef.h>

int find_header(const char *response, const char *name, char *out, size_t out_sz) {
    /* TODO */
    return -1;
}

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