コンテンツにスキップ

ステータスコードとレスポンスヘッダー

wired_http_handler は、ボディだけを返すのではなく wired_http_exchange 全体を埋めるハンドラーです。 そのため、レスポンスのステータスを設定したり、ヘッダーフィールドを追加したりできます。 このページのサーバーは、リダイレクトとクッキーの発行を行い、Authorization ヘッダーまたはクッキーを読んで応答を決めます。

コード

main.c
#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;
}

実行結果

Terminal window
$ ./h3-status-headers --port 4433
conn 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)
Terminal window
$ probe h3req 127.0.0.1:4433 GET /redirect
status 302
location: /target
Terminal window
$ probe h3req 127.0.0.1:4433 GET /login
status 200
content-type: text/plain
set-cookie: session=granted
logged in
Terminal window
$ probe h3req 127.0.0.1:4433 GET /secret
status 401
content-type: text/plain
unauthorized
Terminal window
$ probe h3req 127.0.0.1:4433 GET /secret -H Authorization: Bearer secret-token
status 200
content-type: text/plain
top secret
Terminal window
$ probe h3req 127.0.0.1:4433 GET /secret -H cookie: session=granted
status 200
content-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 そのものを表示します。