ステータスコードとレスポンスヘッダー
wired_http_handler は、ボディだけを返すのではなく wired_http_exchange 全体を埋めるハンドラーです。
そのため、レスポンスのステータスを設定したり、ヘッダーフィールドを追加したりできます。
このページのサーバーは、リダイレクトとクッキーの発行を行い、Authorization ヘッダーまたはクッキーを読んで応答を決めます。
コード
#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;}実行結果
$ ./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 secret/redirectはx->status = 302を設定し、x->fields[0]にlocation: /targetを 1 つ入れ、 0 を返してボディなしの応答にします。/loginは 200 のボディと一緒にset-cookieフィールドを設定します。/secretはwired_http_req_header(req, "authorization", &v)で通常のヘッダーを名前で読み、req->cookieとreq->cookie_lenを直接読みます。 クッキーは専用のフィールドであり、wired_http_req_header経由では読めません(そのドキュメントコメントを参照)。x->fieldsには最大WIRED_HTTP_MAX_FIELDS(8)個まで入ります。 各要素はwired_http_field(name、value の組)で、どちらもワイヤー上(実際に送られるバイト列の上)では小文字です(RFC 9114 4.2)。- probe クライアントの
h3req -H 'name: value'は、追加のリクエストヘッダーを 1 つ送ります。 またCheckRedirectを無効にしているので、302 を隠したまま/targetへ自動で追従することはなく、302 そのものを表示します。