diff options
Diffstat (limited to 'src/url.zig')
-rw-r--r-- | src/url.zig | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/url.zig b/src/url.zig index 605f438ee..dcb1e91cd 100644 --- a/src/url.zig +++ b/src/url.zig @@ -11,6 +11,7 @@ const MutableString = bun.MutableString; const stringZ = bun.stringZ; const default_allocator = bun.default_allocator; const C = bun.C; +const JSC = bun.JSC; // This is close to WHATWG URL, but we don't want the validation errors pub const URL = struct { @@ -35,6 +36,29 @@ pub const URL = struct { username: string = "", port_was_automatically_set: bool = false, + pub fn fromJS(js_value: JSC.JSValue, globalObject: *JSC.JSGlobalObject, allocator: std.mem.Allocator) !URL { + var href = JSC.URL.hrefFromJS(globalObject, js_value); + if (href.tag == .Dead) { + return error.InvalidURL; + } + + return URL.parse(try href.toOwnedSlice(allocator)); + } + + pub fn fromString(allocator: std.mem.Allocator, input: bun.String) !URL { + var href = JSC.URL.hrefFromString(input); + if (href.tag == .Dead) { + return error.InvalidURL; + } + + defer href.deref(); + return URL.parse(try href.toOwnedSlice(allocator)); + } + + pub fn fromUTF8(allocator: std.mem.Allocator, input: []const u8) !URL { + return fromString(allocator, bun.String.fromUTF8(input)); + } + 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"); } @@ -197,8 +221,7 @@ pub const URL = struct { } } - pub fn parse(base_: string) URL { - const base = std.mem.trim(u8, base_, &std.ascii.whitespace); + pub fn parse(base: string) URL { if (base.len == 0) return URL{}; var url = URL{}; url.href = base; @@ -970,7 +993,6 @@ pub const FormData = struct { .Multipart => |boundary| return toJSFromMultipartData(globalThis, input, boundary), } } - const JSC = bun.JSC; pub fn jsFunctionFromMultipartData( globalThis: *JSC.JSGlobalObject, |