fetc h!!!

This commit is contained in:
Jarred Sumner
2021-09-09 05:40:07 -07:00
parent c30ec608b1
commit 8a02ad48a5
34 changed files with 6782 additions and 161 deletions

View File

@@ -20,6 +20,37 @@ pub const URL = struct {
username: string = "",
port_was_automatically_set: bool = false,
pub fn isDomainName(this: *const URL) bool {
for (this.hostname) |c, i| {
switch (c) {
'0'...'9', '.', ':' => {},
else => {
return true;
},
}
}
return false;
}
pub fn isLocalhost(this: *const URL) bool {
return this.hostname.len == 0 or strings.eqlComptime(this.hostname, "localhost") or strings.eqlComptime(this.hostname, "0.0.0.0");
}
pub fn getIPv4Address(this: *const URL) ?std.x.net.ip.Address.IPv4 {
return (if (this.hostname.length > 0)
std.x.os.IPv4.parse(this.hostname)
else
std.x.os.IPv4.parse(this.href)) catch return null;
}
pub fn getIPv6Address(this: *const URL) ?std.x.net.ip.Address.IPv6 {
return (if (this.hostname.length > 0)
std.x.os.IPv6.parse(this.hostname)
else
std.x.os.IPv6.parse(this.href)) catch return null;
}
pub fn displayProtocol(this: *const URL) string {
if (this.protocol.len > 0) {
return this.protocol;
@@ -34,6 +65,10 @@ pub const URL = struct {
return "http";
}
pub inline fn isHTTPS(this: *const URL) bool {
return strings.eqlComptime(this.protocol, "https");
}
pub fn displayHostname(this: *const URL) string {
if (this.hostname.len > 0) {
return this.hostname;
@@ -50,6 +85,10 @@ pub const URL = struct {
return std.fmt.parseInt(u16, this.port, 10) catch null;
}
pub fn getPortAuto(this: *const URL) u16 {
return this.getPort() orelse (if (this.isHTTPS()) @as(u16, 443) else @as(u16, 80));
}
pub fn hasValidPort(this: *const URL) bool {
return (this.getPort() orelse 0) > 1;
}