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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! One HTTP/1.0 or HTTP/1.1 exchange, from the client's side.//!//! The same seam as the server, used the other way round: a//! `transport.Connection` is a reader and a writer, and this writes a request//! into the writer and reads a response out of the reader. Nothing here opens//! anything -- `http_io` dials the socket and hands one over, and a test//! hands over a slice of memory with a scripted server in it.//!//! ## What the client owes the server, and what it must not believe//!//! Almost every rule in here is about framing, because framing is where an//! HTTP/1.1 client is dangerous to itself. A response whose length this//! reads differently from the server that sent it is a response that ends in//! the wrong place, and what follows it on a kept-alive connection is then//! read as the *next* response -- which is response splitting, done to//! oneself. So `protocol.responseFraming` applies §6.3's eight rules in//! order, a disagreement between `Content-Length` and `Transfer-Encoding` is//! refused outright rather than resolved, and the connection is not reused//! when anything about the framing was uncertain.
const std = @import("std");const Io = std.Io;const Allocator = std.mem.Allocator;
const protocol = @import("../protocol.zig");const transport = @import("../transport.zig");const head = @import("head.zig");const body_mod = @import("body.zig");const Request = @import("../client/Request.zig");const Response = @import("../client/Response.zig");
const Headers = protocol.Headers;const Length = protocol.Length;const Version = protocol.Version;const token = protocol.token;const coding = protocol.coding;
pub const Config = struct { /// The buffer a response head is read into. It bounds the whole head, /// which is what stops a server from making a client allocate without /// limit before it has agreed to anything. max_head_len: usize = 16 * 1024, /// The most fields a response may carry. max_fields: usize = 128, /// How many interim responses may come ahead of the final one. /// /// §15.2 of RFC 9110 puts no number on them, so without this a server /// could keep a request waiting for ever on a stream of `100 Continue`s. max_interim_responses: u8 = 16, /// The buffer the body is read through. body_buffer_len: usize = 8 * 1024, /// The version to send. HTTP/1.0 is here because it is the version that /// has no `Host`, no chunked coding and no persistent connection by /// default, which is worth being able to speak deliberately. version: Version = .http_1_1, /// Content codings to ask for, and to undo when a response arrives /// carrying one. /// /// Empty means neither: no `Accept-Encoding` goes out, and a /// `Content-Encoding` that arrives anyway is handed over exactly as it /// came — which §8.4 of RFC 9110 makes the correct thing to do with a /// representation nobody asked to have decoded. /// /// Off by default because it costs memory rather than nothing: undoing a /// coding needs `coding.window_len` — 64 KiB — held for as long as a body /// is being read. That is per connection here, allocated when the first /// coded response arrives and reused afterwards, so a client that asks and /// is never answered with a coding pays nothing. /// /// While this is non-empty the driver owns the `Accept-Encoding` field and /// refuses a caller that sets one, for the reason `Request.reserved` /// exists: two sources for one negotiation is a client that asks for one /// thing and decodes another. accept_encodings: []const coding.Coding = &.{},};
pub const Error = error{ /// The connection failed while the request was going out. WriteFailed, /// The connection failed while the response was coming back. ReadFailed, /// The server closed before any response arrived. NoResponse, /// The response head did not end within `max_head_len`. HeadTooLong, /// More interim responses arrived than `max_interim_responses` allows. TooManyInterimResponses, /// The response head is not something this can read. MalformedResponse, /// The response framed itself two ways at once, or framed itself with a /// length that is not a length. §6.3 of RFC 9112 calls this an /// unrecoverable error, and a client that guessed would be guessing about /// where the next response starts. InvalidFraming, /// The request cannot be sent as described. InvalidRequest, /// `Request.source` ended before the length the request declared. The /// head had already gone, so the connection is not reused. RequestBodyShort, /// `Request.source` failed while the body was being sent -- the reader's /// failure, not the connection's, which is `WriteFailed`. RequestBodyReadFailed, OutOfMemory,};
/// One connection, which may carry more than one exchange.////// Holding the state rather than taking it per call is what makes keep-alive/// expressible: whether the last response permitted another is remembered/// here, and `send` refuses rather than writing a request into a connection/// the server has said it will close.pub const Connection = struct { conn: transport.Connection, config: Config, gpa: Allocator, io: Io, head_buf: []u8, body_buf: []u8, /// Whether another exchange may be sent on this connection. reusable: bool = true, /// Whether `deinit` closes the connection. See `release`. owns_connection: bool = true, /// Set while a response body is being read, so that a second `send` /// before the first body is finished is refused rather than interleaved. body_open: bool = false, fields: std.ArrayList(protocol.Field) = .empty, reader: body_mod.Reader = undefined, /// The window a coded body is undone through, allocated the first time one /// arrives and kept for the connection's life. decode_window: []u8 = &.{}, /// Live only while a coded body is being read, and pointed at by the /// reader handed to the caller — so it must not move, which is why it is /// here rather than on the stack of whatever read the head. decompressor: coding.Decoder = undefined,
/// Takes `conn` over and allocates the head and body buffers `config` /// asks for. From here `deinit` closes `conn`, unless `release` hands it /// back first. /// /// `config` is kept as it is given, and `accept_encodings` with it, so /// that slice must outlive the connection. pub fn init( io: Io, gpa: Allocator, conn: transport.Connection, config: Config, ) Allocator.Error!Connection { // Allocated before the literal and not inside it, which is the whole // of the fix: a `try` in the second field of a returned struct has // nowhere to put an `errdefer`, so a failure there leaked the first // buffer -- and left the caller no `Connection` to `deinit` it // through. Found by zig-weather's allocation-failure test, which is // the kind of fault only a caller that makes every allocation fail in // turn will ever see. const head_buf = try gpa.alloc(u8, config.max_head_len); errdefer gpa.free(head_buf); const body_buf = try gpa.alloc(u8, config.body_buffer_len); errdefer gpa.free(body_buf);
return .{ .conn = conn, .config = config, .gpa = gpa, .io = io, .head_buf = head_buf, .body_buf = body_buf, }; }
test init { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init(gpa, "", .{}); defer memory.deinit();
var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{ .version = .http_1_0, .max_head_len = 4096, }); defer c.deinit(); try std.testing.expect(c.canReuse()); }
/// Closes the underlying connection and gives back what this allocated. /// /// Takes ownership, the way `server.Server.serveConnection` does at the /// other end: a caller that had to remember a second `close` would /// eventually forget one. `release` is the way out for a caller that /// wants the connection afterwards -- an upgrade to WebSocket, or a /// `CONNECT` tunnel, where the exchange is a negotiation and the socket /// is the point. pub fn deinit(c: *Connection) void { if (c.owns_connection) c.conn.close(c.io, c.gpa); c.fields.deinit(c.gpa); c.gpa.free(c.head_buf); c.gpa.free(c.body_buf); c.gpa.free(c.decode_window); c.* = undefined; }
test deinit { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init(gpa, "", .{}); defer memory.deinit();
var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); // Closes the transport connection too; there is no second `close`. c.deinit(); }
/// Hands the connection back and stops owning it, so that `deinit` frees /// only what this allocated and the caller closes what is returned. /// /// What a `101 Switching Protocols` leaves behind: from there the bytes /// are the new protocol's and nothing here should be reading them. pub fn release(c: *Connection) transport.Connection { c.owns_connection = false; return c.conn; }
test release { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init( gpa, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n" ++ "\x81\x02hi", .{}, ); defer memory.deinit();
var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); defer c.deinit();
const res = try c.send(.{ .host = "a.example", .target = "/chat" }); try std.testing.expectEqual(@as(u16, 101), res.status);
// What follows the head is the new protocol's, still unread. var conn = c.release(); defer conn.close(std.testing.io, gpa); try std.testing.expectEqualStrings("\x81\x02hi", try conn.input.take(4)); }
/// Whether this connection may carry another request: the last response /// permitted it, and its body is not still being read. pub fn canReuse(c: *const Connection) bool { return c.reusable and !c.body_open; }
test canReuse { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init( gpa, "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi" ++ "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n", .{}, ); defer memory.deinit();
var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); defer c.deinit();
const first = try c.send(.{ .host = "a.example" }); // Not until the body has been read. try std.testing.expect(!c.canReuse()); gpa.free(try c.readAll(first, 1024)); try std.testing.expect(c.canReuse());
_ = try c.send(.{ .host = "a.example" }); // The server said it would close. try std.testing.expect(!c.canReuse()); }
test "an empty body leaves the connection free at once" { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init( gpa, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" ++ "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok", .{}, ); defer memory.deinit();
var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); defer c.deinit();
_ = try c.send(.{ .host = "a.example" }); // Nothing to read, so nothing to wait for. try std.testing.expect(c.canReuse()); const second = try c.readAll(try c.send(.{ .host = "a.example" }), 64); defer gpa.free(second); try std.testing.expectEqualStrings("ok", second); }
/// Sends a request and reads the response head, past any interim /// responses -- which `request.interim` is told about -- to the final one /// or to a `101`. With `request.expect_continue` and content to send, the /// content is held back until a `100 Continue` arrives. /// /// The body is not read here: it is `response.body.reader`, and a caller /// that does not read it must not reuse the connection, because what is /// left on the wire is the rest of a message and not the start of the /// next one. `canReuse` answers that. /// /// Every slice in the response points into this connection's buffers and /// is good until the next `send` or `deinit`. Refused with /// `error.InvalidRequest` while `canReuse` is false. pub fn send(c: *Connection, request: Request) Error!Response { if (!c.canReuse()) return error.InvalidRequest; // §10.1.1 of RFC 9110: only with content to hold back, and only in a // version with a `100 Continue` to be answered with. const expecting = request.expect_continue and request.hasContent() and c.config.version == .http_1_1; try c.writeHead(request, expecting); if (!expecting) try c.writeContent(request); c.conn.output.flush() catch return error.WriteFailed; return c.readResponse(request, expecting); }
test send { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init( gpa, "HTTP/1.1 201 Created\r\nLocation: /notes/1\r\nContent-Length: 0\r\n\r\n", .{}, ); defer memory.deinit();
var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); defer c.deinit();
const res = try c.send(.{ .method = .post, .host = "a.example", .target = "/notes", .body = .{ .exact = 5 }, .content = "hello", }); try std.testing.expectEqual(@as(u16, 201), res.status); try std.testing.expectEqualStrings("/notes/1", res.header("location").?); try std.testing.expectEqualStrings( "POST /notes HTTP/1.1\r\nHost: a.example\r\nContent-Length: 5\r\n\r\nhello", memory.written(), ); }
fn writeHead(c: *Connection, request: Request, expecting: bool) Error!void { const out = c.conn.output;
// Before a single octet is written, because a target or an authority // that would end the request line early is request splitting, and // this is the driver that frames a message out of lines. try request.validate(); // §3.2 of RFC 9112: an HTTP/1.1 request carries exactly one `Host`, // and this refuses to send one without it rather than emitting a // message every server must reject. if (c.config.version == .http_1_1 and request.host.len == 0) return error.InvalidRequest; // And that the body is one body, of the length it says, before the // head that promises it goes out. _ = try request.declaredContentLength();
out.print("{s} {s} {s}\r\n", .{ request.method.name, request.target, c.config.version.toString(), }) catch return error.WriteFailed;
if (request.host.len > 0) { out.print("Host: {s}\r\n", .{request.host}) catch return error.WriteFailed; } if (expecting) out.writeAll("Expect: 100-continue\r\n") catch return error.WriteFailed;
// §12.5.3 of RFC 9110: what this end will take, in the order it would // rather have them. No weights, because they would say nothing this // order does not -- a weight is for expressing indifference between // codings, and a client that can undo two of them has a first choice. if (c.config.accept_encodings.len > 0) { out.writeAll("Accept-Encoding: ") catch return error.WriteFailed; for (c.config.accept_encodings, 0..) |want, i| { if (i > 0) out.writeAll(", ") catch return error.WriteFailed; out.writeAll(want.name()) catch return error.WriteFailed; } out.writeAll("\r\n") catch return error.WriteFailed; }
for (request.headers.fields.items) |field| { if (Request.isReserved(field.name)) return error.InvalidRequest; // Owned only while the driver is negotiating: with no codings // configured this is a field like any other, and a caller that // wants to ask for one by hand and decode it itself may. if (c.config.accept_encodings.len > 0 and token.eqlIgnoreCase(field.name, "accept-encoding")) { return error.InvalidRequest; } out.print("{s}: {s}\r\n", .{ field.name, field.value }) catch return error.WriteFailed; }
switch (request.body) { .none => {}, .exact => |n| { out.print("Content-Length: {d}\r\n", .{n}) catch return error.WriteFailed; }, .unknown => { // §6.1 of RFC 9112: chunked is HTTP/1.1's, and an HTTP/1.0 // recipient would read the chunk sizes as content. A client // that has to speak 1.0 must know the length first. if (c.config.version != .http_1_1) return error.InvalidRequest; out.writeAll("Transfer-Encoding: chunked\r\n") catch return error.WriteFailed; }, }
out.writeAll("\r\n") catch return error.WriteFailed; }
fn writeContent(c: *Connection, request: Request) Error!void { const out = c.conn.output; if (request.content) |content| switch (request.body) { .none => if (content.len != 0) return error.InvalidRequest, .exact => |n| { if (content.len != n) return error.InvalidRequest; out.writeAll(content) catch return error.WriteFailed; }, .unknown => { // One chunk and the terminator, which is the whole body when // the caller had it in hand. if (content.len > 0) { out.print("{x}\r\n", .{content.len}) catch return error.WriteFailed; out.writeAll(content) catch return error.WriteFailed; out.writeAll("\r\n") catch return error.WriteFailed; } out.writeAll("0\r\n\r\n") catch return error.WriteFailed; }, } else if (request.source) |source| { // Straight from the caller's reader into the connection's writer, // with nothing between them but the two buffers they already // have. `declaredContentLength` has established that this is // `.exact`. // // Past this point the head has promised a length, so a failure // leaves the connection mid-message: nothing that follows on it // could be told from the rest of this body. source.streamExact64(out, request.body.exact) catch |err| { c.reusable = false; return switch (err) { error.EndOfStream => error.RequestBodyShort, error.ReadFailed => error.RequestBodyReadFailed, error.WriteFailed => error.WriteFailed, }; }; } }
/// `holding` is whether the content is still to be sent, waiting on a /// `100 Continue`. fn readResponse(c: *Connection, request: Request, expecting: bool) Error!Response { var holding = expecting; var interims: u8 = 0; const parsed = while (true) { c.fields.clearRetainingCapacity();
const raw = head.read(c.conn.input, c.head_buf) catch |err| switch (err) { error.EndOfStream => return error.NoResponse, error.IncompleteHead => return error.NoResponse, error.HeadTooLong => return error.HeadTooLong, error.ReadFailed => return error.ReadFailed, };
const one = head.parseResponse(raw, c.gpa, &c.fields, c.config.max_fields) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, else => return error.MalformedResponse, };
// §15.2 of RFC 9110: an interim response "is terminated by the // end of the header section", so whatever follows the head is the // next response -- another interim one, or the final one. Its // framing fields describe nothing and are not read. A `101` is // not one of these: it ends HTTP on the connection and is what // the caller gets back. if (one.status < 100 or one.status > 199 or one.status == 101) break one;
interims += 1; if (interims > c.config.max_interim_responses) { // What is on the wire now is the middle of a sequence this // client has stopped reading, and not the start of the next // response. c.reusable = false; return error.TooManyInterimResponses; } if (request.interim) |hook| { const borrowed: Headers = .{ .fields = c.fields }; hook.notify(.{ .status = one.status, .version = one.version, .headers = &borrowed }); } // §10.1.1: what the expectation was waiting for. The content goes // now, and the final response is still to come. if (holding and one.status == 100) { holding = false; try c.writeContent(request); c.conn.output.flush() catch return error.WriteFailed; } };
var headers: Headers = .{ .fields = c.fields }; const framing = protocol.responseFraming( parsed.status, request.method.eql(protocol.Method.head), request.method.eql(protocol.Method.connect), &headers, );
// Whether this connection survives, decided from what the response // said and from the version it said it in. §9.3 of RFC 9112: HTTP/1.1 // is persistent unless `close` is named, and HTTP/1.0 is the reverse. c.reusable = switch (parsed.version) { .http_1_0 => headers.containsToken("connection", "keep-alive"), else => !headers.containsToken("connection", "close"), };
// §10.1.1: answered with a final response, and the content was never // sent. The server may be waiting for it or may close; there is no // telling which, so nothing more goes on this connection. if (holding) c.reusable = false;
// After a `101` the connection speaks whatever was upgraded to (§7.8 // of RFC 9110), and after a 2xx to `CONNECT` it is a tunnel (§9.3.6). // Either way the next octets written are not an HTTP request's, and // `release` is the way to the connection, not another `send`. if (parsed.status == 101) c.reusable = false; if (request.method.eql(protocol.Method.connect) and parsed.status >= 200 and parsed.status < 300) c.reusable = false;
// §6.1 of RFC 9112: a client that receives an HTTP/1.0 message with a // `Transfer-Encoding` "MUST treat the message as if the framing is // faulty, even if a Content-Length is present, and close the // connection after processing the message". `Transfer-Encoding` is not // HTTP/1.0's, so a hop that only knows 1.0 may have framed it // differently. if (parsed.version == .http_1_0 and headers.has("transfer-encoding")) c.reusable = false;
// §6.3 rule 1 wins over every other rule -- a 1xx, a 204, a 304 and // any response to HEAD have no body "regardless of the header fields // present in the message" -- but a sender that put framing fields on // one of those either disagrees with this reader about whether a body // follows, or is an attacker arranging for it to. Either way what // comes next on the wire is not reliably the next response, so the // connection is not offered for one. Found by fuzzing, on a `204` // carrying a `Content-Length`, a `Transfer-Encoding` and five octets. if (framing == .none and !request.method.eql(protocol.Method.head)) { if (headers.get("content-length") != null or headers.get("transfer-encoding") != null) { c.reusable = false; } }
const mode: body_mod.Mode = switch (framing) { .none => .none, .exact => |n| .{ .exact = n }, .chunked => .chunked, // A body that ends with the connection is a body that ends the // connection: there is nothing after it to read. .until_close => blk: { c.reusable = false; break :blk .until_close; }, .invalid => return error.InvalidFraming, };
c.reader = .init(c.conn.input, c.body_buf, mode); // A body of no octets is over before it starts, so there is nothing // to wait for the caller to read and the connection is free at once. c.body_open = switch (mode) { .none => false, .exact => |n| n != 0, .chunked, .until_close => true, };
// §8.4: a content coding is part of the representation, so undoing it // happens above the framing and after it -- the octets the framing // delivers are the coded ones, and these are what the caller reads // instead. var decoding = false; if (c.body_open and c.config.accept_encodings.len > 0) { switch (coding.applied(headers.get("content-encoding"))) { .identity => {}, // A coding this library will not undo. §8.4 makes that the // caller's representation to keep or refuse, not this // driver's to guess at, so it is handed over as it arrived. .unsupported => {}, .one => |applied| { for (c.config.accept_encodings) |want| { if (want != applied) continue; // Sized by the coding, because the two differ by two // orders of magnitude. Grown rather than reallocated // each time, so a connection that sees both pays the // larger once. const need = coding.decodeBufferLen(applied); if (c.decode_window.len < need) { c.gpa.free(c.decode_window); c.decode_window = c.gpa.alloc(u8, need) catch { c.decode_window = &.{}; return error.OutOfMemory; }; } c.decompressor = coding.Decoder.init( applied, &c.reader.interface, c.decode_window, ) catch break; decoding = true; break; } }, } }
return .{ .content_sent = !holding, .status = parsed.status, .version = parsed.version, .reason = parsed.reason, .headers = headers, .body = .{ .reader = if (mode == .none) Response.Body.empty_reader else if (decoding) c.decompressor.reader() else &c.reader.interface, // §8.6: `Content-Length` counts the octets of the *coded* // representation, so once a coding is being undone it no // longer describes what the caller is about to read. Saying // nothing is the only honest answer -- a caller that trusted // it would stop short of the end of a body that decodes // larger, which is nearly always. .length = if (decoding) null else switch (framing) { .exact => |n| n, else => null, }, .present = mode != .none, }, }; }
/// Says the body has been read to its end, so the connection may carry /// another exchange if the response allowed one. /// /// Called by a caller that read the body itself; `readAll` below does it /// for the common case. What is left of the framing is read and thrown /// away first, because a caller can read every octet of the body and /// still leave some of it on the wire: a decoder stops at the end of what /// it decodes, and a chunked coding's last chunk and trailers come after /// that. At most `finish_drain_len` octets are read doing so. /// /// The connection is reused only if that reaches the end of the body. A /// body that was malformed or cut short, a read the transport failed, and /// a caller that stopped more than `finish_drain_len` short all leave the /// rest of the message in doubt, and the connection unusable. pub fn finishBody(c: *Connection) void { if (!c.body_open) return; c.body_open = false; var budget: usize = finish_drain_len; while (!c.reader.finished and c.reader.err == null and budget > 0) { budget -= c.reader.interface.discard(.limited(budget)) catch break; } if (!c.reader.finished) c.reusable = false; }
/// The most `finishBody` reads to find the end of a body. A last chunk /// and its trailers are what it expects to find, and `body.max_trailer_len` /// bounds those; anything longer is a caller that stopped short, and /// reading the rest of its body to save a connection is a bad trade. pub const finish_drain_len = body_mod.max_trailer_len + 1024;
test finishBody { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init( gpa, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n2\r\nhi\r\n0\r\n\r\n", .{}, ); defer memory.deinit();
var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); defer c.deinit();
const res = try c.send(.{ .host = "a.example" }); var sink: [16]u8 = undefined; var w: Io.Writer = .fixed(&sink); _ = try res.body.reader.streamRemaining(&w); try std.testing.expectEqualStrings("hi", w.buffered());
c.finishBody(); try std.testing.expect(c.canReuse()); }
test "a gzipped chunked body leaves the connection where the next response starts" { // gzip's footer ends the decoded stream before the chunked coding's // last chunk has been read, so a caller that read every decoded // octet has still left `0\r\n\r\n` on the wire. const gpa = std.testing.allocator; const hello_gz = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x05\x00\x00\x00"; var memory: transport.memory.Connection = try .init( gpa, "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nTransfer-Encoding: chunked\r\n\r\n" ++ "19\r\n" ++ hello_gz ++ "\r\n0\r\n\r\n" ++ "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok", .{}, ); defer memory.deinit();
var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{ .accept_encodings = &.{.gzip}, }); defer c.deinit();
const first = try c.readAll(try c.send(.{ .host = "a.example" }), 64); defer gpa.free(first); try std.testing.expectEqualStrings("hello", first); try std.testing.expect(c.canReuse());
const second = try c.readAll(try c.send(.{ .host = "a.example" }), 64); defer gpa.free(second); try std.testing.expectEqualStrings("ok", second); }
test "a body the transport failed partway through is not reused" { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init(gpa, "", .{}); defer memory.deinit();
// The head and two octets of a five-octet body arrive, and then the // socket fails -- not the end of the stream, which the body reader // would call a short body, but an error from below it. const Failing = struct { fn stream(r: *Io.Reader, w: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize { _ = r; _ = w; _ = limit; return error.ReadFailed; } }; var buffer: [128]u8 = undefined; const arrived = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhe"; @memcpy(buffer[0..arrived.len], arrived); var failing: Io.Reader = .{ .vtable = &.{ .stream = Failing.stream }, .buffer = &buffer, .seek = 0, .end = arrived.len, }; var conn = memory.connection(); conn.input = &failing;
var c: Connection = try .init(std.testing.io, gpa, conn, .{}); defer c.deinit();
const res = try c.send(.{ .host = "a.example" }); var sink: [16]u8 = undefined; var w: Io.Writer = .fixed(&sink); try std.testing.expectError(error.ReadFailed, res.body.reader.streamRemaining(&w));
// A caller that finishes the body anyway must not get the connection // back: three octets of it are still somewhere between here and the // server. c.finishBody(); try std.testing.expect(!c.canReuse()); }
/// Reads the whole body, at most `limit` octets of it, into memory /// allocated from the connection's `gpa` and owned by the caller, and /// then does what `finishBody` does. /// /// A body longer than `limit`, or one that cannot be read, is /// `error.ReadFailed`, and the connection is not reused: the rest of the /// message may still be on the wire. pub fn readAll(c: *Connection, response: Response, limit: usize) Error![]u8 { const bytes = response.body.readAlloc(c.gpa, limit) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.ReadFailed => { c.reusable = false; c.body_open = false; return error.ReadFailed; }, error.StreamTooLong => { // The body is longer than the caller agreed to hold, so what // is left of it is still on the wire and this connection // cannot be trusted to start the next message. c.reusable = false; c.body_open = false; return error.ReadFailed; }, }; c.finishBody(); return bytes; }
test readAll { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init( gpa, "HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhello world", .{}, ); defer memory.deinit();
var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); defer c.deinit();
const res = try c.send(.{ .host = "a.example" }); const body = try c.readAll(res, 1024); defer gpa.free(body); try std.testing.expectEqualStrings("hello world", body); try std.testing.expect(c.canReuse()); }};
test { std.testing.refAllDecls(@This());}
test "RFC 9110 7.8: a 101 leaves the connection unusable for HTTP" { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init( gpa, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n", .{}, ); defer memory.deinit(); var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); defer c.deinit(); const res = try c.send(.{ .host = "a.example", .target = "/chat" }); try std.testing.expectEqual(@as(u16, 101), res.status); try std.testing.expect(!c.canReuse());}
test "RFC 9110 9.3.6: a 2xx to CONNECT leaves the connection unusable for HTTP" { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init(gpa, "HTTP/1.1 200 OK\r\n\r\n", .{}); defer memory.deinit(); var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); defer c.deinit(); const res = try c.send(.{ .method = .connect, .host = "b.example:443", .target = "b.example:443" }); try std.testing.expectEqual(@as(u16, 200), res.status); try std.testing.expect(!c.canReuse());}
test "RFC 9112 6.1: Transfer-Encoding in an HTTP/1.0 response closes the connection" { const gpa = std.testing.allocator; var memory: transport.memory.Connection = try .init( gpa, "HTTP/1.0 200 OK\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n2\r\nhi\r\n0\r\n\r\n", .{}, ); defer memory.deinit(); var c: Connection = try .init(std.testing.io, gpa, memory.connection(), .{}); defer c.deinit(); const res = try c.send(.{ .host = "a.example" }); gpa.free(try c.readAll(res, 64)); try std.testing.expect(!c.canReuse());}