Something went wrong. Try again.
A URI parser and formatter for Zig, modelled on Python's hyperlink package, with LDAP URLs, file: URIs and sdns:// DNS stamps beside it.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Per-scheme knowledge: default ports, and whether a scheme uses an authority//! (`//host`) or packs everything into an opaque path.//!//! ## Where the default ports come from//!//! Every entry in `default_ports` was checked against IANA's Service Name and//! Transport Protocol Port Number Registry, and, for schemes whose defining//! document states a default, against that RFC.//!//! Where the two disagree the RFC wins. A default port is a property of the//! *URI scheme*, and a scheme does not always share a name with the IANA//! service that sits on its port — nor does sharing a name mean sharing a//! protocol. Ten entries are filed by IANA under a different service name than//! the scheme and are nonetheless correct: `afp` is `afpovertcp`, `dns` is//! `domain`, `ircs` is `ircs-u`, `mms` is `ms-streaming`, `pop` is `pop3`,//! `smb` is `microsoft-ds`, `vnc` is `rfb`, `wais` is `z39.50`, and `ws` and//! `wss` ride on `http` and `https`.//!//! Two entries were simply wrong before this check, and are corrected here.//! Both errors are also present in Python's `hyperlink` and in//! `boltons.urlutils`, which is where this table was originally adapted from://!//! - `nfs` was 111, which is `sunrpc` — the portmapper, not NFS itself.//! RFC 2224 §3 says an NFS URL with no port means 2049, matching IANA.//! - `rtspu` was 5005, which IANA assigns to `avt-profile-2`. RFC 2326 §3.2//! gives `rtsp` and `rtspu` one default of 554; they differ in transport,//! TCP versus UDP, not in port number.//!//! Three further entries look wrong against IANA but are right; each is//! annotated where it appears in the table below.
const std = @import("std");
/// Scheme names are case-insensitive (RFC 3986 §3.1), so the tables match that/// way rather than making every caller lowercase its input first. The map/// buckets candidates by length before comparing, so `eqlAsciiIgnoreCase` only/// ever sees equal-length slices.fn SchemeMap(comptime V: type) type { return std.StaticStringMapWithEql(V, std.static_string_map.eqlAsciiIgnoreCase);}
const default_ports = SchemeMap(u16).initComptime(.{ .{ "acap", 674 }, .{ "afp", 548 }, .{ "dict", 2628 }, .{ "dns", 53 }, .{ "ftp", 21 }, .{ "git", 9418 }, .{ "gopher", 70 }, .{ "http", 80 }, .{ "https", 443 }, .{ "imap", 143 }, .{ "ipp", 631 }, .{ "ipps", 631 }, .{ "irc", 194 }, .{ "ircs", 6697 }, .{ "ldap", 389 }, .{ "ldaps", 636 }, .{ "mms", 1755 }, .{ "msrp", 2855 }, .{ "mtqp", 1038 }, // RFC 2224 §3. Not 111, which is the `sunrpc` portmapper. .{ "nfs", 2049 }, .{ "nntp", 119 }, .{ "nntps", 563 }, .{ "pop", 110 }, // RFC 4157: "the port defaults to 1525". IANA's `prospero` is 191; // 1525 is its `prospero-np`, the non-privileged directory service. .{ "prospero", 1525 }, .{ "redis", 6379 }, .{ "rsync", 873 }, .{ "rtsp", 554 }, .{ "rtsps", 322 }, // RFC 2326 §3.2 gives `rtspu` the same 554 as `rtsp`, over UDP. .{ "rtspu", 554 }, // SSH File Transfer, so the SSH port. IANA's `sftp` is 115, an // unrelated and long-obsolete Simple File Transfer Protocol. .{ "sftp", 22 }, .{ "smb", 445 }, .{ "snmp", 161 }, .{ "ssh", 22 }, .{ "svn", 3690 }, .{ "telnet", 23 }, // Vendor convention rather than an assignment: IANA gives 3784 to // `bfd-control` (RFC 5881). Kept because it is what clients use. .{ "ventrilo", 3784 }, .{ "vnc", 5900 }, .{ "wais", 210 }, .{ "ws", 80 }, .{ "wss", 443 },});
/// The IANA-registered default port for `scheme`, or null if it has none./// `scheme` is matched case-insensitively.pub fn defaultPort(scheme: []const u8) ?u16 { return default_ports.get(scheme);}
/// Schemes whose reference syntax has no `//authority` component: everything/// after the `:` is an opaque path.const rootless_schemes = SchemeMap(void).initComptime(.{ .{"about"}, .{"bitcoin"}, .{"blob"}, .{"data"}, .{"geo"}, .{"magnet"}, .{"mailto"}, .{"news"}, .{"pkcs11"}, .{"sip"}, .{"sips"}, .{"tel"}, .{"urn"},});
/// Whether `scheme` uses an authority (`scheme://host/...`) rather than an/// opaque path (`scheme:body`). Matched case-insensitively. Unknown schemes are/// assumed to use one, which is the overwhelmingly common case.pub fn usesNetloc(scheme: []const u8) bool { return !rootless_schemes.has(scheme);}