Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: request clone now clones headers

Karl Seguin859ad9b15883
diff --git a/src/browser/tests/net/request.html b/src/browser/tests/net/request.html
index 4c3cf53b4..179940d42 100644
--- a/src/browser/tests/net/request.html
+++ b/src/browser/tests/net/request.html
@@ -209,7 +209,56 @@
testing.expectEqual(req1.url, req2.url);
testing.expectEqual(req1.method, req2.method);
testing.expectEqual('value', req2.headers.get('X-Custom'));
+
+ // Each copy owns its headers.
+ req2.headers.set('X-Custom', 'changed');
+ req2.headers.append('X-Other', 'other');
+ testing.expectEqual('value', req1.headers.get('X-Custom'));
+ testing.expectEqual(null, req1.headers.get('X-Other'));
+ }
+
+ {
+ const req1 = new Request('https://example.com/api', { mode: 'no-cors' });
+ const req2 = req1.clone();
+ testing.expectEqual('no-cors', req2.mode);
+ // The no-cors guard carries over: non-safelisted headers are dropped.
+ req2.headers.set('X-Custom', 'value');
+ testing.expectEqual(null, req2.headers.get('X-Custom'));
+ }
+</script>
+
+<script id=request_input_inherits>
+ {
+ const req1 = new Request('https://example.com/api', {
+ mode: 'same-origin',
+ cache: 'no-store',
+ credentials: 'omit',
+ redirect: 'manual',
+ });
+
+ for (const req2 of [new Request(req1), new Request(req1, {}), new Request(req1, { method: 'POST' })]) {
+ testing.expectEqual('same-origin', req2.mode);
+ testing.expectEqual('no-store', req2.cache);
+ testing.expectEqual('omit', req2.credentials);
+ testing.expectEqual('manual', req2.redirect);
+ }
+
+ const req3 = new Request(req1, { mode: 'cors', cache: 'reload', credentials: 'include', redirect: 'error' });
+ testing.expectEqual('cors', req3.mode);
+ testing.expectEqual('reload', req3.cache);
+ testing.expectEqual('include', req3.credentials);
+ testing.expectEqual('error', req3.redirect);
}
+
+ {
+ const req = new Request('https://example.com/api');
+ testing.expectEqual('cors', req.mode);
+ testing.expectEqual('default', req.cache);
+ testing.expectEqual('same-origin', req.credentials);
+ testing.expectEqual('follow', req.redirect);
+ }
+
+ testing.expectError('TypeError', () => new Request('https://example.com/api', { mode: 'navigate' }));
</script>
<script id=body_used type=module>
diff --git a/src/browser/webapi/net/Request.zig b/src/browser/webapi/net/Request.zig
index db2143d1a..a9531ee01 100644
--- a/src/browser/webapi/net/Request.zig
+++ b/src/browser/webapi/net/Request.zig
@@ -17,23 +17,25 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
-
const lp = @import("lightpanda");
const http = @import("../../../network/http.zig");
-const testing = @import("../../../testing.zig");
-const js = @import("../../js/js.zig");
-const Execution = js.Execution;
+
const Page = @import("../../Page.zig");
+const js = @import("../../js/js.zig");
const referrer = @import("../../referrer.zig");
-const AbortSignal = @import("../AbortSignal.zig");
+
+const URL = @import("../URL.zig");
const Blob = @import("../Blob.zig");
+const AbortSignal = @import("../AbortSignal.zig");
const ReadableStream = @import("../streams/ReadableStream.zig");
-const URL = @import("../URL.zig");
+
+const Headers = @import("Headers.zig");
+const FormData = @import("FormData.zig");
const body_init = @import("body_init.zig");
+
+const Execution = js.Execution;
const BodyInit = body_init.BodyInit;
-const FormData = @import("FormData.zig");
-const Headers = @import("Headers.zig");
const Request = @This();
@@ -60,13 +62,13 @@ pub const Input = union(enum) {
pub const InitOpts = struct {
body: ?BodyInit = null,
- cache: Cache = .default,
- credentials: Credentials = .@"same-origin",
+ cache: ?Cache = null,
+ credentials: ?Credentials = null,
headers: ?Headers.InitOpts = null,
method: ?[]const u8 = null,
- mode: Mode = .cors,
+ mode: ?Mode = null,
priority: ?[]const u8 = null,
- redirect: Redirect = .follow,
+ redirect: ?Redirect = null,
referrer: ?[]const u8 = null,
referrerPolicy: ?[]const u8 = null,
signal: ?*AbortSignal = null,
@@ -135,9 +137,27 @@ pub fn init(input: Input, opts_: ?InitOpts, exec: *const Execution) !*Request {
.request => |r| r._method,
};
- const mode = switch (input) {
- .url => opts.mode,
- .request => |r| if (opts_ != null) opts.mode else r._mode,
+ // Absent init members inherit from the input Request.
+ const mode: Mode = if (opts.mode) |m| blk: {
+ if (m == .navigate) {
+ return error.TypeError;
+ }
+ break :blk m;
+ } else switch (input) {
+ .url => .cors,
+ .request => |r| if (r._mode == .navigate and opts_ != null) .@"same-origin" else r._mode,
+ };
+ const cache: Cache = opts.cache orelse switch (input) {
+ .url => .default,
+ .request => |r| r._cache,
+ };
+ const credentials: Credentials = opts.credentials orelse switch (input) {
+ .url => .@"same-origin",
+ .request => |r| r._credentials,
+ };
+ const redirect: Redirect = opts.redirect orelse switch (input) {
+ .url => .follow,
+ .request => |r| r._redirect,
};
const guard = headerGuard(mode);
@@ -212,9 +232,9 @@ pub fn init(input: Input, opts_: ?InitOpts, exec: *const Execution) !*Request {
._arena = arena,
._method = method,
._headers = headers,
- ._cache = opts.cache,
- ._credentials = opts.credentials,
- ._redirect = opts.redirect,
+ ._cache = cache,
+ ._credentials = credentials,
+ ._redirect = redirect,
._mode = mode,
._body = body,
._body_stream = body_stream,
@@ -400,7 +420,7 @@ pub fn clone(self: *Request, exec: *const Execution) !*Request {
._url = try arena.dupeZ(u8, self._url),
._arena = arena,
._method = self._method,
- ._headers = self._headers,
+ ._headers = if (self._headers) |h| try Headers.initGuarded(.{ .obj = h }, h._guard, exec) else null,
._cache = self._cache,
._credentials = self._credentials,
._redirect = self._redirect,
@@ -469,6 +489,7 @@ pub const JsApi = struct {
pub const referrerPolicy = bridge.accessor(Request.getReferrerPolicy, null, .{});
};
+const testing = @import("../../../testing.zig");
test "WebApi: Request" {
try testing.htmlRunner("net/request.html", .{});
}