mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-24 09:23:04 -04:00
encode captured response body during CDP call
This commit is contained in:
@@ -326,7 +326,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
|
||||
const AXNode = @import("AXNode.zig");
|
||||
|
||||
const CapturedResponse = struct {
|
||||
encode: enum { none, base64 },
|
||||
must_encode: bool,
|
||||
data: std.ArrayList(u8),
|
||||
};
|
||||
|
||||
@@ -648,22 +648,22 @@ pub fn BrowserContext(comptime CDP_T: type) type {
|
||||
if (!gop.found_existing) {
|
||||
gop.value_ptr.* = .{
|
||||
.data = .empty,
|
||||
// Encode the data in base64 by default, but use none
|
||||
// encoding for well known content-type.
|
||||
.encode = blk: {
|
||||
// Encode the data in base64 by default, but don't encode
|
||||
// for well known content-type.
|
||||
.must_encode = blk: {
|
||||
const transfer = msg.transfer;
|
||||
if (transfer.response_header.?.contentType()) |ct| {
|
||||
const mime = try Mime.parse(ct);
|
||||
|
||||
if (!mime.isText()) {
|
||||
break :blk .base64;
|
||||
break :blk true;
|
||||
}
|
||||
|
||||
if (std.mem.eql(u8, "UTF-8", mime.charsetString())) {
|
||||
break :blk .none;
|
||||
break :blk false;
|
||||
}
|
||||
}
|
||||
break :blk .base64;
|
||||
break :blk true;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -683,14 +683,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
|
||||
const id = msg.transfer.id;
|
||||
const resp = self.captured_responses.getPtr(id) orelse lp.assert(false, "onHttpResponseData missinf captured response", .{});
|
||||
|
||||
if (resp.encode == .none) {
|
||||
return resp.data.appendSlice(arena, msg.data);
|
||||
}
|
||||
|
||||
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);
|
||||
return resp.data.appendSlice(arena, msg.data);
|
||||
}
|
||||
|
||||
pub fn onHttpRequestAuthRequired(ctx: *anyopaque, data: *const Notification.RequestAuthRequired) !void {
|
||||
|
||||
@@ -210,9 +210,20 @@ fn getResponseBody(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const resp = bc.captured_responses.getPtr(request_id) orelse return error.RequestNotFound;
|
||||
|
||||
try cmd.sendResult(.{
|
||||
.body = resp.data.items,
|
||||
.base64Encoded = resp.encode == .base64,
|
||||
if (!resp.must_encode) {
|
||||
return cmd.sendResult(.{
|
||||
.body = resp.data.items,
|
||||
.base64Encoded = false,
|
||||
}, .{});
|
||||
}
|
||||
|
||||
const encoded_len = std.base64.standard.Encoder.calcSize(resp.data.items.len);
|
||||
const encoded = try cmd.arena.alloc(u8, encoded_len);
|
||||
_ = std.base64.standard.Encoder.encode(encoded, resp.data.items);
|
||||
|
||||
return cmd.sendResult(.{
|
||||
.body = encoded,
|
||||
.base64Encoded = true,
|
||||
}, .{});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user