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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! zig-dns-client's lookups, in the shapes `http_io.Client` takes.//!//! `Client.resolver` wants `HostName.lookup`'s signature, and//! `dns_client.HostLookup.lookup` has exactly that, so it needs nothing. But//! `Client.services` is a queue of zig-http's own `Client.Service`, and//! zig-dns-client's `ServiceLookup` fills a queue of *its* `Service` -- two//! types with the same fields that cannot be handed to each other, and must//! not be made one: zig-dns-client pins a zig-http of its own, always a//! release behind, and one shared type would tie the two pins together. So the//! endpoints are copied across, which is all this file does.//!//! ```zig//! var client: http_io.Client = .{//! .io = io,//! .gpa = gpa,//! .http3 = .advertised,//! .resolver = https_records.hosts,//! .services = https_records.services,//! };//! ```
const std = @import("std");const Io = std.Io;const net = Io.net;
const http_io = @import("http_io");const dns_client = @import("dns_client");
const Client = http_io.Client;const Source = dns_client.ServiceLookup;
/// `Client.resolver`, answered from the system's `/etc/resolv.conf` and/// `/etc/hosts` by zig-dns-client.pub const hosts = dns_client.HostLookup.lookup;
/// `Client.services`, answered the same way.pub fn services( host_name: net.HostName, io: Io, resolved: *Io.Queue(Client.Service), options: Client.ServiceLookupOptions,) Client.ServiceLookupError!void { return bridge(Source.lookup, host_name, io, resolved, options);}
pub const SourceFn = fn ( host_name: net.HostName, io: Io, resolved: *Io.Queue(Source.Service), options: Source.LookupOptions,) Source.Error!void;
/// `Client.services` over any lookup with `ServiceLookup.lookup`'s signature/// -- a `ServiceLookup.resolve` over a `Resolver` of the program's own, say,/// wrapped in a function that knows where that is.pub fn bridge( comptime source: SourceFn, host_name: net.HostName, io: Io, resolved: *Io.Queue(Client.Service), options: Client.ServiceLookupOptions,) Client.ServiceLookupError!void { defer resolved.close(io); // As many as `source` will ever queue, so that it never blocks on a queue // nothing is draining while it runs. var storage: [Source.max_results]Source.Service = undefined; var found: Io.Queue(Source.Service) = .init(&storage); try source(host_name, io, &found, .{ .port = options.port }); while (found.getOne(io)) |service| { resolved.putOne(io, copy(&service)) catch |err| switch (err) { error.Closed => return, error.Canceled => |e| return e, }; } else |err| switch (err) { error.Closed => {}, error.Canceled => |e| return e, }}
/// One endpoint, from zig-dns-client's type to zig-http's.pub fn copy(from: *const Source.Service) Client.Service { var to: Client.Service = .{ .priority = from.priority, .port = from.port, .alpn = .{ .http_1_1 = from.alpn.http_1_1, .h2 = from.alpn.h2, .h3 = from.alpn.h3, }, .no_default_alpn = from.no_default_alpn, }; // Both hold a host name, which is at most 255 octets. to.setTarget(from.target()) catch unreachable; for (from.hints()) |hint| to.addHint(hint); return to;}
const testing = std.testing;
test "an endpoint crosses from one type to the other whole" { var from: Source.Service = .{ .priority = 3, .port = 8443, .no_default_alpn = true }; from.alpn.h3 = true; @memcpy(from.target_buf[0.."svc.example.net".len], "svc.example.net"); from.target_len = "svc.example.net".len; from.hints_buf[0] = .{ .ip4 = .{ .bytes = .{ 192, 0, 2, 1 }, .port = 8443 } }; from.hints_len = 1;
const to = copy(&from); try testing.expectEqual(@as(u16, 3), to.priority); try testing.expectEqual(@as(?u16, 8443), to.port); try testing.expect(to.alpn.h3 and !to.alpn.h2 and !to.alpn.http_1_1); try testing.expect(to.no_default_alpn); try testing.expectEqualStrings("svc.example.net", to.target()); try testing.expectEqual(@as(usize, 1), to.hints().len); try testing.expectEqual([4]u8{ 192, 0, 2, 1 }, to.hints()[0].ip4.bytes);}