From 331d8297a6c092ba133030c5a63265dab535e996 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Wed, 4 Feb 2026 18:11:25 -0800 Subject: [PATCH] add URL.portFancy() --- url.zig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/url.zig b/url.zig index ef1886b5ed014101bcad927e1c984e1ceba6d1af..9e4ca1250c282278a4a79ecc0d187ac2b7c52967 100644 --- a/url.zig +++ b/url.zig @@ -960,6 +960,18 @@ pub const URL = struct { .ipv6 => .{ .ipv6 = parseIPv6(u.hostname[1 .. u.hostname.len - 1]) catch unreachable }, }; } + + pub fn portFancy(u: *const URL) ?u16 { + if (u.port.len > 0) return std.fmt.parseUnsigned(u16, u.port, 10) catch unreachable; + const s = u.scheme(); + if (std.mem.eql(u8, s, "ftp")) return 21; + if (std.mem.eql(u8, s, "file")) return null; + if (std.mem.eql(u8, s, "http")) return 80; + if (std.mem.eql(u8, s, "https")) return 443; + if (std.mem.eql(u8, s, "ws")) return 80; + if (std.mem.eql(u8, s, "wss")) return 443; + return null; + } }; /// https://url.spec.whatwg.org/#special-scheme -- 2.54.0