Something went wrong. Try again.
An HTTP library for Zig 0.17, server and client, HTTP/1.0 through HTTP/3, with the protocol in a module that owns no socket.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! An HTTP/3 client: RFC 9114 from the end that asks.//!//! Written against `transport.Session` and nothing else, which is the same//! seam the server driver uses -- so this works over a real QUIC connection//! from `http_io`, and over a scripted one in a test, without knowing the//! difference. That the two drivers share a seam is not tidiness: it is what//! made a client possible at all without a second QUIC implementation.//!//! ## What HTTP/3 takes away//!//! Most of what the HTTP/2 client does is absent here, and each absence is//! §4.6 handing the job to QUIC. No connection preface, because the handshake//! is one. No stream table, because streams are the transport's. No flow//! control and no WINDOW_UPDATE -- the two faults nginx found in the HTTP/2//! client were both in code that has no counterpart in this file. No//! HPACK-style shared decoder state to lose, because §3.2.2 of RFC 9204 with//! a dynamic table capacity of zero means no field section can depend on an//! insertion.//!//! What is left is: open the control stream, say what the settings are, put a//! HEADERS frame and some DATA frames on a bidirectional stream, and read the//! same shape back -- and, in `run`, listen to what the server's own control//! stream says, which is how a server's GOAWAY reaches the requests it//! applies to.
const std = @import("std");const Io = std.Io;const Allocator = std.mem.Allocator;
const quic = @import("quic");const protocol = @import("../protocol.zig");const coding = protocol.coding;const transport = @import("../transport.zig");const frame = @import("frame.zig");const qpack = @import("qpack.zig");const Request = @import("../client/Request.zig");const Response = @import("../client/Response.zig");
const Field = protocol.Field;const Headers = protocol.Headers;
pub const Config = struct { /// The largest field section this client will accept. max_field_section_size: u64 = 64 * 1024, /// The most fields a response may carry. max_fields: usize = 64, /// How many interim responses may come ahead of the final one. See /// `http1.client.Config.max_interim_responses`. max_interim_responses: u8 = 16, /// The buffer a field section is decoded through. decode_scratch_len: usize = 16 * 1024, /// The most body this client will hold for one response. max_body_len: usize = 8 * 1024 * 1024, /// Content codings to ask for, and to undo when a response carries one. /// The mirror of the other two drivers; `http1.client.Config` has the /// argument for why it is off by default and what it costs when it is not. accept_encodings: []const coding.Coding = &.{},};
pub const Error = error{ /// The session would not carry what was asked of it. StreamUnavailable, /// A `Content-Encoding` this end asked for and then could not undo: the /// octets are not that coding, or what they decode to is larger than /// `Config.max_body_len`. BadContentEncoding, /// The connection failed while the request was going out. WriteFailed, /// The connection failed, or ended, before the response was complete. ReadFailed, /// The response was not something this can read: a stream that did not /// open with HEADERS, a field section that did not decode, or a message /// with no `:status`. MalformedResponse, /// The response head or body was larger than this client will hold. ResponseTooLarge, /// More interim responses arrived than `max_interim_responses` allows. /// The stream is reset with H3_REQUEST_CANCELLED. TooManyInterimResponses, /// The request cannot be sent as described. InvalidRequest, /// The request's field section is larger than the server's /// SETTINGS_MAX_FIELD_SECTION_SIZE, which §4.2.2 says not to send. No /// stream was opened for it. RequestHeadersTooLarge, /// `Request.source` ended before the length the request declared. The /// stream is reset with H3_REQUEST_CANCELLED, since what went out of it is /// not a whole message. RequestBodyShort, /// `Request.source` failed while the body was being sent -- the reader's /// failure, not the connection's, which is `WriteFailed`. The stream is /// reset the same way. RequestBodyReadFailed, /// The server has sent GOAWAY, so it will process no request this /// connection has not already sent, and this one was not sent. §5.2 makes /// it safe to send again on a new connection. GoingAway, OutOfMemory,};
/// One HTTP/3 connection.pub const Connection = struct { gpa: Allocator, session: transport.Session, config: Config, /// §6.2.1: this endpoint's control stream, which may not close for the /// life of the connection -- so it is held here rather than in whatever /// opened it. control: ?transport.Stream = null, /// §5.2: the first request stream the server's GOAWAY said it will not /// process, or `no_goaway` before one has arrived. Written by `run`, read /// by `send` on whatever task the caller sends from. goaway: std.atomic.Value(u64) = .init(no_goaway), /// §6.2.1: whether the server has opened its control stream, of which /// there may be only one. control_in_seen: std.atomic.Value(bool) = .init(false), /// §7.2.4.1: the server's SETTINGS_MAX_FIELD_SECTION_SIZE, which is /// unlimited until its SETTINGS says otherwise. Written by `run`, read by /// `send`, as `goaway` is. peer_max_field_section_size: std.atomic.Value(u64) = .init(std.math.maxInt(u64)),
const no_goaway = std.math.maxInt(u64);
/// A connection over `session`, which has to be established already. /// Nothing is sent until `start`. /// /// There is no `deinit`: a connection owns no memory of its own, and the /// session is the caller's to close. pub fn init(gpa: Allocator, session: transport.Session, config: Config) Connection { return .{ .gpa = gpa, .session = session, .config = config }; }
test init { var scripted: transport.memory_session.Scripted = .{ .incoming = &.{} }; defer scripted.deinit(std.testing.allocator); const c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{ .max_body_len = 64 * 1024, }); try std.testing.expectEqual(@as(usize, 64 * 1024), c.config.max_body_len); // Nothing opened yet. try std.testing.expect(c.control == null); try std.testing.expect(scripted.local(0) == null); }
/// §6.2: opens the control stream and the two QPACK streams, and sends /// SETTINGS. /// /// Separate from `init` because it writes, and because §6.2 requires it /// before anything else: "each side MUST initiate a single control stream /// at the beginning of the connection and send its SETTINGS frame as the /// first frame on this stream". pub fn start(c: *Connection) Error!void { const control = c.session.openUnidirectional() catch return error.StreamUnavailable; c.control = control;
var buffer: [64]u8 = undefined; var w = control.writer(&buffer); writeVarint(&w.interface, @backingInt(frame.StreamType.control)) catch return error.WriteFailed; c.writeSettings(&w.interface) catch return error.WriteFailed; w.interface.flush() catch return error.WriteFailed;
// §4.2 of RFC 9204: both QPACK streams have to exist even though this // client never inserts and so never sends on either. A peer that // waited for them before encoding would wait for ever. inline for (.{ frame.StreamType.qpack_encoder, frame.StreamType.qpack_decoder }) |kind| { const s = c.session.openUnidirectional() catch return error.StreamUnavailable; var b: [8]u8 = undefined; var sw = s.writer(&b); writeVarint(&sw.interface, @backingInt(kind)) catch return error.WriteFailed; sw.interface.flush() catch return error.WriteFailed; } }
test start { var scripted: transport.memory_session.Scripted = .{ .incoming = &.{} }; defer scripted.deinit(std.testing.allocator); var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); try c.start();
// The control stream first, opening with its type and then SETTINGS. const control = scripted.local(0).?.written.items; try std.testing.expectEqual(@as(u8, @backingInt(frame.StreamType.control)), control[0]); try std.testing.expectEqual(@as(u8, @backingInt(frame.Kind.settings)), control[1]); // Then the two QPACK streams, which say what they are and nothing more. try std.testing.expectEqualSlices(u8, &.{0x02}, scripted.local(1).?.written.items); try std.testing.expectEqualSlices(u8, &.{0x03}, scripted.local(2).?.written.items); }
fn writeSettings(c: *Connection, w: *Io.Writer) !void { var body: [32]u8 = undefined; var bw: Io.Writer = .fixed(&body); // §7.2.4.1: the largest field section this end will accept, and a // QPACK capacity of zero -- which §3.2.2 of RFC 9204 turns into a // promise the peer may not insert, so no field section can ever // depend on the dynamic table. try writeVarint(&bw, 0x06); // SETTINGS_MAX_FIELD_SECTION_SIZE try writeVarint(&bw, c.config.max_field_section_size); try writeVarint(&bw, 0x01); // SETTINGS_QPACK_MAX_TABLE_CAPACITY try writeVarint(&bw, 0);
const payload = bw.buffered(); try writeVarint(w, @backingInt(frame.Kind.settings)); try writeVarint(w, payload.len); try w.writeAll(payload); }
/// Sends a request and reads the whole response. /// /// One stream, opened and finished here. Concurrency is the caller's: /// several of these on separate tasks are several QUIC streams, which is /// what §4.6 hands to the transport and the reason this file has no /// stream table of its own. /// /// A request `Request.validate` refuses, whose body does not match its /// declared length, or with an empty `host` is `InvalidRequest` before any /// stream is opened for it. The body is gathered whole, up to /// `Config.max_body_len`, and can be read through `response.body.reader` /// as over any other version or taken as a slice from `Exchange.body`. The /// `Exchange` owns it and every slice in the response, and is the caller's /// to `deinit`. pub fn send(c: *Connection, request: Request) Error!Exchange { // Everything refusable about the request itself, before a stream is // opened for it: a request refused afterwards leaves a stream that // was counted against the peer's limit, carries nothing, and has no // caller left holding it to reset. try request.validate(); // One body, of the length it says, before a stream is spent on it. _ = try request.declaredContentLength(); // §4.3.1 puts the authority in `:authority`, and a request with none // is one no origin can be chosen for. if (request.host.len == 0) return error.InvalidRequest; // §5.2: a server that has sent GOAWAY processes nothing at or above // the identifier it named, and every stream opened from here on is // above it. Refusing before one is opened leaves the caller free to // send the request elsewhere without wondering whether it happened. if (c.goaway.load(.acquire) != no_goaway) return error.GoingAway;
// The field section before the stream as well: §4.2.2 has a client // that knows the server's limit not send a section over it, and one // refused once its stream is open is a stream spent on nothing. const head = try c.encodeHead(request); defer c.gpa.free(head.section); if (head.size > c.peer_max_field_section_size.load(.acquire)) return error.RequestHeadersTooLarge;
const stream = c.session.openBidirectional() catch return error.StreamUnavailable; return c.sendOn(stream, request, head); }
test send { // What the server sends back: a HEADERS frame carrying `:status 200` // (QPACK static index 25), then a DATA frame of "hi". const answer = [_]u8{ 0x01, 0x03, 0x00, 0x00, 0xc0 | 25, 0x00, 0x02, 'h', 'i' }; var scripted: transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = &answer }; defer scripted.deinit(std.testing.allocator);
var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); try c.start(); var exchange = try c.send(.{ .host = "example.com", .target = "/" }); defer exchange.deinit(std.testing.allocator);
try std.testing.expectEqual(@as(u16, 200), exchange.response.status); try std.testing.expectEqual(protocol.Version.http_3, exchange.response.version); try std.testing.expectEqualStrings("hi", exchange.body()); // And through the reader, as a response over any version is read. const body_read = try exchange.response.body.readAlloc(std.testing.allocator, 64); defer std.testing.allocator.free(body_read); try std.testing.expectEqualStrings("hi", body_read);
// §4.3.1: no authority, no origin to send it to. try std.testing.expectError(error.InvalidRequest, c.send(.{ .host = "", .target = "/" })); }
test "RFC 9114 7.2: what a response stream may not carry closes the connection" { const Case = struct { frame: []const u8, code: ?frame.Error }; const cases = [_]Case{ // §7.2.5: PUSH_PROMISE for push 0, to a client that sent no // MAX_PUSH_ID and so allowed none. .{ .frame = &.{ 0x05, 0x01, 0x00 }, .code = .id_error }, // §7.2.6: GOAWAY belongs on the control stream. .{ .frame = &.{ 0x07, 0x01, 0x00 }, .code = .frame_unexpected }, // §7.2.8: HTTP/2's PING, which HTTP/3 does not have. .{ .frame = &.{ 0x06, 0x00 }, .code = .frame_unexpected }, // §9: a reserved type of §7.2.8's other kind, which is ignored. .{ .frame = &.{ 0x21, 0x00 }, .code = null }, }; for (cases) |case| { var answer: [32]u8 = undefined; const head = [_]u8{ 0x01, 0x03, 0x00, 0x00, 0xc0 | 25 }; @memcpy(answer[0..head.len], &head); @memcpy(answer[head.len..][0..case.frame.len], case.frame); var scripted: transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = answer[0 .. head.len + case.frame.len], }; defer scripted.deinit(std.testing.allocator);
var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); try c.start(); if (case.code) |code| { try std.testing.expectError(error.MalformedResponse, c.send(.{ .host = "example.com" })); try std.testing.expectEqual(@as(u64, @backingInt(code)), scripted.closed.?.code); } else { var exchange = try c.send(.{ .host = "example.com" }); exchange.deinit(std.testing.allocator); try std.testing.expect(scripted.closed == null); } } }
/// Reads the streams the server opens, until the session ends or this is /// cancelled: §6.2's control stream and the two QPACK streams, which every /// server opens, and anything else, which it should not. /// /// A caller that never runs this still gets its responses -- they come /// back on the streams `send` opened -- but never hears the server's /// GOAWAY, so goes on sending requests the server has said it will not /// process, and never notices a server breaking §6.2's rules. Run it on a /// task of its own beside the requests, as the reader of a socket is run. /// `io` gives each stream it reads a task, since the control stream does /// not end while the connection lasts. pub fn run(c: *Connection, io: Io) void { var tasks: Io.Group = .init; defer tasks.cancel(io); while (c.session.accept()) |stream| { // §6.1: "clients MUST treat receipt of a server-initiated // bidirectional stream as a connection error of type // H3_STREAM_CREATION_ERROR unless such an extension has been // negotiated", and none has. if (stream.id.directionality() == .bidirectional) { c.closeFor(.stream_creation_error, "a server-initiated bidirectional stream"); return; } tasks.concurrent(io, readUnidirectional, .{ c, stream }) catch c.readUnidirectional(stream); } }
test run { // The server's control stream: its type, then an empty SETTINGS, then // a GOAWAY naming request stream 0 as the first it will not process. var scripted: transport.memory_session.Scripted = .{ .incoming = &.{ .{ .id = .{ .value = 3 }, .data = &.{ 0x00, 0x04, 0x00, 0x07, 0x01, 0x00 } }, } }; defer scripted.deinit(std.testing.allocator); var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); c.run(std.testing.io);
// So nothing is sent: the server has said it will not be processed. try std.testing.expectError(error.GoingAway, c.send(.{ .host = "example.com" })); }
test "RFC 9114 4.2.2: a field section over the server's limit is not sent" { const gpa = std.testing.allocator; // SETTINGS_MAX_FIELD_SECTION_SIZE of 200. A GET for `/` at // example.com is 42 + 44 + 53 + 38 = 177 by §4.2.2's count. const answer = [_]u8{ 0x01, 0x03, 0x00, 0x00, 0xc0 | 25 }; var scripted: transport.memory_session.Scripted = .{ .incoming = &.{ .{ .id = .{ .value = 3 }, .data = &.{ 0x00, 0x04, 0x03, 0x06, 0x40, 0xc8 } }, }, .answer = &answer }; defer scripted.deinit(gpa); var c: Connection = .init(gpa, scripted.session(gpa), .{}); c.run(std.testing.io); try std.testing.expectEqual(@as(u64, 200), c.peer_max_field_section_size.load(.acquire));
// One field of 5 + 64 + 32 more is over, and no stream is opened. var headers: Headers = .empty; defer headers.deinit(gpa); try headers.append(gpa, "x-big", &@as([64]u8, @splat('a'))); try std.testing.expectError( error.RequestHeadersTooLarge, c.send(.{ .host = "example.com", .target = "/", .headers = headers }), ); try std.testing.expectEqual(@as(u64, 0), scripted.next_bidi);
// Within it, the request goes out as ever. var exchange = try c.send(.{ .host = "example.com", .target = "/" }); defer exchange.deinit(gpa); try std.testing.expectEqual(@as(u16, 200), exchange.response.status); }
/// §6.2: a unidirectional stream says what it is in its first varint. fn readUnidirectional(c: *Connection, stream: transport.Stream) void { var buffer: [1024]u8 = undefined; var r = stream.reader(&buffer); const kind = readVarint(&r.interface) catch return; switch (@as(frame.StreamType, @fromBackingInt(kind))) { .control => { // §6.2.1: "only one control stream per peer is permitted". if (c.control_in_seen.swap(true, .acq_rel)) { c.closeFor(.stream_creation_error, "a second control stream"); return; } if (!c.readControl(&r.interface)) return; }, // §4.2 of RFC 9204: this client advertised a dynamic table of no // capacity, so the only instruction the server's encoder may send // is to set the capacity to zero -- `001` and a five-bit prefix of // zero. Anything else inserts into a table that was refused. .qpack_encoder => while (true) { const b = r.interface.takeByte() catch |err| switch (err) { error.EndOfStream => break, else => return, }; if (b != 0x20) { c.closeFor(.qpack_encoder_stream_error, "an insertion into a table of zero capacity"); return; } }, // Acknowledgements of insertions this client never made, read // only to notice the stream ending. .qpack_decoder => _ = r.interface.discardRemaining() catch return, // §4.6: a push stream needs a push ID no greater than the client's // MAX_PUSH_ID, and this client sends none, so "H3_ID_ERROR". .push => { c.closeFor(.id_error, "a push stream, with no MAX_PUSH_ID sent"); return; }, // §6.2: "recipients of unknown stream types MUST either abort // reading of the stream or discard incoming data", and aborting // spares the server sending what will not be read. _ => { c.session.stopSending(stream.id, @backingInt(frame.Error.stream_creation_error)); return; }, } // §6.2.1 for the control stream and §4.2 of RFC 9204 for the other // two: "closure of either unidirectional stream type MUST be treated // as a connection error of type H3_CLOSED_CRITICAL_STREAM". c.closeFor(.closed_critical_stream, "the server closed a critical stream"); }
/// §7.2: the server's control stream, after its type. True when the /// stream ended, which the caller answers; false when a read failed -- /// the connection going, which breaks no rule -- or after closing the /// connection over something the server sent on it. fn readControl(c: *Connection, r: *Io.Reader) bool { var settings_seen = false; while (true) { const kind = readVarint(r) catch |err| return err == error.EndOfStream; const length = readVarint(r) catch |err| return err == error.EndOfStream; const f: frame.Kind = @fromBackingInt(kind); if (!f.allowedOnControlStream()) { c.closeFor(.frame_unexpected, "a frame not allowed on the control stream"); return false; } // §7.2.4: SETTINGS first, and only once. if (!settings_seen) { if (f != .settings) { c.closeFor(.missing_settings, "the control stream did not open with SETTINGS"); return false; } settings_seen = true; } else if (f == .settings) { c.closeFor(.frame_unexpected, "a second SETTINGS frame"); return false; } switch (f) { .goaway => if (!c.onGoaway(r, length)) return false, // §7.2.7: "a client MUST treat the receipt of a MAX_PUSH_ID // frame as a connection error of type H3_FRAME_UNEXPECTED". .max_push_id => { c.closeFor(.frame_unexpected, "a MAX_PUSH_ID from a server"); return false; }, // §7.2.3: a push ID above the client's maximum, and with no // MAX_PUSH_ID sent, every push ID is. .cancel_push => { c.closeFor(.id_error, "a CANCEL_PUSH, with no MAX_PUSH_ID sent"); return false; }, // §4.2.2: the server's limit on a field section is the one // setting that changes what this client sends, since it // inserts into no table and asks for no extension. Checked, // all the same, for the two ways §7.2.4 makes a SETTINGS a // connection error. .settings => { const settings = frame.readSettings(r, length) catch |err| { switch (err) { error.Malformed => c.closeFor(.frame_error, "a malformed SETTINGS frame"), error.ReservedSetting => c.closeFor(.settings_error, "an HTTP/2 setting in SETTINGS"), error.ReadFailed => {}, } return false; }; if (settings.max_field_section_size) |limit| c.peer_max_field_section_size.store(limit, .release); }, // Read past, as a stream is. else => r.discardAll64(length) catch |err| return err == error.EndOfStream, } } }
/// §7.2.6: a GOAWAY from the server, which names a client-initiated /// bidirectional stream and may only ever name a lower one than before. /// False when it broke a rule and the connection is closed. fn onGoaway(c: *Connection, r: *Io.Reader, length: u64) bool { const first = r.peekByte() catch return false; const id_len = @as(usize, 1) << @intCast(first >> 6); if (id_len != length) { c.closeFor(.frame_error, "a GOAWAY whose payload is not one varint"); return false; } const id = readVarint(r) catch return false; // §5.2: "in the server-to-client direction, it carries a QUIC stream // ID for a client-initiated bidirectional stream", and §7.2.6: an // endpoint "MUST NOT increase the value" in a later GOAWAY. const before = c.goaway.load(.acquire); if (id % 4 != 0 or id > before) { c.closeFor(.id_error, "a GOAWAY naming an identifier it may not"); return false; } c.goaway.store(id, .release); return true; }
test "RFC 9114 6.2 and 7.2: what a server's streams may not carry closes the connection" { const Case = struct { stream: []const u8, code: frame.Error, id: u64 = 3 }; const cases = [_]Case{ // §7.2.4: a control stream that opens with anything but SETTINGS. .{ .stream = &.{ 0x00, 0x07, 0x01, 0x00 }, .code = .missing_settings }, // §7.2.4: a second SETTINGS. .{ .stream = &.{ 0x00, 0x04, 0x00, 0x04, 0x00 }, .code = .frame_unexpected }, // §7.2.7: MAX_PUSH_ID is the client's to send. .{ .stream = &.{ 0x00, 0x04, 0x00, 0x0d, 0x01, 0x00 }, .code = .frame_unexpected }, // §7.2.6: a GOAWAY naming a stream no client opens -- 1 is // server-initiated. .{ .stream = &.{ 0x00, 0x04, 0x00, 0x07, 0x01, 0x01 }, .code = .id_error }, // §4.6: a push stream, to a client that allowed no pushes. .{ .stream = &.{ 0x01, 0x00 }, .code = .id_error }, // §4.2 of RFC 9204: an insertion, into a table of no capacity. .{ .stream = &.{ 0x02, 0xc0 }, .code = .qpack_encoder_stream_error }, // §6.1: a bidirectional stream the server opened. .{ .stream = &.{0x00}, .code = .stream_creation_error, .id = 1 }, // §6.2.1: a control stream that ends. .{ .stream = &.{ 0x00, 0x04, 0x00 }, .code = .closed_critical_stream }, }; for (cases) |case| { var scripted: transport.memory_session.Scripted = .{ .incoming = &.{ .{ .id = .{ .value = case.id }, .data = case.stream }, } }; defer scripted.deinit(std.testing.allocator); var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); c.run(std.testing.io); try std.testing.expectEqual(@as(u64, @backingInt(case.code)), scripted.closed.?.code); } }
/// A request's field section, QPACK-encoded, with what §4.2.2 counts it /// as. `section` is the caller's to free. const Head = struct { section: []u8, /// §4.2.2: "the sum of the size of the name and value in bytes plus /// an overhead of 32 bytes for each field", uncompressed. size: u64, /// §10.1.1 of RFC 9110: the content is held back for a /// `100 Continue`. expecting: bool, };
fn encodeHead(c: *Connection, request: Request) Error!Head { var sw: Io.Writer.Allocating = .init(c.gpa); defer sw.deinit();
var fields: std.ArrayList(qpack.Field) = .empty; defer fields.deinit(c.gpa); // §4.3.1: the same control data HTTP/2 carries, in the same // pseudo-header fields, before every ordinary one. fields.append(c.gpa, .{ .name = ":method", .value = request.method.name }) catch return error.OutOfMemory; fields.append(c.gpa, .{ .name = ":scheme", .value = "https" }) catch return error.OutOfMemory; fields.append(c.gpa, .{ .name = ":authority", .value = request.host }) catch return error.OutOfMemory; fields.append(c.gpa, .{ .name = ":path", .value = request.target }) catch return error.OutOfMemory; // §4.1.2: optional here, because the stream's end delimits the // content -- and sent anyway when the length is known, because §8.6 of // RFC 9110 asks for it and because a server may refuse a body without // one. `Request.declaredContentLength` is where that decision lives, // shared with the HTTP/2 driver. The text outlives the encoding below, // which is why it is declared out here. var length_text: [20]u8 = undefined; if (try request.declaredContentLength()) |length| { const text = std.fmt.bufPrint(&length_text, "{d}", .{length}) catch unreachable; fields.append(c.gpa, .{ .name = "content-length", .value = text }) catch return error.OutOfMemory; } // §12.5.3 of RFC 9110, lowercase for §4.2 of RFC 9114 as everything // else here is. The names are static and the buffer holds the commas, // so this outlives the encoding below it. var accept_buf: [64]u8 = undefined; if (c.config.accept_encodings.len > 0) { var at: usize = 0; for (c.config.accept_encodings, 0..) |want, i| { const text = want.name(); if (i > 0) { if (at + 2 > accept_buf.len) return error.InvalidRequest; accept_buf[at] = ','; accept_buf[at + 1] = ' '; at += 2; } if (at + text.len > accept_buf.len) return error.InvalidRequest; @memcpy(accept_buf[at..][0..text.len], text); at += text.len; } fields.append(c.gpa, .{ .name = "accept-encoding", .value = accept_buf[0..at] }) catch return error.OutOfMemory; }
for (request.headers.fields.items) |field| { if (Request.isReserved(field.name)) return error.InvalidRequest; if (c.config.accept_encodings.len > 0 and protocol.token.eqlIgnoreCase(field.name, "accept-encoding")) { return error.InvalidRequest; } fields.append(c.gpa, .{ .name = field.name, .value = field.value }) catch return error.OutOfMemory; } // §10.1.1 of RFC 9110: only with content to hold back. const expecting = request.expect_continue and request.hasContent(); if (expecting) { fields.append(c.gpa, .{ .name = "expect", .value = "100-continue" }) catch return error.OutOfMemory; } qpack.encode(&sw.writer, fields.items) catch return error.OutOfMemory;
var size: u64 = 0; for (fields.items) |field| size += field.name.len + field.value.len + 32; return .{ .section = sw.toOwnedSlice() catch return error.OutOfMemory, .size = size, .expecting = expecting, }; }
fn sendOn(c: *Connection, stream: transport.Stream, request: Request, head: Head) Error!Exchange { // §4.1: the field section first, as one HEADERS frame.
var out_buffer: [4096]u8 = undefined; var w = stream.writer(&out_buffer); writeVarint(&w.interface, @backingInt(frame.Kind.headers)) catch return error.WriteFailed; writeVarint(&w.interface, head.section.len) catch return error.WriteFailed; w.interface.writeAll(head.section) catch return error.WriteFailed;
if (head.expecting) { // The head alone, and the content once a `100 Continue` asks. w.interface.flush() catch return error.WriteFailed; var held: Held = .{ .writer = &w, .request = request }; return c.readResponse(stream, request, &held) catch |err| { // Neither asked for nor declined, and the response has failed: // the content is not coming, and this end's half says so. if (!held.settled) c.session.resetStream(stream.id, @backingInt(frame.Error.request_cancelled)); return err; }; } try c.writeContent(stream, &w, request); return c.readResponse(stream, request, null); }
/// The request's content as DATA frames, and the end of this endpoint's /// half of the stream after it. fn writeContent(c: *Connection, stream: transport.Stream, w: *transport.Stream.Writer, request: Request) Error!void { if (request.content) |content| { if (content.len > 0) { // §7.2.1: a DATA frame, and no flow control to think about -- // §4.6 gives that to QUIC, and the stream writer blocks for // room rather than asking this file to count octets. writeVarint(&w.interface, @backingInt(frame.Kind.data)) catch return error.WriteFailed; writeVarint(&w.interface, content.len) catch return error.WriteFailed; w.interface.writeAll(content) catch return error.WriteFailed; } } else if (request.source) |source| { // `declaredContentLength` has established that this is `.exact`. try c.writeDataFrom(stream, &w.interface, source, request.body.exact); } // §4.1: the end of the request is the end of this endpoint's half of // the stream, which is a FIN on the frame carrying the last octet. w.finish() catch return error.WriteFailed; }
/// §10.1.1 of RFC 9110: content held back for a `100 Continue`, and the /// writer it goes out through when one arrives. const Held = struct { writer: *transport.Stream.Writer, request: Request, /// Sent, or given up on because the server answered without asking. settled: bool = false, sent: bool = false, };
/// The body of a request read as it is sent, one DATA frame at a time. /// /// Several frames rather than one declaring the whole length, although /// §7.2.1 would allow one: each is filled from `source` before its header /// is written, so a reader that ends early is found out before a frame is /// begun that it cannot finish -- and a receiver that holds a frame whole /// before handing it on holds sixteen kibibytes, not the whole body. /// Nothing more of the body than one frame is ever in memory here; QUIC's /// flow control is the stream writer's, which blocks for room. fn writeDataFrom( c: *Connection, stream: transport.Stream, out: *Io.Writer, source: *Io.Reader, length: u64, ) Error!void { var scratch: [16384]u8 = undefined; var left = length; while (left > 0) { const want: usize = @intCast(@min(left, scratch.len)); const got = source.readSliceShort(scratch[0..want]) catch return c.abandon(stream, error.RequestBodyReadFailed); // Fewer than asked for is the reader's end, before the length the // HEADERS frame declared. if (got < want) return c.abandon(stream, error.RequestBodyShort);
writeVarint(out, @backingInt(frame.Kind.data)) catch return error.WriteFailed; writeVarint(out, got) catch return error.WriteFailed; out.writeAll(scratch[0..got]) catch return error.WriteFailed; left -= got; } }
/// §4.1.1 of RFC 9114: a request this end will not complete is ended with /// H3_REQUEST_CANCELLED, as a reset rather than a FIN, so that the server /// is told the message is not whole rather than handed a shorter one. /// Both directions, since nobody is left to read the answer either. /// A connection error: §8 has the whole connection closed with `code`, /// because what the server sent says it and this client no longer agree /// about the protocol, and every other stream on it is in doubt too. fn fail(c: *Connection, code: frame.Error, why: []const u8) Error { c.closeFor(code, why); return error.MalformedResponse; }
/// The same, from `run`, which has no caller to report it to. fn closeFor(c: *Connection, code: frame.Error, why: []const u8) void { c.session.close(@backingInt(code), why); }
fn abandon(c: *Connection, stream: transport.Stream, err: Error) Error { c.session.resetStream(stream.id, @backingInt(frame.Error.request_cancelled)); c.session.stopSending(stream.id, @backingInt(frame.Error.request_cancelled)); return err; }
fn readResponse(c: *Connection, stream: transport.Stream, request: Request, held: ?*Held) Error!Exchange { const interim = request.interim; var arena: std.heap.ArenaAllocator = .init(c.gpa); errdefer arena.deinit();
var read_buffer: [4096]u8 = undefined; var r = stream.reader(&read_buffer);
var status: u16 = 0; var headers: Headers = .empty; var body: std.ArrayList(u8) = .empty; // The arena has one above and this needs its own: every way out of // the loop below after a DATA frame has arrived leaves octets in // here, and the `Exchange` that would have freed them is never // built. Found by fuzzing, on a response that declared more body // than this client will hold. errdefer body.deinit(c.gpa); var saw_headers = false; var saw_trailers = false; var interims: u8 = 0;
while (true) { // The end of the stream between frames is the end of the // response; anywhere else it has cut a frame short, and taking a // cut-off frame header as the end handed the caller a short // response as a whole one. A failed read rather than §7.1's // H3_FRAME_ERROR, because QUIC delivers a server's RESET_STREAM // as the same end and a reset is not a reason to close the // connection; see `Connection.endedEarly` on the server side. _ = r.interface.peekByte() catch |err| switch (err) { error.EndOfStream => break, else => return error.ReadFailed, }; const header = readFrameHeader(&r.interface) catch return error.ReadFailed;
switch (header.kind) { .headers => { if (header.length > c.config.max_field_section_size) return error.ResponseTooLarge; if (saw_trailers) { // §4.1: the trailer section is the last thing a // response carries, and "an HTTP message... is // malformed" with frames after it. It is also what // keeps a server from making a field section of the // whole stream, one HEADERS frame at a time, each // passing the limit above on its own. return error.MalformedResponse; } if (saw_headers) { // §4.1: a second field section is the trailer // section, which this discards the way the HTTP/1.1 // driver does -- unread, so that nothing about it // stays in memory. r.interface.discardAll64(header.length) catch return error.ReadFailed; saw_trailers = true; continue; } const section = arena.allocator().alloc(u8, @intCast(header.length)) catch return error.OutOfMemory; // All of it rather than what has arrived: a QUIC stream // delivers what it has, and a section spanning two // datagrams would otherwise be read short and decoded as // rubbish -- a failure that depends on timing, so absent // on loopback and present on a network. r.interface.readSliceAll(section) catch return error.ReadFailed; status = try c.decodeInto(&arena, section, &headers);
if (status >= 100 and status <= 199) { // §4.5: "HTTP/3 does not support the 101 (Switching // Protocols) informational status code." if (status == 101) return error.MalformedResponse; interims += 1; if (interims > c.config.max_interim_responses) { return c.abandon(stream, error.TooManyInterimResponses); } if (interim) |hook| { hook.notify(.{ .status = status, .version = .http_3, .headers = &headers }); } // §10.1.1 of RFC 9110: what held content was waiting // for. if (held) |h| if (status == 100 and !h.settled) { h.settled = true; try c.writeContent(stream, h.writer, h.request); h.sent = true; }; // §4.1: an interim response, so the final one is // still to come. Nothing but this section is in the // arena yet, so it starts again empty, and a server // sending interim responses costs this client one of // them at a time. _ = arena.reset(.retain_capacity); headers = .empty; continue; } saw_headers = true; if (held) |h| if (!h.settled) { // §10.1.1 of RFC 9110: answered without being asked // for the content. §4.1 of RFC 9114: this endpoint's // half of the stream is reset with H3_NO_ERROR, which // says the content is not coming, rather than ended, // which would say it was empty. QUIC resets the one // direction only, so the response still arrives. h.settled = true; c.session.resetStream(stream.id, @backingInt(frame.Error.no_error)); }; }, .data => { if (!saw_headers or saw_trailers) return error.MalformedResponse; if (body.items.len + header.length > c.config.max_body_len) { return error.ResponseTooLarge; } const at = body.items.len; body.resize(c.gpa, at + @as(usize, @intCast(header.length))) catch return error.OutOfMemory; r.interface.readSliceAll(body.items[at..]) catch return error.ReadFailed; }, // §7.2.5: a push "MUST NOT" be promised with an ID above // the client's MAX_PUSH_ID, and this client sends none, so // every ID is above it and the promise is "a connection // error of type H3_ID_ERROR". .push_promise => return c.fail(.id_error, "a PUSH_PROMISE, with no MAX_PUSH_ID sent"), else => { // §7.2: the control stream's frames, and §7.2.8's that // were HTTP/2's, have no business on a request stream. if (!header.kind.allowedOnRequestStream()) { return c.fail(.frame_unexpected, "a frame not allowed on a request stream"); } // §9: an unknown frame type "MUST be ignored", which is // what makes the grease of §11.2.1 harmless and what // leaves room for extensions nobody has written yet. r.interface.discardAll64(header.length) catch return error.ReadFailed; }, } }
if (!saw_headers) return error.MalformedResponse;
// §4.1.2: "a request or response that is defined as having content // when it contains a Content-Length header field" whose DATA frames // add up to another length is malformed, and a client "MUST NOT // accept" one -- a short body here is a response cut off, which the // caller would otherwise take for the whole of it. Not for the // responses whose `content-length` describes a body that is not sent: // a 204, a 304, or anything answering HEAD (§8.6 of RFC 9110). const bodiless = status == 204 or status == 304 or request.method.eql(protocol.Method.head); if (!bodiless) if (headers.get("content-length")) |value| { const declared = protocol.parseContentLength(value) orelse return c.abandon(stream, error.MalformedResponse); if (declared != body.items.len) return c.abandon(stream, error.MalformedResponse); };
// §8.4 of RFC 9110, off the gathered whole for the same reason the // HTTP/2 driver does it that way: a stream's octets arrive interleaved // with every other stream's, so there is no single reader to wrap. if (c.config.accept_encodings.len > 0 and body.items.len > 0) { switch (coding.applied(headers.get("content-encoding"))) { .identity, .unsupported => {}, .one => |was| for (c.config.accept_encodings) |want| { if (want != was) continue; // Taken for the decode and given back after it, rather // than held on the connection: this `Connection` owns no // memory and so has no `deinit`, and a 64 KiB field would // have made one necessary on every caller for the sake of // a window that is wanted only when a coded body actually // arrives. const window = c.gpa.alloc(u8, coding.decodeBufferLen(was)) catch return error.OutOfMemory; defer c.gpa.free(window); const plain = coding.undo( c.gpa, body.items, was, window, c.config.max_body_len, ) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.Malformed, error.TooLarge, error.Unsupported => return error.BadContentEncoding, }; body.clearRetainingCapacity(); body.appendSlice(c.gpa, plain) catch { c.gpa.free(plain); return error.OutOfMemory; }; c.gpa.free(plain); break; }, } }
// In the arena rather than in the `Exchange`, which is returned by // value: the reader has to stay where `response.body.reader` points. const body_reader = arena.allocator().create(Io.Reader) catch return error.OutOfMemory; body_reader.* = .fixed(body.items); return .{ .arena = arena, .body_storage = body, .response = .{ .status = status, .version = .http_3, // §4.3.2: "HTTP/3 does not define a way to carry the reason // phrase", as HTTP/2 did not. .reason = "", .headers = headers, .body = .{ .reader = body_reader, .length = body.items.len, .present = body.items.len > 0, }, .content_sent = if (held) |h| h.sent else true, }, }; }
/// §4.3.2: a response's control data is one `:status` and nothing else. fn decodeInto( c: *Connection, arena: *std.heap.ArenaAllocator, section: []const u8, headers: *Headers, ) Error!u16 { const scratch = arena.allocator().alloc(u8, c.config.decode_scratch_len) catch return error.OutOfMemory; const decoded = arena.allocator().alloc(qpack.Field, c.config.max_fields) catch return error.OutOfMemory; // §4.2.2 of RFC 9114: the limit this client advertised is on the // decoded section, which the static table can make many times the // size of the frame that was checked against it. const n = qpack.decodeBounded(section, decoded, scratch, c.config.max_field_section_size) catch |err| switch (err) { error.TooLarge, error.TooManyFields, error.TooLong => return error.ResponseTooLarge, else => return error.MalformedResponse, };
var status: ?u16 = null; var seen_ordinary = false; for (decoded[0..n]) |field| { if (field.name.len > 0 and field.name[0] == ':') { // §4.3: pseudo-headers come before every ordinary field, and // a response has exactly one. if (seen_ordinary) return error.MalformedResponse; if (!std.mem.eql(u8, field.name, ":status")) return error.MalformedResponse; if (status != null) return error.MalformedResponse; if (field.value.len != 3) return error.MalformedResponse; var v: u16 = 0; for (field.value) |ch| { if (ch < '0' or ch > '9') return error.MalformedResponse; v = v * 10 + (ch - '0'); } // §15 of RFC 9110: three digits, of which the first is the // class -- and there is no class below 1. if (v < 100) return error.MalformedResponse; status = v; continue; } seen_ordinary = true; // §4.2: "field names MUST be converted to lowercase", so an // uppercase one makes the message malformed. for (field.name) |ch| { if (ch >= 'A' and ch <= 'Z') return error.MalformedResponse; } // The pseudo-header stays out of the list, as on the server side: // a caller finding `:status` among the headers would be seeing the // wire rather than the message. headers.appendAssumeValid(arena.allocator(), field.name, field.value) catch return error.OutOfMemory; } return status orelse error.MalformedResponse; }};
/// A response and the memory it points into.////// Every slice in the response -- each field name and value -- lives in the/// arena here, the same bargain the HTTP/1.1 client makes with its head/// buffer. `deinit` ends it.pub const Exchange = struct { arena: std.heap.ArenaAllocator, body_storage: std.ArrayList(u8), response: Response,
/// The whole response body, with any content coding this end asked for /// already undone. Valid until `deinit`. /// /// The same octets `response.body.reader` reads, as a slice, since they /// are all in memory by the time there is an `Exchange` to ask. pub fn body(e: *const Exchange) []const u8 { return e.body_storage.items; }
test body { const answer = [_]u8{ 0x01, 0x03, 0x00, 0x00, 0xc0 | 25, 0x00, 0x05, 'h', 'e', 'l', 'l', 'o' }; var scripted: transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = &answer }; defer scripted.deinit(std.testing.allocator); var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); try c.start(); var exchange = try c.send(.{ .host = "example.com", .target = "/" }); defer exchange.deinit(std.testing.allocator);
try std.testing.expectEqualStrings("hello", exchange.body()); try std.testing.expectEqual(@as(?u64, 5), exchange.response.body.length); }
/// Frees the body and every slice in `response`. `gpa` has to be the /// allocator the `Connection` was made with. pub fn deinit(e: *Exchange, gpa: Allocator) void { e.body_storage.deinit(gpa); e.arena.deinit(); e.* = undefined; }
test deinit { const answer = [_]u8{ 0x01, 0x03, 0x00, 0x00, 0xc0 | 25 }; var scripted: transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = &answer }; defer scripted.deinit(std.testing.allocator); var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); try c.start(); var exchange = try c.send(.{ .host = "example.com", .target = "/" }); exchange.deinit(std.testing.allocator); }};
fn readFrameHeader(r: *Io.Reader) !frame.Header { // §16 of RFC 9000: a varint's first two bits say how long it is, and the // header's length is the two together -- which `Header.end` counts from. const kind_len = @as(usize, 1) << @intCast((try r.peekByte()) >> 6); const kind = try readVarint(r); const length_len = @as(usize, 1) << @intCast((try r.peekByte()) >> 6); const length = try readVarint(r); return .{ .kind = @fromBackingInt(@intCast(kind)), .length = length, .header_len = kind_len + length_len };}
test readFrameHeader { // A DATA frame whose length takes two octets: 0x40 0x80 is 128. var r: Io.Reader = .fixed(&.{ 0x00, 0x40, 0x80 }); const h = try readFrameHeader(&r); try std.testing.expectEqual(@as(u64, 128), h.length); try std.testing.expectEqual(@as(usize, 3), h.header_len); try std.testing.expectEqual(@as(?usize, 131), h.end());}
fn readVarint(r: *Io.Reader) !u64 { const first = try r.peekByte(); const len: usize = @as(usize, 1) << @intCast(first >> 6); const bytes = try r.take(len); const parsed = quic.varint.parse(bytes) catch return error.InvalidVarint; return parsed.value;}
fn writeVarint(w: *Io.Writer, value: u64) !void { var buf: [8]u8 = undefined; try w.writeAll(quic.varint.write(&buf, value));}
test { std.testing.refAllDecls(@This());}
test "RFC 9114 4.1.2: a response shorter than its content-length is malformed" { // HEADERS { :status 200, content-length 10 }, two octets of DATA, then // the end of the stream: a response cut off, which used to be returned as // the whole of it. 0x54 is a literal with static name 4, content-length. const answer = [_]u8{ 0x01, 0x07, 0x00, 0x00, 0xc0 | 25, 0x54, 0x02, '1', '0', 0x00, 0x02, 'h', 'i' }; var scripted: transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = &answer }; defer scripted.deinit(std.testing.allocator); var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); try c.start(); try std.testing.expectError(error.MalformedResponse, c.send(.{ .host = "example.com", .target = "/" }));}
test "RFC 9114 4.1.2: a HEAD response's content-length describes no content" { const answer = [_]u8{ 0x01, 0x07, 0x00, 0x00, 0xc0 | 25, 0x54, 0x02, '1', '0' }; var scripted: transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = &answer }; defer scripted.deinit(std.testing.allocator); var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); try c.start(); var exchange = try c.send(.{ .method = .head, .host = "example.com", .target = "/" }); defer exchange.deinit(std.testing.allocator); try std.testing.expectEqual(@as(u16, 200), exchange.response.status);}
test "RFC 9114 4.1.2: a response stream ending inside a frame header is not a whole response" { // HEADERS { :status 200 }, then a DATA frame whose two-octet length has // only its first octet before the stream ends. const answer = [_]u8{ 0x01, 0x03, 0x00, 0x00, 0xc0 | 25, 0x00, 0x40 }; var scripted: transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = &answer }; defer scripted.deinit(std.testing.allocator); var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); try c.start(); try std.testing.expectError(error.ReadFailed, c.send(.{ .host = "example.com", .target = "/" })); try std.testing.expect(scripted.closed == null);}
test "RFC 9114 7.2.4.1: an HTTP/2 setting in the server's SETTINGS is H3_SETTINGS_ERROR" { // 0x02 is HTTP/2's SETTINGS_ENABLE_PUSH. var scripted: transport.memory_session.Scripted = .{ .incoming = &.{ .{ .id = .{ .value = 3 }, .data = &.{ 0x00, 0x04, 0x02, 0x02, 0x00 } }, } }; defer scripted.deinit(std.testing.allocator); var c: Connection = .init(std.testing.allocator, scripted.session(std.testing.allocator), .{}); c.run(std.testing.io); try std.testing.expectEqual(@as(u64, @backingInt(frame.Error.settings_error)), scripted.closed.?.code);}