mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-28 07:33:16 +00:00
Remove duplication in cookies instalation
This commit is contained in:
@@ -1110,19 +1110,8 @@ pub const Transfer = struct {
|
||||
}
|
||||
|
||||
// Add cookies from cookie jar.
|
||||
if (req.cookie_jar) |jar| {
|
||||
const arena = self.arena.allocator();
|
||||
var aw: std.Io.Writer.Allocating = .init(arena);
|
||||
try jar.forRequest(req.url, &aw.writer, .{
|
||||
.is_http = true,
|
||||
.origin_url = req.url,
|
||||
.is_navigation = req.resource_type == .document,
|
||||
});
|
||||
const written = aw.written();
|
||||
if (written.len > 0) {
|
||||
try aw.writer.writeByte(0);
|
||||
try conn.setCookies(@ptrCast(written.ptr));
|
||||
}
|
||||
if (try self.getCookieString()) |cookies| {
|
||||
try conn.setCookies(@ptrCast(cookies.ptr));
|
||||
}
|
||||
|
||||
try conn.setPrivate(self);
|
||||
@@ -1176,6 +1165,20 @@ pub const Transfer = struct {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getCookieString(self: *Transfer) !?[:0]const u8 {
|
||||
const jar = self.req.cookie_jar orelse return null;
|
||||
var aw: std.Io.Writer.Allocating = .init(self.arena.allocator());
|
||||
try jar.forRequest(self.req.url, &aw.writer, .{
|
||||
.is_http = true,
|
||||
.origin_url = self.req.url,
|
||||
.is_navigation = self.req.resource_type == .document,
|
||||
});
|
||||
const written = aw.written();
|
||||
if (written.len == 0) return null;
|
||||
try aw.writer.writeByte(0);
|
||||
return written.ptr[0..written.len :0];
|
||||
}
|
||||
|
||||
pub fn format(self: *Transfer, writer: *std.Io.Writer) !void {
|
||||
const req = self.req;
|
||||
return writer.print("{s} {s}", .{ @tagName(req.method), req.url });
|
||||
|
||||
@@ -351,18 +351,9 @@ pub const TransferAsRequestWriter = struct {
|
||||
try jws.objectField(hdr.name);
|
||||
try jws.write(hdr.value);
|
||||
}
|
||||
if (transfer.req.cookie_jar) |jar| {
|
||||
var aw: std.Io.Writer.Allocating = .init(transfer.arena.allocator());
|
||||
try jar.forRequest(transfer.req.url, &aw.writer, .{
|
||||
.is_http = true,
|
||||
.origin_url = transfer.req.url,
|
||||
.is_navigation = transfer.req.resource_type == .document,
|
||||
});
|
||||
const cookie_str = aw.written();
|
||||
if (cookie_str.len > 0) {
|
||||
try jws.objectField("Cookie");
|
||||
try jws.write(cookie_str);
|
||||
}
|
||||
if (try transfer.getCookieString()) |cookies| {
|
||||
try jws.objectField("Cookie");
|
||||
try jws.write(cookies[0 .. cookies.len - 1]);
|
||||
}
|
||||
try jws.endObject();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user