networking · intermediate · ~15 min
Construct a valid HTTP request line + Host header.
Assemble the bytes of a minimal HTTP/1.1 GET request as a string.
Implement int build_get(char *out, int outsz, const char *host, const char *path) that writes this exact request into out:
GET <path> HTTP/1.1\r\nHost: <host>\r\n\r\n
out, outsz: destination buffer and its size.host, path: NUL-terminated strings.Return the length of the request string written.
build_get(b, 256, "example.com", "/index.html")
-> b = "GET /index.html HTTP/1.1\r\nHost: example.com\r\n\r\n"
\r\n\r\n) to terminate the headers.out/outsz: destination buffer and size; host and path: NUL-terminated strings.
The length of the request string written into out.
Use the exact CRLF layout and end with a blank line.
#include <stdio.h>
int build_get(char *out, int outsz, const char *host, const char *path) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.