mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-21 20:24:42 +00:00
encode all captured responses body in base64
This commit is contained in:
@@ -655,10 +655,18 @@ pub fn BrowserContext(comptime CDP_T: type) type {
|
||||
if (!gop.found_existing) {
|
||||
gop.value_ptr.* = .{
|
||||
.data = .empty,
|
||||
.encode = .none,
|
||||
.encode = .base64,
|
||||
};
|
||||
}
|
||||
try gop.value_ptr.data.appendSlice(arena, try arena.dupe(u8, msg.data));
|
||||
|
||||
// Always base64 ecncode the catured response body.
|
||||
// TODO: use the response's content-type to decide to encode or not
|
||||
// the body.
|
||||
const resp = gop.value_ptr;
|
||||
const encoded_len = std.base64.standard.Encoder.calcSize(msg.data.len);
|
||||
const start = resp.data.items.len;
|
||||
try resp.data.resize(arena, start + encoded_len);
|
||||
_ = std.base64.standard.Encoder.encode(resp.data.items[start..], msg.data);
|
||||
}
|
||||
|
||||
pub fn onHttpRequestAuthRequired(ctx: *anyopaque, data: *const Notification.RequestAuthRequired) !void {
|
||||
|
||||
@@ -523,43 +523,3 @@ test "cdp.Network: cookies" {
|
||||
});
|
||||
try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{} }, .{ .id = 10 });
|
||||
}
|
||||
|
||||
test "cdp.Network.getResponseBody base64-encodes UTF-8 responses" {
|
||||
var ctx = testing.context();
|
||||
defer ctx.deinit();
|
||||
|
||||
const bc = try ctx.loadBrowserContext(.{ .id = "BID-GRB", .session_id = "NESI-GRB" });
|
||||
var text_response: std.ArrayList(u8) = .{};
|
||||
try text_response.appendSlice(bc.arena, "hello");
|
||||
try bc.captured_responses.put(bc.arena, 42, text_response);
|
||||
|
||||
try ctx.processMessage(.{
|
||||
.id = 11,
|
||||
.method = "Network.getResponseBody",
|
||||
.params = .{ .requestId = "REQ-42" },
|
||||
});
|
||||
try ctx.expectSentResult(.{
|
||||
.body = "aGVsbG8=",
|
||||
.base64Encoded = true,
|
||||
}, .{ .id = 11 });
|
||||
}
|
||||
|
||||
test "cdp.Network.getResponseBody base64-encodes non-UTF8 responses" {
|
||||
var ctx = testing.context();
|
||||
defer ctx.deinit();
|
||||
|
||||
const bc = try ctx.loadBrowserContext(.{ .id = "BID-GRB-BIN", .session_id = "NESI-GRB-BIN" });
|
||||
var binary_response: std.ArrayList(u8) = .{};
|
||||
try binary_response.appendSlice(bc.arena, &[_]u8{ 0xFF, 0x00, 0x41 });
|
||||
try bc.captured_responses.put(bc.arena, 43, binary_response);
|
||||
|
||||
try ctx.processMessage(.{
|
||||
.id = 12,
|
||||
.method = "Network.getResponseBody",
|
||||
.params = .{ .requestId = "REQ-43" },
|
||||
});
|
||||
try ctx.expectSentResult(.{
|
||||
.body = "/wBB",
|
||||
.base64Encoded = true,
|
||||
}, .{ .id = 12 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user