Lightpanda: the headless browser designed for AI and automation
add request mode for Fetch
Muki Kiboigod3c0291bd1d2
diff --git a/src/browser/webapi/net/Fetch.zig b/src/browser/webapi/net/Fetch.zigindex c198d68ac..74f50bac3 100644--- a/src/browser/webapi/net/Fetch.zig+++ b/src/browser/webapi/net/Fetch.zig@@ -43,6 +43,7 @@ _resolver: js.PromiseResolver.Global,_owns_response: bool,_signal: ?*AbortSignal,_manual_redirect: bool,+_no_cors: bool,pub const Input = Request.Input;pub const InitOpts = Request.InitOpts;@@ -81,6 +82,7 @@ pub fn init(input: Input, options: ?InitOpts, exec: *const Execution) !js.Promis._owns_response = true,._signal = request._signal,._manual_redirect = request._redirect == .manual,+ ._no_cors = request._mode == .@"no-cors",};if (comptime lp.IS_DEBUG) {@@ -103,6 +105,12 @@ pub fn init(input: Input, options: ?InitOpts, exec: *const Execution) !js.Promis.@"same-origin" => .same_origin,.include => .include,},+ .request_mode = switch (request._mode) {+ .cors => .cors,+ .@"no-cors" => .no_cors,+ .@"same-origin" => .same_origin,+ .navigate => .navigate,+ },.origin = exec.origin(),.redirect = switch (request._redirect) {.follow => .follow,@@ -168,6 +176,17 @@ fn httpHeaderDoneCallback(transfer: *Transfer) !Transfer.HeaderResult {res._url = try arena.dupeZ(u8, transfer.req.url);res._is_redirected = transfer.redirectCount().? > 0;+ // no-cors mode: regardless of what the server returned, JS only ever sees+ // an opaque response — status 0, no headers, no body, url "".+ if (self._no_cors) {+ res._status = 0;+ res._status_text = "";+ res._url = "";+ res._type = .@"opaque";+ res._is_redirected = false;+ return .proceed;+ }+// redirect: "manual" surfaces the unfollowed 3xx as an opaque-redirect// filtered response: status 0, no headers, no body.if (self._manual_redirect and HttpClient.isRedirectStatus(res._status)) {@@ -216,6 +235,10 @@ fn httpDataCallback(transfer: *Transfer, data: []const u8) !void {}}+ if (self._no_cors) {+ return;+ }+try self._buf.appendSlice(self._response._arena.allocator(), data);}diff --git a/src/browser/webapi/net/Request.zig b/src/browser/webapi/net/Request.zigindex 18e8d1ff3..115fdcffa 100644--- a/src/browser/webapi/net/Request.zig+++ b/src/browser/webapi/net/Request.zig@@ -46,6 +46,7 @@ _arena: *lp.Arena,_cache: Cache,_credentials: Credentials,_redirect: Redirect,+_mode: Mode,_signal: ?*AbortSignal,_body_used: bool = false,@@ -62,6 +63,7 @@ pub const InitOpts = struct {method: ?[]const u8 = null,priority: ?[]const u8 = null,redirect: Redirect = .follow,+ mode: Mode = .cors,signal: ?*AbortSignal = null,};@@ -91,6 +93,14 @@ const Cache = enum {pub const js_enum_from_string = true;};+const Mode = enum {+ cors,+ @"no-cors",+ @"same-origin",+ navigate,+ pub const js_enum_from_string = true;+};+pub fn init(input: Input, opts_: ?InitOpts, exec: *const Execution) !*Request {const arena = try exec.getPinnedArena(.medium, "Request");errdefer arena.release();@@ -148,6 +158,11 @@ pub fn init(input: Input, opts_: ?InitOpts, exec: *const Execution) !*Request {.request => |r| r._signal,};+ const mode = switch (input) {+ .url => opts.mode,+ .request => |r| if (opts_ != null) opts.mode else r._mode,+ };+const self = try arena.create(Request);self.* = .{._url = url,@@ -157,6 +172,7 @@ pub fn init(input: Input, opts_: ?InitOpts, exec: *const Execution) !*Request {._cache = opts.cache,._credentials = opts.credentials,._redirect = opts.redirect,+ ._mode = mode,._body = body,._signal = signal,};@@ -216,6 +232,10 @@ pub fn getRedirect(self: *const Request) []const u8 {return @tagName(self._redirect);}+pub fn getMode(self: *const Request) []const u8 {+ return @tagName(self._mode);+}+pub fn getSignal(self: *const Request) ?*AbortSignal {return self._signal;}@@ -356,6 +376,7 @@ pub fn clone(self: *const Request, exec: *const Execution) !*Request {._cache = self._cache,._credentials = self._credentials,._redirect = self._redirect,+ ._mode = self._mode,._body = if (self._body) |b| try arena.dupe(u8, b) else null,._signal = self._signal,};@@ -379,6 +400,7 @@ pub const JsApi = struct {pub const cache = bridge.accessor(Request.getCache, null, .{});pub const credentials = bridge.accessor(Request.getCredentials, null, .{});pub const redirect = bridge.accessor(Request.getRedirect, null, .{});+ pub const mode = bridge.accessor(Request.getMode, null, .{});pub const signal = bridge.accessor(Request.getSignal, null, .{});pub const bodyUsed = bridge.accessor(Request.getBodyUsed, null, .{});pub const blob = bridge.function(Request.blob, .{});diff --git a/src/network/CorsGate.zig b/src/network/CorsGate.zigindex 38fab7f57..59e4af9e0 100644--- a/src/network/CorsGate.zig+++ b/src/network/CorsGate.zig@@ -127,6 +127,15 @@ pub fn check(self: *CorsGate, transfer: *Transfer) !Result {const origin = req.origin orelse "null";try transfer.setHeader(ORIGIN, origin, .{});+ if (req.request_mode == .no_cors) {+ log.debug(.cors, "cross origin", .{+ .url = req.url,+ .origin = origin,+ .mode = "no-cors",+ });+ return .allowed;+ }+if (!requiresPreflight(transfer)) {log.debug(.cors, "cross origin", .{.url = req.url,diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zigindex 8a2fc2eab..f999278c3 100644--- a/src/network/HttpClient.zig+++ b/src/network/HttpClient.zig@@ -417,6 +417,15 @@ fn isHostAdblocked(self: *const Client, url: [:0]const u8) bool {return blocker.matchHostname(hostname) == .blocked;}+fn isCrossOriginModeAllowed(transfer: *const Transfer) bool {+ const req = &transfer.req;+ if (req.request_mode != .same_origin) {+ return true;+ }+ const origin = req.origin orelse return false;+ return URL.isSameOrigin(req.url, origin);+}+pub fn getUserAgent(self: *const Client) [:0]const u8 {return self.user_agent_override orelse self.network.config.http_headers.user_agent;}@@ -1025,6 +1034,15 @@ fn pipeline(self: *Client, transfer: *Transfer, from: SubmitFrom) !void {if (try self.cacheLookup(transfer)) {return;}++ if (!isCrossOriginModeAllowed(transfer) and !transfer.req.internal) {+ log.warn(.http, "blocked by mode", .{+ .url = transfer.req.url,+ .mode = @tagName(transfer.req.request_mode),+ });+ return transfer.failAsync(error.ModeBlocked);+ }+if (self.obey_cors and !transfer.req.internal) {switch (try self.cors.check(transfer)) {.allowed => {},@@ -1817,6 +1835,13 @@ pub const Request = struct {include,};+ pub const RequestMode = enum {+ cors,+ no_cors,+ same_origin,+ navigate,+ };+method: Method,url: [:0]const u8,body: ?[]const u8 = null,@@ -1825,6 +1850,7 @@ pub const Request = struct {referrer_policy: ?referrer.Policy = null,basic_auth_credentials: ?[:0]const u8 = null,credentials_mode: CredentialsMode = .same_origin,+ request_mode: RequestMode = .cors,timeout_ms: u32 = 0,skip_cache: bool = false,