Skip to content

Hello, HTTP/3

An HTTP/3 server is a request handler plus a server identity. This page answers every request with hello.

Code

main.c
#define WIRED_MAIN
#include "wired.h"
/* Called once per request (and again while *more is set). Fill body_out and
* return 1 to answer 200. */
static int on_request(
void* ctx,
const wired_h3reqdrive_req* req,
u64 offset,
wired_obuf* body_out,
const char** content_type,
int* more,
u64* total_size) {
(void)ctx;
(void)req;
(void)offset;
(void)more;
(void)total_size;
wired_log_str("request /\n");
*content_type = "text/plain";
wired_obuf_printf(body_out, "hello");
return 1;
}
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 = {.cb = on_request};
wired_srvrun_obs obs = {0};
return wired_server_run(port, &id, h, obs) ? 0 : 1;
}

Output

Terminal window
$ ./h3-hello --port 4433
request /
conn closed by peer (CONNECTION_CLOSE)
Terminal window
$ probe h3get 127.0.0.1:4433 /
GET / status=200 content-type=text/plain body="hello"
  • wired_server_run(port, &id, h, obs) runs QUIC, TLS 1.3 and HTTP/3 on a UDP port and calls your handler once per request.
  • wired_srvboot_id is the server identity: X25519 key pair, certificate signing seed, connection id and ServerHello.random. wired_srvboot_demo(&id, &keys, 0x50, "guide-h3") fills it with fixed demo bytes (a self-signed certificate; never use it in production). keys holds the key material id points at, so it is static.
  • The handler writes the body into body_out, sets *content_type, and returns 1. Returning 0 sends a body-less 200.
  • wired_cliargs_int reads --port from the command line.