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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! A demonstration server, which exists to be read as much as to be run.//!//! Every route here is chosen to show one thing the library does that a//! `hello world` would not: a body whose length is not known in advance, a//! request body read back, a status set by hand, and a handler with state of//! its own. About eighty lines of it are the handler and the rest is argument//! parsing.//!//! ```console//! $ zig build run -- --port 8080//! $ curl -v http://127.0.0.1:8080///! ```
const std = @import("std");const Io = std.Io;const net = Io.net;
const http = @import("http");const http_io = @import("http_io");const quic = @import("quic");const Request = http.server.Request;const Response = http.server.Response;
const usage = \\usage: zig-http [--address A] [--port N] \\ \\ --address A The address to bind to (default 127.0.0.1). Note that \\ 0.0.0.0 is every IPv4 address and no IPv6 one, so a \\ client that resolves the host to an AAAA record will be \\ refused. On Linux, :: is usually both families at once. \\ --port N The port to bind to, or 0 to let the kernel choose \\ (default 8080) \\ --h3-port N Also serve HTTP/3 on this UDP port. Needs --cert and \\ --key, because RFC 9114 has no cleartext form -- QUIC \\ carries TLS and there is no version of it that does \\ not, so `h3c` does not exist the way `h2c` does. \\ --fetch URL Fetch a URL and print the response instead of serving \\ anything. The same binary either way, which is the \\ demonstration: one library, both ends. Without \\ --fetch-version this is `http_io.Client`: the name is \\ resolved by the standard library's HostName.lookup, \\ https offers h2 and http/1.1 by ALPN and \\ speaks whichever the server picks, redirects are \\ followed, and the certificate is checked against the \\ system's store unless --fetch-ca says otherwise. \\ --fetch-http3 M \\ only: fetch an https URL over HTTP/3 or fail. prefer: \\ over HTTP/3 if the server answers over QUIC, and over \\ TCP if it does not. advertised: over HTTP/3 once the \\ origin has said so with Alt-Svc, which one --fetch \\ sees only across a redirect. Not through a proxy, and \\ never for an http URL. \\ --fetch-timeout S \\ Give up on --fetch after S seconds, all of it counted: \\ the lookup, the connection, the handshake, redirects \\ and the body. Not with --fetch-version. \\ --fetch-proxy URL \\ Fetch through this HTTP proxy, http:// or https://, \\ with any credentials in the URL sent as Basic \\ Proxy-Authorization. An http URL is forwarded to it \\ and an https one tunnelled through it with CONNECT. \\ Without it, http_proxy, https_proxy, all_proxy and \\ no_proxy are read from the environment as curl reads \\ them. Not with --fetch-version. \\ --fetch-gzip Ask for `gzip` and undo it, which §8.4 of RFC 9110 \\ makes a decision rather than a default: a content \\ coding is part of the representation, so a client \\ that did not ask for one gets what arrived. All three \\ versions. \\ --fetch-host H \\ The name to send as SNI and to check the certificate \\ against, when it is not the address in the URL. What \\ curl's --resolve is for: the URL says where to connect \\ and this says what is expected to be there. Needs \\ --fetch-version. \\ --fetch-ca P Trust the certificates in P for `--fetch` over https. \\ With --fetch-version and without this, an https fetch \\ does not verify at all, which is for pointing this at \\ a test server and nothing else. \\ --fetch-version V \\ 1.0, 1.1, 2 or 3: one driver, spoken whatever the \\ server would rather, to an address rather than a \\ name. Over http, version 2 is by prior knowledge, \\ since RFC 9113 removed the Upgrade dance; over https \\ it is by ALPN, which that RFC leaves as the only other \\ way in. Version 3 wants an https URL, because RFC 9114 \\ has no cleartext form. \\ --tls Serve HTTPS on --port rather than HTTP. Needs --cert \\ and --key, and is the only way a browser will speak \\ HTTP/2 here: RFC 9113 gives that version the ALPN \\ name `h2` and removed every other way of asking for \\ it. A separate switch rather than "a certificate was \\ given", because HTTP/3 needs one too and turning this \\ port into HTTPS as a side effect of asking for \\ HTTP/3 would be a surprise. \\ --cert P The certificate chain, in PEM. Used by --tls and by \\ --h3-port, which is one certificate because it is one \\ name answered over two transports. \\ --key P The private key for --cert, in PEM. \\ --h3-window N How much each HTTP/3 stream may hold in each direction, \\ in kilobytes (default 4). What a response may have in \\ the air at once, so raising it is what makes a large \\ body leave faster -- and what it costs is this times \\ sixteen streams, charged per connection alive at once \\ rather than per connection permitted, so raising it \\ costs nothing until somebody connects. The tracking \\ table is sized from it, since a window whose packets \\ cannot all be remembered is a connection that waits \\ for room in the table instead of for the network. \\ --h3-max-window N \\ The largest window a connection may be grown to, in \\ kilobytes (default 64). A send window is worth \\ `window / round-trip`, so no one number suits every \\ peer: the default window is already past the \\ bandwidth-delay product of a path across a building \\ and a fraction of one across an ocean. Every \\ connection starts at --h3-window and one that fills \\ its buffer while the path and the peer would both \\ have taken more is doubled, up to this; one that \\ never does costs nothing more. Set it equal to \\ --h3-window to turn the growing off. \\;
pub fn main(init: std.process.Init) !void { const gpa = init.gpa;
var address_text: []const u8 = "127.0.0.1"; var port: u16 = 8080; var h3_port: ?u16 = null; var h3_window: usize = 4096; var h3_max_window: usize = 64 * 1024; var cert_path: ?[]const u8 = null; var serve_tls = false; var fetch_url: ?[]const u8 = null; var fetch_version: ?[]const u8 = null; var fetch_ca: ?[]const u8 = null; var fetch_host: ?[]const u8 = null; var fetch_gzip = false; var fetch_proxy: ?[]const u8 = null; var fetch_timeout: ?u32 = null; var fetch_http3: http_io.Client.Http3 = .off; var key_path: ?[]const u8 = null;
var args: std.process.Args.Iterator = .init(init.minimal.args); _ = args.skip(); while (args.next()) |arg| { if (std.mem.eql(u8, arg, "--address")) { address_text = args.next() orelse return fail("--address wants an address"); } else if (std.mem.eql(u8, arg, "--port")) { const text = args.next() orelse return fail("--port wants a number"); port = std.fmt.parseInt(u16, text, 10) catch return fail("--port wants a number"); } else if (std.mem.eql(u8, arg, "--h3-max-window")) { const text = args.next() orelse return fail("--h3-max-window wants a number"); const kb = std.fmt.parseInt(usize, text, 10) catch return fail("--h3-max-window wants a number"); if (kb == 0) return fail("--h3-max-window wants at least 1"); h3_max_window = kb * 1024; } else if (std.mem.eql(u8, arg, "--h3-window")) { const text = args.next() orelse return fail("--h3-window wants a number"); const kb = std.fmt.parseInt(usize, text, 10) catch return fail("--h3-window wants a number"); if (kb == 0) return fail("--h3-window wants at least 1"); h3_window = kb * 1024; } else if (std.mem.eql(u8, arg, "--h3-port")) { const text = args.next() orelse return fail("--h3-port wants a number"); h3_port = std.fmt.parseInt(u16, text, 10) catch return fail("--h3-port wants a number"); } else if (std.mem.eql(u8, arg, "--fetch")) { fetch_url = args.next() orelse return fail("--fetch wants a URL"); } else if (std.mem.eql(u8, arg, "--fetch-version")) { fetch_version = args.next() orelse return fail("--fetch-version wants 1.0, 1.1 or 2"); } else if (std.mem.eql(u8, arg, "--fetch-ca")) { fetch_ca = args.next() orelse return fail("--fetch-ca wants a path"); } else if (std.mem.eql(u8, arg, "--fetch-host")) { fetch_host = args.next() orelse return fail("--fetch-host wants a name"); } else if (std.mem.eql(u8, arg, "--fetch-gzip")) { fetch_gzip = true; } else if (std.mem.eql(u8, arg, "--fetch-http3")) { const text = args.next() orelse return fail("--fetch-http3 wants only or prefer"); fetch_http3 = std.meta.stringToEnum(http_io.Client.Http3, text) orelse return fail("--fetch-http3 wants only or prefer"); } else if (std.mem.eql(u8, arg, "--fetch-timeout")) { const text = args.next() orelse return fail("--fetch-timeout wants a number of seconds"); fetch_timeout = std.fmt.parseInt(u32, text, 10) catch return fail("--fetch-timeout wants a number of seconds"); } else if (std.mem.eql(u8, arg, "--fetch-proxy")) { fetch_proxy = args.next() orelse return fail("--fetch-proxy wants a URL"); } else if (std.mem.eql(u8, arg, "--tls")) { serve_tls = true; } else if (std.mem.eql(u8, arg, "--cert")) { cert_path = args.next() orelse return fail("--cert wants a path"); } else if (std.mem.eql(u8, arg, "--key")) { key_path = args.next() orelse return fail("--key wants a path"); } else { std.debug.print("{s}", .{usage}); std.process.exit(2); } }
const address = net.IpAddress.parse(address_text, port) catch return fail("that is not an address");
// The threaded implementation, because a connection blocks on its socket // and the point of the accept loop is that the next one does not wait for // it. `concurrent_limit` is left unlimited so that `Io.Group.concurrent` // really is concurrent. var threaded: Io.Threaded = .init(gpa, .{}); defer threaded.deinit(); const io = threaded.io();
if (fetch_url) |url| return fetch(io, gpa, url, fetch_version, fetch_ca, fetch_host, fetch_gzip, .{ .url = fetch_proxy, .environ_map = init.environ_map, }, fetch_timeout, fetch_http3);
// One source of randomness for both listeners: the TLS handshake needs // it, and so does QUIC below. var seed: [32]u8 = undefined; try Io.randomSecure(io, &seed); var rng_impl = std.Random.DefaultPrng.init(std.mem.readInt(u64, seed[0..8], .little)); const rng = rng_impl.random();
// One certificate for both ports, because that is what a certificate is: // the same name, answered over TCP and over QUIC. The two listeners even // share the type -- `tests/typecheck.zig` asserts it, since both modules // reach the same tls.zig through the same dependency. var auth: ?http_io.tls.CertKeyPair = null; defer if (auth) |*a| a.deinit(gpa);
if (cert_path) |certificate| { const key = key_path orelse return fail("--cert needs --key"); auth = http_io.tls.CertKeyPair.fromFilePath( gpa, io, .cwd(), certificate, key, ) catch return fail("could not read the certificate or the key"); }
// `--tls` rather than the presence of a certificate, because HTTP/3 needs // one too and turning the byte-stream port into HTTPS as a side effect of // asking for HTTP/3 would be a surprise. var plain_listener: ?http_io.tcp.Listener = null; var tls_listener: ?http_io.tls.Listener = null; defer { if (plain_listener) |*l| l.deinit(io); if (tls_listener) |*l| l.deinit(io); }
if (serve_tls) { if (auth == null) return fail("--tls needs --cert and --key"); tls_listener = http_io.tls.Listener.listen(io, address, .{ .auth = &auth.?, .rng = rng, }) catch return fail("could not listen"); } else { plain_listener = try http_io.tcp.Listener.listen(io, address, .{}); }
const seam: http.transport.Listener = if (tls_listener) |*l| l.listener() else plain_listener.?.listener(); const bound = if (tls_listener) |*l| l.localAddress() else plain_listener.?.localAddress();
var counter: Counter = .{}; var server: http.server.Server = .init( io, gpa, seam, .init(&counter, Counter.serve), .{}, );
std.debug.print("listening on {s}://{f}/\n", .{ if (tls_listener != null) "https" else "http", bound, });
// HTTP/3 alongside, on the same handler. That is the whole demonstration: // one `Counter` answering requests that arrived over a byte stream and // over QUIC, with nothing in it that knows which. if (h3_port) |udp_port| { // The same certificate the TLS listener would use, loaded once // above: one name, two transports. if (auth == null) return fail("--h3-port needs --cert and --key");
var reset_key: quic.stateless_reset.Key = undefined; try Io.randomSecure(io, &reset_key); var token_key: quic.retry.Token.Key = undefined; try Io.randomSecure(io, &token_key);
const udp = net.IpAddress.parse(address_text, udp_port) catch return fail("that is not an address"); var socket = try udp.bind(io, .{ .mode = .dgram }); defer socket.close(io);
var h3: http_io.http3.Listener = undefined; // The tracking table follows the send window rather than being a // second knob, because there is only one right answer: it has to hold // the packets that window puts in the air, and `quic.Connection` // asserts as much. Doubling the floor leaves room for the packets // that are smaller than §14.1's minimum, which is most of them. const sizes: quic.Connection.Sizes = .{ .send_window = h3_window, .recv_window = h3_window, .tracked = @max(64, 2 * ((h3_window + 1199) / 1200)), }; // A ceiling below the starting window would be a ceiling already // exceeded, so an operator who lowers one and not the other gets the // growing turned off rather than an assertion. const ceiling = @max(h3_window, h3_max_window); try h3.init(io, gpa, &socket, .init(&counter, Counter.serve), .{ .auth = &auth.?, .rng = rng, .reset_key = reset_key, .token_key = token_key, .sizes = sizes, .max_send_window = ceiling, }); defer h3.deinit();
std.debug.print("listening on https://{f}/ (HTTP/3)\n", .{udp});
// Two loops, one process. `concurrent` rather than `async` because // the latter may run the function inline before returning, which // here would mean serving QUIC and never accepting a TCP connection. var both: Io.Group = .init; defer both.cancel(io); try both.concurrent(io, runQuic, .{&h3}); try server.serve(); return; }
try server.serve();}
fn runQuic(listener: *http_io.http3.Listener) void { listener.run() catch |err| { std.debug.print("zig-http: the HTTP/3 listener stopped: {s}\n", .{@errorName(err)}); };}
fn fail(message: []const u8) noreturn { std.debug.print("zig-http: {s}\n\n{s}", .{ message, usage }); std.process.exit(2);}
/// The handler, with one piece of state so that there is something to show/// that a handler may have some.////// The counter is atomic because a handler is shared: `Server.serve` gives/// each connection a task of its own, so two requests really can be inside/// this at the same time. A plain `usize` here would be a data race that a/// single client would never reveal and a load test finds immediately — which/// is what `tests/nixos/siege.nix` is for, and why it checks that the count/// comes out exactly right rather than approximately.const Counter = struct { requests: std.atomic.Value(usize) = .init(0),
fn serve(c: *Counter, req: *Request, res: *Response) anyerror!void { // Monotonic is enough: nothing is published through this counter, and // the only requirement is that no increment is lost. _ = c.requests.fetchAdd(1, .monotonic);
// The one piece of routing this demonstration has. A real one would // resolve the path first -- `req.target.resolvePath` -- and match on // that, because `/a/../b` and `/b` are the same resource and matching // on the raw target would treat them as two. if (std.mem.eql(u8, req.target.path, "/")) return c.index(req, res); if (std.mem.eql(u8, req.target.path, "/echo")) return echo(req, res); if (std.mem.eql(u8, req.target.path, "/count")) return c.count(req, res); if (std.mem.eql(u8, req.target.path, "/stream")) return stream(req, res); if (std.mem.eql(u8, req.target.path, "/bulk")) return bulk(req, res); if (std.mem.eql(u8, req.target.path, "/trailers")) return trailers(req, res);
res.status = .not_found; try res.setHeader("Content-Type", "text/plain; charset=utf-8"); try res.sendPrint("no such thing as {s}\n", .{req.target.path}); }
fn index(c: *Counter, req: *Request, res: *Response) anyerror!void { _ = .{ c, req }; try res.setHeader("Content-Type", "text/plain; charset=utf-8"); try res.send( \\zig-http \\ \\ GET / this \\ POST /echo says back whatever you send it \\ GET /count how many requests this process has served \\ GET /stream a body whose length nobody knows in advance \\ GET /bulk a megabyte of it, which is enough to meet flow control \\ GET /trailers fields sent after the body, which say how it went \\ ); }
fn count(c: *Counter, req: *Request, res: *Response) anyerror!void { _ = req; try res.setHeader("Content-Type", "text/plain; charset=utf-8"); try res.sendPrint("{d}\n", .{c.requests.load(.monotonic)}); }};
/// Reads the request body and sends it back.////// The limit is not optional: a body of unknown length is whatever the client/// decides to send, and a handler that reads until it stops is one a single/// client can stop.fn echo(req: *Request, res: *Response) anyerror!void { if (!req.body.isPresent()) { res.status = .length_required; return res.sendStatus(.length_required); } const body = req.body.readAlloc(req.arena, 1 << 20) catch |err| switch (err) { error.StreamTooLong => return res.sendStatus(.payload_too_large), else => return err, }; try res.setHeader( "Content-Type", req.header("content-type") orelse "application/octet-stream", ); try res.send(body);}
/// A response whose length is not known when the head goes out.////// Which costs a chunked transfer coding over HTTP/1.1, a closed connection/// over HTTP/1.0, and an ordinary run of DATA frames over HTTP/2 — where the/// chunked coding does not exist and sending `transfer-encoding` at all would/// make the message malformed. The handler does not have to know which of the/// three it is talking to, and that is the whole argument for `Response`/// being shaped the way it is.fn stream(req: *Request, res: *Response) anyerror!void { _ = req; try res.setHeader("Content-Type", "text/plain; charset=utf-8"); const w = try res.beginBody(.unknown); for (1..11) |i| { try w.print("line {d}\n", .{i}); // Without the flush these would all leave in one chunk, which is // correct but shows nothing. try w.flush(); } try res.end();}
/// A body big enough that flow control happens.////// `/stream` shows a length nobody knows in advance; this shows what happens/// when there is a lot of it. Over HTTP/1.1 that is a great many chunks; over/// HTTP/2 the send window closes after the first 65535 octets and does not/// open again until the client says so — which is the one part of HTTP/2 that/// no small response reaches, and the part a server cannot fake being right/// about./// RFC 9110 §6.5: a checksum of a body nobody knew the length of, sent after/// it.////// This is what a trailer section is for and it is hard to demonstrate any/// other way: the two fields below cannot be sent in the head, because/// neither value exists until the last octet has been produced. The `Trailer`/// field says in advance what will come, which §6.6.2 recommends so that a/// recipient can decide whether to keep them before it has read the body.////// Which framings can carry them is the driver's business and not this/// handler's, which is the point of writing it this way: over HTTP/1.1 they/// go in the chunked terminator, over HTTP/2 and HTTP/3 they are a second/// field section, and a response of declared length has nowhere to put them/// and drops them. Nothing here changes to say so.fn trailers(req: *Request, res: *Response) anyerror!void { _ = req; try res.setHeader("Content-Type", "text/plain; charset=utf-8"); try res.setHeader("Trailer", "x-octets, x-checksum");
const w = try res.beginBody(.unknown); var octets: usize = 0; var sum: u32 = 0; for (1..6) |i| { var line: [32]u8 = undefined; const text = try std.fmt.bufPrint(&line, "line {d}\n", .{i}); try w.writeAll(text); octets += text.len; for (text) |c| sum = sum *% 31 +% c; try w.flush(); }
// Into the arena, because a trailer value is not copied and has to outlive // `end` -- the same rule as a header value, and easier to break here since // the obvious place to compute one is a local buffer. try res.trailers.append(res.arena, "x-octets", try std.fmt.allocPrint(res.arena, "{d}", .{octets})); try res.trailers.append(res.arena, "x-checksum", try std.fmt.allocPrint(res.arena, "{x:0>8}", .{sum})); try res.end();}
fn bulk(req: *Request, res: *Response) anyerror!void { _ = req; const line = "the quick brown fox jumps over the lazy dog......................\n"; const total = 1 << 20;
try res.setHeader("Content-Type", "text/plain; charset=utf-8"); const w = try res.beginBody(.{ .exact = total }); var written: usize = 0; while (written < total) { const take = @min(line.len, total - written); try w.writeAll(line[0..take]); written += take; } try res.end();}
test { std.testing.refAllDecls(@This());}
/// Fetches one URL and prints what came back.////// Here because a client wants proving against servers this library did not/// write, and a guest needs something to run. That it is the same binary that/// serves is the point rather than a convenience: one library, both ends.fn fetch( io: Io, gpa: std.mem.Allocator, url: []const u8, version_choice: ?[]const u8, ca_path: ?[]const u8, host_override: ?[]const u8, want_gzip: bool, proxies: ProxyChoice, timeout_seconds: ?u32, use_http3: http_io.Client.Http3,) !void { const version_text = version_choice orelse { if (host_override != null) return fail("--fetch-host needs --fetch-version"); return fetchNegotiated(io, gpa, url, ca_path, want_gzip, proxies, timeout_seconds, use_http3); }; if (use_http3 != .off) return fail("--fetch-http3 cannot be used with --fetch-version; that is --fetch-version 3"); if (timeout_seconds != null) return fail("--fetch-timeout cannot be used with --fetch-version"); // One driver pointed at one socket is exactly what a proxy is not. if (proxies.url != null) return fail("--fetch-proxy cannot be used with --fetch-version"); const target = http.protocol.Target.parse(url) catch return fail("that is not a URL"); if (target.form != .absolute) return fail("--fetch wants an absolute URL"); const authority = target.authority orelse return fail("--fetch wants a host"); const scheme = target.scheme orelse "http"; const over_quic = std.mem.eql(u8, version_text, "3"); // §3.1 of RFC 9114: HTTP/3 has no cleartext form -- QUIC carries TLS and // there is no version of it that does not -- so the schemes are the other // way round from the byte-stream versions here. const secure = std.mem.eql(u8, scheme, "https"); if (over_quic and !secure) return fail("--fetch-version 3 wants https"); if (!secure and !std.mem.eql(u8, scheme, "http")) return fail("--fetch speaks http and https"); // `host:port`, with the scheme's default when no port is given. var host = authority; var port: u16 = if (secure) http.default_tls_port else http.default_port; if (std.mem.lastIndexOfScalar(u8, authority, ':')) |at| { host = authority[0..at]; port = std.fmt.parseInt(u16, authority[at + 1 ..], 10) catch return fail("that is not a port"); }
// An address and not a name. A version given by hand is one driver // pointed at one socket, and this path dials exactly what it was told; // `std.Io.net.IpAddress.resolve` parses a literal, and the "resolve" in // its name is about an IPv6 interface scope. Looking a name up is what // `http_io.Client` does, and leaving out --fetch-version is how to get it. const address = net.IpAddress.resolve(io, host, port) catch return fail("--fetch-version wants an address, not a name; leave it out to have the name resolved"); const path = if (target.path.len == 0) "/" else target.path;
var out_buf: [4096]u8 = undefined; var stdout: Io.File.Writer = .init(.stdout(), io, &out_buf); const out = &stdout.interface;
// Both TLS paths want these, so they are made once here rather than in // each branch. var seed: [32]u8 = undefined; try Io.randomSecure(io, &seed); var rng_impl = std.Random.DefaultPrng.init(std.mem.readInt(u64, seed[0..8], .little)); const rng = rng_impl.random();
// Without `--fetch-ca` nothing is verified, which is why the flag is the // thing that switches verification on rather than a `--insecure` that // switches it off: the safe shape for a tool that exists to be pointed at // a test server is one where the careless invocation is obviously the // careless one. var trust: http_io.tls.CertBundle = .{ .bytes = .empty, .map = .empty }; defer trust.deinit(gpa); if (ca_path) |bundle_path| { trust = http_io.tls.certBundleFromFilePath(gpa, io, .cwd(), bundle_path) catch return fail("could not read the file --fetch-ca named"); } const verify = ca_path != null; // Where to connect and what to expect when you get there are different // questions, and a URL that is required to carry an address cannot answer // the second. This is the split curl spells `--resolve`. const verify_name = host_override orelse host;
if (over_quic) { const any: net.IpAddress = .{ .ip4 = .unspecified(0) }; var socket = try any.bind(io, .{ .mode = .dgram }); defer socket.close(io);
var dialer: http_io.http3.Dialer = undefined; dialer.connect(io, gpa, &socket, address, .{ .host = verify_name, .root_ca = trust, .insecure_skip_verify = !verify, .rng = rng, }) catch return fail("could not connect"); defer dialer.deinit();
// Its own task, because a stream read blocks until a datagram // arrives and this is what makes one arrive. var tasks: Io.Group = .init; defer tasks.cancel(io); try tasks.concurrent(io, pumpQuic, .{&dialer}); defer dialer.stop();
var c: http.http3.client.Connection = .init(gpa, dialer.session(), .{ .accept_encodings = if (want_gzip) &.{.gzip} else &.{}, }); try c.start(); var exchange = try c.send(.{ .host = authority, .target = path }); defer exchange.deinit(gpa);
try out.print("{d}\n", .{exchange.response.status}); for (exchange.response.headers.fields.items) |field| { try out.print("{s}: {s}\n", .{ field.name, field.value }); } try out.print("\n{s}", .{exchange.body()}); try out.flush(); return; }
if (std.mem.eql(u8, version_text, "2")) { const conn = if (secure) http_io.tls.connect(io, gpa, address, .{ .host = verify_name, .root_ca = trust, .insecure_skip_verify = !verify, .rng = rng, // §3.1 of RFC 9113: `h2`, and nothing beside it. Offering // `http/1.1` as well would let a server that cannot speak // HTTP/2 hand back a connection this branch would go on to // speak HTTP/2 down regardless. .alpn_protocols = &.{"h2"}, }) catch return fail("could not connect") else try http_io.tcp.connect(io, gpa, address, .{});
// Offering one protocol is not the same as having it agreed. §3.2 of // RFC 7301 asks a server with nothing in common to send a // `no_application_protocol` alert, and not every server does -- some // simply answer with no ALPN extension at all, which is a completed // handshake and an unanswered question. Speaking HTTP/2 into that // would be this client deciding a version the server never agreed to. if (secure) { const selected = conn.info.alpn orelse return fail("the server agreed to no protocol: it may not speak HTTP/2"); if (!std.mem.eql(u8, selected, "h2")) return fail("the server did not select h2"); }
var c = try http.http2.client.Connection.init(io, gpa, conn, .{ .accept_encodings = if (want_gzip) &.{.gzip} else &.{}, }); defer c.deinit(); try c.handshake();
const handle = try c.begin(.{ .host = authority, .target = path }); const res = try c.await(handle); try out.print("{d}\n", .{res.status}); for (res.headers.fields.items) |field| { try out.print("{s}: {s}\n", .{ field.name, field.value }); } try out.print("\n{s}", .{c.body(handle)}); c.finish(handle); } else { const wanted: http.protocol.Version = if (std.mem.eql(u8, version_text, "1.0")) .http_1_0 else .http_1_1;
const conn = if (secure) http_io.tls.connect(io, gpa, address, .{ .host = verify_name, .root_ca = trust, .insecure_skip_verify = !verify, .rng = rng, // §3.1 of RFC 9113 again, from the other side: offering `h2` // here would let a server select a version this branch is not // about to speak. .alpn_protocols = &.{"http/1.1"}, }) catch return fail("could not connect") else try http_io.tcp.connect(io, gpa, address, .{}); var c = try http.http1.client.Connection.init(io, gpa, conn, .{ .version = wanted, .accept_encodings = if (want_gzip) &.{.gzip} else &.{}, }); defer c.deinit();
const res = try c.send(.{ .host = authority, .target = path }); const body = try c.readAll(res, 1 << 22); defer gpa.free(body);
try out.print("{d}\n", .{res.status}); for (res.headers.fields.items) |field| { try out.print("{s}: {s}\n", .{ field.name, field.value }); } try out.print("\n{s}", .{body}); } try out.flush();}
/// `--fetch` with no version: `http_io.Client`, which resolves the name,/// negotiates the version and follows redirects./// Where `--fetch` finds a proxy: `--fetch-proxy`, or the environment.const ProxyChoice = struct { url: ?[]const u8, environ_map: *const std.process.Environ.Map,};
fn fetchNegotiated( io: Io, gpa: std.mem.Allocator, url: []const u8, ca_path: ?[]const u8, want_gzip: bool, proxies: ProxyChoice, timeout_seconds: ?u32, use_http3: http_io.Client.Http3,) !void { var trust: ?http_io.tls.CertBundle = null; defer if (trust) |*bundle| bundle.deinit(gpa); if (ca_path) |bundle_path| { trust = http_io.tls.certBundleFromFilePath(gpa, io, .cwd(), bundle_path) catch return fail("could not read the file --fetch-ca named"); }
var client: http_io.Client = .{ .io = io, .gpa = gpa, .root_ca = trust, .accept_encodings = if (want_gzip) &.{.gzip} else &.{}, .http3 = use_http3, .timeout = if (timeout_seconds) |seconds| .{ .duration = .{ .raw = .fromSeconds(seconds), .clock = .awake } } else .none, }; defer client.deinit();
// `--fetch-proxy` for both schemes, which is curl's `--proxy`; otherwise // the variables curl reads. Either way `no_proxy` still applies. var proxy_arena: std.heap.ArenaAllocator = .init(gpa); defer proxy_arena.deinit(); if (proxies.url) |text| { const chosen = http_io.proxy.Proxy.parse(proxy_arena.allocator(), text) catch return fail("--fetch-proxy wants an http:// or https:// URL"); client.http_proxy = chosen; client.https_proxy = chosen; } client.initDefaultProxies(proxy_arena.allocator(), proxies.environ_map) catch return fail("a proxy variable in the environment is not an http:// or https:// URL");
var body: Io.Writer.Allocating = .init(gpa); defer body.deinit(); var result = client.fetch(.{ .location = .{ .url = url }, .response_writer = &body.writer, }) catch |err| { std.debug.print("zig-http: could not fetch {s}: {t}\n", .{ url, err }); std.process.exit(1); }; defer result.deinit();
// The same shape the `--fetch-version` paths print, with the version the // negotiation settled on beside the status since that is the one thing // they were told and this was not. var out_buf: [4096]u8 = undefined; var stdout: Io.File.Writer = .init(.stdout(), io, &out_buf); const out = &stdout.interface; try out.print("{d} {t}\n", .{ @intFromEnum(result.status), result.version }); for (result.headers.fields.items) |field| { try out.print("{s}: {s}\n", .{ field.name, field.value }); } try out.print("\n{s}", .{body.written()}); try out.flush();}
fn pumpQuic(dialer: *http_io.http3.Dialer) void { dialer.run() catch {};}