Something went wrong. Try again.
An HTTP library for Zig 0.17, server and client, HTTP/1.0 through HTTP/3, with the protocol in a module that owns no socket.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The HTTP/1.x and HTTP/2 clients, driven over a slice of memory.//!//! A scripted server: the response is a string literal and what the client//! wrote is read back and asserted. No socket, no port, no scheduling -- the//! same shape `tests/e2e.zig` has for the server, and for the same reason. A//! framing bug reproduces from a literal rather than from a packet capture.
const std = @import("std");const Io = std.Io;const testing = std.testing;
const http = @import("http");const client = http.http1.client;
/// Runs one exchange against a scripted response and hands the live response/// to `check`.////// A callback rather than a return value, because every slice in a response/// points into the connection's head buffer -- see the note at the top of/// `client/Response.zig`. Returning one would hand back a response whose/// fields had been freed, which is how this helper was first written and what/// the first run of these tests segfaulted on.fn exchange( gpa: std.mem.Allocator, response: []const u8, request: http.client.Request, config: client.Config, comptime check: fn (*client.Connection, http.client.Response, []const u8, []const u8) anyerror!void,) !void { const io = testing.io; var memory = try http.transport.memory.Connection.init(gpa, response, .{}); defer memory.deinit();
var conn = try client.Connection.init(io, gpa, memory.connection(), config); defer conn.deinit();
const res = try conn.send(request); const body = try conn.readAll(res, 1 << 20); defer gpa.free(body);
try check(&conn, res, body, memory.written());}
test "a GET, and the request that went out to get it" { try exchange( testing.allocator, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\nContent-Type: text/plain\r\n\r\nhello", .{ .host = "example.com", .target = "/index.html" }, .{}, struct { fn check(conn: *client.Connection, res: http.client.Response, body: []const u8, sent: []const u8) !void { try testing.expectEqual(@as(u16, 200), res.status); try testing.expect(res.isOk()); try testing.expectEqualStrings("OK", res.reason); try testing.expectEqualStrings("text/plain", res.header("content-type").?); try testing.expectEqualStrings("hello", body); // §9.3 of RFC 9112: HTTP/1.1 is persistent unless `close` is // named. try testing.expect(conn.canReuse());
// §3.2: exactly one `Host`, written by the driver rather than // the caller, so the authority has one source. try testing.expectEqualStrings( "GET /index.html HTTP/1.1\r\nHost: example.com\r\n\r\n", sent, ); } }.check, );}
test "RFC 9112 6.3 rule 1: a HEAD response is head only, whatever it declares" { // The field says five octets and there are none, because a `HEAD` is // answered with the head a `GET` would have had. A client that believed // the field would read the next response as this one's content. try exchange( testing.allocator, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n", .{ .host = "example.com", .method = http.protocol.Method.head }, .{}, struct { fn check(_: *client.Connection, res: http.client.Response, body: []const u8, _: []const u8) !void { try testing.expectEqual(@as(u16, 200), res.status); try testing.expectEqualStrings("", body); try testing.expect(!res.body.present); // The field is still readable, because it describes what a // `GET` would have returned. try testing.expectEqualStrings("5", res.header("content-length").?); } }.check, );}
test "a chunked response is the content, with no way to ask that it was chunked" { try exchange( testing.allocator, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n" ++ "5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n", .{ .host = "example.com" }, .{}, struct { fn check(conn: *client.Connection, res: http.client.Response, body: []const u8, _: []const u8) !void { try testing.expectEqualStrings("hello world", body); try testing.expectEqual(@as(?u64, null), res.body.length); // The coding ends the body, so the connection survives it. try testing.expect(conn.canReuse()); } }.check, );}
test "RFC 9112 6.3 rule 3: a response framed two ways at once is refused" { // "Such a message might indicate an attempt to perform request smuggling // or response splitting and ought to be handled as an error." Guessing // which to believe is guessing where the next response starts. const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init( gpa, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\nTransfer-Encoding: chunked\r\n\r\nhello", .{}, ); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
try testing.expectError(error.InvalidFraming, conn.send(.{ .host = "example.com" }));}
test "RFC 9112 6.3 rule 8: a response with no framing runs to the close" { try exchange( testing.allocator, "HTTP/1.0 200 OK\r\n\r\neverything until the socket shuts", .{ .host = "example.com" }, .{}, struct { fn check(conn: *client.Connection, res: http.client.Response, body: []const u8, _: []const u8) !void { try testing.expectEqualStrings("everything until the socket shuts", body); try testing.expectEqual(http.protocol.Version.http_1_0, res.version); // The body ended because the connection did, so there is no // next message on it. try testing.expect(!conn.canReuse()); } }.check, );}
test "RFC 9112 9.3: an HTTP/1.0 response is not persistent unless it says so" { try exchange( testing.allocator, "HTTP/1.0 200 OK\r\nContent-Length: 2\r\n\r\nhi", .{ .host = "example.com" }, .{}, struct { fn check(conn: *client.Connection, _: http.client.Response, _: []const u8, _: []const u8) !void { try testing.expect(!conn.canReuse()); } }.check, ); try exchange( testing.allocator, "HTTP/1.0 200 OK\r\nContent-Length: 2\r\nConnection: keep-alive\r\n\r\nhi", .{ .host = "example.com" }, .{}, struct { fn check(conn: *client.Connection, _: http.client.Response, _: []const u8, _: []const u8) !void { try testing.expect(conn.canReuse()); } }.check, );}
test "an HTTP/1.1 response naming close ends the connection" { try exchange( testing.allocator, "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nhi", .{ .host = "example.com" }, .{}, struct { fn check(conn: *client.Connection, _: http.client.Response, _: []const u8, _: []const u8) !void { try testing.expect(!conn.canReuse()); } }.check, );}
test "a request body with a known length is sent with one" { try exchange( testing.allocator, "HTTP/1.1 204 \r\n\r\n", .{ .host = "example.com", .method = http.protocol.Method.post, .target = "/submit", .body = .{ .exact = 4 }, .content = "data", }, .{}, struct { fn check(_: *client.Connection, res: http.client.Response, _: []const u8, sent: []const u8) !void { try testing.expectEqual(@as(u16, 204), res.status); try testing.expectEqualStrings( "POST /submit HTTP/1.1\r\nHost: example.com\r\nContent-Length: 4\r\n\r\ndata", sent, ); } }.check, );}
test "a request body of unknown length is chunked, and only in HTTP/1.1" { try exchange( testing.allocator, "HTTP/1.1 204 \r\n\r\n", .{ .host = "example.com", .method = http.protocol.Method.post, .body = .unknown, .content = "data", }, .{}, struct { fn check(_: *client.Connection, _: http.client.Response, _: []const u8, sent: []const u8) !void { try testing.expectEqualStrings( "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\n" ++ "4\r\ndata\r\n0\r\n\r\n", sent, ); } }.check, );
// §6.1: chunked is HTTP/1.1's. An HTTP/1.0 recipient would read the sizes // as content, so the driver refuses rather than sending it. const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init(gpa, "HTTP/1.0 204 \r\n\r\n", .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{ .version = .http_1_0 }); defer conn.deinit(); try testing.expectError(error.InvalidRequest, conn.send(.{ .host = "example.com", .method = http.protocol.Method.post, .body = .unknown, .content = "data", }));}
test "an HTTP/1.1 request without an authority is refused before it is sent" { // §3.2: "A client MUST send a Host header field in all HTTP/1.1 request // messages." Sending one without is sending a message every server must // reject, which is worth refusing here rather than discovering there. const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 200 OK\r\n\r\n", .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit(); try testing.expectError(error.InvalidRequest, conn.send(.{ .host = "" }));}
test "a target carrying a CRLF is refused, and nothing goes out" { // §2.2 of RFC 9112: a message is a sequence of lines ended by CRLF, so a // target carrying one does not travel as a target. It ends the request // line, and everything after it is read as fields -- a field the caller // never sent, which the server has no way to tell from one it did. A // caller that builds a path out of something it was given reaches this // without doing anything unusual, which is what makes it worth refusing // here rather than trusting every caller to have checked. // // Found by fuzzing what the client writes rather than what it reads. // Before this, `send` reported success and put four lines on the wire: // // GET /a // X-Injected: yes // Junk: HTTP/1.1 // Host: example.com const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 200 OK\r\n\r\n", .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
try testing.expectError(error.InvalidRequest, conn.send(.{ .host = "example.com", .target = "/a\r\nX-Injected: yes\r\nJunk: ", })); // Refused *before* it is sent, which is the half that matters: a driver // that noticed after writing the request line would have already sent // the injected field. try testing.expectEqualStrings("", memory.written());
// The authority goes out on a line of its own and splits the same way. try testing.expectError(error.InvalidRequest, conn.send(.{ .host = "example.com\r\nX-Injected: yes", .target = "/", })); try testing.expectEqualStrings("", memory.written());}
/// The fixture, gzipped. std's codec rather than anything of this library's,/// because what these tests are about is the wiring: whether the driver asks,/// notices, and undoes — not whether deflate works, which `protocol.coding`/// has its own round trip for.fn gzipped(plain: []const u8, into: []u8) ![]u8 { var out: std.Io.Writer = .fixed(into); var window: [http.protocol.coding.window_len]u8 = undefined; var c = try http.protocol.coding.gzipCompressor(&out, &window, .default); try c.writer.writeAll(plain); try c.finish(); return out.buffered();}
/// Standing a connection up and taking it down again, for/// `checkAllAllocationFailures` to run once per allocation with that one/// failing.////// `init` and not a whole exchange, and the reason is worth leaving here so/// nobody spends an afternoon widening it: the memory transport writes through/// an `Io.Writer.Allocating`, and a `drain` can only report `Writer.Error`, so/// an allocation that fails while the request is going out arrives as/// `error.WriteFailed` with the `OutOfMemory` already lost. That is correct of/// the interface and fatal to this tool, which insists on seeing/// `error.OutOfMemory` from every failing run. Covering the send path needs a/// transport whose writes cannot allocate, which is a different fixture.fn initAndDeinit(gpa: std.mem.Allocator) !void { var memory = try http.transport.memory.Connection.init(gpa, "", .{}); defer memory.deinit();
var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); conn.deinit();}
test "an allocation that fails while standing up a connection frees what it took" { // `init` allocated its two buffers inside the returned struct literal, // where a `try` on the second has nowhere to put an `errdefer` -- so a // failure there leaked the first, and left the caller no `Connection` to // free it through. Reported from zig-weather, whose own tests make every // allocation fail in turn, which is the only way anybody sees this. // // `checkAllAllocationFailures` runs the body once to count the // allocations, then once for each with that one failing, and insists every // failing run reports `OutOfMemory` *and* gives back every octet it took. try testing.checkAllAllocationFailures(testing.allocator, initAndDeinit, .{});}
/// One whole response carrying `coded` as a gzipped body, built without an/// allocator so that a fixture cannot be what a leak check reports.fn codedResponse(coded: []const u8, into: []u8) ![]u8 { var head_buf: [128]u8 = undefined; const head_text = try std.fmt.bufPrint( &head_buf, "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Length: {d}\r\n\r\n", .{coded.len}, ); @memcpy(into[0..head_text.len], head_text); @memcpy(into[head_text.len..][0..coded.len], coded); return into[0 .. head_text.len + coded.len];}
/// Long enough and repetitive enough that the coded form is plainly smaller,/// so a test that accidentally compared coded with coded would not pass.const compressible = repeat("a content coding is part of the representation. ", 24);
test "RFC 9110 12.5.3: a client that asks for gzip is answered and undoes it" { const gpa = testing.allocator;
var coded_buf: [4096]u8 = undefined; const coded = try gzipped(compressible, &coded_buf); try testing.expect(coded.len < compressible.len);
var whole: [8192]u8 = undefined; const response = try codedResponse(coded, &whole);
var memory = try http.transport.memory.Connection.init(gpa, response, .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{ .accept_encodings = &.{.gzip}, }); defer conn.deinit();
const res = try conn.send(.{ .host = "example.com", .target = "/" });
// §12.5.3: it asked. Without this the server has no reason to have sent // the coding, and the rest of the test would be checking a fixture. try testing.expect(std.mem.indexOf(u8, memory.written(), "Accept-Encoding: gzip\r\n") != null);
// §8.6: `Content-Length` counted the coded octets, and the caller is // about to read the decoded ones — so the length it was given must not // be the one on the wire. Saying nothing is the only honest answer. try testing.expectEqual(@as(?u64, null), res.body.length);
const body = try conn.readAll(res, 1 << 20); defer gpa.free(body); try testing.expectEqualStrings(compressible, body);}
test "RFC 9110 8.4: a coding nobody asked to have undone is handed over coded" { // A content coding is part of the representation, so a client that did // not ask for one gets what arrived rather than a driver's opinion of it. // This is the behaviour the README argues for, now with a second way to // reach it: the codings are configured off. const gpa = testing.allocator;
var coded_buf: [4096]u8 = undefined; const coded = try gzipped(compressible, &coded_buf);
var whole: [8192]u8 = undefined; const response = try codedResponse(coded, &whole);
var memory = try http.transport.memory.Connection.init(gpa, response, .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
const res = try conn.send(.{ .host = "example.com", .target = "/" }); // Nothing was asked for, so nothing was sent to ask. try testing.expect(std.mem.indexOf(u8, memory.written(), "Accept-Encoding") == null); // And the length still describes what the caller will read, because what // the caller will read is what arrived. try testing.expectEqual(@as(?u64, coded.len), res.body.length);
const body = try conn.readAll(res, 1 << 20); defer gpa.free(body); try testing.expectEqualStrings(coded, body);}
test "RFC 9110 8.4: a coding this library cannot undo is not pretended about" { // `br` is a coding this library names and cannot perform. Handing the // body over coded is right; what would be wrong is decoding it as gzip, // or refusing a response whose representation is simply not this // driver's business. const gpa = testing.allocator; const response = "HTTP/1.1 200 OK\r\nContent-Encoding: br\r\nContent-Length: 5\r\n\r\nzzzzz";
var memory = try http.transport.memory.Connection.init(gpa, response, .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{ .accept_encodings = &.{.gzip}, }); defer conn.deinit();
const res = try conn.send(.{ .host = "example.com", .target = "/" }); try testing.expectEqual(@as(?u64, 5), res.body.length); const body = try conn.readAll(res, 1 << 20); defer gpa.free(body); try testing.expectEqualStrings("zzzzz", body);}
test "the driver owns Accept-Encoding only while it is negotiating" { // Two sources for one negotiation is a client that asks for one thing and // decodes another, which is why the driver refuses a caller-set field // while it is asking for codings itself -- and why it does not refuse one // when it is not, since then the field is the caller's and so is whatever // comes back. const gpa = testing.allocator;
var asking = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 204 No Content\r\n\r\n", .{}); defer asking.deinit(); var with = try client.Connection.init(testing.io, gpa, asking.connection(), .{ .accept_encodings = &.{.gzip}, }); defer with.deinit();
var headers: http.protocol.Headers = .empty; defer headers.deinit(gpa); try headers.append(gpa, "Accept-Encoding", "br"); try testing.expectError(error.InvalidRequest, with.send(.{ .host = "example.com", .target = "/", .headers = headers, }));
var quiet = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 204 No Content\r\n\r\n", .{}); defer quiet.deinit(); var without = try client.Connection.init(testing.io, gpa, quiet.connection(), .{}); defer without.deinit(); _ = try without.send(.{ .host = "example.com", .target = "/", .headers = headers }); try testing.expect(std.mem.indexOf(u8, quiet.written(), "Accept-Encoding: br\r\n") != null);}
test "two exchanges on one connection" { // What keep-alive is for, and why the driver holds state rather than // taking it per call: the second request is written into the connection // the first left behind. const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init( gpa, "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\none" ++ "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\ntwo", .{}, ); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
const first = try conn.send(.{ .host = "example.com", .target = "/1" }); const first_body = try conn.readAll(first, 1 << 20); defer gpa.free(first_body); try testing.expectEqualStrings("one", first_body); try testing.expect(conn.canReuse());
const second = try conn.send(.{ .host = "example.com", .target = "/2" }); const second_body = try conn.readAll(second, 1 << 20); defer gpa.free(second_body); try testing.expectEqualStrings("two", second_body);}
test "a body left unread makes the connection unusable rather than corrupt" { // What is left on the wire is the rest of a message, and the next request // written into it would be answered by the remains of this one. const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init( gpa, "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\none", .{}, ); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
_ = try conn.send(.{ .host = "example.com" }); try testing.expect(!conn.canReuse()); try testing.expectError(error.InvalidRequest, conn.send(.{ .host = "example.com" }));}
test "a bodiless response that declares a body ends the connection" { // §6.3 rule 1 outranks rule 3: a 204 has no body "regardless of the // header fields present in the message", so this is framed as bodiless // and that is right. What is not right is reusing the connection -- // `abcde` is still on the wire, and the next request written here would // be answered by it. That is response splitting performed on oneself. // // Found by fuzzing the client against arbitrary server bytes. try exchange( testing.allocator, "HTTP/1.1 204 OK\r\nContent-Length: 5\r\nTransfer-Encoding: chunked\r\n\r\nabcde", .{ .host = "example.com" }, .{}, struct { fn check(conn: *client.Connection, res: http.client.Response, body: []const u8, _: []const u8) !void { try testing.expectEqual(@as(u16, 204), res.status); try testing.expectEqualStrings("", body); try testing.expect(!res.body.present); try testing.expect(!conn.canReuse()); } }.check, );}
test "RFC 9110 15: a status outside every class is carried, not refused" { // "A client MUST understand the class of any status code... and treat an // unrecognized status code as being equivalent to the x00 status code of // that class." A code with no class at all is outside that rule, and // refusing it would make this library the thing that broke rather than // the server that sent it. try exchange( testing.allocator, "HTTP/1.1 804 Nonsense\r\nContent-Length: 2\r\n\r\nhi", .{ .host = "example.com" }, .{}, struct { fn check(_: *client.Connection, res: http.client.Response, body: []const u8, _: []const u8) !void { try testing.expectEqual(@as(u16, 804), res.status); try testing.expectEqual(http.client.Response.Class.unknown, res.class()); try testing.expect(!res.isOk()); try testing.expectEqualStrings("hi", body); } }.check, );}
/// `s` said `n` times over: what `s ** n` spelled before Zig 0.17 took the/// operator away.inline fn repeat(comptime s: []const u8, comptime n: usize) *const [s.len * n]u8 { comptime { const rows: [n][s.len]u8 = @splat(s[0..s.len].*); const flat: [s.len * n]u8 = @bitCast(rows); return ♭ }}
/// 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]]; }};
test "RFC 9110 15.2: a 100 Continue ahead of the response is skipped" { // "A client MUST be able to parse one or more 1xx responses received // prior to a final response, even if the client does not expect one." // This client sends no `Expect`, and a server may send a `100` anyway. try exchange( testing.allocator, "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok", .{ .host = "example.com" }, .{}, struct { fn check(conn: *client.Connection, res: http.client.Response, body: []const u8, _: []const u8) !void { try testing.expectEqual(@as(u16, 200), res.status); try testing.expectEqualStrings("ok", body); try testing.expect(conn.canReuse()); } }.check, );}
test "RFC 8297: each Early Hints is told to the hook, and none is the response" { const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 103 Early Hints\r\nLink: </a.css>; rel=preload\r\n\r\n" ++ "HTTP/1.1 103 Early Hints\r\nLink: </b.js>; rel=preload\r\n\r\n" ++ "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nContent-Type: text/html\r\n\r\nhi", .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
var seen: Interims = .{}; const res = try conn.send(.{ .host = "example.com", .interim = seen.hook() }); const body = try conn.readAll(res, 1024); defer gpa.free(body);
try testing.expectEqual(@as(u16, 200), res.status); try testing.expectEqualStrings("hi", body); // The final response's fields are its own, and none of the hints'. try testing.expectEqualStrings("text/html", res.header("content-type").?); try testing.expect(res.header("link") == null);
try testing.expectEqual(@as(usize, 2), seen.count); try testing.expectEqual(@as(u16, 103), seen.statuses[0]); try testing.expectEqualStrings("</a.css>; rel=preload", seen.link(0)); try testing.expectEqual(@as(u16, 103), seen.statuses[1]); try testing.expectEqualStrings("</b.js>; rel=preload", seen.link(1));}
test "an interim response does not leave the final one for the next request" { // What this client did before it knew about interim responses: it // handed back the `103` as the response, left the `200` unread on the // wire, and called the connection reusable -- so the next request on it // was answered with the previous request's response. const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 103 Early Hints\r\n\r\nHTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nfirst" ++ "HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nsecond", .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
const first = try conn.send(.{ .host = "example.com", .target = "/1" }); const first_body = try conn.readAll(first, 1024); defer gpa.free(first_body); try testing.expectEqualStrings("first", first_body);
try testing.expect(conn.canReuse()); const second = try conn.send(.{ .host = "example.com", .target = "/2" }); const second_body = try conn.readAll(second, 1024); defer gpa.free(second_body); try testing.expectEqualStrings("second", second_body);}
test "more interim responses than allowed is an error, and ends the connection's reuse" { const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 100 Continue\r\n\r\n" ++ "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 100 Continue\r\n\r\n" ++ "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{ .max_interim_responses = 2 }); defer conn.deinit();
try testing.expectError(error.TooManyInterimResponses, conn.send(.{ .host = "example.com" })); try testing.expect(!conn.canReuse());}
test "RFC 9110 15.2.2: a 101 is the final response, not an interim one" { // Informational by number and final in meaning: what follows it is the // new protocol's, and reading it as another response head would be // reading a WebSocket frame as HTTP. try exchange( testing.allocator, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n", .{ .host = "example.com" }, .{}, struct { fn check(_: *client.Connection, res: http.client.Response, body: []const u8, _: []const u8) !void { try testing.expectEqual(@as(u16, 101), res.status); try testing.expectEqualStrings("websocket", res.header("upgrade").?); try testing.expectEqualStrings("", body); } }.check, );}
test "RFC 9110 15.2: framing fields on an interim response are ignored" { // "A 1xx response is terminated by the end of the header section", so a // `Content-Length` on one describes nothing, and the response after it // starts where the head ends. try exchange( testing.allocator, "HTTP/1.1 103 Early Hints\r\nContent-Length: 5\r\n\r\n" ++ "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok", .{ .host = "example.com" }, .{}, struct { fn check(_: *client.Connection, res: http.client.Response, body: []const u8, _: []const u8) !void { try testing.expectEqual(@as(u16, 200), res.status); try testing.expectEqualStrings("ok", body); } }.check, );}
// The HTTP/2 client, over the same kind of scripted connection: the server's// half of the exchange as frames written out by hand.
const h2 = http.http2.client;
/// §4.1: a frame header and its payload.fn h2Frame(w: *Io.Writer, kind: u8, flags: u8, stream: u31, payload: []const u8) !void { try w.writeInt(u24, @intCast(payload.len), .big); try w.writeByte(kind); try w.writeByte(flags); try w.writeInt(u32, stream, .big); try w.writeAll(payload);}
const h2_settings: u8 = 0x04;const h2_headers: u8 = 0x01;const h2_data: u8 = 0x00;const end_stream: u8 = 0x01;const end_headers: u8 = 0x04;
/// RFC 7541: `:status` as a literal with an indexed name (index 8), for the/// codes the static table has no entry for.fn status(comptime code: []const u8) [2 + code.len]u8 { return [_]u8{ 0x08, code.len } ++ code[0..code.len].*;}
/// RFC 7541 6.2.2: a literal field with a literal name, not indexed.fn literal(comptime name: []const u8, comptime value: []const u8) [3 + name.len + value.len]u8 { return [_]u8{ 0x00, name.len } ++ name[0..name.len].* ++ [_]u8{value.len} ++ value[0..value.len].*;}
/// `:status: 200`, which the static table has at index 8.const status_200 = [_]u8{0x88};
/// Runs one request on stream 1 against `frames` and hands what came of it to/// `check`, with the body if there was a response.fn h2Exchange( frames: []const u8, config: h2.Config, interim: ?http.client.Request.InterimHook,) !struct { status: u16, link: bool, trailer: bool, body: [16]u8, body_len: usize } { const gpa = testing.allocator; var source: [1024]u8 = undefined; var sw: Io.Writer = .fixed(&source); try h2Frame(&sw, h2_settings, 0, 0, ""); try sw.writeAll(frames);
var memory = try http.transport.memory.Connection.init(gpa, sw.buffered(), .{}); defer memory.deinit(); var c = try h2.Connection.init(testing.io, gpa, memory.connection(), config); _ = c.releaseConnection(); defer c.deinit(); try c.handshake();
const handle = try c.begin(.{ .host = "example.com", .interim = interim }); const res = try c.await(handle); defer c.finish(handle);
var out: @typeInfo(@typeInfo(@TypeOf(h2Exchange)).@"fn".return_type.?).error_union.payload = .{ .status = res.status, .link = res.header("link") != null, .trailer = res.header("x-trailer") != null, .body = undefined, .body_len = 0, }; // §8.3: control data never reaches the fields. for (res.headers.fields.items) |field| try testing.expect(field.name[0] != ':'); const body = c.body(handle); out.body_len = @min(body.len, out.body.len); @memcpy(out.body[0..out.body_len], body[0..out.body_len]); return out;}
test "RFC 9113 8.1: an interim response's HEADERS come before the final one's" { var seen: Interims = .{}; var frames: [256]u8 = undefined; var w: Io.Writer = .fixed(&frames); try h2Frame(&w, h2_headers, end_headers, 1, &(status("103") ++ literal("link", "</a.css>; rel=preload"))); try h2Frame(&w, h2_headers, end_headers, 1, &(status("103") ++ literal("link", "</b.js>; rel=preload"))); try h2Frame(&w, h2_headers, end_headers, 1, &(status_200 ++ literal("content-type", "text/html"))); try h2Frame(&w, h2_data, end_stream, 1, "hi");
const got = try h2Exchange(w.buffered(), .{}, seen.hook()); try testing.expectEqual(@as(u16, 200), got.status); try testing.expect(!got.link); try testing.expectEqualStrings("hi", got.body[0..got.body_len]);
try testing.expectEqual(@as(usize, 2), seen.count); try testing.expectEqual(@as(u16, 103), seen.statuses[0]); try testing.expectEqualStrings("</a.css>; rel=preload", seen.link(0)); try testing.expectEqualStrings("</b.js>; rel=preload", seen.link(1));}
test "RFC 9113 8.1: an interim response that ends the stream is malformed" { // "A HEADERS frame with the END_STREAM flag set that carries an // informational status code is malformed." var frames: [64]u8 = undefined; var w: Io.Writer = .fixed(&frames); try h2Frame(&w, h2_headers, end_headers | end_stream, 1, &status("103")); try testing.expectError(error.MalformedResponse, h2Exchange(w.buffered(), .{}, null));}
test "RFC 9113 8.6: a 101 is malformed in HTTP/2" { // "HTTP/2 removes support for the 101 (Switching Protocols) // informational status code." var frames: [64]u8 = undefined; var w: Io.Writer = .fixed(&frames); try h2Frame(&w, h2_headers, end_headers, 1, &status("101")); try h2Frame(&w, h2_headers, end_headers | end_stream, 1, &status_200); try testing.expectError(error.MalformedResponse, h2Exchange(w.buffered(), .{}, null));}
test "more interim responses than an HTTP/2 client allows is an error" { var frames: [128]u8 = undefined; var w: Io.Writer = .fixed(&frames); for (0..3) |_| try h2Frame(&w, h2_headers, end_headers, 1, &status("100")); try h2Frame(&w, h2_headers, end_headers | end_stream, 1, &status_200); try testing.expectError( error.TooManyInterimResponses, h2Exchange(w.buffered(), .{ .max_interim_responses = 2 }, null), );}
test "RFC 9113 8.1: a trailer section is read past, and not mixed into the head" { var frames: [128]u8 = undefined; var w: Io.Writer = .fixed(&frames); try h2Frame(&w, h2_headers, end_headers, 1, &status_200); try h2Frame(&w, h2_data, 0, 1, "hi"); try h2Frame(&w, h2_headers, end_headers | end_stream, 1, &(literal("x-trailer", "yes")));
const got = try h2Exchange(w.buffered(), .{}, null); try testing.expectEqual(@as(u16, 200), got.status); try testing.expect(!got.trailer); try testing.expectEqualStrings("hi", got.body[0..got.body_len]);}
test "RFC 9113 8.1: a trailer section that does not end the stream is malformed" { // A trailer section is the last thing a message carries, and "the last // frame in the sequence bears an END_STREAM flag". var frames: [128]u8 = undefined; var w: Io.Writer = .fixed(&frames); try h2Frame(&w, h2_headers, end_headers, 1, &status_200); try h2Frame(&w, h2_headers, end_headers, 1, &(literal("x-trailer", "yes"))); try h2Frame(&w, h2_data, end_stream, 1, "late"); try testing.expectError(error.MalformedResponse, h2Exchange(w.buffered(), .{}, null));}
test "RFC 9113 8.1.1: one stream's malformed response is that stream's error alone" { // A malformed response is a *stream* error. It used to come back from // whichever `await` happened to be reading when it arrived -- so a bad // response on stream 3 failed the request on stream 1. const gpa = testing.allocator; var source: [256]u8 = undefined; var sw: Io.Writer = .fixed(&source); try h2Frame(&sw, h2_settings, 0, 0, ""); // An uppercase field name, which §8.2.1 makes malformed. try h2Frame(&sw, h2_headers, end_headers | end_stream, 3, &(status_200 ++ literal("X-Up", "no"))); try h2Frame(&sw, h2_headers, end_headers | end_stream, 1, &status_200);
var memory = try http.transport.memory.Connection.init(gpa, sw.buffered(), .{}); defer memory.deinit(); var c = try h2.Connection.init(testing.io, gpa, memory.connection(), .{}); _ = c.releaseConnection(); defer c.deinit(); try c.handshake();
const one = try c.begin(.{ .host = "example.com", .target = "/1" }); const three = try c.begin(.{ .host = "example.com", .target = "/3" }); try testing.expectEqual(@as(u16, 200), (try c.await(one)).status); c.finish(one); try testing.expectError(error.MalformedResponse, c.await(three)); c.finish(three);}
test "RFC 9110 15: a :status below 100 is malformed" { // Three digits is the form, and 100 the least of them: there is no // class below the 1xx one for a client to understand a `013` as. Found by // fuzzing, from a `103` seed one digit away. var frames: [64]u8 = undefined; var w: Io.Writer = .fixed(&frames); try h2Frame(&w, h2_headers, end_headers | end_stream, 1, &status("013")); try testing.expectError(error.MalformedResponse, h2Exchange(w.buffered(), .{}, null));}
// RFC 9110 10.1.1: the 100-continue expectation, over HTTP/1.1.
test "RFC 9110 10.1.1: a 100 Continue asks for the content, and it follows" { const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 201 Created\r\nContent-Length: 0\r\n\r\n", .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
var seen: Interims = .{}; const res = try conn.send(.{ .method = http.protocol.Method.put, .host = "example.com", .target = "/upload", .body = .{ .exact = 5 }, .content = "hello", .expect_continue = true, .interim = seen.hook(), }); try testing.expectEqual(@as(u16, 201), res.status); try testing.expect(res.content_sent); const body = try conn.readAll(res, 16); defer gpa.free(body); try testing.expect(conn.canReuse()); try testing.expectEqualStrings( "PUT /upload HTTP/1.1\r\nHost: example.com\r\nExpect: 100-continue\r\nContent-Length: 5\r\n\r\nhello", memory.written(), ); // The `100` is an interim response like any other. try testing.expectEqual(@as(usize, 1), seen.count); try testing.expectEqual(@as(u16, 100), seen.statuses[0]);}
test "RFC 9110 10.1.1: a final response first, and the content is never sent" { // The point of the expectation: a server that was going to refuse the // upload says so, and none of it goes. What was promised on the // connection is now unknowable -- the server may be waiting for the // content or may close -- so it is not offered for reuse. const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 413 Content Too Large\r\nContent-Length: 0\r\n\r\n", .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
var source: Io.Reader = .fixed("a large upload"); const res = try conn.send(.{ .method = http.protocol.Method.put, .host = "example.com", .target = "/upload", .body = .{ .exact = 14 }, .source = &source, .expect_continue = true, }); try testing.expectEqual(@as(u16, 413), res.status); try testing.expect(!res.content_sent); try testing.expect(!conn.canReuse()); try testing.expectEqualStrings( "PUT /upload HTTP/1.1\r\nHost: example.com\r\nExpect: 100-continue\r\nContent-Length: 14\r\n\r\n", memory.written(), ); // Not a byte of it was read, so it can be sent again elsewhere. try testing.expectEqual(@as(usize, 0), source.seek);}
test "RFC 9110 10.1.1: an expectation with nothing to hold back is not sent" { // "A client MUST NOT generate a 100-continue expectation in a request // that does not include content." try exchange( testing.allocator, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", .{ .host = "example.com", .expect_continue = true }, .{}, struct { fn check(_: *client.Connection, res: http.client.Response, _: []const u8, sent: []const u8) !void { try testing.expectEqual(@as(u16, 200), res.status); try testing.expect(res.content_sent); try testing.expectEqualStrings("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n", sent); } }.check, );}
test "RFC 9110 10.1.1: nor over HTTP/1.0, which has no 100 to answer it with" { try exchange( testing.allocator, "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n", .{ .method = http.protocol.Method.post, .host = "example.com", .body = .{ .exact = 2 }, .content = "hi", .expect_continue = true, }, .{ .version = .http_1_0 }, struct { fn check(_: *client.Connection, res: http.client.Response, _: []const u8, sent: []const u8) !void { try testing.expect(res.content_sent); try testing.expectEqualStrings( "POST / HTTP/1.0\r\nHost: example.com\r\nContent-Length: 2\r\n\r\nhi", sent, ); } }.check, );}
test "RFC 9110 10.1.1: other interim responses before the 100 are waited through" { const gpa = testing.allocator; var memory = try http.transport.memory.Connection.init(gpa, "HTTP/1.1 103 Early Hints\r\nLink: </a.css>; rel=preload\r\n\r\n" ++ "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 204 No Content\r\n\r\n", .{}); defer memory.deinit(); var conn = try client.Connection.init(testing.io, gpa, memory.connection(), .{}); defer conn.deinit();
var seen: Interims = .{}; const res = try conn.send(.{ .method = http.protocol.Method.post, .host = "example.com", .body = .{ .exact = 2 }, .content = "hi", .expect_continue = true, .interim = seen.hook(), }); try testing.expectEqual(@as(u16, 204), res.status); try testing.expect(res.content_sent); try testing.expect(std.mem.endsWith(u8, memory.written(), "\r\n\r\nhi")); try testing.expectEqual(@as(usize, 2), seen.count); try testing.expectEqual(@as(u16, 103), seen.statuses[0]); try testing.expectEqual(@as(u16, 100), seen.statuses[1]);}
// RFC 9110 10.1.1 over HTTP/2: the head on its own, and the content once the// server asks for it -- read back from the frames the client wrote.
/// One frame the client wrote.const Sent = struct { kind: u8, flags: u8, stream: u31, payload: []const u8 };
/// The frames in what the client wrote, after the 24-octet preface.fn sentFrames(written: []const u8, out: []Sent) []Sent { var at: usize = 24; var n: usize = 0; while (at + 9 <= written.len and n < out.len) { const len = std.mem.readInt(u24, written[at..][0..3], .big); out[n] = .{ .kind = written[at + 3], .flags = written[at + 4], .stream = @intCast(std.mem.readInt(u32, written[at + 5 ..][0..4], .big) & 0x7fff_ffff), .payload = written[at + 9 ..][0..len], }; n += 1; at += 9 + len; } return out[0..n];}
/// Whether the client's HEADERS on `stream` carried `expect: 100-continue`,/// decoding every field block in order, since the table is connection-wide.fn sentExpectation(written: []const u8, stream: u31) !bool { const gpa = testing.allocator; var frames_buf: [32]Sent = undefined; var scratch: [4096]u8 = undefined; var decoder: http.http2.hpack.Decoder = .init(4096, &scratch, .{}); defer decoder.deinit(gpa); var found = false; for (sentFrames(written, &frames_buf)) |f| { if (f.kind != h2_headers) continue; var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); var fields: std.ArrayList(http.protocol.Field) = .empty; try decoder.decode(gpa, arena.allocator(), f.payload, &fields); if (f.stream != stream) continue; for (fields.items) |field| { if (std.mem.eql(u8, field.name, "expect") and std.mem.eql(u8, field.value, "100-continue")) found = true; } } return found;}
/// Whether the client sent DATA on `stream`, and what, and whether it reset it.fn sentOn(written: []const u8, stream: u31) struct { data: [32]u8, data_len: usize, ended: bool, reset: ?u32 } { var frames_buf: [32]Sent = undefined; var data: [32]u8 = undefined; var data_len: usize = 0; var ended = false; var reset: ?u32 = null; for (sentFrames(written, &frames_buf)) |f| { if (f.stream != stream) continue; switch (f.kind) { h2_data => { @memcpy(data[data_len..][0..f.payload.len], f.payload); data_len += f.payload.len; if (f.flags & end_stream != 0) ended = true; }, h2_headers => if (f.flags & end_stream != 0) { ended = true; }, 0x03 => reset = std.mem.readInt(u32, f.payload[0..4], .big), else => {}, } } return .{ .data = data, .data_len = data_len, .ended = ended, .reset = reset };}
test "RFC 9110 10.1.1: over HTTP/2, the content waits for the 100 and then follows" { const gpa = testing.allocator; var source: [256]u8 = undefined; var sw: Io.Writer = .fixed(&source); try h2Frame(&sw, h2_settings, 0, 0, ""); try h2Frame(&sw, h2_headers, end_headers, 1, &status("100")); try h2Frame(&sw, h2_headers, end_headers | end_stream, 1, &status("201"));
var memory = try http.transport.memory.Connection.init(gpa, sw.buffered(), .{}); defer memory.deinit(); var c = try h2.Connection.init(testing.io, gpa, memory.connection(), .{}); _ = c.releaseConnection(); defer c.deinit(); try c.handshake();
const handle = try c.begin(.{ .method = http.protocol.Method.put, .host = "example.com", .body = .{ .exact = 5 }, .content = "hello", .expect_continue = true, }); // `begin` sends the head alone: nothing of the content has gone yet. try testing.expect(try sentExpectation(memory.written(), 1)); const before = sentOn(memory.written(), 1); try testing.expectEqual(@as(usize, 0), before.data_len); try testing.expect(!before.ended);
const res = try c.await(handle); defer c.finish(handle); try testing.expectEqual(@as(u16, 201), res.status); try testing.expect(res.content_sent); const after = sentOn(memory.written(), 1); try testing.expectEqualStrings("hello", after.data[0..after.data_len]); try testing.expect(after.ended); try testing.expectEqual(@as(?u32, null), after.reset);}
test "RFC 9110 10.1.1: over HTTP/2, a final response first, and the stream is closed unsent" { // RFC 9113 8.1: a response complete before the request is, and the // request's half of the stream is ended with RST_STREAM of NO_ERROR // rather than with content nobody will read. const gpa = testing.allocator; var source: [256]u8 = undefined; var sw: Io.Writer = .fixed(&source); try h2Frame(&sw, h2_settings, 0, 0, ""); try h2Frame(&sw, h2_headers, end_headers | end_stream, 1, &status("401"));
var memory = try http.transport.memory.Connection.init(gpa, sw.buffered(), .{}); defer memory.deinit(); var c = try h2.Connection.init(testing.io, gpa, memory.connection(), .{}); _ = c.releaseConnection(); defer c.deinit(); try c.handshake();
var reader: Io.Reader = .fixed("a large upload"); const handle = try c.begin(.{ .method = http.protocol.Method.put, .host = "example.com", .body = .{ .exact = 14 }, .source = &reader, .expect_continue = true, }); const res = try c.await(handle); defer c.finish(handle); try testing.expectEqual(@as(u16, 401), res.status); try testing.expect(!res.content_sent); const sent = sentOn(memory.written(), 1); try testing.expectEqual(@as(usize, 0), sent.data_len); try testing.expectEqual(@as(?u32, 0), sent.reset); try testing.expectEqual(@as(usize, 0), reader.seek);}
test "RFC 9110 10.1.1: over HTTP/2, a 100 read while another stream is awaited waits for its own" { // The 100 for stream 3 arrives while stream 1 is being awaited. Its // content goes from `await` for stream 3, not from whichever `await` // read the 100. const gpa = testing.allocator; var source: [256]u8 = undefined; var sw: Io.Writer = .fixed(&source); try h2Frame(&sw, h2_settings, 0, 0, ""); try h2Frame(&sw, h2_headers, end_headers, 3, &status("100")); try h2Frame(&sw, h2_headers, end_headers | end_stream, 1, &status_200); try h2Frame(&sw, h2_headers, end_headers | end_stream, 3, &status("201"));
var memory = try http.transport.memory.Connection.init(gpa, sw.buffered(), .{}); defer memory.deinit(); var c = try h2.Connection.init(testing.io, gpa, memory.connection(), .{}); _ = c.releaseConnection(); defer c.deinit(); try c.handshake();
const one = try c.begin(.{ .host = "example.com", .target = "/1" }); const three = try c.begin(.{ .method = http.protocol.Method.post, .host = "example.com", .target = "/3", .body = .{ .exact = 2 }, .content = "hi", .expect_continue = true, }); try testing.expectEqual(@as(u16, 200), (try c.await(one)).status); c.finish(one); try testing.expectEqual(@as(usize, 0), sentOn(memory.written(), 3).data_len);
try testing.expectEqual(@as(u16, 201), (try c.await(three)).status); c.finish(three); const sent = sentOn(memory.written(), 3); try testing.expectEqualStrings("hi", sent.data[0..sent.data_len]);}
test "RFC 9110 10.1.1: over HTTP/2, no content means no expectation" { const gpa = testing.allocator; var source: [64]u8 = undefined; var sw: Io.Writer = .fixed(&source); try h2Frame(&sw, h2_settings, 0, 0, ""); try h2Frame(&sw, h2_headers, end_headers | end_stream, 1, &status_200);
var memory = try http.transport.memory.Connection.init(gpa, sw.buffered(), .{}); defer memory.deinit(); var c = try h2.Connection.init(testing.io, gpa, memory.connection(), .{}); _ = c.releaseConnection(); defer c.deinit(); try c.handshake(); const handle = try c.begin(.{ .host = "example.com", .expect_continue = true }); _ = try c.await(handle); c.finish(handle); try testing.expect(!try sentExpectation(memory.written(), 1)); try testing.expect(sentOn(memory.written(), 1).ended);}