// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! `http_io.Client` against this library's own server, over real sockets. //! //! What is being checked is the part the drivers do not do: that a URL //! becomes an address, a target and an authority; that the version is the one //! the TLS handshake settled on rather than the one the client hoped for; and //! that a redirect is followed the way §15.4 of RFC 9110 describes and no //! further. Every test that dials a name dials `localhost`, which the //! resolver answers from `/etc/hosts` -- so the lookup is real, and the //! suite still needs no network. //! //! The certificate in `testdata/` is self-signed for `localhost`, with a //! validity of a hundred years, and exists only for this. const std = @import("std"); const Io = std.Io; const net = Io.net; const testing = std.testing; const http = @import("http"); const http_io = @import("http_io"); const Request = http.server.Request; const Response = http.server.Response; const Client = http_io.Client; const gpa = testing.allocator; const cert_pem = @embedFile("testdata/localhost.cert.pem"); const key_pem = @embedFile("testdata/localhost.key.pem"); /// What every response carries as `Alt-Svc`, when a test sets it: how the /// fixture advertises HTTP/3. Tests run one at a time, and each that sets /// this puts it back. var alt_svc_value: []const u8 = ""; /// Says what arrived, so a test can tell which version carried it and what /// the target turned into on the way. fn routes(req: *Request, res: *Response) anyerror!void { const path = req.target.path; if (alt_svc_value.len > 0) try res.setHeader("Alt-Svc", alt_svc_value); if (std.mem.eql(u8, path, "/moved")) return res.redirect(.found, "landed?from=moved"); if (std.mem.eql(u8, path, "/see-other")) return res.redirect(.see_other, "/landed"); if (std.mem.eql(u8, path, "/keep")) return res.redirect(.temporary_redirect, "/landed"); if (std.mem.eql(u8, path, "/loop")) return res.redirect(.found, "/loop"); // Escaped reserved characters in a `Location`, which have to reach the // next request as escaped as they were written. if (std.mem.eql(u8, path, "/escaped")) return res.redirect(.found, "landed?q=a%2Bb%26c%3Dd%2Fe"); if (std.mem.eql(u8, path, "/missing")) return res.sendStatus(.not_found); // Port 1, where nothing listens: following this is a refused connection, // and refusing it is `error.InsecureRedirect` -- so the two outcomes say // which happened without anything having to answer. if (std.mem.eql(u8, path, "/downgrade")) return res.redirect(.found, "http://127.0.0.1:1/"); if (std.mem.eql(u8, path, "/elsewhere")) { // The same server under another name, which is another origin: the // host differs even though the socket does not. const authority = req.authority() orelse return error.NoAuthority; const colon = std.mem.findScalarLast(u8, authority, ':') orelse return error.NoPort; const location = try std.fmt.allocPrint(req.arena, "http://127.0.0.1{s}/credential", .{authority[colon..]}); return res.redirect(.found, location); } if (std.mem.eql(u8, path, "/squish")) { // Compressed if the client asked, and only then: `compress` is the // server's willingness, and `Accept-Encoding` is still what decides. // No floor, so that the choice is the negotiation's alone. res.compress = .{ .min_length = 0 }; try res.setHeader("Content-Type", "text/plain"); return res.send(try squishBody(req.arena)); } if (std.mem.eql(u8, path, "/squish-empty")) { res.compress = .{ .min_length = 0 }; try res.setHeader("Content-Type", "text/plain"); return res.send(""); } if (std.mem.eql(u8, path, "/verify")) { // Reads the body as it arrives and checks every octet against the // pattern `Pattern` produces, holding none of it -- so a body of any // size costs this handler what a small one does, and a single wrong, // missing or repeated octet is reported with where it was. // // Into a buffer of its own rather than peeked: the HTTP/2 driver's // body reader has no buffer to peek into, and says so by asserting. 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; // Short only at the end of the stream. if (n < chunk.len) break; } return res.sendPrint("{t} {s} {d} octets", .{ req.version, req.method.name, at }); } // RFC 8297: two Early Hints and then the answer, which is what a server // that knows its subresources before it knows its response sends. if (std.mem.eql(u8, path, "/hints")) { var hints: http.protocol.Headers = .empty; try hints.append(req.arena, "link", "; rel=preload"); try res.sendInterim(.early_hints, &hints); hints = .empty; try hints.append(req.arena, "link", "; rel=preload"); try res.sendInterim(.early_hints, &hints); return res.sendPrint("{t} hinted", .{req.version}); } // A hint, and then a redirect to more of them: what is hinted on the way // through a redirect is told too. if (std.mem.eql(u8, path, "/hinted-move")) { var hints: http.protocol.Headers = .empty; try hints.append(req.arena, "link", "; rel=preload"); try res.sendInterim(.early_hints, &hints); return res.redirect(.found, "/hints"); } // Refuses without reading the body, which is the answer a 100-continue // expectation is there to get early. if (std.mem.eql(u8, path, "/private")) return res.sendStatus(.unauthorized); // Will not take a request that carries an expectation, the way an old // intermediary might not. if (std.mem.eql(u8, path, "/no-expect")) { if (req.header("expect") != null) return res.sendStatus(.expectation_failed); const body = try req.body.readAlloc(req.arena, 1 << 20); return res.sendPrint("{t} took {d} octets", .{ req.version, body.len }); } // Sends the upload elsewhere before it has been asked for. if (std.mem.eql(u8, path, "/upload-elsewhere")) return res.redirect(.temporary_redirect, "/verify"); if (std.mem.eql(u8, path, "/credential")) { return res.send(req.header("authorization") orelse "none"); } const body = try req.body.readAlloc(req.arena, 1 << 20); try res.setHeader("X-Echoed", "yes"); try res.addHeader("X-Twice", "one"); try res.addHeader("X-Twice", "two"); try res.sendPrint("{t} {s} {s} {s} {s}", .{ req.version, req.method.name, path, req.target.query orelse "-", body, }); } /// What `/squish` serves: a mebibyte of numbered lines. Larger than an HTTP/2 /// stream window and than any buffer the HTTP/1.1 driver reads through, so a /// decoder that only managed the first chunk is caught; compressible, so the /// coding is worth something; and different on every line, so a decoder that /// repeated or dropped a stretch is caught too. fn squishBody(allocator: std.mem.Allocator) ![]u8 { // Exactly this long, so that it is freed at the size it was allocated. const body = try allocator.alloc(u8, 1 << 20); // Defined throughout, including the tail a cut-off line may not reach. @memset(body, '.'); var out: Io.Writer = .fixed(body); var n: usize = 0; // The last line is cut off where the buffer ends, which a fixed writer // reports as a failed write. while (true) : (n += 1) { out.print("line {d} of the body that is sent compressed\n", .{n}) catch break; } return body; } /// A reader that makes its octets as they are asked for: a body of any length /// with nothing behind it in memory, so a test of a streamed upload measures /// the client and not the test. /// /// `fail_at` makes it fail rather than end, at that offset, which is the /// other way a source can let a request down. 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, }; } /// Not a repeating block, so that an offset error of a whole block -- /// a chunk sent twice, or skipped -- is caught. 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; // Into the reader's own buffer, which the interface allows and which // keeps this to one loop. 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; } }; /// A server on a loopback port, with TLS in front of it or not. /// /// Must not be moved once started: the running task holds a pointer to the /// server, which holds one to the listener. const Fixture = struct { plain: http_io.tcp.Listener = undefined, secure: http_io.tls.Listener = undefined, is_secure: bool = false, auth: http_io.tls.CertKeyPair = undefined, prng: std.Random.DefaultPrng = .init(0x7301), server: http.server.Server = undefined, tasks: Io.Group = .init, /// HTTP/3, when `serveHttp3` has been called: a UDP socket on the same /// port number as the TCP one, as a real server has it, so that one URL /// reaches either. udp: ?Io.net.Socket = null, quic: http_io.http3.Listener = undefined, quic_tasks: Io.Group = .init, fn startPlain(f: *Fixture) !void { const io = testing.io; f.* = .{}; f.plain = try .listen(io, .{ .ip4 = .loopback(0) }, .{}); errdefer f.plain.deinit(io); try f.serve(f.plain.listener()); } /// `alpn` is what the server will select from; empty, it sends no ALPN /// extension at all, which is a server that does not negotiate. fn startSecure(f: *Fixture, alpn: []const []const u8) !void { const io = testing.io; f.* = .{ .is_secure = true }; f.auth = try .fromSlice(gpa, io, cert_pem, key_pem); errdefer f.auth.deinit(gpa); f.secure = try .listen(io, .{ .ip4 = .loopback(0) }, .{ .auth = &f.auth, .alpn_protocols = alpn, .rng = f.prng.random(), }); errdefer f.secure.deinit(io); try f.serve(f.secure.listener()); } fn serve(f: *Fixture, listener: http.transport.Listener) !void { const io = testing.io; f.server = .init(io, gpa, listener, .initStateless(routes), .{ .http1 = .{ .send_date = false, .server_name = null }, }); // Without a second task the accept and the connect deadlock. f.tasks.concurrent(io, run, .{&f.server}) catch return error.SkipZigTest; } fn run(server: *http.server.Server) void { server.serve() catch {}; } /// Answers HTTP/3 on the TCP listener's port number, with the same /// certificate and the same routes. Only for a secure fixture: §3.1 of /// RFC 9114 has no cleartext form. fn serveHttp3(f: *Fixture) !void { return f.serveHttp3On(f.port()); } /// The same, on a port of the kernel's choosing -- which is what an /// `Alt-Svc` naming another port looks like. `http3Port` says which. fn serveHttp3Elsewhere(f: *Fixture) !void { return f.serveHttp3On(0); } fn http3Port(f: *const Fixture) u16 { return f.udp.?.address.getPort(); } fn serveHttp3On(f: *Fixture, port_number: u16) !void { const io = testing.io; std.debug.assert(f.is_secure); const at: net.IpAddress = .{ .ip4 = .loopback(port_number) }; f.udp = try at.bind(io, .{ .mode = .dgram }); errdefer { f.udp.?.close(io); f.udp = null; } try f.quic.init(io, gpa, &f.udp.?, .initStateless(routes), .{ .auth = &f.auth, .rng = f.prng.random(), .reset_key = @splat(0x33), .token_key = @splat(0x44), .max_connections = 8, }); f.quic_tasks.concurrent(io, runQuic, .{&f.quic}) catch return error.SkipZigTest; } fn runQuic(listener: *http_io.http3.Listener) void { listener.run() catch {}; } fn stop(f: *Fixture) void { const io = testing.io; if (f.udp) |*socket| { f.quic.stop(); f.quic_tasks.cancel(io); f.quic.deinit(); socket.close(io); f.udp = null; } f.server.shutdown(); f.tasks.await(io) catch {}; if (f.is_secure) { f.secure.deinit(io); f.auth.deinit(gpa); } else { f.plain.deinit(io); } } fn port(f: *const Fixture) u16 { const address = if (f.is_secure) f.secure.localAddress() else f.plain.localAddress(); return address.getPort(); } fn url(f: *const Fixture, buffer: []u8, host: []const u8, rest: []const u8) ![]const u8 { const scheme = if (f.is_secure) "https" else "http"; return std.fmt.bufPrint(buffer, "{s}://{s}:{d}{s}", .{ scheme, host, f.port(), rest }); } }; /// The certificate the fixture serves, as the only thing a client trusts. fn trustFixture() !http_io.tls.CertBundle { const auth: http_io.tls.CertKeyPair = try .fromSlice(gpa, testing.io, cert_pem, key_pem); return auth.bundle; } const Fetched = struct { result: Client.FetchResult, body: Io.Writer.Allocating, fn deinit(f: *Fetched) void { f.result.deinit(); f.body.deinit(); } }; fn fetch(client: *Client, options: Client.FetchOptions) !Fetched { var out: Fetched = .{ .result = undefined, .body = .init(gpa) }; errdefer out.body.deinit(); var o = options; o.response_writer = &out.body.writer; out.result = try client.fetch(o); return out; } test "https negotiates HTTP/2 by ALPN when the server offers it" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust }; defer client.deinit(); var buffer: [128]u8 = undefined; // A name, so the lookup is exercised; a query, which travels; and a // fragment, which does not. var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/hello?x=1#top") } }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); try testing.expectEqual(http.protocol.Version.http_2, got.result.version); try testing.expectEqualStrings("http_2 GET /hello x=1 ", got.body.written()); try expectEchoedFields(&got.result.headers); } /// What `routes` sets on every echo, read back through `FetchResult.headers`. fn expectEchoedFields(headers: *const http.protocol.Headers) !void { // Case-insensitive lookup, which matters: HTTP/2 lowercases every name. try testing.expectEqualStrings("yes", headers.get("x-echoed").?); try testing.expectEqualStrings("yes", headers.get("X-ECHOED").?); // A repeated field is kept as the separate lines it arrived as. var twice = headers.values("x-twice"); try testing.expectEqualStrings("one", twice.next().?); try testing.expectEqualStrings("two", twice.next().?); try testing.expectEqual(@as(?[]const u8, null), twice.next()); } test "https falls back to HTTP/1.1 when the server does not select h2" { var f: Fixture = undefined; try f.startSecure(&.{"http/1.1"}); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_1_1, got.result.version); try testing.expectEqualStrings("http_1_1 GET / - ", got.body.written()); try expectEchoedFields(&got.result.headers); } test "RFC 7301 3.1: a server that negotiates nothing is spoken to in HTTP/1.1" { // No ALPN extension in the ServerHello at all. The server still reads the // first octets to choose, so HTTP/2 would work too -- but the client has // no way to know that, and HTTP/1.1 is what it can rely on. var f: Fixture = undefined; try f.startSecure(&.{}); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_1_1, got.result.version); } test "with HTTP/2 turned off only http/1.1 is offered" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = false }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_1_1, got.result.version); } test "a certificate nobody trusts is refused" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); // Explicitly empty rather than null, which would read the system store -- // and the answer would then depend on the machine running the test. var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = .empty }; defer client.deinit(); var buffer: [128]u8 = undefined; try testing.expectError( error.HandshakeFailed, client.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }), ); } test "cleartext is HTTP/1.1, unless the caller knows better" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var buffer: [128]u8 = undefined; const url = try f.url(&buffer, "127.0.0.1", "/a%2Fb/c"); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = url } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_1_1, got.result.version); // The escaped slash is still escaped when it reaches the server. try testing.expectEqualStrings("http_1_1 GET /a%2Fb/c - ", got.body.written()); // §3.3 of RFC 9113: prior knowledge, which is the only way left to speak // HTTP/2 without TLS. var knowing: Client = .{ .io = testing.io, .gpa = gpa, .http2_prior_knowledge = true }; defer knowing.deinit(); var again = try fetch(&knowing, .{ .location = .{ .url = url } }); defer again.deinit(); try testing.expectEqual(http.protocol.Version.http_2, again.result.version); } test "gzip is asked for, and undone, over every version" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var plain: Fixture = undefined; try plain.startPlain(); defer plain.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); const expected = try squishBody(gpa); defer gpa.free(expected); var buffer: [128]u8 = undefined; const Case = struct { url: []const u8, h2: bool, version: http.protocol.Version }; const secure_url = try gpa.dupe(u8, try f.url(&buffer, "localhost", "/squish")); defer gpa.free(secure_url); const plain_url = try gpa.dupe(u8, try plain.url(&buffer, "localhost", "/squish")); defer gpa.free(plain_url); const cases = [_]Case{ // Gathered whole and then undone: `coding.undo` over the DATA frames. .{ .url = secure_url, .h2 = true, .version = .http_2 }, // Undone as it streams: `coding.Decoder` wrapped around the body. .{ .url = secure_url, .h2 = false, .version = .http_1_1 }, .{ .url = plain_url, .h2 = false, .version = .http_1_1 }, }; for (cases) |case| { var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = case.h2, .accept_encodings = &.{.gzip}, }; defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = case.url } }); defer got.deinit(); try testing.expectEqual(case.version, got.result.version); // That it really was coded, or everything below would pass against a // server that ignored the request and a client that decoded nothing. try testing.expectEqualStrings("gzip", got.result.headers.get("content-encoding").?); try testing.expectEqual(expected.len, got.body.written().len); try testing.expectEqualSlices(u8, expected, got.body.written()); } } test "without asking, the body arrives as it is" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); const expected = try squishBody(gpa); defer gpa.free(expected); for ([_]bool{ true, false }) |h2| { var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = h2 }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/squish") } }); defer got.deinit(); // §12.5.3 of RFC 9110: no `Accept-Encoding`, so the server is left to // assume anything is acceptable -- and this one, asked for nothing, // sends nothing it would have to be asked for. try testing.expectEqual(@as(?[]const u8, null), got.result.headers.get("content-encoding")); try testing.expectEqualSlices(u8, expected, got.body.written()); } } test "an empty body asked for in gzip is still empty" { // The edge where a decoder is handed nothing, or a gzip member of nothing, // and must answer with nothing rather than an error. var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); for ([_]bool{ true, false }) |h2| { var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = h2, .accept_encodings = &.{.gzip}, }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/squish-empty") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Status.ok, got.result.status); try testing.expectEqualStrings("", got.body.written()); } } test "a large body streamed from a reader, over both versions" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var plain: Fixture = undefined; try plain.startPlain(); defer plain.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); // Thirty-two mebibytes: far past every window HTTP/2 opens by default and // every buffer either end has, so it can only arrive by being streamed. const size: u64 = 32 << 20; var buffer: [128]u8 = undefined; const secure_url = try gpa.dupe(u8, try f.url(&buffer, "localhost", "/verify")); defer gpa.free(secure_url); const plain_url = try gpa.dupe(u8, try plain.url(&buffer, "localhost", "/verify")); defer gpa.free(plain_url); const Case = struct { url: []const u8, h2: bool, expected: []const u8 }; const cases = [_]Case{ .{ .url = secure_url, .h2 = true, .expected = "http_2 PUT 33554432 octets" }, .{ .url = secure_url, .h2 = false, .expected = "http_1_1 PUT 33554432 octets" }, .{ .url = plain_url, .h2 = false, .expected = "http_1_1 PUT 33554432 octets" }, }; for (cases) |case| { var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = case.h2 }; defer client.deinit(); var source_buffer: [8192]u8 = undefined; var source: Pattern = .init(&source_buffer, size); var got = try fetch(&client, .{ .location = .{ .url = case.url }, .method = http.protocol.Method.put, .payload_reader = .{ .reader = &source.interface, .length = size }, }); defer got.deinit(); try testing.expectEqualStrings(case.expected, got.body.written()); // Exactly the declared length was read, and nothing past it. try testing.expectEqual(size, source.produced); } } test "a streamed body declares exactly what it sends, and no more is read" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); for ([_]bool{ true, false }) |h2| { var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = h2 }; defer client.deinit(); // A reader holding more than the length given: the rest stays put. var source_buffer: [4096]u8 = undefined; var source: Pattern = .init(&source_buffer, 1 << 20); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/verify") }, .payload_reader = .{ .reader = &source.interface, .length = 1000 }, }); defer got.deinit(); // `POST` by default, as for `payload`. const expected = if (h2) "http_2 POST 1000 octets" else "http_1_1 POST 1000 octets"; try testing.expectEqualStrings(expected, got.body.written()); try testing.expectEqual(@as(usize, 1000), source.produced - source.interface.bufferedLen()); } } test "a reader that ends early is an error, never a shorter body" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); for ([_]bool{ true, false }) |h2| { var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = h2 }; defer client.deinit(); var buffer: [128]u8 = undefined; const url = try f.url(&buffer, "localhost", "/verify"); // Promises a mebibyte, has half. var short_buffer: [8192]u8 = undefined; var short: Pattern = .init(&short_buffer, 512 << 10); try testing.expectError(error.RequestBodyShort, client.fetch(.{ .location = .{ .url = url }, .method = http.protocol.Method.put, .payload_reader = .{ .reader = &short.interface, .length = 1 << 20 }, })); // And one that fails part way, which is the reader's failure and not // the connection's. var failing_buffer: [8192]u8 = undefined; var failing: Pattern = .init(&failing_buffer, 1 << 20); failing.fail_at = 300 << 10; try testing.expectError(error.RequestBodyReadFailed, client.fetch(.{ .location = .{ .url = url }, .method = http.protocol.Method.put, .payload_reader = .{ .reader = &failing.interface, .length = 1 << 20 }, })); // The server is still there for the next request, whatever the last // one left it holding. var ok_buffer: [4096]u8 = undefined; var ok: Pattern = .init(&ok_buffer, 64); var got = try fetch(&client, .{ .location = .{ .url = url }, .payload_reader = .{ .reader = &ok.interface, .length = 64 }, }); defer got.deinit(); try testing.expect(std.mem.endsWith(u8, got.body.written(), " 64 octets")); } } test "a streamed body is not sent twice by a redirect" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var buffer: [128]u8 = undefined; // `.auto` does not follow with a body, so the 307 is the response. { var source_buffer: [4096]u8 = undefined; var source: Pattern = .init(&source_buffer, 100); var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/keep") }, .payload_reader = .{ .reader = &source.interface, .length = 100 }, }); defer got.deinit(); try testing.expectEqual(.temporary_redirect, got.result.status); } // Told to follow, a 307 would need the body again: refused, not resent // from a reader that has already given it up. { var source_buffer: [4096]u8 = undefined; var source: Pattern = .init(&source_buffer, 100); try testing.expectError(error.StreamedPayloadRedirected, client.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/keep") }, .payload_reader = .{ .reader = &source.interface, .length = 100 }, .redirects = .{ .follow = 1 }, })); } // A 303 needs no body -- it is answered with a GET -- so it is followed. { var source_buffer: [4096]u8 = undefined; var source: Pattern = .init(&source_buffer, 100); var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/see-other") }, .payload_reader = .{ .reader = &source.interface, .length = 100 }, .redirects = .{ .follow = 1 }, }); defer got.deinit(); try testing.expectEqualStrings("http_1_1 GET /landed - ", got.body.written()); } // And one body, not two. var source_buffer: [4096]u8 = undefined; var source: Pattern = .init(&source_buffer, 5); try testing.expectError(error.InvalidRequest, client.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/") }, .payload = "hello", .payload_reader = .{ .reader = &source.interface, .length = 5 }, })); } test "a POST carries its payload, over both versions" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var buffer: [128]u8 = undefined; const url = try f.url(&buffer, "localhost", "/submit"); for ([_]bool{ true, false }) |h2| { var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = h2 }; defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = url }, .payload = "hello" }); defer got.deinit(); const expected = if (h2) "http_2 POST /submit - hello" else "http_1_1 POST /submit - hello"; try testing.expectEqualStrings(expected, got.body.written()); } } test "a relative Location is resolved against the URL that was fetched" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/moved") } }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); // `landed?from=moved` against `/moved` is `/landed?from=moved`: RFC 3986 // §5.2.3 replaces the last segment. try testing.expectEqualStrings("http_1_1 GET /landed from=moved ", got.body.written()); } test "RFC 3986 2.2: an escaped reserved character in a query is sent escaped" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var buffer: [128]u8 = undefined; // `%2B` read as `+` is a space to any server that reads a query as a // form; the rest would split one value into several. const url = try f.url(&buffer, "localhost", "/search?q=a%2Bb%26c%3Dd%2Fe&r=x+y"); for ([_]bool{ true, false }) |h2| { var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = h2 }; defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = url } }); defer got.deinit(); const expected = if (h2) "http_2 GET /search q=a%2Bb%26c%3Dd%2Fe&r=x+y " else "http_1_1 GET /search q=a%2Bb%26c%3Dd%2Fe&r=x+y "; try testing.expectEqualStrings(expected, got.body.written()); } } test "RFC 3986 2.2: and so is one in a Location that is followed" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/escaped") } }); defer got.deinit(); try testing.expectEqualStrings("http_1_1 GET /landed q=a%2Bb%26c%3Dd%2Fe ", got.body.written()); } /// Hands the body to `writer` only for a `2xx`, as a download into a file /// would, and remembers what it was shown. const OnSuccess = struct { writer: *Io.Writer, status: ?std.http.Status = null, content_type: [32]u8 = undefined, content_type_len: usize = 0, fn choose(context: *anyopaque, head: Client.ResponseHead) Io.Writer.Error!?*Io.Writer { const self: *OnSuccess = @ptrCast(@alignCast(context)); self.status = head.status; const content_type = head.headers.get("content-type") orelse ""; self.content_type_len = @min(content_type.len, self.content_type.len); @memcpy(self.content_type[0..self.content_type_len], content_type[0..self.content_type_len]); return if (head.status.class() == .success) self.writer else null; } }; test "a writer chosen from the status keeps an error page out of a download" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var buffer: [128]u8 = undefined; for ([_]bool{ true, false }) |h2| { var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .http2 = h2 }; defer client.deinit(); var body: Io.Writer.Allocating = .init(gpa); defer body.deinit(); var chooser: OnSuccess = .{ .writer = &body.writer }; const writer_for: Client.ResponseWriterFor = .{ .context = &chooser, .choose = OnSuccess.choose }; var missing = try client.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/missing") }, .response_writer_for = writer_for, }); defer missing.deinit(); try testing.expectEqual(.not_found, missing.status); try testing.expectEqual(@as(?std.http.Status, .not_found), chooser.status); try testing.expectEqualStrings("", body.written()); // Asked about the response that is handed back, not the redirect // that led to it. var found = try client.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/moved") }, .response_writer_for = writer_for, }); defer found.deinit(); try testing.expectEqual(@as(?std.http.Status, .ok), chooser.status); const expected = if (h2) "http_2 GET /landed from=moved " else "http_1_1 GET /landed from=moved "; try testing.expectEqualStrings(expected, body.written()); } } test "a choice that fails is the fetch's failure" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var buffer: [128]u8 = undefined; const Refuse = struct { fn choose(_: *anyopaque, _: Client.ResponseHead) Io.Writer.Error!?*Io.Writer { return error.WriteFailed; } }; var nothing: u8 = 0; try testing.expectError(error.WriteFailed, client.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/hello") }, .response_writer_for = .{ .context = ¬hing, .choose = Refuse.choose }, })); // And a writer can be given or chosen, not both. var sink: Io.Writer.Discarding = .init(&.{}); try testing.expectError(error.InvalidRequest, client.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/hello") }, .response_writer = &sink.writer, .response_writer_for = .{ .context = ¬hing, .choose = Refuse.choose }, })); } test "RFC 9110 15.4.4: a 303 is followed with a GET and no content" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/see-other") }, .payload = "form data", // A payload means `auto` would not follow at all. .redirects = .{ .follow = 1 }, }); defer got.deinit(); try testing.expectEqualStrings("http_1_1 GET /landed - ", got.body.written()); } test "RFC 9110 15.4.8: a 307 keeps the method and the content" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/keep") }, .payload = "form data", .redirects = .{ .follow = 1 }, }); defer got.deinit(); try testing.expectEqualStrings("http_1_1 POST /landed - form data", got.body.written()); } test "a POST is not redirected unless the caller asks" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/keep") }, .payload = "form data", }); defer got.deinit(); try testing.expectEqual(.temporary_redirect, got.result.status); } test "a redirect from https to http is refused when asked" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var buffer: [128]u8 = undefined; const url = try f.url(&buffer, "localhost", "/downgrade"); // By default the downgrade is followed, as a browser would follow it -- // to port 1, where it is refused. var following: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust }; defer following.deinit(); try testing.expectError(error.ConnectionRefused, following.fetch(.{ .location = .{ .url = url } })); // Asked to, it is refused before anything is sent. var refusing: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .refuse_insecure_redirects = true, }; defer refusing.deinit(); try testing.expectError(error.InsecureRedirect, refusing.fetch(.{ .location = .{ .url = url } })); // And a redirect that stays on https is still followed. var got = try fetch(&refusing, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/moved") } }); defer got.deinit(); try testing.expectEqualStrings("http_2 GET /landed from=moved ", got.body.written()); } test "redirects stop where they were told to" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var buffer: [128]u8 = undefined; const url = try f.url(&buffer, "localhost", "/loop"); try testing.expectError(error.TooManyRedirects, client.fetch(.{ .location = .{ .url = url } })); var got = try fetch(&client, .{ .location = .{ .url = url }, .redirects = .unhandled }); defer got.deinit(); try testing.expectEqual(.found, got.result.status); } test "a credential does not follow a redirect to another origin" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); var buffer: [128]u8 = undefined; const secret: []const http.protocol.Field = &.{.{ .name = "Authorization", .value = "Bearer secret" }}; // The same origin keeps it. var direct = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "127.0.0.1", "/credential") }, .privileged_headers = secret, }); defer direct.deinit(); try testing.expectEqualStrings("Bearer secret", direct.body.written()); // `localhost` to `127.0.0.1` is the same socket and another origin. var moved = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/elsewhere") }, .privileged_headers = secret, }); defer moved.deinit(); try testing.expectEqualStrings("none", moved.body.written()); // And what is merely extra goes wherever the request goes. var extra = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/elsewhere") }, .extra_headers = secret, }); defer extra.deinit(); try testing.expectEqualStrings("Bearer secret", extra.body.written()); } /// A resolver that knows one name, and says so for every other. Its state is /// at container level because `Client.resolver` is `HostName.lookup`'s /// signature, which has nowhere else to keep any. const one_name = struct { const name = "fixture.test"; var asked: usize = 0; fn lookup( host_name: net.HostName, io: Io, resolved: *Io.Queue(net.HostName.LookupResult), options: net.HostName.LookupOptions, ) net.HostName.LookupError!void { defer resolved.close(io); asked += 1; if (!std.ascii.eqlIgnoreCase(host_name.bytes, name)) return error.UnknownHostName; resolved.putOne(io, .{ .address = .{ .ip4 = .loopback(options.port) } }) catch |err| switch (err) { error.Canceled => |e| return e, error.Closed => {}, }; } }; test "a resolver of the caller's own is the one asked" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); // `.test` is reserved by RFC 6761 and resolves nowhere, so an answer can // only have come from this resolver. one_name.asked = 0; var client: Client = .{ .io = testing.io, .gpa = gpa, .resolver = one_name.lookup }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "fixture.test", "/named") } }); defer got.deinit(); try testing.expectEqualStrings("http_1_1 GET /named - ", got.body.written()); try testing.expectEqual(@as(usize, 1), one_name.asked); try testing.expectError( error.UnknownHostName, client.fetch(.{ .location = .{ .url = try f.url(&buffer, "elsewhere.test", "/") } }), ); // An address literal is not a name, and asks nobody. var direct = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "127.0.0.1", "/") } }); defer direct.deinit(); try testing.expectEqual(@as(usize, 2), one_name.asked); } /// A proxy of the smallest kind that is still one. /// /// `CONNECT` is honoured for real: whatever authority was asked for, it /// connects to that port on the loopback address and copies octets both ways /// until each side has finished -- so a TLS handshake and an HTTP/2 /// connection run through it exactly as they would through squid. A /// forwarded request is *not* forwarded. The proxy answers it itself with the /// request line it received, which is the thing a test wants to see, and /// means the origin's name never has to resolve: that it was the proxy's to /// resolve is the point. const TestProxy = struct { plain: http_io.tcp.Listener = undefined, secure: http_io.tls.Listener = undefined, is_secure: bool = false, auth: http_io.tls.CertKeyPair = undefined, prng: std.Random.DefaultPrng = .init(0x9110), /// The `Proxy-Authorization` to insist on, or null to insist on nothing. required: ?[]const u8 = null, connects: std.atomic.Value(usize) = .init(0), tasks: Io.Group = .init, fn start(p: *TestProxy, secure: bool, required: ?[]const u8) !void { const io = testing.io; p.* = .{ .is_secure = secure, .required = required }; if (secure) { p.auth = try .fromSlice(gpa, io, cert_pem, key_pem); errdefer p.auth.deinit(gpa); p.secure = try .listen(io, .{ .ip4 = .loopback(0) }, .{ .auth = &p.auth, .alpn_protocols = &.{"http/1.1"}, .rng = p.prng.random(), }); } else { p.plain = try .listen(io, .{ .ip4 = .loopback(0) }, .{}); } p.tasks.concurrent(io, acceptLoop, .{p}) catch return error.SkipZigTest; } fn listener(p: *TestProxy) http.transport.Listener { return if (p.is_secure) p.secure.listener() else p.plain.listener(); } fn stop(p: *TestProxy) void { const io = testing.io; p.listener().shutdown(io); p.tasks.await(io) catch {}; if (p.is_secure) { p.secure.deinit(io); p.auth.deinit(gpa); } else { p.plain.deinit(io); } } fn port(p: *const TestProxy) u16 { const address = if (p.is_secure) p.secure.localAddress() else p.plain.localAddress(); return address.getPort(); } fn acceptLoop(p: *TestProxy) void { const io = testing.io; while (true) { const conn = p.listener().accept(io, gpa, .{}) catch |err| switch (err) { error.ConnectionAborted => continue, else => return, }; p.tasks.concurrent(io, serve, .{ p, conn }) catch { var owned = conn; owned.close(io, gpa); }; } } fn serve(p: *TestProxy, accepted: http.transport.Connection) void { const io = testing.io; var conn = accepted; defer conn.close(io, gpa); p.exchange(&conn) catch {}; } fn exchange(p: *TestProxy, conn: *http.transport.Connection) !void { const io = testing.io; const in = conn.input; const out = conn.output; var line_buffer: [512]u8 = undefined; const first = try in.takeDelimiterInclusive('\n'); const request_line = std.mem.trimEnd(u8, first, "\r\n"); if (request_line.len > line_buffer.len) return error.TooLong; @memcpy(line_buffer[0..request_line.len], request_line); const line = line_buffer[0..request_line.len]; var authorization_buffer: [256]u8 = undefined; var authorization: ?[]const u8 = null; while (true) { const field = std.mem.trimEnd(u8, try in.takeDelimiterInclusive('\n'), "\r\n"); if (field.len == 0) break; const colon = std.mem.findScalar(u8, field, ':') orelse continue; if (std.ascii.eqlIgnoreCase(field[0..colon], "proxy-authorization")) { const value = std.mem.trim(u8, field[colon + 1 ..], " "); @memcpy(authorization_buffer[0..value.len], value); authorization = authorization_buffer[0..value.len]; } } if (p.required) |want| { if (authorization == null or !std.mem.eql(u8, authorization.?, want)) { try out.writeAll("HTTP/1.1 407 Proxy Authentication Required\r\n" ++ "Proxy-Authenticate: Basic realm=\"test\"\r\n" ++ "Content-Length: 0\r\nConnection: close\r\n\r\n"); return out.flush(); } } if (std.mem.startsWith(u8, line, "CONNECT ")) { _ = p.connects.fetchAdd(1, .monotonic); const authority = line["CONNECT ".len..std.mem.findScalarLast(u8, line, ' ').?]; const colon = std.mem.findScalarLast(u8, authority, ':').?; const upstream_port = try std.fmt.parseInt(u16, authority[colon + 1 ..], 10); var upstream = try http_io.tcp.connect(io, gpa, .{ .ip4 = .loopback(upstream_port) }, .{}); defer upstream.close(io, gpa); try out.writeAll("HTTP/1.1 200 Connection established\r\n\r\n"); try out.flush(); // Both directions at once, or a TLS handshake waits for itself. var back: Io.Group = .init; defer back.cancel(io); back.concurrent(io, pump, .{ upstream.input, out, conn }) catch return error.SkipZigTest; pump(in, upstream.output, &upstream); back.await(io) catch {}; return; } try out.print("HTTP/1.1 200 OK\r\nContent-Length: {d}\r\nConnection: close\r\n\r\n{s}", .{ line.len, line }); try out.flush(); } /// Copies until the reading side ends, then says so to the writing side. fn pump(from: *Io.Reader, to: *Io.Writer, to_conn: *http.transport.Connection) void { while (true) { _ = from.stream(to, .unlimited) catch break; to.flush() catch break; } to.flush() catch {}; to_conn.shutdownWrite(testing.io); } }; test "an http URL is forwarded, in absolute form, and the name is the proxy's" { var proxy: TestProxy = undefined; try proxy.start(false, null); defer proxy.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa, .http_proxy = .{ .host = "127.0.0.1", .port = proxy.port() }, }; defer client.deinit(); // `.test` resolves nowhere, so the fetch working at all says this end // never tried to look it up. var got = try fetch(&client, .{ .location = .{ .url = "http://origin.test:8080/a%2Fb?q=1#frag" } }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); // §3.2.2 of RFC 9112: the whole URI, less the fragment. try testing.expectEqualStrings("GET http://origin.test:8080/a%2Fb?q=1 HTTP/1.1", got.body.written()); try testing.expectEqual(@as(usize, 0), proxy.connects.load(.monotonic)); } test "a credential in the proxy URL becomes Proxy-Authorization" { // RFC 7617 §2: `user:pass`, base64. var proxy: TestProxy = undefined; try proxy.start(false, "Basic dXNlcjpwYXNz"); defer proxy.stop(); var arena_state: std.heap.ArenaAllocator = .init(gpa); defer arena_state.deinit(); var url_buffer: [64]u8 = undefined; const proxy_url = try std.fmt.bufPrint(&url_buffer, "http://user:pass@127.0.0.1:{d}", .{proxy.port()}); var client: Client = .{ .io = testing.io, .gpa = gpa, .http_proxy = try .parse(arena_state.allocator(), proxy_url), }; defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = "http://origin.test/" } }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); // Without it a forwarding proxy's `407` is simply the response: it is an // answer to the request, from the only server this end spoke to. var bare: Client = .{ .io = testing.io, .gpa = gpa, .http_proxy = .{ .host = "127.0.0.1", .port = proxy.port() }, }; defer bare.deinit(); var refused = try fetch(&bare, .{ .location = .{ .url = "http://origin.test/" } }); defer refused.deinit(); try testing.expectEqual(.proxy_auth_required, refused.result.status); } test "an https URL is tunnelled, and HTTP/2 is negotiated inside the tunnel" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var proxy: TestProxy = undefined; try proxy.start(false, "Basic dXNlcjpwYXNz"); defer proxy.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .https_proxy = .{ .host = "127.0.0.1", .port = proxy.port(), .authorization = "Basic dXNlcjpwYXNz", }, }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/through") } }); defer got.deinit(); // The certificate checked was the origin's -- `localhost`, which the // proxy has no key for -- and ALPN reached the origin through the tunnel. try testing.expectEqual(http.protocol.Version.http_2, got.result.version); try testing.expectEqualStrings("http_2 GET /through - ", got.body.written()); try testing.expectEqual(@as(usize, 1), proxy.connects.load(.monotonic)); // A tunnel refused is an error, not a response: nothing the proxy says // in answer to `CONNECT` is from the origin. var bare: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .https_proxy = .{ .host = "127.0.0.1", .port = proxy.port() }, }; defer bare.deinit(); try testing.expectError( error.ProxyAuthenticationRequired, bare.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }), ); } test "TLS to the proxy, and TLS inside that to the origin" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var proxy: TestProxy = undefined; try proxy.start(true, null); defer proxy.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, // `localhost`, because the proxy's certificate is checked too. .https_proxy = .{ .secure = true, .host = "localhost", .port = proxy.port() }, }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/nested") } }); defer got.deinit(); try testing.expectEqualStrings("http_2 GET /nested - ", got.body.written()); try testing.expectEqual(@as(usize, 1), proxy.connects.load(.monotonic)); } test "no_proxy sends a host direct" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa, // Nothing listens on port 1, so going through it would fail. .http_proxy = .{ .host = "127.0.0.1", .port = 1 }, .no_proxy = &.{"localhost"}, }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/direct") } }); defer got.deinit(); try testing.expectEqualStrings("http_1_1 GET /direct - ", got.body.written()); try testing.expectError( error.ConnectionRefused, client.fetch(.{ .location = .{ .url = try f.url(&buffer, "127.0.0.1", "/") } }), ); } test "proxies from the environment" { var env: std.process.Environ.Map = .init(gpa); defer env.deinit(); try env.put("http_proxy", "proxy.example:3128"); try env.put("HTTPS_PROXY", "https://user:pw@secure.example"); // Empty is unset, so this does not hide ALL_PROXY... which is also unset. try env.put("https_proxy", ""); try env.put("no_proxy", "localhost, .internal"); var arena_state: std.heap.ArenaAllocator = .init(gpa); defer arena_state.deinit(); var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); try client.initDefaultProxies(arena_state.allocator(), &env); try testing.expectEqualStrings("proxy.example", client.http_proxy.?.host); try testing.expectEqual(@as(u16, 3128), client.http_proxy.?.port); try testing.expect(client.https_proxy.?.secure); try testing.expectEqualStrings("secure.example", client.https_proxy.?.host); try testing.expectEqualStrings("Basic dXNlcjpwdw==", client.https_proxy.?.authorization.?); try testing.expectEqual(@as(usize, 2), client.no_proxy.len); } /// A port that accepts connections and never says anything: the listener is /// open, so the kernel completes the handshake into its backlog, and nothing /// ever calls `accept`. A request is written and no answer ever comes, which /// is the case a timeout exists for. const Silent = struct { listener: http_io.tcp.Listener, fn open() !Silent { return .{ .listener = try .listen(testing.io, .{ .ip4 = .loopback(0) }, .{}) }; } fn close(s: *Silent) void { s.listener.deinit(testing.io); } fn url(s: *const Silent, buffer: []u8, scheme: []const u8) ![]const u8 { return std.fmt.bufPrint(buffer, "{s}://127.0.0.1:{d}/", .{ scheme, s.listener.localAddress().getPort() }); } }; fn millis(n: i64) Io.Timeout { return .{ .duration = .{ .raw = .fromMilliseconds(n), .clock = .awake } }; } test "a server that never answers is a timeout, not a hang" { var silent: Silent = try .open(); defer silent.close(); var client: Client = .{ .io = testing.io, .gpa = gpa, .timeout = millis(200) }; defer client.deinit(); var buffer: [64]u8 = undefined; const started = Io.Clock.awake.now(testing.io); // Waiting for a response head, on a connection that was made. try testing.expectError(error.Timeout, client.fetch(.{ .location = .{ .url = try silent.url(&buffer, "http") } })); // Waiting for a ServerHello, in the middle of a TLS handshake. try testing.expectError(error.Timeout, client.fetch(.{ .location = .{ .url = try silent.url(&buffer, "https") } })); const took = started.durationTo(Io.Clock.awake.now(testing.io)); // Two timeouts of 200 ms: well under a few seconds, however slow the // machine, and far under forever, which is what this would take without. try testing.expect(took.toMilliseconds() < 5_000); } test "a fetch that finishes in time is untouched by its timeout" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .timeout = .{ .duration = .{ .raw = .fromSeconds(30), .clock = .awake } }, }; defer client.deinit(); var buffer: [128]u8 = undefined; // A redirect as well, which is inside the same budget. var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/moved") } }); defer got.deinit(); try testing.expectEqualStrings("http_2 GET /landed from=moved ", got.body.written()); try expectEchoedFields(&got.result.headers); } test "a deadline already past fails at once" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client: Client = .{ .io = testing.io, .gpa = gpa, .timeout = .{ .deadline = .{ .raw = .{ .nanoseconds = 0 }, .clock = .awake } }, }; defer client.deinit(); var buffer: [128]u8 = undefined; // Either nothing got started or it was cancelled: what cannot happen is a // result, or a leak of one, which the testing allocator would report. const outcome = client.fetch(.{ .location = .{ .url = try f.url(&buffer, "127.0.0.1", "/") } }); if (outcome) |result| { // The one legitimate way to get here: the fetch won a race it was // given no time for, which a timeout that keeps a finished answer // allows. Keep the test honest about that rather than flaky. var owned = result; owned.deinit(); } else |err| try testing.expectEqual(error.Timeout, err); } /// `localhost` as 127.0.0.1 and nothing else. The HTTP/3 fixture listens on /// IPv4 alone, and a QUIC handshake to `::1` would be silence that costs a /// whole handshake timeout before the address that works is tried -- a test /// of patience rather than of anything here. fn loopbackOnly( host_name: net.HostName, io: Io, resolved: *Io.Queue(net.HostName.LookupResult), options: net.HostName.LookupOptions, ) net.HostName.LookupError!void { defer resolved.close(io); if (!std.ascii.eqlIgnoreCase(host_name.bytes, "localhost")) return error.UnknownHostName; resolved.putOne(io, .{ .address = .{ .ip4 = .loopback(options.port) } }) catch |err| switch (err) { error.Canceled => |e| return e, error.Closed => {}, }; } fn http3Client(trust: http_io.tls.CertBundle, mode: Client.Http3) Client { return .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .resolver = loopbackOnly, .http3 = mode, // Loopback answers in microseconds, and a server that is not there // should not hold a test up for long. .http3_handshake_timeout = .fromMilliseconds(500), }; } test "http3 = .only fetches over HTTP/3, certificate checked" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3(); var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .only); defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/hello?x=1") } }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); try testing.expectEqual(http.protocol.Version.http_3, got.result.version); try testing.expectEqualStrings("http_3 GET /hello x=1 ", got.body.written()); // Lowercase on the wire, found without case here. try expectEchoedFields(&got.result.headers); } test "http3 = .only against a server with no HTTP/3 is the handshake's failure" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .only); defer client.deinit(); var buffer: [128]u8 = undefined; const outcome = client.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); // Silence, or the ICMP port-unreachable loopback sends back: either way // no QUIC, and no quiet retreat to TCP. if (outcome) |result| { var owned = result; owned.deinit(); return error.TestUnexpectedResult; } else |err| switch (err) { error.HandshakeTimeout, error.NetworkFailed => {}, else => return err, } } test "http3 = .prefer uses HTTP/3 where there is one, and TCP where there is not" { var trust = try trustFixture(); defer trust.deinit(gpa); var buffer: [128]u8 = undefined; { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3(); var client = http3Client(trust, .prefer); defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_3, got.result.version); } { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var client = http3Client(trust, .prefer); defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); // Fell back, and ALPN then chose as it always does. try testing.expectEqual(http.protocol.Version.http_2, got.result.version); try testing.expectEqualStrings("http_2 GET / - ", got.body.written()); } } test "over HTTP/3: a body, a streamed body, a redirect and gzip" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3(); var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .only); defer client.deinit(); var buffer: [128]u8 = undefined; { var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/submit") }, .payload = "hello", }); defer got.deinit(); try testing.expectEqualStrings("http_3 POST /submit - hello", got.body.written()); } { const size: u64 = 4 << 20; var source_buffer: [8192]u8 = undefined; var source: Pattern = .init(&source_buffer, size); var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/verify") }, .method = http.protocol.Method.put, .payload_reader = .{ .reader = &source.interface, .length = size }, }); defer got.deinit(); try testing.expectEqualStrings("http_3 PUT 4194304 octets", got.body.written()); } { // Two QUIC connections, one per hop, since nothing is kept. var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/moved") } }); defer got.deinit(); try testing.expectEqualStrings("http_3 GET /landed from=moved ", got.body.written()); } { var gzip_client = http3Client(trust, .only); gzip_client.accept_encodings = &.{.gzip}; defer gzip_client.deinit(); const expected = try squishBody(gpa); defer gpa.free(expected); var got = try fetch(&gzip_client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/squish") } }); defer got.deinit(); try testing.expectEqualStrings("gzip", got.result.headers.get("content-encoding").?); try testing.expectEqualSlices(u8, expected, got.body.written()); } } test "HTTP/3 is never used where it cannot be" { var trust = try trustFixture(); defer trust.deinit(gpa); var buffer: [128]u8 = undefined; // An `http` URL: RFC 9114 has no cleartext form, so `.only` still means // TCP here rather than an error. { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client = http3Client(trust, .only); defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_1_1, got.result.version); } // Through a proxy: tunnelled over TCP, and HTTP/2 inside the tunnel, // though the origin answers HTTP/3 as well. { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3(); var proxy: TestProxy = undefined; try proxy.start(false, null); defer proxy.stop(); var client = http3Client(trust, .only); client.https_proxy = .{ .host = "127.0.0.1", .port = proxy.port() }; defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_2, got.result.version); try testing.expectEqual(@as(usize, 1), proxy.connects.load(.monotonic)); } } test "a timeout cuts a QUIC handshake short" { // A server that never answers over UDP, a handshake allowed ten seconds, // and a fetch allowed a fifth of one: cancellation has to reach into the // QUIC handshake, or this takes the ten. var silent: Io.net.Socket = try (Io.net.IpAddress{ .ip4 = .loopback(0) }).bind(testing.io, .{ .mode = .dgram }); defer silent.close(testing.io); var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .only); client.http3_handshake_timeout = .fromSeconds(10); client.timeout = millis(200); defer client.deinit(); var buffer: [128]u8 = undefined; const url = try std.fmt.bufPrint(&buffer, "https://localhost:{d}/", .{silent.address.getPort()}); const started = Io.Clock.awake.now(testing.io); try testing.expectError(error.Timeout, client.fetch(.{ .location = .{ .url = url } })); const took = started.durationTo(Io.Clock.awake.now(testing.io)); try testing.expect(took.toMilliseconds() < 5_000); } fn advertise(buffer: []u8, comptime fmt: []const u8, args: anytype) ![]const u8 { const value = try std.fmt.bufPrint(buffer, fmt, args); alt_svc_value = value; return value; } test "RFC 7838: an origin that advertises HTTP/3 is fetched over it next time" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); // Another port than the TCP one, so that the port used can only have // come from what was advertised. try f.serveHttp3Elsewhere(); try testing.expect(f.http3Port() != f.port()); var value_buffer: [64]u8 = undefined; _ = try advertise(&value_buffer, "h3=\":{d}\"; ma=60", .{f.http3Port()}); defer alt_svc_value = ""; var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .advertised); defer client.deinit(); var buffer: [128]u8 = undefined; const url = try gpa.dupe(u8, try f.url(&buffer, "localhost", "/hello")); defer gpa.free(url); // Nothing said yet: TCP, and ALPN's choice. { var got = try fetch(&client, .{ .location = .{ .url = url } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_2, got.result.version); } // Said, and remembered. const target = client.own_alt_svc.lookup(testing.io, "localhost", f.port()).?; try testing.expectEqual(f.http3Port(), target.port); { var got = try fetch(&client, .{ .location = .{ .url = url } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_3, got.result.version); try testing.expectEqualStrings("http_3 GET /hello - ", got.body.written()); } } test "RFC 7838 2.1: an alternative on another host is still checked as the origin" { // The certificate is for `localhost`, and the alternative is named // 127.0.0.1 -- which no certificate here covers. It works only because // SNI and the check are the origin's name, as §2.1 and §2.3 require. var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3Elsewhere(); var value_buffer: [64]u8 = undefined; _ = try advertise(&value_buffer, "h3=\"127.0.0.1:{d}\"", .{f.http3Port()}); defer alt_svc_value = ""; var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .advertised); defer client.deinit(); var buffer: [128]u8 = undefined; const url = try gpa.dupe(u8, try f.url(&buffer, "localhost", "/")); defer gpa.free(url); var first = try fetch(&client, .{ .location = .{ .url = url } }); first.deinit(); var second = try fetch(&client, .{ .location = .{ .url = url } }); defer second.deinit(); try testing.expectEqual(http.protocol.Version.http_3, second.result.version); } test "RFC 7838 2.4: an alternative that fails falls back to TCP and is left alone" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); // Nothing listens on this UDP port. var nothing: Io.net.Socket = try (Io.net.IpAddress{ .ip4 = .loopback(0) }).bind(testing.io, .{ .mode = .dgram }); const dead_port = nothing.address.getPort(); nothing.close(testing.io); var value_buffer: [64]u8 = undefined; _ = try advertise(&value_buffer, "h3=\":{d}\"", .{dead_port}); defer alt_svc_value = ""; var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .advertised); defer client.deinit(); var buffer: [128]u8 = undefined; const url = try gpa.dupe(u8, try f.url(&buffer, "localhost", "/")); defer gpa.free(url); for (0..3) |_| { var got = try fetch(&client, .{ .location = .{ .url = url } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_2, got.result.version); } // Known, and marked: not offered until the wait is over, though every // response advertised it again. try testing.expect(client.own_alt_svc.lookup(testing.io, "localhost", f.port()) == null); try testing.expectEqual(@as(usize, 1), client.own_alt_svc.len); } test "RFC 7838 3: clear, learned over HTTP/3 itself" { // §2.4: an alternative in use "is then treated in every way as the // origin; this includes the ability to advertise alternative services". var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3Elsewhere(); var value_buffer: [64]u8 = undefined; _ = try advertise(&value_buffer, "h3=\":{d}\"", .{f.http3Port()}); defer alt_svc_value = ""; var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .advertised); defer client.deinit(); var buffer: [128]u8 = undefined; const url = try gpa.dupe(u8, try f.url(&buffer, "localhost", "/")); defer gpa.free(url); var learned = try fetch(&client, .{ .location = .{ .url = url } }); learned.deinit(); // Now the server withdraws it, and the next response -- which comes over // HTTP/3, since the alternative is still cached -- says so. alt_svc_value = "clear"; { var got = try fetch(&client, .{ .location = .{ .url = url } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_3, got.result.version); } try testing.expectEqual(@as(usize, 0), client.own_alt_svc.len); { var got = try fetch(&client, .{ .location = .{ .url = url } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_2, got.result.version); } } test "one cache shared by clients: what one learns the other uses" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3Elsewhere(); var value_buffer: [64]u8 = undefined; _ = try advertise(&value_buffer, "h3=\":{d}\"", .{f.http3Port()}); defer alt_svc_value = ""; var trust = try trustFixture(); defer trust.deinit(gpa); var shared: http_io.AltSvc = .{}; var first = http3Client(trust, .advertised); first.alt_svc = &shared; defer first.deinit(); var second = http3Client(trust, .advertised); second.alt_svc = &shared; defer second.deinit(); var buffer: [128]u8 = undefined; const url = try gpa.dupe(u8, try f.url(&buffer, "localhost", "/")); defer gpa.free(url); var learned = try fetch(&first, .{ .location = .{ .url = url } }); learned.deinit(); // The second client's very first fetch. var got = try fetch(&second, .{ .location = .{ .url = url } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_3, got.result.version); // And neither kept a private copy. try testing.expectEqual(@as(usize, 0), second.own_alt_svc.len); } test "Alt-Svc is learned only where it can be trusted, and used only where it can be" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3Elsewhere(); var plain: Fixture = undefined; try plain.startPlain(); defer plain.stop(); var value_buffer: [64]u8 = undefined; _ = try advertise(&value_buffer, "h3=\":{d}\"", .{f.http3Port()}); defer alt_svc_value = ""; var trust = try trustFixture(); defer trust.deinit(gpa); var buffer: [128]u8 = undefined; // Over cleartext: nothing about it is authenticated, so nothing is taken. { var client = http3Client(trust, .advertised); defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = try plain.url(&buffer, "localhost", "/") } }); got.deinit(); try testing.expectEqual(@as(usize, 0), client.own_alt_svc.len); } // Through a proxy: §2.4 routes it through the proxy rather than to the // alternative, so there is nothing to learn it for. { var proxy: TestProxy = undefined; try proxy.start(false, null); defer proxy.stop(); var client = http3Client(trust, .advertised); client.https_proxy = .{ .host = "127.0.0.1", .port = proxy.port() }; defer client.deinit(); for (0..2) |_| { var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_2, got.result.version); } try testing.expectEqual(@as(usize, 0), client.own_alt_svc.len); } // In another mode, nothing listens for it. { var client = http3Client(trust, .off); defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); got.deinit(); try testing.expectEqual(@as(usize, 0), client.own_alt_svc.len); } } /// An HTTPS-record lookup written in the test, as `loopbackOnly` is for /// addresses: it answers every name with `fake_records`, and says what it was /// asked. Container-level for the reason `Client.services` gives -- the hook /// has `HostName.lookup`'s signature, which has nowhere else to keep any. const fake_services = struct { var records: []const Client.Service = &.{}; var protected_failure: bool = false; var calls: usize = 0; var last_port: u16 = 0; fn reset() void { records = &.{}; protected_failure = false; calls = 0; last_port = 0; } fn lookup( host_name: net.HostName, io: Io, resolved: *Io.Queue(Client.Service), options: Client.ServiceLookupOptions, ) Client.ServiceLookupError!void { _ = host_name; defer resolved.close(io); calls += 1; last_port = options.port; if (protected_failure) return error.ProtectedResolutionFailed; for (records) |record| resolved.putOne(io, record) catch |err| switch (err) { error.Canceled => |e| return e, error.Closed => return, }; } }; /// `localhost` and every `.test` name, as 127.0.0.1: a TargetName that is not /// the origin's has to resolve somewhere. fn loopbackNames( host_name: net.HostName, io: Io, resolved: *Io.Queue(net.HostName.LookupResult), options: net.HostName.LookupOptions, ) net.HostName.LookupError!void { defer resolved.close(io); const known = std.ascii.eqlIgnoreCase(host_name.bytes, "localhost") or std.ascii.endsWithIgnoreCase(host_name.bytes, ".test"); if (!known) return error.UnknownHostName; resolved.putOne(io, .{ .address = .{ .ip4 = .loopback(options.port) } }) catch |err| switch (err) { error.Canceled => |e| return e, error.Closed => {}, }; } fn recordsClient(trust: http_io.tls.CertBundle) Client { var client = http3Client(trust, .advertised); client.resolver = loopbackNames; client.services = fake_services.lookup; return client; } fn service(target: []const u8, port: ?u16, alpn: Client.Service.Alpn, no_default_alpn: bool) Client.Service { var s: Client.Service = .{ .port = port, .alpn = alpn, .no_default_alpn = no_default_alpn }; s.setTarget(target) catch unreachable; return s; } /// A TCP port and a UDP port with nothing behind them. fn deadPorts() !struct { tcp: u16, udp: u16 } { var tcp = try http_io.tcp.Listener.listen(testing.io, .{ .ip4 = .loopback(0) }, .{}); const tcp_port = tcp.localAddress().getPort(); tcp.deinit(testing.io); var udp: Io.net.Socket = try (Io.net.IpAddress{ .ip4 = .loopback(0) }).bind(testing.io, .{ .mode = .dgram }); const udp_port = udp.address.getPort(); udp.close(testing.io); return .{ .tcp = tcp_port, .udp = udp_port }; } test "RFC 9460: an HTTPS record takes the very first request to HTTP/3" { fake_services.reset(); defer fake_services.reset(); var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3Elsewhere(); // `alpn=h3 no-default-alpn port=Q`: QUIC only, on another port. const records = [_]Client.Service{service("", f.http3Port(), .{ .h3 = true }, true)}; fake_services.records = &records; var trust = try trustFixture(); defer trust.deinit(gpa); var client = recordsClient(trust); defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/first") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_3, got.result.version); try testing.expectEqualStrings("http_3 GET /first - ", got.body.written()); // Asked for the origin's port, which §9.1 builds the query name from. try testing.expectEqual(f.port(), fake_services.last_port); } test "RFC 9460 9.1: a TargetName elsewhere, with the origin's certificate" { fake_services.reset(); defer fake_services.reset(); var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3Elsewhere(); const records = [_]Client.Service{service("svc.test", f.http3Port(), .{ .h3 = true }, true)}; fake_services.records = &records; var trust = try trustFixture(); defer trust.deinit(gpa); var client = recordsClient(trust); defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); // `svc.test` is in no certificate here: it works because the name checked // is the origin's, as §9.1 and §9.4 require. try testing.expectEqual(http.protocol.Version.http_3, got.result.version); } test "RFC 9460 7.1.2: a TCP endpoint, tried by priority" { fake_services.reset(); defer fake_services.reset(); var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3Elsewhere(); const dead = try deadPorts(); // Out of order on purpose. Priority 1 is TLS over TCP at the fixture's // port -- while the URL names a port where nothing listens -- and // priority 2 is QUIC. HTTP/2 back means the record was used, and the // lower priority first. const records = [_]Client.Service{ service("", f.http3Port(), .{ .h3 = true }, true), blk: { var s = service("", f.port(), .{ .h2 = true }, false); s.priority = 1; break :blk s; }, }; var sorted = records; sorted[0].priority = 2; fake_services.records = &sorted; var trust = try trustFixture(); defer trust.deinit(gpa); var client = recordsClient(trust); defer client.deinit(); var buffer: [128]u8 = undefined; const url = try std.fmt.bufPrint(&buffer, "https://localhost:{d}/tcp", .{dead.tcp}); var got = try fetch(&client, .{ .location = .{ .url = url } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_2, got.result.version); try testing.expectEqualStrings("http_2 GET /tcp - ", got.body.written()); } test "RFC 9460 3: when every endpoint fails, the origin is still there" { fake_services.reset(); defer fake_services.reset(); var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); const dead = try deadPorts(); const records = [_]Client.Service{ service("", dead.udp, .{ .h3 = true }, true), service("", dead.tcp, .{ .h2 = true }, false), }; fake_services.records = &records; var trust = try trustFixture(); defer trust.deinit(gpa); var client = recordsClient(trust); defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_2, got.result.version); } test "RFC 9460 3.1: a protected resolution that fails abandons the fetch" { fake_services.reset(); defer fake_services.reset(); var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); fake_services.protected_failure = true; var trust = try trustFixture(); defer trust.deinit(gpa); var client = recordsClient(trust); defer client.deinit(); var buffer: [128]u8 = undefined; // The origin is right there over TCP. Using it anyway is exactly the // downgrade §3.1 is guarding against. try testing.expectError( error.ProtectedResolutionFailed, client.fetch(.{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }), ); } test "RFC 9460 9.5: an http URL with an HTTPS record is fetched as https" { fake_services.reset(); defer fake_services.reset(); var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3Elsewhere(); const records = [_]Client.Service{service("", f.http3Port(), .{ .h3 = true }, true)}; fake_services.records = &records; var trust = try trustFixture(); defer trust.deinit(gpa); var buffer: [128]u8 = undefined; // `http` to a port that only speaks TLS: this works only if it was // upgraded, and the port -- not 80 -- is kept. const url = try std.fmt.bufPrint(&buffer, "http://localhost:{d}/up", .{f.port()}); { var client = recordsClient(trust); defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = url }, // A redirect nothing was sent for, so none of the budget is spent. .redirects = .unhandled, }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_3, got.result.version); try testing.expectEqualStrings("http_3 GET /up - ", got.body.written()); try testing.expectEqual(f.port(), fake_services.last_port); } // Turned off: the URL is fetched as written, and the records not asked. { fake_services.calls = 0; var client = recordsClient(trust); client.https_record_upgrade = false; defer client.deinit(); if (client.fetch(.{ .location = .{ .url = url } })) |result| { var owned = result; owned.deinit(); return error.TestUnexpectedResult; } else |_| {} try testing.expectEqual(@as(usize, 0), fake_services.calls); } } test "RFC 9460 9.5: explicit port 80 becomes 443, and no record means no upgrade" { fake_services.reset(); defer fake_services.reset(); var trust = try trustFixture(); defer trust.deinit(gpa); // With a record: asked for 443, and upgraded to `https://localhost:443/` // -- where nothing listens here, which is the proof of where it went. { const records = [_]Client.Service{service("", null, .{ .h2 = true }, false)}; fake_services.records = &records; var client = recordsClient(trust); client.http3_handshake_timeout = .fromMilliseconds(200); defer client.deinit(); if (client.fetch(.{ .location = .{ .url = "http://localhost:80/" } })) |result| { var owned = result; owned.deinit(); return error.TestUnexpectedResult; } else |_| {} try testing.expectEqual(@as(u16, 443), fake_services.last_port); } // Without one: asked, answered with nothing, and fetched as `http`. { fake_services.reset(); var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var client = recordsClient(trust); defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); defer got.deinit(); try testing.expectEqual(http.protocol.Version.http_1_1, got.result.version); try testing.expectEqual(@as(usize, 1), fake_services.calls); } } test "HTTPS records are not asked where they cannot be used" { fake_services.reset(); defer fake_services.reset(); var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); var trust = try trustFixture(); defer trust.deinit(gpa); var buffer: [128]u8 = undefined; const url = try gpa.dupe(u8, try f.url(&buffer, "localhost", "/")); defer gpa.free(url); // Through a proxy (§3.2). { var proxy: TestProxy = undefined; try proxy.start(false, null); defer proxy.stop(); var client = recordsClient(trust); client.https_proxy = .{ .host = "127.0.0.1", .port = proxy.port() }; defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = url } }); got.deinit(); } // In a mode that does not use them. { var client = recordsClient(trust); client.http3 = .prefer; defer client.deinit(); var got = try fetch(&client, .{ .location = .{ .url = url } }); got.deinit(); } // For an address literal, which has no records. { var client = recordsClient(trust); defer client.deinit(); client.insecure_skip_verify = true; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "127.0.0.1", "/") } }); got.deinit(); } try testing.expectEqual(@as(usize, 0), fake_services.calls); } test "what the client will not send" { var client: Client = .{ .io = testing.io, .gpa = gpa }; defer client.deinit(); try testing.expectError(error.UnsupportedUriScheme, client.fetch(.{ .location = .{ .url = "ftp://localhost/" } })); // §4.2.4 of RFC 9110. try testing.expectError(error.UserinfoInUri, client.fetch(.{ .location = .{ .url = "http://user:pw@localhost/" } })); try testing.expectError(error.UriMissingHost, client.fetch(.{ .location = .{ .url = "http:///no-host" } })); try testing.expectError(error.InvalidHostName, client.fetch(.{ .location = .{ .url = "http://bad_name/" } })); try testing.expectError(error.ReservedField, client.fetch(.{ .location = .{ .url = "http://127.0.0.1:1/" }, .extra_headers = &.{.{ .name = "Content-Length", .value = "4" }}, })); } /// What `FetchOptions.interim` was told, copied out while it was valid. const Hints = struct { links: [8][64]u8 = undefined, lens: [8]usize = @splat(0), count: usize = 0, fn hook(h: *Hints) http.client.Request.InterimHook { return .{ .context = h, .call = record }; } fn record(context: ?*anyopaque, head: http.client.Request.InterimHead) void { const h: *Hints = @ptrCast(@alignCast(context.?)); if (head.status != 103 or h.count == h.links.len) return; const value = head.headers.get("link") orelse return; const n = @min(value.len, h.links[h.count].len); @memcpy(h.links[h.count][0..n], value[0..n]); h.lens[h.count] = n; h.count += 1; } fn link(h: *const Hints, at: usize) []const u8 { return h.links[at][0..h.lens[at]]; } }; /// Fetches `/hints` and `/hinted-move` with `client`, and checks what the hook /// was told against what the server sent, over whatever version it speaks. fn expectHints(client: *Client, f: *Fixture, host: []const u8, version: []const u8) !void { var buffer: [128]u8 = undefined; { var hints: Hints = .{}; var got = try fetch(client, .{ .location = .{ .url = try f.url(&buffer, host, "/hints") }, .interim = hints.hook(), }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); var expected: [32]u8 = undefined; try testing.expectEqualStrings( try std.fmt.bufPrint(&expected, "{s} hinted", .{version}), got.body.written(), ); // The hints are told, and are not the response's fields. try testing.expect(got.result.headers.get("link") == null); try testing.expectEqual(@as(usize, 2), hints.count); try testing.expectEqualStrings("; rel=preload", hints.link(0)); try testing.expectEqualStrings("; rel=preload", hints.link(1)); } { // On every hop, in the order they came. var hints: Hints = .{}; var got = try fetch(client, .{ .location = .{ .url = try f.url(&buffer, host, "/hinted-move") }, .interim = hints.hook(), }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); try testing.expectEqual(@as(usize, 3), hints.count); try testing.expectEqualStrings("; rel=preload", hints.link(0)); try testing.expectEqualStrings("; rel=preload", hints.link(1)); } { // And skipped, unseen, when nobody asked. var got = try fetch(client, .{ .location = .{ .url = try f.url(&buffer, host, "/hints") } }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); } } test "RFC 8297: Early Hints reach the hook over HTTP/1.1 and HTTP/2" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var plain: Client = .{ .io = testing.io, .gpa = gpa }; defer plain.deinit(); try expectHints(&plain, &f, "127.0.0.1", "http_1_1"); var knowing: Client = .{ .io = testing.io, .gpa = gpa, .http2_prior_knowledge = true }; defer knowing.deinit(); try expectHints(&knowing, &f, "127.0.0.1", "http_2"); } test "RFC 8297: Early Hints reach the hook over HTTP/3" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3(); var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .only); defer client.deinit(); try expectHints(&client, &f, "localhost", "http_3"); } /// The 100-continue expectation through `fetch`, over whatever version /// `client` speaks: an upload that goes once asked for, one that is refused /// without a byte of it sent, one that is redirected before it is sent and /// so can follow, and a server that will not have the expectation at all. fn expectContinue(client: *Client, f: *Fixture, host: []const u8, version: []const u8) !void { var buffer: [128]u8 = undefined; var expected: [64]u8 = undefined; const size: u64 = 256 * 1024; { var source_buffer: [8192]u8 = undefined; var source: Pattern = .init(&source_buffer, size); var got = try fetch(client, .{ .location = .{ .url = try f.url(&buffer, host, "/verify") }, .method = http.protocol.Method.put, .payload_reader = .{ .reader = &source.interface, .length = size }, .expect_continue = .always, }); defer got.deinit(); try testing.expectEqualStrings( try std.fmt.bufPrint(&expected, "{s} PUT {d} octets", .{ version, size }), got.body.written(), ); } { var source_buffer: [8192]u8 = undefined; var source: Pattern = .init(&source_buffer, size); var got = try fetch(client, .{ .location = .{ .url = try f.url(&buffer, host, "/private") }, .method = http.protocol.Method.put, .payload_reader = .{ .reader = &source.interface, .length = size }, .expect_continue = .{ .above = 1024 }, }); defer got.deinit(); try testing.expectEqual(.unauthorized, got.result.status); // Refused before it was asked for, so none of it was read. try testing.expectEqual(@as(u64, 0), source.produced); } { // A 307 before the content was sent: the reader is untouched, so the // redirect can be followed with it, which without the expectation is // `error.StreamedPayloadRedirected`. var source_buffer: [8192]u8 = undefined; var source: Pattern = .init(&source_buffer, size); var got = try fetch(client, .{ .location = .{ .url = try f.url(&buffer, host, "/upload-elsewhere") }, .method = http.protocol.Method.put, .payload_reader = .{ .reader = &source.interface, .length = size }, .expect_continue = .always, .redirects = .{ .follow = 1 }, }); defer got.deinit(); try testing.expectEqualStrings( try std.fmt.bufPrint(&expected, "{s} PUT {d} octets", .{ version, size }), got.body.written(), ); } { // RFC 9110 15.5.18: a 417, and the request is made again without it. var got = try fetch(client, .{ .location = .{ .url = try f.url(&buffer, host, "/no-expect") }, .payload = "hello", .expect_continue = .always, }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); try testing.expectEqualStrings( try std.fmt.bufPrint(&expected, "{s} took 5 octets", .{version}), got.body.written(), ); } { // And below the threshold there is no expectation to refuse. var got = try fetch(client, .{ .location = .{ .url = try f.url(&buffer, host, "/no-expect") }, .payload = "hello", .expect_continue = .{ .above = 1024 }, }); defer got.deinit(); try testing.expectEqual(.ok, got.result.status); } } test "RFC 9110 10.1.1: a 100-continue expectation over HTTP/1.1 and HTTP/2" { var f: Fixture = undefined; try f.startPlain(); defer f.stop(); var plain: Client = .{ .io = testing.io, .gpa = gpa }; defer plain.deinit(); try expectContinue(&plain, &f, "127.0.0.1", "http_1_1"); var knowing: Client = .{ .io = testing.io, .gpa = gpa, .http2_prior_knowledge = true }; defer knowing.deinit(); try expectContinue(&knowing, &f, "127.0.0.1", "http_2"); } test "RFC 9110 10.1.1: a 100-continue expectation over HTTP/3" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); try f.serveHttp3(); var trust = try trustFixture(); defer trust.deinit(gpa); var client = http3Client(trust, .only); defer client.deinit(); try expectContinue(&client, &f, "localhost", "http_3"); } /// A fetch run in a task of its own, so that a test can see whether it ended. const BackgroundFetch = struct { client: *Client, url: []const u8, done: std.atomic.Value(bool) = .init(false), timed_out: bool = false, fn run(p: *BackgroundFetch) void { defer p.done.store(true, .release); if (p.client.fetch(.{ .location = .{ .url = p.url } })) |result| { var owned = result; owned.deinit(); } else |err| p.timed_out = err == error.Timeout; } }; test "a timeout during an HTTPS-record endpoint's TLS handshake ends the fetch" { fake_services.reset(); defer fake_services.reset(); var silent: Silent = try .open(); var silent_open = true; defer if (silent_open) silent.close(); // An endpoint reached over TLS, at the origin's own (silent) port. const records = [_]Client.Service{service("", null, .{ .h2 = true }, false)}; fake_services.records = &records; var client: Client = .{ .io = testing.io, .gpa = gpa, .insecure_skip_verify = true, .resolver = loopbackNames, .services = fake_services.lookup, .http3 = .advertised, .timeout = millis(200), }; defer client.deinit(); var buffer: [64]u8 = undefined; const url = try std.fmt.bufPrint(&buffer, "https://silent.test:{d}/", .{silent.listener.localAddress().getPort()}); var background: BackgroundFetch = .{ .client = &client, .url = url }; var group: Io.Group = .init; defer group.cancel(testing.io); group.concurrent(testing.io, BackgroundFetch.run, .{&background}) catch return error.SkipZigTest; // Fifteen times the timeout. try testing.io.sleep(.fromSeconds(3), .awake); const finished = background.done.load(.acquire); // Closing the listener resets the connections in its backlog, which is // what ends the handshake if its cancel has been swallowed again -- // `fetch` waits for its task uncancelably, so nothing else could, and // without this a regression would hang the test rather than fail it. silent.close(); silent_open = false; try group.await(testing.io); try testing.expect(finished); try testing.expect(background.timed_out); } test "one client that never sends a ClientHello does not stop a TLS server accepting" { var f: Fixture = undefined; try f.startSecure(&.{ "h2", "http/1.1" }); defer f.stop(); // A TCP connection to the TLS port that says nothing. Its handshake used // to run in the server's accept loop, which then accepted nobody else // until this client spoke or left. var idle = try http_io.tcp.connect(testing.io, gpa, f.secure.localAddress(), .{}); // Closed before the fixture stops, so that its handshake ends. defer idle.close(testing.io, gpa); try testing.io.sleep(.fromMilliseconds(100), .awake); var trust = try trustFixture(); defer trust.deinit(gpa); var client: Client = .{ .io = testing.io, .gpa = gpa, .root_ca = trust, .timeout = millis(2000), }; defer client.deinit(); var buffer: [128]u8 = undefined; var got = try fetch(&client, .{ .location = .{ .url = try f.url(&buffer, "localhost", "/") } }); got.deinit(); }