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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! A `transport.Session` backed by a real QUIC connection.//!//! The whole of what this file does is forward: `quic_io.Session` already//! offers accepting, opening, reading and writing streams, and//! `transport.Session` is those operations with the QUIC taken out of their//! names. What it buys is that the HTTP/3 driver above it names none of this//! -- no `quic_io`, no socket, no datagram -- so the driver can be handed a//! scripted session in a test, the way the HTTP/1.1 driver is handed a//! scripted byte stream.
const std = @import("std");const Io = std.Io;
const quic = @import("quic");const quic_io = @import("quic_io");const http = @import("http");
const transport = http.transport;
/// Wraps one `quic_io.Session` as a `transport.Session`. It borrows `s`,/// which has to outlive what is returned.////// `http3.Listener` and `http3.Dialer` call this themselves; it is here for a/// caller holding a QUIC session that came some other way.pub fn session(s: *quic_io.Session) transport.Session { return .{ .context = s, .vtable = &vtable };}
test session { const testing = std.testing; const io = testing.io; const gpa = testing.allocator; const http3 = @import("http3.zig");
// An HTTP/3 server to dial, which needs a task to pump it. const Serve = struct { fn run(l: *http3.Listener) void { l.run() catch {}; } fn answer(_: *http.server.Request, res: *http.server.Response) anyerror!void { try res.send("hello"); } }; var server_prng: std.Random.DefaultPrng = .init(1); const any: Io.net.IpAddress = .{ .ip4 = .loopback(0) }; var server_socket = try any.bind(io, .{ .mode = .dgram }); defer server_socket.close(io); var listener: http3.Listener = undefined; try listener.init(io, gpa, &server_socket, .initStateless(Serve.answer), .{ .rng = server_prng.random(), .reset_key = @splat(0x11), .token_key = @splat(0x22), }); defer listener.deinit(); var tasks: Io.Group = .init; defer tasks.cancel(io); tasks.concurrent(io, Serve.run, .{&listener}) catch return error.SkipZigTest; defer listener.stop();
var client_prng: std.Random.DefaultPrng = .init(2); var client_socket = try any.bind(io, .{ .mode = .dgram }); defer client_socket.close(io); var dialer: http3.Dialer = undefined; try dialer.connect(io, gpa, &client_socket, server_socket.address, .{ .host = "localhost", .insecure_skip_verify = true, .rng = client_prng.random(), }); defer dialer.deinit();
// The QUIC session underneath the dialer, seen through the seam. const s = session(dialer.client.session); try testing.expect(s.waitEstablished()); const stream = try s.openBidirectional(); try testing.expectEqual(@as(u64, 0), stream.id.value);}
const vtable: transport.Session.VTable = .{ .waitEstablished = waitEstablished, .accept = accept, .openUnidirectional = openUnidirectional, .openBidirectional = openBidirectional, .streamIn = streamIn, .writeOut = writeOut, .finish = finish, .finishWith = finishWith, .received = received, .resetStream = resetStream, .stopSending = stopSending, .close = close,};
fn cast(context: ?*anyopaque) *quic_io.Session { return @ptrCast(@alignCast(context.?));}
fn waitEstablished(context: ?*anyopaque) bool { return cast(context).waitEstablished();}
fn accept(context: ?*anyopaque) ?quic.stream.Id { const stream = cast(context).accept() orelse return null; return stream.id;}
fn openUnidirectional(context: ?*anyopaque) transport.Session.OpenError!quic.stream.Id { const stream = cast(context).openUnidirectional() catch return error.StreamUnavailable; return stream.id;}
fn openBidirectional(context: ?*anyopaque) transport.Session.OpenError!quic.stream.Id { const stream = cast(context).openBidirectional() catch return error.StreamUnavailable; return stream.id;}
fn streamIn( context: ?*anyopaque, id: quic.stream.Id, w: *Io.Writer, limit: Io.Limit,) Io.Reader.StreamError!usize { const s = cast(context); // `quic_io.Stream.Reader` holds a buffer so that it can be an // `Io.Reader`; here the caller above the seam already has one, so the // reader is built around nothing and used only for the one call that // moves octets. Nothing is buffered in it between calls. var stream: quic_io.Stream = .{ .session = s, .id = id }; var reader = stream.reader(&.{}); return reader.interface.vtable.stream(&reader.interface, w, limit);}
fn writeOut(context: ?*anyopaque, id: quic.stream.Id, bytes: []const u8) Io.Writer.Error!void { return cast(context).writeStream(id, bytes) catch error.WriteFailed;}
fn finish(context: ?*anyopaque, id: quic.stream.Id) Io.Writer.Error!void { const s = cast(context); var stream: quic_io.Stream = .{ .session = s, .id = id }; var writer = stream.writer(&.{}); return writer.finish();}
/// The last octets and the FIN together, which is one packet rather than two/// and is what every other QUIC implementation puts on the wire.fn finishWith(context: ?*anyopaque, id: quic.stream.Id, bytes: []const u8) Io.Writer.Error!void { return cast(context).writeStreamFinished(id, bytes) catch error.WriteFailed;}
fn received(context: ?*anyopaque, id: quic.stream.Id) bool { const stream: quic_io.Stream = .{ .session = cast(context), .id = id }; return stream.received();}
fn resetStream(context: ?*anyopaque, id: quic.stream.Id, code: u64) void { // A reset the session cannot send is a session that has already gone, and // there is nothing left to tell the peer. cast(context).resetStream(id, code) catch {};}
fn stopSending(context: ?*anyopaque, id: quic.stream.Id, code: u64) void { cast(context).stopSending(id, code) catch {};}
fn close(context: ?*anyopaque, code: u64, why: []const u8) void { cast(context).closeApplication(code, why);}