// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The HTTP/3 driver end to end, over a real UDP socket. //! //! The client here is written by hand rather than borrowed, and that is the //! whole value of the test: it encodes its own field section, opens its own //! control stream, and frames its own request, so nothing it does comes from //! the code it is checking except the QPACK encoder -- which is anchored //! separately by the representation tests in `qpack`. //! //! What it proves is the part a unit test cannot reach. The driver's pieces //! all compiled before any of this existed; whether a datagram arriving on a //! socket becomes a `Request`, and whether what a handler writes comes back //! as DATA frames a client can read, is a different question and the only one //! that matters. const std = @import("std"); const Io = std.Io; const net = Io.net; const testing = std.testing; const http = @import("http"); const quic = @import("quic"); const quic_io = @import("quic_io"); const http_io = @import("http_io"); const h3 = http.http3; /// The listener owns a UDP socket, so it lives in the I/O module; everything /// else about HTTP/3 is in `http`. const h3_io = http_io.http3; /// Answers every request with what it was asked, so that the test can tell a /// request that arrived from one that was invented. /// What this file's hand-rolled client gives each of its connections, which /// is also what it may advertise. The library's defaults: these tests are /// about HTTP/3 and not about how fast QUIC can be made to go. const client_sizes: quic.Connection.Sizes = .{}; fn echo(_: ?*anyopaque, req: *http.server.Request, res: *http.server.Response) anyerror!void { try res.headers.append(res.arena, "content-type", "text/plain"); var body: [256]u8 = undefined; const text = try std.fmt.bufPrint(&body, "{s} {s} {s}", .{ req.method.name, req.target.raw, req.version.toString(), }); try res.send(text); } test "RFC 9114: a request over QUIC becomes the same Request a handler has always seen" { const io = testing.io; const gpa = testing.allocator; var rng_impl = std.Random.DefaultPrng.init(0x9114); const rng = rng_impl.random(); const bind: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try bind.bind(io, .{ .mode = .dgram }); defer socket.close(io); const address = socket.address; var listener: h3_io.Listener = undefined; try listener.init(io, gpa, &socket, .{ .context = null, .serveFn = echo, }, .{ .rng = rng, .reset_key = @splat(0x33), .token_key = @splat(0x44), .max_connections = 2, }); defer listener.deinit(); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, run, .{&listener}); // Stopped whatever happens, so that a failure ends the test rather than // leaving the pump running and the teardown waiting on it. defer listener.stop(); var answer: [256]u8 = undefined; const got = try ask(io, gpa, address, rng, &answer); // The handler saw a `GET /hello` over HTTP/3, and said so. try testing.expectEqualStrings("GET /hello HTTP/3", got); } test "RFC 9114 4.2: an upper case field name is a stream error, not a status" { // Found by pointing aioquic at this server, which is the only client here // willing to send such a thing: curl lower cases on the way out, so no // assertion built on curl can reach the rule at all. // // §4.2 says a field name "MUST be converted to lowercase prior to their // encoding" and that a message containing an upper case one "MUST be // treated as malformed", and §4.1.2 says a malformed message is "a stream // error of type H3_MESSAGE_ERROR". This server used to answer `400`, // which is friendlier and is not what the rule says -- and the difference // is not cosmetic, because a status code claims the request was // understood well enough to be answered, which a field section this // server has refused to read plainly was not. const io = testing.io; const gpa = testing.allocator; var rng_impl = std.Random.DefaultPrng.init(0x0402); const rng = rng_impl.random(); const bind: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try bind.bind(io, .{ .mode = .dgram }); defer socket.close(io); const address = socket.address; var listener: h3_io.Listener = undefined; try listener.init(io, gpa, &socket, .{ .context = null, .serveFn = echo, }, .{ .rng = rng, .reset_key = @splat(0x33), .token_key = @splat(0x44), .max_connections = 2, }); defer listener.deinit(); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, run, .{&listener}); defer listener.stop(); // Written by hand, because this library's own encoder lower cases a // field name on the way out and so cannot produce the thing under test -- // the same reason `tests/h2/frames.zig` carries an HPACK writer of its // own. An encoder that normalizes is the right encoder; it just makes a // poor adversary. var section: [256]u8 = undefined; var sw: Io.Writer = .fixed(§ion); try sw.writeByte(0); // §4.5.1: Required Insert Count, zero try sw.writeByte(0); // and Base, zero try putLiteral(&sw, ":method", "GET"); try putLiteral(&sw, ":scheme", "https"); try putLiteral(&sw, ":path", "/hello"); try putLiteral(&sw, ":authority", "h3.example"); try putLiteral(&sw, "X-Upper", "no"); var answer: [256]u8 = undefined; const outcome = try speak(io, gpa, address, rng, &answer, sw.buffered()); switch (outcome) { .reset => |code| try testing.expectEqual( @backingInt(h3.frame.Error.message_error), code, ), .answered => |bytes| { std.debug.print("expected a reset, got {s}\n", .{bytes}); return error.TestUnexpectedResult; }, } } /// §4.5.6 of RFC 9204: a literal field line with a literal name, with neither /// half Huffman coded and neither length long enough to need a continuation. /// /// Seven lines, and deliberately not the library's encoder: what is being /// tested is what this server does with octets somebody else chose. fn putLiteral(w: *Io.Writer, name: []const u8, value: []const u8) !void { // `001` marks the pattern, the `N` and `H` bits are zero, and the name's // length is what is left, which is three bits and not four -- getting // that wrong makes the section undecodable rather than malformed, and // the server answers a connection error instead of the stream error // under test. try putPrefixed(w, 0x20, 3, name.len); try w.writeAll(name); // The value's `H` bit is zero and its length is a seven bit prefix. try putPrefixed(w, 0x00, 7, value.len); try w.writeAll(value); } /// §4.1.1 of RFC 9204, which is §5.1 of RFC 7541 unchanged: an integer in the /// low `bits` of a byte whose high bits are already spoken for, continuing /// into as many further bytes as it takes. fn putPrefixed(w: *Io.Writer, pattern: u8, bits: u3, value: usize) !void { const max: usize = (@as(usize, 1) << bits) - 1; if (value < max) return w.writeByte(pattern | @as(u8, @intCast(value))); try w.writeByte(pattern | @as(u8, @intCast(max))); var left = value - max; while (left >= 128) : (left /= 128) try w.writeByte(@intCast(left % 128 + 128)); try w.writeByte(@intCast(left)); } /// Five lines, each flushed, and five more that are not. /// /// The two halves are the whole of what this handler is for: a flush means /// "send this now" and its absence means "send it when convenient", and the /// number of DATA frames that come out is the only way to tell from outside /// which of the two a driver did. fn lines(_: ?*anyopaque, _: *http.server.Request, res: *http.server.Response) anyerror!void { const w = try res.beginBody(.unknown); for (0..5) |i| { try w.print("flushed {d}\n", .{i}); try w.flush(); } for (0..5) |i| try w.print("batched {d}\n", .{i}); try res.end(); } test "RFC 9114 7.2.1: a flush is a DATA frame, and not flushing is not" { // The body writer buffers, because a driver that sent every write as its // own DATA frame sent every write as its own datagram -- a megabyte of // sixty-six octet lines left as fifteen thousand packets averaging a // hundred and seventeen octets, and ran at a sixth of the speed it does // now. // // What buffering must not cost is the handler's ability to say when // something leaves. So `flush` is implemented on the body writer rather // than inherited, and empties the stream writer as well, and this is the // assertion that it does: five flushed lines are five frames, and five // unflushed ones are one. const io = testing.io; const gpa = testing.allocator; var rng_impl = std.Random.DefaultPrng.init(0x7201); const rng = rng_impl.random(); const bind: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try bind.bind(io, .{ .mode = .dgram }); defer socket.close(io); const address = socket.address; var listener: h3_io.Listener = undefined; try listener.init(io, gpa, &socket, .{ .context = null, .serveFn = lines, }, .{ .rng = rng, .reset_key = @splat(0x33), .token_key = @splat(0x44), .max_connections = 2, }); defer listener.deinit(); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, run, .{&listener}); defer listener.stop(); var raw: [8192]u8 = undefined; const stream = try askRaw(io, gpa, address, rng, &raw); var frames: usize = 0; var body: [512]u8 = undefined; var used: usize = 0; var at: usize = 0; while (at < stream.len) { const kind = try quic.varint.parse(stream[at..]); at += kind.len; const length = try quic.varint.parse(stream[at..]); at += length.len; const end = at + @as(usize, @intCast(length.value)); if (end > stream.len) break; if (kind.value == 0x00) { frames += 1; @memcpy(body[used..][0..(end - at)], stream[at..end]); used += end - at; } at = end; } // Everything arrived, in order, whichever way it was framed. var want: [512]u8 = undefined; var wants: Io.Writer = .fixed(&want); for (0..5) |i| try wants.print("flushed {d}\n", .{i}); for (0..5) |i| try wants.print("batched {d}\n", .{i}); try testing.expectEqualStrings(wants.buffered(), body[0..used]); // Five flushes, then one frame for everything after them. A driver that // buffered the flushed lines too would send fewer; one that flushed every // write would send ten. try testing.expectEqual(@as(usize, 6), frames); } /// A response whose trailer is only knowable once the body has been produced. /// A response whose trailer is only knowable once the body has been produced. fn trailered(_: ?*anyopaque, _: *http.server.Request, res: *http.server.Response) anyerror!void { const w = try res.beginBody(.unknown); try w.writeAll("abcde"); try res.trailers.append(res.arena, "x-length", "5"); // §6.5.1 of RFC 9110: refused on the way out, wherever a handler puts it. try res.trailers.append(res.arena, "content-length", "99"); return res.end(); } test "RFC 9114 4.1: a trailer section is a second HEADERS frame" { // The same semantics as HTTP/1.1's chunked trailer section and HTTP/2's // second field block, in the framing HTTP/3 already has: a HEADERS frame // after the DATA frames, ending where the stream does. One layer fewer // than either -- there is no chunked coding to arrange and no END_STREAM // flag to set, because QUIC says where the stream stops. const io = testing.io; const gpa = testing.allocator; var rng_impl = std.Random.DefaultPrng.init(0x0401); const rng = rng_impl.random(); const bind: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try bind.bind(io, .{ .mode = .dgram }); defer socket.close(io); const address = socket.address; var listener: h3_io.Listener = undefined; try listener.init(io, gpa, &socket, .{ .context = null, .serveFn = trailered, }, .{ .rng = rng, .reset_key = @splat(0x77), .token_key = @splat(0x88), .max_connections = 2, }); defer listener.deinit(); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, run, .{&listener}); defer listener.stop(); var answer: [256]u8 = undefined; var trailer_value: [64]u8 = undefined; const trailer = try askForTrailers(io, gpa, address, rng, &answer, &trailer_value); try testing.expectEqualStrings("x-length", trailer.name); try testing.expectEqualStrings("5", trailer.value); // The forbidden one did not travel. try testing.expect(!trailer.saw_content_length); } const Trailer = struct { name: []const u8, value: []const u8, saw_content_length: bool, }; fn run(listener: *h3_io.Listener) void { listener.run() catch {}; } /// A hand-written HTTP/3 client: handshake, control stream, one request. /// /// Returns the body, which is what most callers want. `askRaw` returns the /// whole response stream for the one that wants the second field section. fn ask( io: Io, gpa: std.mem.Allocator, to: net.IpAddress, rng: std.Random, answer: []u8, ) ![]const u8 { var raw: [8192]u8 = undefined; const stream = try askRaw(io, gpa, to, rng, &raw); return readResponse(stream, answer); } fn askRaw( io: Io, gpa: std.mem.Allocator, to: net.IpAddress, rng: std.Random, answer: []u8, ) ![]const u8 { var section: [512]u8 = undefined; var sw: Io.Writer = .fixed(§ion); try h3.qpack.encode(&sw, default_fields); return switch (try speak(io, gpa, to, rng, answer, sw.buffered())) { .answered => |bytes| bytes, .reset => error.StreamReset, }; } /// The field section a well-formed request carries here, which every test but /// the malformed one uses. const default_fields: []const h3.qpack.Field = &.{ .{ .name = ":method", .value = "GET" }, .{ .name = ":scheme", .value = "https" }, .{ .name = ":path", .value = "/hello" }, .{ .name = ":authority", .value = "h3.example" }, }; /// What became of the request stream: an answer, or the code the server reset /// it with. §4.1.2 makes the second one an outcome rather than a failure, so /// it is a case of the return value and not an error. const Outcome = union(enum) { answered: []const u8, reset: u64, }; fn speak( io: Io, gpa: std.mem.Allocator, to: net.IpAddress, rng: std.Random, answer: []u8, section: []const u8, ) !Outcome { var local_id: [8]u8 = undefined; rng.bytes(&local_id); var destination: [8]u8 = undefined; rng.bytes(&destination); const parameters: quic.parameters.Parameters = .{ // What this client's own buffers hold, which `Connection.init` // asserts: a window is a promise about room, and the room here is // `client_sizes` and nothing else. .initial_max_data = quic.Connection.max_streams * client_sizes.recv_window, .initial_max_stream_data_bidi_local = client_sizes.recv_window, .initial_max_stream_data_bidi_remote = client_sizes.recv_window, .initial_max_streams_bidi = 4, .initial_max_stream_data_uni = client_sizes.recv_window, .initial_max_streams_uni = 8, .max_idle_timeout = 5_000, .active_connection_id_limit = 4, .initial_source_connection_id = &local_id, }; var encoded: [256]u8 = undefined; const written = try quic.parameters.write(¶meters, &encoded, .client); var tls_client: quic.tls_handshake.Client = try .init(.{ .host = "h3.example", .root_ca = .{ .bytes = .empty, .map = .empty }, // §3.1: the token is `h3` and nothing else. .alpn_protocols = &.{"h3"}, .transport_parameters = written, .rng = rng, .now_sec = 0, .insecure_skip_verify = true, }); const memory = try gpa.create(quic.Connection.Storage(client_sizes)); defer gpa.destroy(memory); const connection = try gpa.create(quic.Connection); defer gpa.destroy(connection); connection.init(.{ .role = .client, .memory = memory.memory(), .handshake = tls_client.handshake(), .local_id = &local_id, .destination_id = &destination, .local = parameters, .rng = rng, }); const from: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try from.bind(io, .{ .mode = .dgram }); defer socket.close(io); const origin = Io.Timestamp.now(io, .awake); var out: [2048]u8 = undefined; var in: [2048]u8 = undefined; var sent = false; var request_stream: quic.stream.Id = .{ .value = 0 }; var steps: usize = 0; while (steps < 200) : (steps += 1) { const at: u64 = @intCast(@max( Io.Timestamp.now(io, .awake).nanoseconds - origin.nanoseconds, 0, )); if (connection.isEstablished() and !sent) { // §6.2: the control stream, and SETTINGS first on it. A server // that never sees one is entitled to close the connection, so // this is not optional politeness. const control = try connection.openUnidirectional(); var control_buf: [64]u8 = undefined; var cw: Io.Writer = .fixed(&control_buf); try putVarint(&cw, 0x00); // control try putVarint(&cw, 0x04); // SETTINGS try putVarint(&cw, 0x00); // of length zero try connection.write(control, cw.buffered()); // §4.2 of RFC 9204: both QPACK streams exist and carry nothing. for ([_]u64{ 0x02, 0x03 }) |kind| { const s = try connection.openUnidirectional(); var b: [8]u8 = undefined; var bw: Io.Writer = .fixed(&b); try putVarint(&bw, kind); try connection.write(s, bw.buffered()); } // §4.1: a HEADERS frame carrying a QPACK field section. request_stream = try connection.openBidirectional(); var head: [16]u8 = undefined; const n = try h3.frame.write(&head, .headers, section.len); try connection.write(request_stream, head[0..n]); try connection.write(request_stream, section); try connection.finishStream(request_stream); sent = true; } while (try connection.send(&out, at)) |datagram| { try socket.send(io, &to, datagram.bytes); } if (sent) { if (connection.streamResetCode(request_stream)) |code| return .{ .reset = code }; const available = connection.readable(request_stream); if (available.len > 0 and connection.streamReceived(request_stream)) { const n = @min(available.len, answer.len); @memcpy(answer[0..n], available[0..n]); return .{ .answered = answer[0..n] }; } } const message = socket.receiveTimeout(io, &in, .{ .duration = .{ .raw = .{ .nanoseconds = 200 * std.time.ns_per_ms }, .clock = .awake, } }) catch |err| switch (err) { error.Timeout => { connection.onTimeout(at); continue; }, else => return err, }; try connection.receive(message.data, 0, at); } return error.NoAnswer; } /// Reads the HEADERS frame and whatever DATA frames follow, and returns the /// body -- which is the part the handler wrote. /// The same exchange, keeping the *second* field section rather than the /// body -- which is what a trailer section is. fn askForTrailers( io: Io, gpa: std.mem.Allocator, to: net.IpAddress, rng: std.Random, answer: []u8, value_out: []u8, ) !Trailer { const bytes = try askRaw(io, gpa, to, rng, answer); var at: usize = 0; var sections: usize = 0; var found: ?Trailer = null; var saw_content_length = false; var name_out: [64]u8 = undefined; while (at < bytes.len) { const kind = try quic.varint.parse(bytes[at..]); at += kind.len; const length = try quic.varint.parse(bytes[at..]); at += length.len; const end = at + @as(usize, @intCast(length.value)); if (end > bytes.len) return error.Truncated; if (kind.value == 0x01) { sections += 1; // §4.1: the first field section is the response head; a second // one, after the DATA frames, is the trailer section. if (sections == 2) { var fields: [16]h3.qpack.Field = undefined; var scratch: [1024]u8 = undefined; const n = try h3.qpack.decode(bytes[at..end], &fields, &scratch); for (fields[0..n]) |f| { if (std.mem.eql(u8, f.name, "content-length")) saw_content_length = true; if (found != null) continue; @memcpy(name_out[0..f.name.len], f.name); @memcpy(value_out[0..f.value.len], f.value); found = .{ .name = name_out[0..f.name.len], .value = value_out[0..f.value.len], .saw_content_length = false, }; } } } at = end; } var trailer = found orelse return error.NoTrailerSection; trailer.saw_content_length = saw_content_length; return trailer; } /// An extended CONNECT, and then speaking into the stream it opens. /// /// Unlike `ask`, the request half is *not* ended: a tunnel with a closed /// sending half is not a tunnel, and §5 of RFC 8441 makes ending the stream /// the way it is closed. So the client writes its DATA frame after the /// request rather than before the FIN, which is the whole shape of a /// bootstrapped protocol. fn connectAndSpeak( io: Io, gpa: std.mem.Allocator, to: net.IpAddress, rng: std.Random, answer: []u8, ) ![]const u8 { var local_id: [8]u8 = undefined; rng.bytes(&local_id); var destination: [8]u8 = undefined; rng.bytes(&destination); const parameters: quic.parameters.Parameters = .{ // What this client's own buffers hold, which `Connection.init` // asserts: a window is a promise about room, and the room here is // `client_sizes` and nothing else. .initial_max_data = quic.Connection.max_streams * client_sizes.recv_window, .initial_max_stream_data_bidi_local = client_sizes.recv_window, .initial_max_stream_data_bidi_remote = client_sizes.recv_window, .initial_max_streams_bidi = 4, .initial_max_stream_data_uni = client_sizes.recv_window, .initial_max_streams_uni = 8, .max_idle_timeout = 5_000, .active_connection_id_limit = 4, .initial_source_connection_id = &local_id, }; var encoded: [256]u8 = undefined; const written = try quic.parameters.write(¶meters, &encoded, .client); var tls_client: quic.tls_handshake.Client = try .init(.{ .host = "h3.example", .root_ca = .{ .bytes = .empty, .map = .empty }, .alpn_protocols = &.{"h3"}, .transport_parameters = written, .rng = rng, .now_sec = 0, .insecure_skip_verify = true, }); const memory = try gpa.create(quic.Connection.Storage(client_sizes)); defer gpa.destroy(memory); const connection = try gpa.create(quic.Connection); defer gpa.destroy(connection); connection.init(.{ .role = .client, .memory = memory.memory(), .handshake = tls_client.handshake(), .local_id = &local_id, .destination_id = &destination, .local = parameters, .rng = rng, }); const from: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try from.bind(io, .{ .mode = .dgram }); defer socket.close(io); const origin = Io.Timestamp.now(io, .awake); var out: [2048]u8 = undefined; var in: [2048]u8 = undefined; var sent = false; var request_stream: quic.stream.Id = .{ .value = 0 }; var steps: usize = 0; while (steps < 200) : (steps += 1) { const at: u64 = @intCast(@max( Io.Timestamp.now(io, .awake).nanoseconds - origin.nanoseconds, 0, )); if (connection.isEstablished() and !sent) { const control = try connection.openUnidirectional(); var control_buf: [64]u8 = undefined; var cw: Io.Writer = .fixed(&control_buf); try putVarint(&cw, 0x00); try putVarint(&cw, 0x04); try putVarint(&cw, 0x00); try connection.write(control, cw.buffered()); for ([_]u64{ 0x02, 0x03 }) |kind| { const u = try connection.openUnidirectional(); var b: [8]u8 = undefined; var bw: Io.Writer = .fixed(&b); try putVarint(&bw, kind); try connection.write(u, bw.buffered()); } // RFC 8441 §4, unchanged by RFC 9220: CONNECT with `:protocol`, // and unlike an ordinary CONNECT it carries `:scheme` and // `:path` as well -- which is what distinguishes the extended // form from the tunnel-to-an-authority one. var section: [512]u8 = undefined; var sw: Io.Writer = .fixed(§ion); try h3.qpack.encode(&sw, &.{ .{ .name = ":method", .value = "CONNECT" }, .{ .name = ":protocol", .value = "echo" }, .{ .name = ":scheme", .value = "https" }, .{ .name = ":path", .value = "/tunnel" }, .{ .name = ":authority", .value = "h3.example" }, }); request_stream = try connection.openBidirectional(); var head: [16]u8 = undefined; const n = try h3.frame.write(&head, .headers, sw.buffered().len); try connection.write(request_stream, head[0..n]); try connection.write(request_stream, sw.buffered()); // Into the tunnel, as a DATA frame, without ending this half. const message = "hello through the tunnel"; var data_head: [16]u8 = undefined; const dn = try h3.frame.write(&data_head, .data, message.len); try connection.write(request_stream, data_head[0..dn]); try connection.write(request_stream, message); sent = true; } while (try connection.send(&out, at)) |datagram| { try socket.send(io, &to, datagram.bytes); } if (sent) { const available = connection.readable(request_stream); // The server ends the stream when the handler stops speaking, // which is §5's orderly close. // The status, and then the connection is kept alive until the // handler has read its half. A tunnel is two independent // directions -- which is the whole point of one -- so the client // does not stop pumping the moment its own half is answered. // The status, and then the echo that comes back through the // tunnel -- which is the direction that needs both halves to // stay open. if (readStatus(available, answer)) |_| { if (echoedPayload(available)) |payload| { const n = @min(payload.len, answer.len); @memcpy(answer[0..n], payload[0..n]); return answer[0..n]; } } } const message = socket.receiveTimeout(io, &in, .{ .duration = .{ .raw = .{ .nanoseconds = 200 * std.time.ns_per_ms }, .clock = .awake, } }) catch |err| switch (err) { error.Timeout => { connection.onTimeout(at); continue; }, else => return err, }; try connection.receive(message.data, 0, at); } return error.NoAnswer; } /// The payload of the first DATA frame after the response head, which is the /// server's half of the tunnel. fn echoedPayload(bytes: []const u8) ?[]const u8 { var at: usize = 0; while (at < bytes.len) { const kind = quic.varint.parse(bytes[at..]) catch return null; at += kind.len; const length = quic.varint.parse(bytes[at..]) catch return null; at += length.len; const end = at + @as(usize, @intCast(length.value)); if (end > bytes.len) return null; if (kind.value == 0x00 and length.value > 0) return bytes[at..end]; at = end; } return null; } /// The `:status` from the first field section, or null while it has not all /// arrived. fn readStatus(bytes: []const u8, out: []u8) ?[]const u8 { if (bytes.len < 2) return null; const kind = quic.varint.parse(bytes) catch return null; const length = quic.varint.parse(bytes[kind.len..]) catch return null; const at = kind.len + length.len; const end = at + @as(usize, @intCast(length.value)); if (kind.value != 0x01 or end > bytes.len) return null; var fields: [16]h3.qpack.Field = undefined; var scratch: [1024]u8 = undefined; const n = h3.qpack.decode(bytes[at..end], &fields, &scratch) catch return null; for (fields[0..n]) |f| { if (!std.mem.eql(u8, f.name, ":status")) continue; @memcpy(out[0..f.value.len], f.value); return out[0..f.value.len]; } return null; } fn readResponse(bytes: []const u8, answer: []u8) ![]const u8 { var at: usize = 0; var used: usize = 0; var saw_headers = false; while (at < bytes.len) { const kind = try quic.varint.parse(bytes[at..]); at += kind.len; const length = try quic.varint.parse(bytes[at..]); at += length.len; const end = at + @as(usize, @intCast(length.value)); if (end > bytes.len) return error.Truncated; switch (kind.value) { 0x01 => { // §4.1: the response head. Decoded with the library's own // QPACK, which is legitimate here for the reason the HTTP/2 // suite gives for the same choice: by this point the decoder // has already correctly understood a request encoded by the // hand-written encoder above. var fields: [16]h3.qpack.Field = undefined; var scratch: [1024]u8 = undefined; const n = try h3.qpack.decode(bytes[at..end], &fields, &scratch); var status: []const u8 = ""; for (fields[0..n]) |f| { if (std.mem.eql(u8, f.name, ":status")) status = f.value; } if (!std.mem.eql(u8, status, "200")) return error.NotOk; saw_headers = true; }, 0x00 => { const payload = bytes[at..end]; if (used + payload.len > answer.len) return error.TooLong; @memcpy(answer[used..][0..payload.len], payload); used += payload.len; }, // §7.2.8: anything else is stepped over. else => {}, } at = end; } if (!saw_headers) return error.NoHead; return answer[0..used]; } fn putVarint(w: *Io.Writer, value: u64) !void { var buf: [8]u8 = undefined; try w.writeAll(quic.varint.write(&buf, value)); } test "RFC 9114 5.2: a shutdown answers what is in flight and refuses what is not" { // §5.2: "the identifier in a GOAWAY frame indicates the highest-numbered // request... that might have been processed", and a request at or above it // was not. Which is the whole value of the frame: a client that is told // H3_REQUEST_REJECTED knows the request did not happen and may send it // somewhere else, where a connection that simply closed leaves it having // to guess -- and guessing wrong means either losing a request or making // it twice. const io = testing.io; const gpa = testing.allocator; var rng_impl = std.Random.DefaultPrng.init(0x0502); const rng = rng_impl.random(); const bind: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try bind.bind(io, .{ .mode = .dgram }); defer socket.close(io); const address = socket.address; var listener: h3_io.Listener = undefined; try listener.init(io, gpa, &socket, .{ .context = null, .serveFn = echo, }, .{ .rng = rng, .reset_key = @splat(0x55), .token_key = @splat(0x66), .max_connections = 2, }); defer listener.deinit(); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, run, .{&listener}); defer listener.stop(); // One request answered, which is what proves the connection was working // before the shutdown rather than after it. var answer: [256]u8 = undefined; const got = try ask(io, gpa, address, rng, &answer); try testing.expectEqualStrings("GET /hello HTTP/3", got); // §5.2: and now the shutdown, which every live connection is told about. listener.shutdown(); // A connection that arrives after it is refused rather than served. What // is asserted is that it does not get an answer -- the code it is refused // with is the client's to read, and the hand-written client here does not // parse CONNECTION_CLOSE. var second: [256]u8 = undefined; try testing.expectError(error.NoAnswer, ask(io, gpa, address, rng, &second)); } /// What a tunnelling handler saw, so that the test can check it from outside. const Tunnel = struct { seen: [64]u8 = undefined, len: std.atomic.Value(usize) = .init(0), upgraded: std.atomic.Value(bool) = .init(false), }; /// A handler that takes the stream over, which is what a WebSocket's would do /// after the opening handshake. fn tunnelling(context: ?*anyopaque, req: *http.server.Request, res: *http.server.Response) anyerror!void { const t: *Tunnel = @ptrCast(@alignCast(context.?)); if (!req.offersUpgrade("echo")) { try res.send("no"); return; } const tunnel = try res.upgrade("echo"); t.upgraded.store(true, .release); // One `stream` call, not `readSliceShort`. The latter keeps asking until // the destination is full or the stream ends, which is right for a // message of known length and wrong for a tunnel -- the peer has not // ended its half and is not going to, so the second ask waits for ever. var into: Io.Writer = .fixed(&t.seen); const n = try tunnel.input.stream(&into, .limited(t.seen.len)); t.len.store(n, .release); // And back the other way, on the same stream, with both halves open. try tunnel.output.writeAll(t.seen[0..n]); try tunnel.output.flush(); } test "RFC 9220: an extended CONNECT hands the stream over" { // RFC 9220 is three pages, and most of them are about what it does not // change from RFC 8441: the same setting identifier, the same `:protocol` // in the request, the same `2xx` rather than a `101`. What differs is // what a tunnel is made of -- and it is simpler here than in either // version before it, because the request body and the tunnel are the same // QUIC stream. Over HTTP/1.1 an upgrade has to refuse when the request // body has not been read, since those octets sit in front of the new // protocol's first one; here there is nothing to be in front of anything. const io = testing.io; const gpa = testing.allocator; var rng_impl = std.Random.DefaultPrng.init(0x9220); const rng = rng_impl.random(); const bind: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try bind.bind(io, .{ .mode = .dgram }); defer socket.close(io); const address = socket.address; var listener: h3_io.Listener = undefined; var tunnel: Tunnel = .{}; try listener.init(io, gpa, &socket, .{ .context = &tunnel, .serveFn = tunnelling, }, .{ .rng = rng, .reset_key = @splat(0x99), .token_key = @splat(0xaa), .max_connections = 2, }); defer listener.deinit(); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, run, .{&listener}); defer listener.stop(); var answer: [512]u8 = undefined; // What came back through the tunnel, which only arrives if the server's // half works while the client's is still open. const came_back = try connectAndSpeak(io, gpa, address, rng, &answer); try testing.expectEqualStrings("hello through the tunnel", came_back); // §5.1 of RFC 8441: the answer was a `2xx`, not a `101`. There is nothing // to switch -- the stream is the tunnel the moment the status goes out. try testing.expect(tunnel.upgraded.load(.acquire)); // And the octets the client sent after the request reached the handler // through the tunnel rather than being read as part of the request. // // Waited for rather than read straight away: the client returns when the // status arrives, and the handler is on a task of its own that has not // necessarily read its side yet. A tunnel is two independent directions, // which is the whole point of one, so a test of it has to be written that // way too. const n = tunnel.len.load(.acquire); try testing.expectEqualStrings("hello through the tunnel", tunnel.seen[0..n]); } test "a request is served with no socket, no handshake and no port" { // The whole reason `transport.Session` exists. Every other test in this // file binds a UDP port and completes a QUIC handshake to ask a question // about HTTP/3 framing, which means a failure in any of them is a failure // somewhere in QUIC, TLS, QPACK and HTTP/3 at once. This one hands the // driver a request as a slice, the way `tests/e2e.zig` has always been // able to for HTTP/1.1. const io = std.testing.io; const gpa = std.testing.allocator; // §7.2.1: a HEADERS frame carrying a QPACK field section, which is the // whole of a GET request. var section: [512]u8 = undefined; var sw: Io.Writer = .fixed(§ion); try h3.qpack.encode(&sw, default_fields); const fields = sw.buffered(); var request: [640]u8 = undefined; var rw: Io.Writer = .fixed(&request); try putVarint(&rw, 0x01); try putVarint(&rw, fields.len); try rw.writeAll(fields); // Stream 0: §2.1 of RFC 9000 makes the first client-initiated // bidirectional stream, which §4.1 of RFC 9114 makes a request. var scripted: http.transport.memory_session.Scripted = .{ .incoming = &.{.{ .id = .{ .value = 0 }, .data = rw.buffered() }}, }; defer scripted.deinit(gpa); var connection: h3.Connection = .init( io, gpa, scripted.session(gpa), .{ .context = null, .serveFn = echo }, .{}, ); connection.run(); // The answer is on the same stream, and it is this library's own bytes // read back by this library's own decoder -- which is exactly what the // socket-driven tests in this file check, at a fraction of the machinery. const answered = scripted.wrote(.{ .value = 0 }) orelse return error.NothingWritten; try std.testing.expect(answered.len > 0); var answer: [256]u8 = undefined; const body = try readResponse(answered, &answer); try std.testing.expectEqualStrings("GET /hello HTTP/3", body); // §6.2.1: the control stream is opened before anything is served, and it // is the first stream this endpoint opened. const control = scripted.local(0) orelse return error.NoControlStream; try std.testing.expect(control.written.items.len > 0); } fn runDialer(d: *h3_io.Dialer) void { d.run() catch {}; } test "this library's HTTP/3 client against this library's HTTP/3 server" { // The whole stack, both ends, over a real UDP socket: a QUIC handshake // with a certificate, §6.2's control streams in both directions, a QPACK // field section encoded by one end and decoded by the other, and a // response read back. Every other test in this file drives the server // with a hand-rolled client; this is the driver that ships. const io = testing.io; const gpa = testing.allocator; var rng_impl = std.Random.DefaultPrng.init(0x9115); const rng = rng_impl.random(); const bind: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try bind.bind(io, .{ .mode = .dgram }); defer socket.close(io); const address = socket.address; var listener: h3_io.Listener = undefined; try listener.init(io, gpa, &socket, .{ .context = null, .serveFn = echo, }, .{ .rng = rng, .reset_key = @splat(0x35), .token_key = @splat(0x46), .max_connections = 2, }); defer listener.deinit(); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, run, .{&listener}); defer listener.stop(); const client_bind: net.IpAddress = try .parse("127.0.0.1", 0); var client_socket = try client_bind.bind(io, .{ .mode = .dgram }); defer client_socket.close(io); var dialer: h3_io.Dialer = undefined; try dialer.connect(io, gpa, &client_socket, address, .{ .host = "h3.example", .insecure_skip_verify = true, .rng = rng, }); defer dialer.deinit(); // Its own task: a stream read blocks until a datagram arrives, and this // is what makes one arrive. var client_tasks: Io.Group = .init; defer client_tasks.cancel(io); try client_tasks.concurrent(io, runDialer, .{&dialer}); defer dialer.stop(); var c: http.http3.client.Connection = .init(gpa, dialer.session(), .{}); try c.start(); var exchange = try c.send(.{ .host = "h3.example", .target = "/hello" }); defer exchange.deinit(gpa); try testing.expectEqual(@as(u16, 200), exchange.response.status); try testing.expectEqual(http.protocol.Version.http_3, exchange.response.version); try testing.expectEqualStrings("GET /hello HTTP/3", exchange.body()); try testing.expectEqualStrings("text/plain", exchange.response.headers.get("content-type").?); } /// A reader that makes its octets as they are asked for, so a streamed body /// has nothing behind it in memory. `tests/fetch.zig` has the same one, for /// the other two versions; test modules do not share code. const Pattern = struct { interface: Io.Reader, produced: u64 = 0, total: u64, fail_at: ?u64 = null, fn init(buffer: []u8, total: u64) Pattern { return .{ .interface = .{ .vtable = &.{ .stream = stream }, .buffer = buffer, .seek = 0, .end = 0 }, .total = total, }; } fn octetAt(i: u64) u8 { return @truncate((i *% 0x9E3779B97F4A7C15) >> 56); } fn stream(r: *Io.Reader, w: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize { _ = .{ w, limit }; const p: *Pattern = @fieldParentPtr("interface", r); if (p.fail_at) |at| if (p.produced >= at) return error.ReadFailed; if (p.produced == p.total) return error.EndOfStream; if (r.seek == r.end) { r.seek = 0; r.end = 0; } else if (r.end == r.buffer.len) { const kept = r.end - r.seek; std.mem.copyForwards(u8, r.buffer[0..kept], r.buffer[r.seek..r.end]); r.seek = 0; r.end = kept; } var stop = p.total; if (p.fail_at) |at| stop = @min(stop, at); const n: usize = @intCast(@min(r.buffer.len - r.end, stop - p.produced)); for (r.buffer[r.end..][0..n], 0..) |*octet, i| octet.* = octetAt(p.produced + i); p.produced += n; r.end += n; return 0; } }; /// Checks every octet of the body against `Pattern` as it arrives, holding /// none of it, and says how many there were -- or where the first wrong one /// was. fn verifies(_: ?*anyopaque, req: *http.server.Request, res: *http.server.Response) anyerror!void { const reader = req.body.reader; var chunk: [16384]u8 = undefined; var at: u64 = 0; while (true) { const n = try reader.readSliceShort(&chunk); for (chunk[0..n], 0..) |octet, i| { if (octet != Pattern.octetAt(at + i)) return res.sendPrint("wrong at {d}", .{at + i}); } at += n; if (n < chunk.len) break; } try res.sendPrint("{s} {d} octets", .{ req.method.name, at }); } test "a request body streamed from a reader over HTTP/3" { const io = testing.io; const gpa = testing.allocator; var rng_impl = std.Random.DefaultPrng.init(0x9117); const rng = rng_impl.random(); const bind: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try bind.bind(io, .{ .mode = .dgram }); defer socket.close(io); const address = socket.address; var listener: h3_io.Listener = undefined; try listener.init(io, gpa, &socket, .{ .context = null, .serveFn = verifies }, .{ .rng = rng, .reset_key = @splat(0x39), .token_key = @splat(0x4a), .max_connections = 2, }); defer listener.deinit(); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, run, .{&listener}); defer listener.stop(); const client_bind: net.IpAddress = try .parse("127.0.0.1", 0); var client_socket = try client_bind.bind(io, .{ .mode = .dgram }); defer client_socket.close(io); var dialer: h3_io.Dialer = undefined; try dialer.connect(io, gpa, &client_socket, address, .{ .host = "h3.example", .insecure_skip_verify = true, .rng = rng, }); defer dialer.deinit(); var client_tasks: Io.Group = .init; defer client_tasks.cancel(io); try client_tasks.concurrent(io, runDialer, .{&dialer}); defer dialer.stop(); var c: http.http3.client.Connection = .init(gpa, dialer.session(), .{}); try c.start(); // Many times any stream window this session opens, so it can only have // arrived by being streamed through QUIC's flow control. const size: u64 = 4 << 20; { var source_buffer: [8192]u8 = undefined; var source: Pattern = .init(&source_buffer, size); var exchange = try c.send(.{ .host = "h3.example", .target = "/upload", .method = http.protocol.Method.put, .body = .{ .exact = size }, .source = &source.interface, }); defer exchange.deinit(gpa); try testing.expectEqualStrings("PUT 4194304 octets", exchange.body()); try testing.expectEqual(size, source.produced); } // A reader that ends early: the stream is reset rather than finished, so // the server is not handed a shorter body as though it were the whole. { var source_buffer: [8192]u8 = undefined; var source: Pattern = .init(&source_buffer, 100 << 10); try testing.expectError(error.RequestBodyShort, c.send(.{ .host = "h3.example", .target = "/upload", .method = http.protocol.Method.put, .body = .{ .exact = 1 << 20 }, .source = &source.interface, })); } // And one that fails, which is the reader's fault and not the session's. { var source_buffer: [8192]u8 = undefined; var source: Pattern = .init(&source_buffer, 1 << 20); source.fail_at = 50 << 10; try testing.expectError(error.RequestBodyReadFailed, c.send(.{ .host = "h3.example", .target = "/upload", .method = http.protocol.Method.put, .body = .{ .exact = 1 << 20 }, .source = &source.interface, })); } // A reset stream is that stream's end and no other's: the same session // carries the next request. { var source_buffer: [8192]u8 = undefined; var source: Pattern = .init(&source_buffer, 1000); var exchange = try c.send(.{ .host = "h3.example", .target = "/upload", .method = http.protocol.Method.post, .body = .{ .exact = 1000 }, .source = &source.interface, }); defer exchange.deinit(gpa); try testing.expectEqualStrings("POST 1000 octets", exchange.body()); } } /// Answers with what the request said its content length was, which is how a /// test asks whether a `Content-Length` crossed the wire. fn declares(_: ?*anyopaque, req: *http.server.Request, res: *http.server.Response) anyerror!void { const declared = req.body.length; const body = try req.body.readAlloc(std.testing.allocator, 1 << 20); defer std.testing.allocator.free(body); switch (declared) { .none => try res.send("none"), .exact => |n| try res.sendPrint("exact {d}", .{n}), .unknown => try res.send("unknown"), } } test "RFC 9110 8.6: an HTTP/3 request with a body declares how long it is" { // §4.1.2 of RFC 9114 makes the field optional -- the DATA frames and the // stream's end delimit the content -- and §8.6 of RFC 9110 asks for it // anyway when the length is known. A server may insist: Cloudflare's // DNS-over-HTTP/3 endpoint answers 400 to a POST without one, which is // what sent this driver looking. // // The server end is the proof. `req.body.length` is read out of the field // section and from nowhere else, so an answer of "exact 12" is the field // having been encoded, carried, and decoded. const io = testing.io; const gpa = testing.allocator; var rng_impl = std.Random.DefaultPrng.init(0x9116); const rng = rng_impl.random(); const bind: net.IpAddress = try .parse("127.0.0.1", 0); var socket = try bind.bind(io, .{ .mode = .dgram }); defer socket.close(io); const address = socket.address; var listener: h3_io.Listener = undefined; try listener.init(io, gpa, &socket, .{ .context = null, .serveFn = declares, }, .{ .rng = rng, .reset_key = @splat(0x37), .token_key = @splat(0x48), .max_connections = 2, }); defer listener.deinit(); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, run, .{&listener}); defer listener.stop(); const client_bind: net.IpAddress = try .parse("127.0.0.1", 0); var client_socket = try client_bind.bind(io, .{ .mode = .dgram }); defer client_socket.close(io); var dialer: h3_io.Dialer = undefined; try dialer.connect(io, gpa, &client_socket, address, .{ .host = "h3.example", .insecure_skip_verify = true, .rng = rng, }); defer dialer.deinit(); var client_tasks: Io.Group = .init; defer client_tasks.cancel(io); try client_tasks.concurrent(io, runDialer, .{&dialer}); defer dialer.stop(); var c: http.http3.client.Connection = .init(gpa, dialer.session(), .{}); try c.start(); { var exchange = try c.send(.{ .host = "h3.example", .target = "/submit", .method = .post, .body = .{ .exact = 12 }, .content = "hello, world", }); defer exchange.deinit(gpa); try testing.expectEqual(@as(u16, 200), exchange.response.status); try testing.expectEqualStrings("exact 12", exchange.body()); } { // And nothing where there is nothing to declare: a request with no // body is not a request with an empty one, so no `Content-Length: 0`. // // What the server makes of that is `unknown` rather than `none`, and // the difference between the versions is the point. HTTP/2 puts // END_STREAM on the HEADERS frame, so a server knows there is no // content as it reads the field section; over HTTP/3 the end of the // stream is a fact about the stream, which a driver that has only read // one frame cannot yet have. So an HTTP/3 recipient of a body with no // `Content-Length` knows the length only once it stops arriving -- // which is the whole reason §8.6 of RFC 9110 asks a sender that knows // it to say so, and the reason the first half of this test matters. var exchange = try c.send(.{ .host = "h3.example", .target = "/hello" }); defer exchange.deinit(gpa); try testing.expectEqual(@as(u16, 200), exchange.response.status); try testing.expectEqualStrings("unknown", exchange.body()); } } test "RFC 9112 2.2: the other two clients refuse a splitting target as well" { // The injection is HTTP/1.1's -- a CRLF ends a line there and nowhere // else -- so the temptation is to check it only in that driver. Two // reasons not to. §8.2.1 of RFC 9113 forbids CR, LF and NUL in a field // value, so a request carrying one is malformed on its own terms; and a // gateway converting it to HTTP/1.1 would do the splitting on our // behalf, one hop further along than anything here can see. // // The check lives in `client.Request.validate`, which all three drivers // call. This asserts that they do: a driver that quietly stopped calling // it would compile, pass every other test, and emit the message again. const gpa = testing.allocator; const io = testing.io; const splitting: http.client.Request = .{ .host = "example.com", .target = "/a\r\nX-Injected: yes", }; { // An empty SETTINGS, which is all the handshake needs. var memory = try http.transport.memory.Connection.init( gpa, &[_]u8{ 0, 0, 0, 0x04, 0, 0, 0, 0, 0 }, .{}, ); defer memory.deinit(); var c = try http.http2.client.Connection.init(io, gpa, memory.connection(), .{}); _ = c.releaseConnection(); defer c.deinit(); try c.handshake(); try testing.expectError(error.InvalidRequest, c.begin(splitting)); } { var scripted: http.transport.memory_session.Scripted = .{ .incoming = &.{}, // A HEADERS frame whose field section is an indexed `:status: 200`. .answer = &[_]u8{ 0x01, 0x03, 0x00, 0x00, 0xd9 }, }; defer scripted.deinit(gpa); var c: http.http3.client.Connection = .init(gpa, scripted.session(gpa), .{}); try c.start(); try testing.expectError(error.InvalidRequest, c.send(splitting)); // Nothing was opened for it, so nothing half-written is waiting on a // stream the caller cannot reach to reset. try testing.expect(scripted.find(.{ .value = 0 }) == null); } } test "RFC 9113 8.1: a stream that ends without a field section is not a response" { // "An HTTP message (request or response) consists of... a HEADERS frame // containing the header section." A stream ended by a DATA frame with no // field section before it carries no message, and a client that answered // with the zero its status was initialised to would be handing back a // response that never arrived. // // Found by fuzzing the HTTP/2 client against arbitrary server frames. const gpa = testing.allocator; const io = testing.io; // An empty SETTINGS, then a DATA frame on stream 1 with END_STREAM. const source = [_]u8{ 0, 0, 0, 0x04, 0, 0, 0, 0, 0, } ++ [_]u8{ 0, 0, 3, 0x00, 0x01, 0, 0, 0, 1, 'a', 'b', 'c', }; var memory = try http.transport.memory.Connection.init(gpa, &source, .{}); defer memory.deinit(); var c = try http.http2.client.Connection.init(io, gpa, memory.connection(), .{}); _ = c.releaseConnection(); defer c.deinit(); try c.handshake(); const handle = try c.begin(.{ .host = "example.com", .target = "/" }); try testing.expectError(error.MalformedResponse, c.await(handle)); } /// What a server made of a scripted connection: whether it closed the /// connection and why, the status it answered stream 0 with, the SETTINGS it /// advertised, and how many octets it allocated along the way. const Served = struct { closed: ?u64 = null, status: ?[3]u8 = null, /// Every HEADERS frame's `:status` on stream 0, in order: interim /// responses, then the final one. statuses: [4][3]u8 = undefined, heads: usize = 0, settings: Settings = .{}, allocated: usize = 0, }; /// §7.2.4.1 and RFC 9204 §5: the three settings the QPACK attacks turn on. const Settings = struct { max_field_section_size: ?u64 = null, qpack_max_table_capacity: ?u64 = null, qpack_blocked_streams: ?u64 = null, /// A control stream as written: its type, then a SETTINGS frame. fn parse(bytes: []const u8) !Settings { var at: usize = 0; const take = struct { fn varint(b: []const u8, i: *usize) !u64 { const v = try quic.varint.parse(b[i.*..]); i.* += v.len; return v.value; } }.varint; if (try take(bytes, &at) != 0x00) return error.NotAControlStream; if (try take(bytes, &at) != 0x04) return error.NoSettings; const end = at + @as(usize, @intCast(try take(bytes, &at))); var out: Settings = .{}; while (at < end) { const id = try take(bytes, &at); const value = try take(bytes, &at); switch (id) { 0x06 => out.max_field_section_size = value, 0x01 => out.qpack_max_table_capacity = value, 0x07 => out.qpack_blocked_streams = value, else => {}, } } return out; } }; /// Runs a server with `config` and `handler` over the streams in `incoming`, /// with no socket, and reports what it did. fn serveScripted( config: h3.Connection.Config, handler: http.server.Handler, incoming: []const http.transport.memory_session.Incoming, ) !Served { var counting: std.testing.FailingAllocator = .init(testing.allocator, .{}); const gpa = counting.allocator(); var scripted: http.transport.memory_session.Scripted = .{ .incoming = incoming }; defer scripted.deinit(gpa); var connection: h3.Connection = .init(testing.io, gpa, scripted.session(gpa), handler, config); connection.run(); var served: Served = .{ .allocated = counting.allocated_bytes }; if (scripted.closed) |closed| served.closed = closed.code; if (scripted.local(0)) |control| served.settings = try .parse(control.written.items); if (scripted.wrote(.{ .value = 0 })) |answered| { var status: [3]u8 = undefined; if (readStatus(answered, &status)) |got| { if (got.len == 3) served.status = got[0..3].*; } var at: usize = 0; while (at < answered.len) { const kind = quic.varint.parse(answered[at..]) catch break; const length = quic.varint.parse(answered[at + kind.len ..]) catch break; const start = at + kind.len + length.len; const end = start + @as(usize, @intCast(length.value)); if (end > answered.len) break; if (kind.value == 0x01 and served.heads < served.statuses.len) { if (readStatus(answered[at..end], &status)) |got| { if (got.len == 3) { served.statuses[served.heads] = got[0..3].*; served.heads += 1; } } } at = end; } } return served; } /// One request, as a HEADERS frame carrying `section` followed by `rest`. fn requestStream(buffer: []u8, section: []const u8, rest: []const u8) ![]const u8 { var rw: Io.Writer = .fixed(buffer); try putVarint(&rw, 0x01); try putVarint(&rw, section.len); try rw.writeAll(section); try rw.writeAll(rest); return rw.buffered(); } /// One request, run through a server with `config` and no socket, and the /// status it was answered with. fn statusFor(gpa: std.mem.Allocator, config: h3.Connection.Config, section: []const u8, out: []u8) !?[]const u8 { _ = gpa; var request: [4096]u8 = undefined; const served = try serveScripted(config, .{ .context = null, .serveFn = echo }, &.{ .{ .id = .{ .value = 0 }, .data = try requestStream(&request, section, "") }, }); // §4.2.2: one request's problem, never the connection's. if (served.closed) |code| { std.debug.print("the connection was closed: 0x{x}\n", .{code}); return error.ConnectionClosed; } const status = served.status orelse return null; @memcpy(out[0..3], &status); return out[0..3]; } test "RFC 9114 4.2.2: the field section limit is on the decoded section, not the frame" { // The "header accordion" of the QPACK amplification attacks: index 58 of // the static table is one octet on the wire and a 101 octet field line by // §4.2.2's arithmetic, so a HEADERS frame well inside the limit can // decode to many times it. A server that measured the frame would hand // every one of those lines to the handler. const gpa = testing.allocator; var section: [512]u8 = undefined; var sw: Io.Writer = .fixed(§ion); try h3.qpack.encode(&sw, default_fields); try sw.splatByteAll(0xc0 | 58, 16); // Thirty-odd octets of frame, and over 1600 of field section. try testing.expect(sw.buffered().len < 64); var status: [3]u8 = undefined; try testing.expectEqualStrings("431", (try statusFor( gpa, .{ .max_field_section_size = 1024 }, sw.buffered(), &status, )).?); // And the same request is served where the limit allows it. try testing.expectEqualStrings("200", (try statusFor( gpa, .{ .max_field_section_size = 4096 }, sw.buffered(), &status, )).?); } test "RFC 9114 4.2.2: more field lines than the server keeps is a 431, not a connection error" { // Running out of room to decode into is this server's limit, the same as // the size, and with no dynamic table an abandoned section leaves nothing // out of step -- so it is one request's answer, and the connection's // other requests go on. const gpa = testing.allocator; var section: [512]u8 = undefined; var sw: Io.Writer = .fixed(§ion); try h3.qpack.encode(&sw, default_fields); // `accept: */*`, index 29, as many times as it takes. try sw.splatByteAll(0xc0 | 29, 8); var status: [3]u8 = undefined; try testing.expectEqualStrings("431", (try statusFor( gpa, .{ .max_fields = 8 }, sw.buffered(), &status, )).?); } /// The HTTP/3 client against one canned response stream. fn clientGets(answer: []const u8, config: http.http3.client.Config) !u16 { const gpa = testing.allocator; var scripted: http.transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = answer, }; defer scripted.deinit(gpa); var c: http.http3.client.Connection = .init(gpa, scripted.session(gpa), config); try c.start(); var exchange = try c.send(.{ .host = "example.com", .target = "/" }); defer exchange.deinit(gpa); return exchange.response.status; } test "RFC 9114 4.1: a response carries one trailer section and nothing after it" { // A HEADERS frame with an indexed `:status: 200`, and one with nothing in // it but the prefix, which is a trailer section with no fields. const response = [_]u8{ 0x01, 0x03, 0x00, 0x00, 0xd9 }; const trailer = [_]u8{ 0x01, 0x02, 0x00, 0x00 }; try testing.expectEqual(@as(u16, 200), try clientGets(&(response ++ trailer), .{})); // A second trailer section is how a server would make a field section // of the whole stream, one frame at a time, each inside the limit; and // a DATA frame after the trailer is a body after the message ended. try testing.expectError( error.MalformedResponse, clientGets(&(response ++ trailer ++ trailer), .{}), ); try testing.expectError( error.MalformedResponse, clientGets(&(response ++ trailer ++ [_]u8{ 0x00, 0x01, 'x' }), .{}), ); } test "RFC 9114 4.2.2: the client's field section limit is on the decoded section too" { // `:status: 200` and eight of static index 58: eleven octets of frame and // over 800 of field section. const response = [_]u8{ 0x01, 0x0b, 0x00, 0x00, 0xd9 } ++ @as([8]u8, @splat(0xc0 | 58)); try testing.expectError( error.ResponseTooLarge, clientGets(&response, .{ .max_field_section_size = 512 }), ); try testing.expectEqual(@as(u16, 200), try clientGets(&response, .{ .max_field_section_size = 1024 })); } // Regression tests for the rest of the QPACK "header accordion" attacks // (Féry 2026, in the README's references). None of them reaches this // library as it stands; each test pins down the reason, so that a change // which takes the reason away fails here rather than in somebody's server. /// Drains the request body and answers, which is what takes a server past /// the first HEADERS frame of a request stream into whatever follows it. fn drains(_: ?*anyopaque, req: *http.server.Request, res: *http.server.Response) anyerror!void { _ = try req.body.reader.discardRemaining(); try res.send("drained"); } /// §4.5.6 of RFC 9204: `count` field lines, each a one-octet literal name /// with an empty value, after the request's own. fn tinyFields(buffer: []u8, count: usize) ![]const u8 { var sw: Io.Writer = .fixed(buffer); try h3.qpack.encode(&sw, default_fields); for (0..count) |_| try putLiteral(&sw, "a", ""); return sw.buffered(); } test "header accordions: the limits a server advertises are finite and promise no dynamic table" { // aioquic stored a peer's limit and never applied it, and several servers // advertised none at all and so took anything. A default is what most // servers run with, so the default is what is pinned: a finite field // section size, and a QPACK table of zero, which is the whole defence // against the dynamic-table accordion -- a peer told there is no table // cannot insert a 4 KiB entry to refer to thousands of times. const served = try serveScripted(.{}, .{ .context = null, .serveFn = echo }, &.{}); const defaults: h3.Connection.Config = .{}; try testing.expectEqual(@as(?u64, defaults.max_field_section_size), served.settings.max_field_section_size); try testing.expect(defaults.max_field_section_size <= 64 * 1024); try testing.expectEqual(@as(?u64, 0), served.settings.qpack_max_table_capacity); try testing.expectEqual(@as(?u64, 0), served.settings.qpack_blocked_streams); // And what it advertises is what it enforces. const configured = try serveScripted( .{ .max_field_section_size = 777 }, .{ .context = null, .serveFn = echo }, &.{}, ); try testing.expectEqual(@as(?u64, 777), configured.settings.max_field_section_size); } test "header accordions: an insertion on the QPACK encoder stream ends the connection" { // The first half of the dynamic-table accordion of lsquic and Bun: one // large entry put in the table, to be referred to cheaply afterwards. // RFC 9204 §3.2.2: with a capacity of zero "the encoder MUST NOT insert", // and §4.1.4 makes one a QPACK_ENCODER_STREAM_ERROR. // // Client-initiated unidirectional stream 2: type 0x02, then §4.3.3's // Insert with Literal Name, `01HNNNNN`, of a name `x` and a value of // 4000 octets that are never sent -- the instruction is refused on its // first octet, before anything is allocated for it. const served = try serveScripted(.{}, .{ .context = null, .serveFn = echo }, &.{ .{ .id = .{ .value = 2 }, .data = &.{ 0x02, 0x41, 'x', 0x7f, 0xa1, 0x1e } }, }); try testing.expectEqual(@as(?u64, @backingInt(h3.frame.Error.qpack_encoder_stream_error)), served.closed); try testing.expect(served.allocated < 64 * 1024); } test "header accordions: a field section that refers to the dynamic table is refused" { // The second half: thousands of one-octet references to that entry, each // of which lsquic copied out in full. With no table there is nothing to // copy, and §2.2.3 of RFC 9204 makes a reference to an entry that does // not exist QPACK_DECOMPRESSION_FAILED -- a connection error, and the // request is never handed to a handler. var request: [256]u8 = undefined; const references = [_]u8{ 0, 0 } ++ @as([16]u8, @splat(0x80)); const indexed = try serveScripted(.{}, .{ .context = null, .serveFn = echo }, &.{ .{ .id = .{ .value = 0 }, .data = try requestStream(&request, &references, "") }, }); try testing.expectEqual(@as(?u64, @backingInt(h3.frame.Error.qpack_decompression_failed)), indexed.closed); try testing.expect(indexed.status == null); // And a Required Insert Count that is not zero, which is the section // saying it depends on insertions before it says anything else. const prefix = [_]u8{ 0x02, 0x00, 0xd1 }; const required = try serveScripted(.{}, .{ .context = null, .serveFn = echo }, &.{ .{ .id = .{ .value = 0 }, .data = try requestStream(&request, &prefix, "") }, }); try testing.expectEqual(@as(?u64, @backingInt(h3.frame.Error.qpack_decompression_failed)), required.closed); try testing.expect(required.status == null); } test "header accordions: every field line costs 32 octets over its name and value" { // quiche counted only the name and value, so a 1 MiB limit let in a // million one-octet lines where §4.2.2 allows about 32,000 -- and each // line is a struct, a slot in a list and two slices, whatever its length. // // Sixty empty lines named `a`: about 113 octets of names and values, // which a limit of 1024 would take, and over 2000 octets of field section // once each line is charged its 32. var section: [512]u8 = undefined; const fields = try tinyFields(§ion, 60); var status: [3]u8 = undefined; try testing.expectEqualStrings("431", (try statusFor( testing.allocator, .{ .max_field_section_size = 1024 }, fields, &status, )).?); try testing.expectEqualStrings("200", (try statusFor( testing.allocator, .{ .max_field_section_size = 4096 }, fields, &status, )).?); } test "header accordions: a field count configured past what the server holds is still a 431" { // The h3 crate decoded into a list and then appended to a map with an // `expect`, so a section with more lines than the map could hold was a // panic. Here the configured count is clamped to the array the server // decodes into, and a section that overflows it is a 431, however large // the configuration says it may be. var section: [1024]u8 = undefined; const fields = try tinyFields(§ion, 200); var status: [3]u8 = undefined; try testing.expectEqualStrings("431", (try statusFor( testing.allocator, .{ .max_fields = 100_000, .max_field_section_size = 1 << 30 }, fields, &status, )).?); } test "header accordions: a frame's declared length allocates nothing until it is accepted" { // quiche allocated what a SETTINGS or GOAWAY frame said it would carry // before reading any of it, so a peer could declare 16 MiB and send none. // §16 of RFC 9000: an eight-octet varint, here 1 GiB. const gib = [_]u8{ 0xc0, 0, 0, 0, 0x40, 0, 0, 0 }; const bound = 256 * 1024; // A request's HEADERS frame: refused on its length, with a 431. const headers = [_]u8{0x01} ++ gib; const request = try serveScripted(.{}, .{ .context = null, .serveFn = echo }, &.{ .{ .id = .{ .value = 0 }, .data = &headers }, }); try testing.expectEqualStrings("431", &request.status.?); try testing.expect(request.allocated < bound); // SETTINGS on the control stream: stepped over as it arrives. const settings = [_]u8{ 0x00, 0x04 } ++ gib; const control = try serveScripted(.{}, .{ .context = null, .serveFn = echo }, &.{ .{ .id = .{ .value = 2 }, .data = &settings }, }); try testing.expect(control.allocated < bound); // GOAWAY after an empty SETTINGS: refused, since one identifier is at // most eight octets. const goaway = [_]u8{ 0x00, 0x04, 0x00, 0x07 } ++ gib; const away = try serveScripted(.{}, .{ .context = null, .serveFn = echo }, &.{ .{ .id = .{ .value = 2 }, .data = &goaway }, }); try testing.expectEqual(@as(?u64, @backingInt(h3.frame.Error.frame_error)), away.closed); try testing.expect(away.allocated < bound); } test "header accordions: header blocks after the first on a request stream are not kept" { // lsquic's 64 KiB cap was per HEADERS frame rather than per stream, and // it took any number of them before the body, so a chain of frames each // inside the cap held as much as the attacker cared to send. Here the // second field section on a request stream is the trailer section, and // it and everything after it are read past without being stored: what // the server allocates for a request is the same with one trailing frame // as with five hundred. const gpa = testing.allocator; var section: [512]u8 = undefined; var sw: Io.Writer = .fixed(§ion); try h3.qpack.encode(&sw, default_fields); // A trailer section of one 1000-octet value. var trailer_section: [1100]u8 = undefined; var tw: Io.Writer = .fixed(&trailer_section); try tw.writeAll(&.{ 0, 0 }); try putLiteral(&tw, "x-pad", &@as([1000]u8, @splat('p'))); var trailer: [1200]u8 = undefined; var fw: Io.Writer = .fixed(&trailer); try putVarint(&fw, 0x01); try putVarint(&fw, tw.buffered().len); try fw.writeAll(tw.buffered()); var allocated: [2]usize = undefined; for ([_]usize{ 1, 500 }, &allocated) |frames, *out| { const rest = try gpa.alloc(u8, frames * fw.buffered().len); defer gpa.free(rest); for (0..frames) |i| @memcpy(rest[i * fw.buffered().len ..][0..fw.buffered().len], fw.buffered()); const stream = try gpa.alloc(u8, rest.len + 1024); defer gpa.free(stream); const served = try serveScripted(.{}, .{ .context = null, .serveFn = drains }, &.{ .{ .id = .{ .value = 0 }, .data = try requestStream(stream, sw.buffered(), rest) }, }); try testing.expectEqualStrings("200", &served.status.?); out.* = served.allocated; } // Five hundred frames are half a megabyte of trailer; a server keeping // them would show it here. try testing.expect(allocated[1] < allocated[0] + 16 * 1024); } /// The HTTP/3 client against one canned response stream, counting what it /// allocates. fn clientCounting(answer: []const u8, config: http.http3.client.Config, allocated: *usize) !u16 { var counting: std.testing.FailingAllocator = .init(testing.allocator, .{}); defer allocated.* = counting.allocated_bytes; const gpa = counting.allocator(); var scripted: http.transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = answer, }; defer scripted.deinit(gpa); var c: http.http3.client.Connection = .init(gpa, scripted.session(gpa), config); try c.start(); var exchange = try c.send(.{ .host = "example.com", .target = "/" }); defer exchange.deinit(gpa); return exchange.response.status; } test "header accordions: the limits the client advertises are finite and promise no dynamic table" { var scripted: http.transport.memory_session.Scripted = .{ .incoming = &.{} }; defer scripted.deinit(testing.allocator); var c: http.http3.client.Connection = .init(testing.allocator, scripted.session(testing.allocator), .{}); try c.start(); const settings: Settings = try .parse(scripted.local(0).?.written.items); const defaults: http.http3.client.Config = .{}; try testing.expectEqual(@as(?u64, defaults.max_field_section_size), settings.max_field_section_size); try testing.expect(defaults.max_field_section_size <= 64 * 1024); try testing.expectEqual(@as(?u64, 0), settings.qpack_max_table_capacity); } test "header accordions: a response that refers to the dynamic table is malformed" { // `:status: 200` and then a reference to dynamic entry 0. try testing.expectError( error.MalformedResponse, clientGets(&.{ 0x01, 0x04, 0x00, 0x00, 0xd9, 0x80 }, .{}), ); } test "header accordions: the client charges 32 octets a line too" { // `:status: 200` and sixty empty lines named `a`: about 70 octets of // names and values and over 2000 of field section. var section: [256]u8 = undefined; var sw: Io.Writer = .fixed(§ion); try sw.writeAll(&.{ 0x00, 0x00, 0xd9 }); for (0..60) |_| try putLiteral(&sw, "a", ""); var response: [300]u8 = undefined; var rw: Io.Writer = .fixed(&response); try putVarint(&rw, 0x01); try putVarint(&rw, sw.buffered().len); try rw.writeAll(sw.buffered()); try testing.expectError( error.ResponseTooLarge, clientGets(rw.buffered(), .{ .max_field_section_size = 1024 }), ); try testing.expectEqual(@as(u16, 200), try clientGets(rw.buffered(), .{ .max_field_section_size = 4096 })); // And more lines than it was configured to hold is the same answer, not // a panic. try testing.expectError(error.ResponseTooLarge, clientGets(rw.buffered(), .{ .max_fields = 8 })); } test "header accordions: a response frame's declared length allocates nothing until it is accepted" { const gib = [_]u8{ 0xc0, 0, 0, 0, 0x40, 0, 0, 0 }; var allocated: usize = 0; try testing.expectError( error.ResponseTooLarge, clientCounting(&([_]u8{0x01} ++ gib), .{}, &allocated), ); try testing.expect(allocated < 256 * 1024); // A DATA frame after the head, declaring more body than the client holds. try testing.expectError( error.ResponseTooLarge, clientCounting(&([_]u8{ 0x01, 0x03, 0x00, 0x00, 0xd9, 0x00 } ++ gib), .{}, &allocated), ); try testing.expect(allocated < 256 * 1024); } // Interim responses, which §4.1 of RFC 9114 puts ahead of the final response // on the same request stream, each as a HEADERS frame of its own. /// What an `InterimHook` was told, copied out while it was still valid. const Interims = struct { count: usize = 0, statuses: [8]u16 = @splat(0), links: [8][64]u8 = undefined, link_lens: [8]usize = @splat(0), fn hook(i: *Interims) http.client.Request.InterimHook { return .{ .context = i, .call = record }; } fn record(context: ?*anyopaque, head: http.client.Request.InterimHead) void { const i: *Interims = @ptrCast(@alignCast(context.?)); if (i.count < i.statuses.len) { i.statuses[i.count] = head.status; const value = head.headers.get("link") orelse ""; const n = @min(value.len, i.links[i.count].len); @memcpy(i.links[i.count][0..n], value[0..n]); i.link_lens[i.count] = n; } i.count += 1; } fn link(i: *const Interims, at: usize) []const u8 { return i.links[at][0..i.link_lens[at]]; } }; /// A HEADERS frame around a QPACK field section of `representations`. fn h3Headers(w: *Io.Writer, representations: []const u8) !void { try putVarint(w, 0x01); try putVarint(w, representations.len + 2); try w.writeAll(&.{ 0, 0 }); try w.writeAll(representations); } /// RFC 9204 Appendix A: `:status 103` is static index 24, `:status 100` is 63 /// and `:status 200` is 25, each an indexed field line. const qpack_103 = [_]u8{0xc0 | 24}; const qpack_100 = [_]u8{ 0xff, 63 - 63 }; const qpack_200 = [_]u8{0xc0 | 25}; /// §4.5.4: `link` (static index 11) with a literal value. fn qpackLink(comptime value: []const u8) [2 + value.len]u8 { return [_]u8{ 0x50 | 11, value.len } ++ value[0..value.len].*; } /// The HTTP/3 client against `answer`, with `interim` told of what it is told. fn clientBody( answer: []const u8, config: http.http3.client.Config, interim: ?http.client.Request.InterimHook, body: []u8, ) !struct { status: u16, body: []const u8 } { const gpa = testing.allocator; var scripted: http.transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = answer }; defer scripted.deinit(gpa); var c: http.http3.client.Connection = .init(gpa, scripted.session(gpa), config); try c.start(); var exchange = try c.send(.{ .host = "example.com", .target = "/", .interim = interim }); defer exchange.deinit(gpa); // §4.3: control data never reaches the fields. for (exchange.response.headers.fields.items) |field| try testing.expect(field.name[0] != ':'); try testing.expect(exchange.response.headers.get("link") == null); const n = @min(body.len, exchange.body().len); @memcpy(body[0..n], exchange.body()[0..n]); return .{ .status = exchange.response.status, .body = body[0..n] }; } test "RFC 9114 4.1: interim responses come before the final one, and are told, not returned" { var answer: [256]u8 = undefined; var w: Io.Writer = .fixed(&answer); try h3Headers(&w, &qpack_100); try h3Headers(&w, &(qpack_103 ++ qpackLink("; rel=preload"))); try h3Headers(&w, &(qpack_103 ++ qpackLink("; rel=preload"))); try h3Headers(&w, &qpack_200); try w.writeAll(&.{ 0x00, 0x02, 'h', 'i' }); var seen: Interims = .{}; var body: [16]u8 = undefined; const got = try clientBody(w.buffered(), .{}, seen.hook(), &body); try testing.expectEqual(@as(u16, 200), got.status); try testing.expectEqualStrings("hi", got.body); try testing.expectEqual(@as(usize, 3), seen.count); try testing.expectEqual(@as(u16, 100), seen.statuses[0]); try testing.expectEqual(@as(u16, 103), seen.statuses[1]); try testing.expectEqualStrings("; rel=preload", seen.link(1)); try testing.expectEqualStrings("; rel=preload", seen.link(2)); // And skipped unseen when nobody asked. const unseen = try clientBody(w.buffered(), .{}, null, &body); try testing.expectEqual(@as(u16, 200), unseen.status); } test "RFC 9114 4.5: a 101 is malformed in HTTP/3" { // "HTTP/3 does not support the 101 (Switching Protocols) informational // status code." var answer: [64]u8 = undefined; var w: Io.Writer = .fixed(&answer); // §4.5.4: `:status` by name reference, index 24, and a literal value. try h3Headers(&w, &.{ 0x5f, 24 - 15, 3, '1', '0', '1' }); try h3Headers(&w, &qpack_200); var body: [16]u8 = undefined; try testing.expectError(error.MalformedResponse, clientBody(w.buffered(), .{}, null, &body)); } test "RFC 9114 4.1: a stream that ends after an interim response carries no response" { var answer: [64]u8 = undefined; var w: Io.Writer = .fixed(&answer); try h3Headers(&w, &qpack_103); var body: [16]u8 = undefined; try testing.expectError(error.MalformedResponse, clientBody(w.buffered(), .{}, null, &body)); } test "more interim responses than an HTTP/3 client allows is an error" { var answer: [64]u8 = undefined; var w: Io.Writer = .fixed(&answer); for (0..3) |_| try h3Headers(&w, &qpack_100); try h3Headers(&w, &qpack_200); var body: [16]u8 = undefined; try testing.expectError( error.TooManyInterimResponses, clientBody(w.buffered(), .{ .max_interim_responses = 2 }, null, &body), ); } test "RFC 9110 15: a :status below 100 is malformed in HTTP/3 too" { var answer: [64]u8 = undefined; var w: Io.Writer = .fixed(&answer); try h3Headers(&w, &.{ 0x5f, 24 - 15, 3, '0', '1', '3' }); var body: [16]u8 = undefined; try testing.expectError(error.MalformedResponse, clientBody(w.buffered(), .{}, null, &body)); } // RFC 9110 10.1.1 over HTTP/3: the head on its own, and the content once the // server asks for it. /// What the client wrote on stream 0: whether its HEADERS asked for a /// 100-continue expectation, the DATA it sent, whether it ended its half, /// and the code it reset it with. const H3Sent = struct { expectation: bool = false, data: [32]u8 = undefined, data_len: usize = 0, finished: bool = false, reset: ?u64 = null, fn of(scripted: *const http.transport.memory_session.Scripted) !H3Sent { var out: H3Sent = .{}; const state = scripted.find(.{ .value = 0 }) orelse return out; out.finished = state.finished; out.reset = state.reset; const bytes = state.written.items; var at: usize = 0; while (at < bytes.len) { const kind = try quic.varint.parse(bytes[at..]); at += kind.len; const length = try quic.varint.parse(bytes[at..]); at += length.len; const payload = bytes[at..][0..@intCast(length.value)]; at += payload.len; switch (kind.value) { 0x01 => { var fields: [32]h3.qpack.Field = undefined; var scratch: [1024]u8 = undefined; const n = try h3.qpack.decode(payload, &fields, &scratch); for (fields[0..n]) |f| { if (std.mem.eql(u8, f.name, "expect") and std.mem.eql(u8, f.value, "100-continue")) { out.expectation = true; } } }, 0x00 => { @memcpy(out.data[out.data_len..][0..payload.len], payload); out.data_len += payload.len; }, else => {}, } } return out; } }; /// The HTTP/3 client sending `request` against `answer`, and what came of it. fn expectOver3(answer: []const u8, request: http.client.Request) !struct { status: u16, content_sent: bool, sent: H3Sent } { const gpa = testing.allocator; var scripted: http.transport.memory_session.Scripted = .{ .incoming = &.{}, .answer = answer }; defer scripted.deinit(gpa); var c: http.http3.client.Connection = .init(gpa, scripted.session(gpa), .{}); try c.start(); var exchange = try c.send(request); defer exchange.deinit(gpa); return .{ .status = exchange.response.status, .content_sent = exchange.response.content_sent, .sent = try H3Sent.of(&scripted), }; } test "RFC 9110 10.1.1: over HTTP/3, the content follows the 100" { var answer: [64]u8 = undefined; var w: Io.Writer = .fixed(&answer); try h3Headers(&w, &qpack_100); try h3Headers(&w, &.{ 0x5f, 24 - 15, 3, '2', '0', '1' }); const got = try expectOver3(w.buffered(), .{ .method = http.protocol.Method.put, .host = "example.com", .body = .{ .exact = 5 }, .content = "hello", .expect_continue = true, }); try testing.expectEqual(@as(u16, 201), got.status); try testing.expect(got.content_sent); try testing.expect(got.sent.expectation); try testing.expectEqualStrings("hello", got.sent.data[0..got.sent.data_len]); try testing.expect(got.sent.finished); try testing.expectEqual(@as(?u64, null), got.sent.reset); } test "RFC 9110 10.1.1: over HTTP/3, a final response first, and the content is never sent" { // The request's half of the stream is reset with H3_NO_ERROR, §4.1's way // of saying the content is not coming, rather than ended as though it had // been empty. var answer: [64]u8 = undefined; var w: Io.Writer = .fixed(&answer); try h3Headers(&w, &.{ 0x5f, 24 - 15, 3, '4', '0', '1' }); var reader: Io.Reader = .fixed("a large upload"); const got = try expectOver3(w.buffered(), .{ .method = http.protocol.Method.put, .host = "example.com", .body = .{ .exact = 14 }, .source = &reader, .expect_continue = true, }); try testing.expectEqual(@as(u16, 401), got.status); try testing.expect(!got.content_sent); try testing.expectEqual(@as(usize, 0), got.sent.data_len); try testing.expectEqual(@as(?u64, @backingInt(h3.frame.Error.no_error)), got.sent.reset); try testing.expectEqual(@as(usize, 0), reader.seek); } test "RFC 9110 10.1.1: over HTTP/3, no content means no expectation" { var answer: [16]u8 = undefined; var w: Io.Writer = .fixed(&answer); try h3Headers(&w, &qpack_200); const got = try expectOver3(w.buffered(), .{ .host = "example.com", .expect_continue = true }); try testing.expect(!got.sent.expectation); try testing.expect(got.sent.finished); } // RFC 9110 10.1.1 on the HTTP/3 server: the same rule the other two follow. /// A POST's field section, with `expect` after it. fn expectingSection(buffer: []u8, value: []const u8) ![]const u8 { var sw: Io.Writer = .fixed(buffer); try h3.qpack.encode(&sw, &.{ .{ .name = ":method", .value = "POST" }, .{ .name = ":scheme", .value = "https" }, .{ .name = ":path", .value = "/upload" }, .{ .name = ":authority", .value = "h3.example" }, .{ .name = "expect", .value = value }, }); return sw.buffered(); } /// Answers without reading the body. fn refusesH3(_: ?*anyopaque, _: *http.server.Request, res: *http.server.Response) anyerror!void { return res.sendStatus(.unauthorized); } test "RFC 9110 10.1.1: over HTTP/3, a 100 Continue goes out when the handler asks for the body" { var section: [128]u8 = undefined; var request: [256]u8 = undefined; const served = try serveScripted(.{}, .{ .context = null, .serveFn = drains }, &.{.{ .id = .{ .value = 0 }, .data = try requestStream(&request, try expectingSection(§ion, "100-continue"), &.{ 0x00, 0x05, 'h', 'e', 'l', 'l', 'o' }), // The client has not finished the stream: it is holding the content // back, which is what the `100` is for. .received = false, }}); try testing.expectEqual(@as(?u64, null), served.closed); try testing.expectEqual(@as(usize, 2), served.heads); try testing.expectEqualStrings("100", &served.statuses[0]); try testing.expectEqualStrings("200", &served.statuses[1]); } test "RFC 9110 10.1.1: over HTTP/3, no 100 for a request whose content has already ended" { // The client finished the stream without waiting -- its content is all // here, or there never was any -- so nothing is held back to ask for, and // §10.1.1 lets a server that "has already received some or all of the // content" omit the `100`. The HTTP/2 driver sees the same from // END_STREAM on the HEADERS frame. var section: [128]u8 = undefined; var request: [256]u8 = undefined; for ([_][]const u8{ "", &.{ 0x00, 0x05, 'h', 'e', 'l', 'l', 'o' } }) |content| { const served = try serveScripted(.{}, .{ .context = null, .serveFn = drains }, &.{.{ .id = .{ .value = 0 }, .data = try requestStream(&request, try expectingSection(§ion, "100-continue"), content), }}); try testing.expectEqual(@as(usize, 1), served.heads); try testing.expectEqualStrings("200", &served.statuses[0]); } } test "RFC 9110 10.1.1: over HTTP/3, no 100 when the handler answers without the body" { var section: [128]u8 = undefined; var request: [256]u8 = undefined; const served = try serveScripted(.{}, .{ .context = null, .serveFn = refusesH3 }, &.{.{ .id = .{ .value = 0 }, .data = try requestStream(&request, try expectingSection(§ion, "100-continue"), ""), }}); try testing.expectEqual(@as(usize, 1), served.heads); try testing.expectEqualStrings("401", &served.statuses[0]); } test "RFC 9110 10.1.1: over HTTP/3, an expectation the server does not understand is a 417" { var section: [128]u8 = undefined; var request: [256]u8 = undefined; const served = try serveScripted(.{}, .{ .context = null, .serveFn = drains }, &.{.{ .id = .{ .value = 0 }, .data = try requestStream(&request, try expectingSection(§ion, "something-else"), &.{ 0x00, 0x02, 'h', 'i' }), }}); try testing.expectEqual(@as(usize, 1), served.heads); try testing.expectEqualStrings("417", &served.statuses[0]); }