mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 16:28:58 +00:00
setCookie
This commit is contained in:
@@ -29,6 +29,7 @@ pub fn processMessage(cmd: anytype) !void {
|
|||||||
setCacheDisabled,
|
setCacheDisabled,
|
||||||
setExtraHTTPHeaders,
|
setExtraHTTPHeaders,
|
||||||
deleteCookies,
|
deleteCookies,
|
||||||
|
setCookie,
|
||||||
setCookies,
|
setCookies,
|
||||||
}, cmd.input.action) orelse return error.UnknownMethod;
|
}, cmd.input.action) orelse return error.UnknownMethod;
|
||||||
|
|
||||||
@@ -38,6 +39,7 @@ pub fn processMessage(cmd: anytype) !void {
|
|||||||
.setCacheDisabled => return cmd.sendResult(null, .{}),
|
.setCacheDisabled => return cmd.sendResult(null, .{}),
|
||||||
.setExtraHTTPHeaders => return setExtraHTTPHeaders(cmd),
|
.setExtraHTTPHeaders => return setExtraHTTPHeaders(cmd),
|
||||||
.deleteCookies => return deleteCookies(cmd),
|
.deleteCookies => return deleteCookies(cmd),
|
||||||
|
.setCookie => return setCookie(cmd),
|
||||||
.setCookies => return setCookies(cmd),
|
.setCookies => return setCookies(cmd),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,9 +170,7 @@ fn percentEncodedDomain(allocator: Allocator, default_url: ?[]const u8, domain:
|
|||||||
} else return null;
|
} else return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setCookies(cmd: anytype) !void {
|
const CdpCookie = struct {
|
||||||
const params = (try cmd.params(struct {
|
|
||||||
cookies: []const struct {
|
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
value: []const u8,
|
value: []const u8,
|
||||||
url: ?[]const u8 = null,
|
url: ?[]const u8 = null,
|
||||||
@@ -185,18 +185,40 @@ fn setCookies(cmd: anytype) !void {
|
|||||||
sourceScheme: ?CookieSourceScheme = null,
|
sourceScheme: ?CookieSourceScheme = null,
|
||||||
// sourcePort: Temporary ability and it will be removed from CDP
|
// sourcePort: Temporary ability and it will be removed from CDP
|
||||||
partitionKey: ?CookiePartitionKey = null,
|
partitionKey: ?CookiePartitionKey = null,
|
||||||
},
|
};
|
||||||
|
|
||||||
|
fn setCookie(cmd: anytype) !void {
|
||||||
|
const params = (try cmd.params(
|
||||||
|
CdpCookie,
|
||||||
|
)) orelse return error.InvalidParams;
|
||||||
|
|
||||||
|
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||||
|
try setCdpCookie(&bc.session.cookie_jar, params);
|
||||||
|
|
||||||
|
try cmd.sendResult(.{ .success = true }, .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setCookies(cmd: anytype) !void {
|
||||||
|
const params = (try cmd.params(struct {
|
||||||
|
cookies: []const CdpCookie,
|
||||||
})) orelse return error.InvalidParams;
|
})) orelse return error.InvalidParams;
|
||||||
|
|
||||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||||
for (params.cookies) |param| {
|
for (params.cookies) |param| {
|
||||||
|
try setCdpCookie(&bc.session.cookie_jar, param);
|
||||||
|
}
|
||||||
|
|
||||||
|
try cmd.sendResult(null, .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setCdpCookie(cookie_jar: *CookieJar, param: CdpCookie) !void {
|
||||||
if (param.priority != .Medium or param.sameParty != null or param.sourceScheme != null or param.partitionKey != null) {
|
if (param.priority != .Medium or param.sameParty != null or param.sourceScheme != null or param.partitionKey != null) {
|
||||||
return error.NotYetImplementedParams;
|
return error.NotYetImplementedParams;
|
||||||
}
|
}
|
||||||
if (param.name.len == 0) return error.InvalidParams;
|
if (param.name.len == 0) return error.InvalidParams;
|
||||||
if (param.value.len == 0) return error.InvalidParams;
|
if (param.value.len == 0) return error.InvalidParams;
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(bc.session.cookie_jar.allocator);
|
var arena = std.heap.ArenaAllocator.init(cookie_jar.allocator);
|
||||||
errdefer arena.deinit();
|
errdefer arena.deinit();
|
||||||
const a = arena.allocator();
|
const a = arena.allocator();
|
||||||
|
|
||||||
@@ -218,10 +240,7 @@ fn setCookies(cmd: anytype) !void {
|
|||||||
.None => .none,
|
.None => .none,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
try bc.session.cookie_jar.add(cookie, std.time.timestamp());
|
try cookie_jar.add(cookie, std.time.timestamp());
|
||||||
}
|
|
||||||
|
|
||||||
return cmd.sendResult(null, .{});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upsert a header into the headers array.
|
// Upsert a header into the headers array.
|
||||||
|
|||||||
Reference in New Issue
Block a user