Skip to content

Lightpanda: the headless browser designed for AI and automation

add origin field to HttpClient Request

Muki Kiboigob114382c4452
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index dff8f8bd5..b916acb33 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -857,7 +857,7 @@ pub fn navigate(self: *Frame, request_url: [:0]const u8, opts: NavigateOpts) !vo
// do, they probably don't want the cached version.
.skip_cache = self.parent == null,
.throttle = self.parent == null,
- .cookie_origin = opts.initiator_url,
+ .origin = self.origin,
.resource_type = .document,
.header_callback = frameHeaderDoneCallback,
.data_callback = frameDataCallback,
@@ -2382,6 +2382,7 @@ pub fn loadExternalStylesheet(self: *Frame, link: *Element.Html.Link, href: []co
const transfer = http_client.newRequest(.{
.url = resolved,
.method = .GET,
+ .origin = self.origin,
.resource_type = .stylesheet,
.shutdown_callback = HttpClient.noopShutdown, // syncRequest installs its own
}, &self._http_owner) catch |err| {
diff --git a/src/browser/ScriptManager.zig b/src/browser/ScriptManager.zig
index bb0d9db08..7abcbf0d9 100644
--- a/src/browser/ScriptManager.zig
+++ b/src/browser/ScriptManager.zig
@@ -134,6 +134,7 @@ pub fn preloadScript(self: *ScriptManager, element: ?*Element.Html, url: []const
.ctx = script,
.url = owned_url,
.method = .GET,
+ .origin = frame.origin,
.resource_type = .script,
.start_callback = if (log.enabled(.http, .debug)) Script.startCallback else null,
.header_callback = Script.headerCallback,
@@ -345,6 +346,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
const transfer = try self.base.client.newRequest(.{
.url = remote_url,
.method = .GET,
+ .origin = frame.origin,
.resource_type = .script,
.shutdown_callback = HttpClient.noopShutdown, // syncRequest installs its own
}, &frame._http_owner);
@@ -389,6 +391,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
.ctx = script,
.url = remote_url,
.method = .GET,
+ .origin = frame.origin,
.resource_type = .script,
.start_callback = if (log.enabled(.http, .debug)) Script.startCallback else null,
.header_callback = Script.headerCallback,
diff --git a/src/browser/webapi/SharedWorkerGlobalScope.zig b/src/browser/webapi/SharedWorkerGlobalScope.zig
index 84b5b9039..1e640c9b5 100644
--- a/src/browser/webapi/SharedWorkerGlobalScope.zig
+++ b/src/browser/webapi/SharedWorkerGlobalScope.zig
@@ -105,6 +105,7 @@ pub fn init(frame: *Frame, url: [:0]const u8, name: []const u8, worker_type: Wor
.method = .GET,
.url = owned_url,
.resource_type = .worker,
+ .origin = frame.origin,
.header_callback = httpHeaderCallback,
.data_callback = httpDataCallback,
.done_callback = httpDoneCallback,
diff --git a/src/browser/webapi/Worker.zig b/src/browser/webapi/Worker.zig
index 7b6751250..e7c0e3421 100644
--- a/src/browser/webapi/Worker.zig
+++ b/src/browser/webapi/Worker.zig
@@ -106,6 +106,7 @@ pub fn init(url: []const u8, options: ?WorkerOptions, frame: *Frame) !*Worker {
.frame_id = self._frame_id,
.loader_id = self._loader_id,
.resource_type = if (self._type == .module) .script else .worker,
+ .origin = frame.origin,
.header_callback = httpHeaderCallback,
.data_callback = httpDataCallback,
.done_callback = httpDoneCallback,
diff --git a/src/browser/webapi/WorkerGlobalScope.zig b/src/browser/webapi/WorkerGlobalScope.zig
index 75c2d0e62..d2199aa40 100644
--- a/src/browser/webapi/WorkerGlobalScope.zig
+++ b/src/browser/webapi/WorkerGlobalScope.zig
@@ -430,6 +430,7 @@ fn importScript(self: *WorkerGlobalScope, arena: Allocator, url: [:0]const u8) !
.url = resolved_url,
.method = .GET,
.resource_type = .worker,
+ .origin = self.origin,
.shutdown_callback = HttpClient.noopShutdown, // syncRequest installs its own
}, &self._http_owner) catch |err| {
log.warn(.http, "importScript", .{ .url = resolved_url, .err = err });
diff --git a/src/browser/webapi/net/EventSource.zig b/src/browser/webapi/net/EventSource.zig
index 93534a2d1..a3ac7319b 100644
--- a/src/browser/webapi/net/EventSource.zig
+++ b/src/browser/webapi/net/EventSource.zig
@@ -179,6 +179,7 @@ fn connect(self: *EventSource) !void {
.url = self._url,
.method = .GET,
.cookies = cookie_support,
+ .origin = exec.origin(),
.resource_type = .eventsource,
.streaming = true,
.header_callback = httpHeaderDoneCallback,
diff --git a/src/browser/webapi/net/Fetch.zig b/src/browser/webapi/net/Fetch.zig
index e815214b1..36a12a62c 100644
--- a/src/browser/webapi/net/Fetch.zig
+++ b/src/browser/webapi/net/Fetch.zig
@@ -98,6 +98,7 @@ pub fn init(input: Input, options: ?InitOpts, exec: *const Execution) !js.Promis
.include => true,
.@"same-origin" => exec.isSameOrigin(request._url),
},
+ .origin = exec.origin(),
.redirect = switch (request._redirect) {
.follow => .follow,
.manual => .manual,
diff --git a/src/browser/webapi/net/XMLHttpRequest.zig b/src/browser/webapi/net/XMLHttpRequest.zig
index de328b8ea..21b7f825d 100644
--- a/src/browser/webapi/net/XMLHttpRequest.zig
+++ b/src/browser/webapi/net/XMLHttpRequest.zig
@@ -316,6 +316,7 @@ pub fn send(self: *XMLHttpRequest, body_: ?BodyInit, exec_: *const Execution) !v
.method = self._method,
.body = self._request_body,
.cookies = cookie_support,
+ .origin = exec.origin(),
.resource_type = .xhr,
.timeout_ms = self._timeout,
.header_callback = httpHeaderDoneCallback,
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index 902994691..936a6f4f1 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -626,10 +626,14 @@ pub fn newRequest(self: *Client, req: Request, owner: ?*Owner) anyerror!*Transfe
.url => |url| .{ .url = try arena.dupeZ(u8, url) },
};
owned.cookie_origin = null;
+
if (req.credentials) |c| {
owned.credentials = try arena.dupeZ(u8, c);
}
+ const raw_origin: ?[]const u8 = req.origin orelse if (owner) |o| o.origin.* else null;
+ owned.origin = if (raw_origin) |origin| try arena.dupe(u8, origin) else null;
+
// The body can be larger, so callers can signal, via the
// `body_outlives_request` flag that they guarantee that the body
// will outlive the transfer (and thus doesn't need to be duped)
@@ -1808,6 +1812,9 @@ pub const Request = struct {
// initiator of a top-level navigation isn't the frame being navigated.
cookie_origin: ?Cookie.SiteForCookies = null,
+ // The Origin of the Request.
+ origin: ?[]const u8,
+
// Requests that are internal to the browser and skip various layers,
// these do not need to be deferred and do not obey robots.txt.
internal: bool = false,
@@ -4143,6 +4150,7 @@ fn testTransfer(arena: *lp.Arena) Transfer {
.req = .{
.method = .GET,
.url = "http://example.com/",
+ .origin = "",
.resource_type = .document,
.shutdown_callback = noopShutdown,
},
@@ -4371,6 +4379,7 @@ test "HttpClient: fulfillIntercepted survives a done_callback that tears down th
.req = .{
.method = .GET,
.url = "http://example.com/",
+ .origin = "",
.resource_type = .document,
.shutdown_callback = noopShutdown,
.ctx = &ctx,
@@ -4588,6 +4597,7 @@ test "HttpClient: aborting a robots-parked transfer unlinks it from the gate" {
.req = .{
.method = .GET,
.url = "http://example.com/",
+ .origin = "",
.resource_type = .document,
.shutdown_callback = noopShutdown,
},
@@ -4651,6 +4661,7 @@ test "HttpClient: fulfillIntercepted follows a 3xx redirect" {
.method = .POST,
.url = "http://example.com/start",
.body = "payload",
+ .origin = "",
.resource_type = .document,
.shutdown_callback = noopShutdown,
.ctx = undefined,
@@ -4690,6 +4701,7 @@ test "HttpClient: fulfillIntercepted follows a 3xx redirect" {
.method = .POST,
.url = "http://example.com/start",
.body = "payload",
+ .origin = "",
.resource_type = .document,
.shutdown_callback = noopShutdown,
.ctx = undefined,
@@ -4753,6 +4765,7 @@ test "HttpClient: fulfillIntercepted delivers a 3xx without a Location as the re
.req = .{
.method = .GET,
.url = "http://example.com/",
+ .origin = "",
.resource_type = .document,
.shutdown_callback = noopShutdown,
.ctx = &ctx,
@@ -4816,6 +4829,7 @@ test "HttpClient: abortParked survives an error_callback that tears down the own
.req = .{
.method = .GET,
.url = "http://example.com/",
+ .origin = "",
.resource_type = .document,
.shutdown_callback = noopShutdown,
.ctx = &ctx,
@@ -4889,6 +4903,7 @@ test "HttpClient: abort survives an error_callback that tears down the owner" {
.req = .{
.method = .GET,
.url = "http://example.com/",
+ .origin = "",
.resource_type = .xhr,
.shutdown_callback = noopShutdown,
.ctx = &ctx,
@@ -4921,6 +4936,7 @@ test "HttpClient: abort survives an error_callback that tears down the owner" {
.req = .{
.method = .GET,
.url = "http://example.com/",
+ .origin = "",
.resource_type = .xhr,
.shutdown_callback = noopShutdown,
.ctx = &ctx,
diff --git a/src/network/RobotsGate.zig b/src/network/RobotsGate.zig
index 19a737ad9..3ff2e1e23 100644
--- a/src/network/RobotsGate.zig
+++ b/src/network/RobotsGate.zig
@@ -116,6 +116,7 @@ fn fetchThenResume(self: *RobotsGate, robots_url: [:0]const u8, transfer: *Trans
.document_frame_id = transfer.req.document_frame_id,
.loader_id = transfer.req.loader_id,
.notification = transfer.req.notification,
+ .origin = null,
.ctx = robots_ctx,
.header_callback = RobotsContext.headerCallback,
.data_callback = RobotsContext.dataCallback,