cybersecurity · intermediate · ~15 min

Parse an http/https URL safely

Practise parsing a foundational structure that's a popular attack surface.

Challenge

Given

typedef struct {
    char scheme[8];   // "http" or "https"
    char host[128];   // hostname (no port)
    int  port;        // 80/443 by default
    char path[256];   // "/" if not present
} url_t;

implement int parse_http_url(const char *url, url_t *out) that fills out from URLs of the form scheme://host[:port][/path]. Reject anything that isn't http/https, hosts longer than 127 chars, or invalid ports. Return 0 on success, -1 on failure.

Starter code

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

int parse_http_url(const char *url, url_t *out) {
    /* TODO */
    return -1;
}

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