networking · intermediate · ~15 min

Build an HTTP response

Emit a status line, Content-Length, and body.

Challenge

Implement int build_response(char *out, int outsz, int code, const char *body) writing HTTP/1.1 <code> OK\r\nContent-Length: <len>\r\n\r\n<body>. Return the length.

Starter code

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

int build_response(char *out, int outsz, int code, const char *body) {
    /* TODO */
    return 0;
}

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