Status codes and response headers
wired_http_handler fills a wired_http_exchange instead of just a body, so
it can set the response status and add header fields. This page redirects,
sets a cookie, and reads either an Authorization header or a cookie back to
decide what to answer.
Code
#define WIRED_MAIN#include "wired.h"
/* 1 if the request carries either the Bearer token or the session cookie * a prior /login round would have handed out. */static int authorized(const wired_h3reqdrive_req* req) { wired_span v; if (wired_http_req_header(req, wired_span_cstr("authorization"), &v) && wired_span_eq_cstr(v, "Bearer secret-token")) return 1; return wired_span_eq_cstr( wired_span_of(req->cookie, req->cookie_len), "session=granted");}
/* Three routes, one wired_http_handler each demonstrates: a 3xx with * Location, a Set-Cookie, and an authorization check reading either the * Authorization header or the cookie set by /login. */static int on_request(void* ctx, wired_http_exchange* x) { (void)ctx; wired_span path = wired_h3req_path(x->req); if (wired_span_eq_cstr(path, "/redirect")) { x->status = 302; wired_http_add_field(x, "location", "/target"); return 0; } if (wired_span_eq_cstr(path, "/login")) { wired_http_add_field(x, "set-cookie", "session=granted"); return wired_http_reply_text(x, 200, "logged in"); } if (wired_span_eq_cstr(path, "/secret")) return authorized(x->req) ? wired_http_reply_text(x, 200, "top secret") : wired_http_reply_text(x, 401, "unauthorized"); return wired_http_reply_text(x, 404, "not found");}
int wired_main(int argc, char** argv) { /* Fixed demo identity (self-signed certificate; never for production). */ static wired_srvboot_demo_keys keys; wired_srvboot_id id; wired_srvboot_demo(&id, &keys, 0x50, "guide-h3");
u16 port = (u16)wired_cliargs_int(argc, argv, "--port", 4433); wired_srvrun_handler h = {.http = on_request}; wired_srvrun_obs obs = {0}; return wired_server_run(port, &id, h, obs) ? 0 : 1;}Output
$ ./h3-status-headers --port 4433conn closed by peer (CONNECTION_CLOSE)conn closed by peer (CONNECTION_CLOSE)conn closed by peer (CONNECTION_CLOSE)conn closed by peer (CONNECTION_CLOSE)conn closed by peer (CONNECTION_CLOSE)$ probe h3req 127.0.0.1:4433 GET /redirectstatus 302location: /target$ probe h3req 127.0.0.1:4433 GET /loginstatus 200content-type: text/plainset-cookie: session=granted
logged in$ probe h3req 127.0.0.1:4433 GET /secretstatus 401content-type: text/plain
unauthorized$ probe h3req 127.0.0.1:4433 GET /secret -H Authorization: Bearer secret-tokenstatus 200content-type: text/plain
top secret$ probe h3req 127.0.0.1:4433 GET /secret -H cookie: session=grantedstatus 200content-type: text/plain
top secretHow it works
Section titled “How it works”/redirectsetsx->status = 302and onex->fields[0](location: /target), then returns 0 for an empty body./loginsets aset-cookiefield alongside a 200 body./secretcallswired_http_req_header(req, "authorization", &v)to read a regular header by name, and readsreq->cookie/req->cookie_lendirectly — a dedicated field, not looked up throughwired_http_req_header(see its doc comment).x->fieldsholds up toWIRED_HTTP_MAX_FIELDS(8) entries; each is awired_http_field(name, value), both lowercase on the wire (RFC 9114 4.2).- The probe client’s
h3req -H 'name: value'sends one extra request header, andCheckRedirectis disabled so it prints the 302 itself instead of silently following it to/target.