Skip to content

Commit 41eb040

Browse files
committed
add cookies to XHR requests
1 parent ab97475 commit 41eb040

File tree

6 files changed

+187
-277
lines changed

6 files changed

+187
-277
lines changed

src/browser/browser.zig

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ pub const Page = struct {
373373

374374
var response = try request.sendSync(.{});
375375
const header = response.header;
376-
try self.processHTTPResponse(self.uri, &header);
376+
try self.session.cookie_jar.populateFromResponse(self.uri, &header);
377377

378378
log.info("GET {any} {d}", .{ self.uri, header.status });
379379

@@ -438,7 +438,9 @@ pub const Page = struct {
438438

439439
// replace the user context document with the new one.
440440
try session.env.setUserContext(.{
441+
.uri = self.uri,
441442
.document = html_doc,
443+
.cookie_jar = @ptrCast(&self.session.cookie_jar),
442444
.http_client = @ptrCast(self.session.http_client),
443445
});
444446

@@ -614,14 +616,14 @@ pub const Page = struct {
614616
const u = try std.Uri.resolve_inplace(self.uri, res_src, &b);
615617

616618
var request = try self.newHTTPRequest(.GET, u, .{
617-
.origin = self.uri,
619+
.origin_uri = self.uri,
618620
.navigation = false,
619621
});
620622
defer request.deinit();
621623

622624
var response = try request.sendSync(.{});
623625
var header = response.header;
624-
try self.processHTTPResponse(u, &header);
626+
try self.session.cookie_jar.populateFromResponse(u, &header);
625627

626628
log.info("fetch {any}: {d}", .{ u, header.status });
627629

@@ -650,46 +652,21 @@ pub const Page = struct {
650652
try s.eval(arena, &self.session.env, body);
651653
}
652654

653-
const RequestOpts = struct {
654-
origin: ?std.Uri = null,
655-
navigation: bool = true,
656-
};
657-
fn newHTTPRequest(self: *const Page, method: http.Request.Method, uri: std.Uri, opts: RequestOpts) !http.Request {
655+
fn newHTTPRequest(self: *const Page, method: http.Request.Method, uri: std.Uri, opts: storage.cookie.LookupOpts) !http.Request {
658656
const session = self.session;
659657
var request = try session.http_client.request(method, uri);
660658
errdefer request.deinit();
661659

662-
var cookies = try session.cookie_jar.forRequest(
663-
self.arena,
664-
std.time.timestamp(),
665-
opts.origin,
666-
uri,
667-
opts.navigation,
668-
);
669-
defer cookies.deinit(self.arena);
660+
var arr: std.ArrayListUnmanaged(u8) = .{};
661+
try session.cookie_jar.forRequest(uri, arr.writer(self.arena), opts);
670662

671-
if (cookies.len() > 0) {
672-
var arr: std.ArrayListUnmanaged(u8) = .{};
673-
try cookies.write(arr.writer(self.arena));
663+
if (arr.items.len > 0) {
674664
try request.addHeader("Cookie", arr.items, .{});
675665
}
676666

677667
return request;
678668
}
679669

680-
fn processHTTPResponse(self: *const Page, uri: std.Uri, header: *const http.ResponseHeader) !void {
681-
const session = self.session;
682-
const now = std.time.timestamp();
683-
var it = header.iterate("set-cookie");
684-
while (it.next()) |set_cookie| {
685-
const c = storage.Cookie.parse(self.arena, uri, set_cookie) catch |err| {
686-
log.warn("Couldn't parse cookie '{s}': {}\n", .{ set_cookie, err });
687-
continue;
688-
};
689-
try session.cookie_jar.add(c, now);
690-
}
691-
}
692-
693670
const Script = struct {
694671
element: *parser.Element,
695672
kind: Kind,

src/main_tests.zig

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ const apiweb = @import("apiweb.zig");
2828
const Window = @import("html/window.zig").Window;
2929
const xhr = @import("xhr/xhr.zig");
3030
const storage = @import("storage/storage.zig");
31-
const url = @import("url/url.zig");
32-
const URL = url.URL;
31+
const URL = @import("url/url.zig").URL;
3332
const urlquery = @import("url/query.zig");
3433
const Location = @import("html/location.zig").Location;
3534

@@ -54,7 +53,7 @@ const EventTestExecFn = @import("events/event.zig").testExecFn;
5453
const XHRTestExecFn = xhr.testExecFn;
5554
const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn;
5655
const StorageTestExecFn = storage.testExecFn;
57-
const URLTestExecFn = url.testExecFn;
56+
const URLTestExecFn = @import("url/url.zig").testExecFn;
5857
const HTMLElementTestExecFn = @import("html/elements.zig").testExecFn;
5958
const MutationObserverTestExecFn = @import("dom/mutation_observer.zig").testExecFn;
6059

@@ -91,16 +90,23 @@ fn testExecFn(
9190
var http_client = try @import("http/client.zig").Client.init(alloc, 5);
9291
defer http_client.deinit();
9392

93+
// alias global as self and window
94+
var window = Window.create(null, null);
95+
96+
const url = "https://lightpanda.io/opensource-browser/";
97+
var u = try URL.constructor(alloc, url, null);
98+
defer u.deinit(alloc);
99+
100+
var cookie_jar = storage.CookieJar.init(alloc);
101+
defer cookie_jar.deinit();
102+
94103
try js_env.setUserContext(.{
104+
.uri = try std.Uri.parse(url),
95105
.document = doc,
106+
.cookie_jar = &cookie_jar,
96107
.http_client = &http_client,
97108
});
98109

99-
// alias global as self and window
100-
var window = Window.create(null, null);
101-
102-
var u = try URL.constructor(alloc, "https://lightpanda.io/opensource-browser/", null);
103-
defer u.deinit(alloc);
104110
var location = Location{ .url = &u };
105111
try window.replaceLocation(&location);
106112

0 commit comments

Comments
 (0)