Lightpanda: the headless browser designed for AI and automation
webapi: let rust-url fully handle ports
Karl Seguin43937dd8dd4c
diff --git a/src/browser/URL.zig b/src/browser/URL.zigindex c80611df1..182731a9e 100644--- a/src/browser/URL.zig+++ b/src/browser/URL.zig@@ -445,9 +445,7 @@ fn applySetter(current: [:0]const u8, allocator: Allocator, setter: anytype, arg}pub fn setProtocol(current: [:0]const u8, value: []const u8, allocator: Allocator) ![:0]const u8 {- // The scheme ends at the first ':'- const scheme = value[0 .. std.mem.findScalar(u8, value, ':') orelse value.len];- return applySetter(current, allocator, U.url_set_scheme, .{ scheme.ptr, scheme.len });+ return applySetter(current, allocator, U.url_set_scheme, .{ value.ptr, value.len });}pub fn setHost(current: [:0]const u8, value: []const u8, allocator: Allocator) ![:0]const u8 {@@ -458,24 +456,8 @@ pub fn setHostname(current: [:0]const u8, value: []const u8, allocator: Allocatoreturn applySetter(current, allocator, U.url_set_hostname, .{ value.ptr, value.len });}-pub fn setPort(current: [:0]const u8, value: ?[]const u8, allocator: Allocator) ![:0]const u8 {- const port_str = value orelse "";- if (port_str.len == 0) {- return applySetter(current, allocator, U.url_set_port_to_null, .{});- }- // Leading digits only ("8080abc" is 8080), after the URL parser's tab and- // newline removal; no digits or > 65535 is ignored.- var port: u32 = 0;- var digits: usize = 0;- for (port_str) |c| {- if (c == '\t' or c == '\n' or c == '\r') continue;- if (!std.ascii.isDigit(c)) break;- port = port * 10 + (c - '0');- if (port > std.math.maxInt(u16)) return current;- digits += 1;- }- if (digits == 0) return current;- return applySetter(current, allocator, U.url_set_port, .{@as(u16, @intCast(port))});+pub fn setPort(current: [:0]const u8, value: []const u8, allocator: Allocator) ![:0]const u8 {+ return applySetter(current, allocator, U.url_set_port, .{ value.ptr, value.len });}pub fn setPathname(current: [:0]const u8, value: []const u8, allocator: Allocator) ![:0]const u8 {diff --git a/src/browser/tests/element/html/anchor.html b/src/browser/tests/element/html/anchor.htmlindex 29028a1ef..ba49965fe 100644--- a/src/browser/tests/element/html/anchor.html+++ b/src/browser/tests/element/html/anchor.html@@ -124,8 +124,11 @@testing.expectEqual('foo.bar', link.hostname);testing.expectEqual('https://foo.bar/?q=bar#frag', link.href);+ // null is stringified to "null", which has no digits and is ignored.+ link.port = '8080';link.port = null;- testing.expectEqual('', link.port);+ testing.expectEqual('8080', link.port);+ link.port = '';testing.expectEqual('https://foo.bar/?q=bar#frag', link.href);link.href = 'foo';@@ -348,6 +351,11 @@a.port = '0443';testing.expectEqual('https://h.com/p', a.href);+ a.host = 'h.com:8080abc';+ testing.expectEqual('https://h.com:8080/p', a.href);+ a.host = 'g.com:abc';+ testing.expectEqual('https://g.com:8080/p', a.href);+a.href = 'https://h.com/p';a.hostname = '';testing.expectEqual('https://h.com/p', a.href);@@ -357,5 +365,7 @@testing.expectEqual('https://h.com/p', a.href);a.protocol = 'ftp:';testing.expectEqual('ftp://h.com/p', a.href);+ a.protocol = 'http:x';+ testing.expectEqual('http://h.com/p', a.href);}</script>diff --git a/src/browser/tests/url.html b/src/browser/tests/url.htmlindex bcab61e4a..e6bdd103d 100644--- a/src/browser/tests/url.html+++ b/src/browser/tests/url.html@@ -647,6 +647,20 @@url.host = '[::1.2.3.]'; // broken IPv6 literaltesting.expectEqual('example.com', url.host);}++ // The port is its leading digits; a bad port still sets the host and leaves+ // the existing port alone.+ {+ const url = new URL('https://example.com:81/path');+ url.host = 'a.com:8080abc';+ testing.expectEqual('https://a.com:8080/path', url.href);+ url.host = 'b.com:abc';+ testing.expectEqual('https://b.com:8080/path', url.href);+ url.host = 'c.com:65536';+ testing.expectEqual('https://c.com:8080/path', url.href);+ url.host = 'd.com:\t90\n';+ testing.expectEqual('https://d.com:90/path', url.href);+ }</script><script id=port>@@ -707,6 +721,37 @@url.port = '65535';testing.expectEqual('65535', url.port);}++ // The port is its leading digits, after tab and newline removal.+ {+ const url = new URL('https://example.com/path');+ url.port = '8080abc';+ testing.expectEqual('8080', url.port);+ url.port = '\t81\n';+ testing.expectEqual('81', url.port);+ url.port = '+82';+ testing.expectEqual('81', url.port);+ url.port = ' 82';+ testing.expectEqual('81', url.port);+ url.port = '\t';+ testing.expectEqual('81', url.port);+ url.port = '0443';+ testing.expectEqual('', url.port);+ }++ // null is stringified to "null", which has no digits and is ignored.+ {+ const url = new URL('https://example.com:8080/path');+ url.port = null;+ testing.expectEqual('8080', url.port);+ }++ // A file URL can't have a port.+ {+ const url = new URL('file:///tmp/x');+ url.port = '8080';+ testing.expectEqual('file:///tmp/x', url.href);+ }</script><script id=protocol>@@ -750,6 +795,13 @@testing.expectEqual('http:', url.protocol);testing.expectEqual('http://example.com/path', url.href);}++ // The scheme ends at the first ':'.+ {+ const url = new URL('https://example.com/path');+ url.protocol = 'ftp:x';+ testing.expectEqual('ftp://example.com/path', url.href);+ }</script><script id=pathname>diff --git a/src/browser/webapi/URL.zig b/src/browser/webapi/URL.zigindex 485a61ca6..4a45c5a76 100644--- a/src/browser/webapi/URL.zig+++ b/src/browser/webapi/URL.zig@@ -157,21 +157,8 @@ pub fn getPort(self: *URL) []const u8 {return std.mem.print(&self._port, "{d}", .{port}) catch unreachable;}-/// Spec requires us to silently ignore errors of this setter.-fn setPort(self: *URL, maybe_value: ?[]const u8) void {- // A null or empty value clears the port.- const value = maybe_value orelse {- _ = U.url_set_port_to_null(self._url);- return;- };- if (value.len == 0) {- _ = U.url_set_port_to_null(self._url);- return;- }-- // Ignore invalid port numbers, leaving the port unchanged.- const port = std.fmt.parseInt(u16, value, 10) catch return;- _ = U.url_set_port(self._url, port);+fn setPort(self: *URL, value: []const u8) void {+ _ = U.url_set_port(self._url, value.ptr, value.len);}// searchParam pushes its mutations to URL, so self._url is always in syncdiff --git a/src/browser/webapi/element/html/Anchor.zig b/src/browser/webapi/element/html/Anchor.zigindex bcdb1c1bb..200531e52 100644--- a/src/browser/webapi/element/html/Anchor.zig+++ b/src/browser/webapi/element/html/Anchor.zig@@ -117,7 +117,7 @@ pub fn getPort(self: *Anchor, frame: *Frame) ![]const u8 {return port;}-fn setPort(self: *Anchor, value: ?[]const u8, frame: *Frame) !void {+fn setPort(self: *Anchor, value: []const u8, frame: *Frame) !void {const href = try getResolvedHref(self, frame) orelse return;const new_href = try URL.setPort(href, value, frame.call_arena);try setHref(self, new_href, frame);diff --git a/src/browser/webapi/element/html/Area.zig b/src/browser/webapi/element/html/Area.zigindex e7d0a3394..5a9f99445 100644--- a/src/browser/webapi/element/html/Area.zig+++ b/src/browser/webapi/element/html/Area.zig@@ -140,7 +140,7 @@ pub fn getPort(self: *Area, frame: *Frame) ![]const u8 {return port;}-fn setPort(self: *Area, value: ?[]const u8, frame: *Frame) !void {+fn setPort(self: *Area, value: []const u8, frame: *Frame) !void {const href = try getResolvedHref(self, frame) orelse return;const new_href = try URL.setPort(href, value, frame.call_arena);try setHref(self, new_href, frame);diff --git a/src/rust/html5ever/url.rs b/src/rust/html5ever/url.rsindex 59a1d736a..540e0997c 100644--- a/src/rust/html5ever/url.rs+++ b/src/rust/html5ever/url.rs@@ -361,19 +361,21 @@ pub unsafe extern "C" fn url_get_origin(url: *const Url) -> OwnedString {OwnedString { ptr, len }}+/// WHATWG `port` setter: "" clears the port, otherwise the leading digits are+/// the port ("8080abc" is 8080). No digits or > 65535 leaves the url untouched.#[no_mangle]-pub unsafe extern "C" fn url_set_port(url: *mut Url, port: u16) -> i32 {+pub unsafe extern "C" fn url_set_port(url: *mut Url, ptr: *const c_uchar, len: usize) -> i32 {let url = unsafe { &mut *url };- match url.set_port(Some(port)) {- Ok(()) => 0,- Err(()) => -1,+ let input = match str_from(ptr, len) {+ Some(s) => s,+ None => return -1,+ };+ // Only "" clears. quirks would also clear on a value that is all tabs and+ // newlines, which the spec parses (stripped) to no digits = failure.+ if !input.is_empty() && input.chars().all(|c| matches!(c, '\t' | '\n' | '\r')) {+ return -1;}-}--#[no_mangle]-pub unsafe extern "C" fn url_set_port_to_null(url: *mut Url) -> i32 {- let url = unsafe { &mut *url };- match url.set_port(None) {+ match ::url::quirks::set_port(url, input) {Ok(()) => 0,Err(()) => -1,}@@ -472,8 +474,9 @@ pub unsafe extern "C" fn url_get_host(0}-/// rust-url's `set_host` discards any trailing `:port`, so we split it off-/// ourselves (respecting IPv6 `[...]` literals) and apply the port separately.+/// WHATWG `host` setter. A bad port ("h.com:abc", "h.com:65536") still sets+/// the host and leaves the port as it was; trailing junk after the port digits+/// is ignored ("h.com:8080abc" is :8080).#[no_mangle]pub unsafe extern "C" fn url_set_host(url: *mut Url, ptr: *const c_uchar, len: usize) -> i32 {let url = unsafe { &mut *url };@@ -481,64 +484,13 @@ pub unsafe extern "C" fn url_set_host(url: *mut Url, ptr: *const c_uchar, len: uSome(s) => s,None => return -1,};-- // The WHATWG host setter parses in "host state", which stops at the first- // '/', '?', '#' (and '\' for special schemes); the remainder is ignored- // rather than making the whole value invalid.- let special = matches!(- url.scheme(),- "http" | "https" | "ws" | "wss" | "ftp" | "file"- );- let end = input- .find(|c| c == '/' || c == '?' || c == '#' || (special && c == '\\'))- .unwrap_or(input.len());- let input = &input[..end];-- // Find the port separator ':', but only outside an IPv6 [...] literal.- let colon = if input.starts_with('[') {- input- .find(']')- .and_then(|b| input[b..].find(':').map(|i| b + i))- } else {- input.find(':')- };-- let Some(i) = colon else {- // No port given: set the host only, leaving any existing port untouched.- return if url.set_host(Some(input)).is_ok() {- 0- } else {- -1- };- };-- let (host, port_str) = (&input[..i], &input[i + 1..]);-- // A trailing colon with no digits ("host:") supplies an empty port, which- // the WHATWG host setter ignores: set the host only, leaving any existing- // port untouched (matches Chrome and Firefox).- if port_str.is_empty() {- return if url.set_host(Some(host)).is_ok() {- 0- } else {- -1- };- }-- // Validate the port up-front so we never apply the host and then fail.- let new_port: u16 = match port_str.parse::<u16>() {- Ok(p) => p,- Err(_) => return -1,- };-- if url.set_host(Some(host)).is_err() {- return -1;+ match ::url::quirks::set_host(url, input) {+ Ok(()) => 0,+ Err(()) => -1,}- // set_port only errors on cannot-be-a-base, already ruled out by set_host.- let _ = url.set_port(Some(new_port));- 0}+/// WHATWG `protocol` setter: the scheme ends at the first ':' ("ftp:x" is ftp).#[no_mangle]pub unsafe extern "C" fn url_set_scheme(url: *mut Url, ptr: *const c_uchar, len: usize) -> i32 {let url = unsafe { &mut *url };@@ -547,7 +499,7 @@ pub unsafe extern "C" fn url_set_scheme(url: *mut Url, ptr: *const c_uchar, len:None => return -1,};- match url.set_scheme(slice) {+ match ::url::quirks::set_protocol(url, slice) {Ok(()) => 0,Err(()) => -1,}diff --git a/src/sys/url.zig b/src/sys/url.zigindex 2126a227a..eb44ee5ba 100644--- a/src/sys/url.zig+++ b/src/sys/url.zig@@ -63,9 +63,8 @@ pub extern "c" fn url_get_host(url: *const Url, out_ptr: *[*]const u8, out_len:/// This function allocates on Rust-side.pub extern "c" fn url_get_origin(url: *const Url) OwnedString;pub extern "c" fn free_owned_string(owned: OwnedString) void;-pub extern "c" fn url_set_port(url: *Url, port: u16) i32;-/// Sets the port to null.-pub extern "c" fn url_set_port_to_null(url: *Url) i32;+/// WHATWG port setter: "" clears the port, otherwise the leading digits.+pub extern "c" fn url_set_port(url: *Url, ptr: [*]const u8, len: usize) i32;pub extern "c" fn url_set_username(url: *Url, ptr: [*]const u8, len: usize) i32;pub extern "c" fn url_get_username(url: *const Url, out_ptr: *[*]const u8, out_len: *usize) void;pub extern "c" fn url_set_password(url: *Url, ptr: [*]const u8, len: usize) i32;