authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-04 18:11:25 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-04 18:11:25 -08:00
log331d8297a6c092ba133030c5a63265dab535e996
tree906fe9b70a23abf0d87420e824420097e1d98c5a
parent0b13565e2f8e96e594f26885cc9bfc92162b1a0f

add URL.portFancy()


1 files changed, 12 insertions(+), 0 deletions(-)

url.zig+12
...@@ -960,6 +960,18 @@ pub const URL = struct {...@@ -960,6 +960,18 @@ pub const URL = struct {
960 .ipv6 => .{ .ipv6 = parseIPv6(u.hostname[1 .. u.hostname.len - 1]) catch unreachable },960 .ipv6 => .{ .ipv6 = parseIPv6(u.hostname[1 .. u.hostname.len - 1]) catch unreachable },
961 };961 };
962 }962 }
963
964 pub fn portFancy(u: *const URL) ?u16 {
965 if (u.port.len > 0) return std.fmt.parseUnsigned(u16, u.port, 10) catch unreachable;
966 const s = u.scheme();
967 if (std.mem.eql(u8, s, "ftp")) return 21;
968 if (std.mem.eql(u8, s, "file")) return null;
969 if (std.mem.eql(u8, s, "http")) return 80;
970 if (std.mem.eql(u8, s, "https")) return 443;
971 if (std.mem.eql(u8, s, "ws")) return 80;
972 if (std.mem.eql(u8, s, "wss")) return 443;
973 return null;
974 }
963};975};
964976
965/// https://url.spec.whatwg.org/#special-scheme977/// https://url.spec.whatwg.org/#special-scheme