mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-28 15:40:04 +00: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 AXNode = @import("AXNode.zig");
|
||||||
|
|
||||||
const CapturedResponse = struct {
|
const CapturedResponse = struct {
|
||||||
encode: enum { none, base64 },
|
must_encode: bool,
|
||||||
data: std.ArrayList(u8),
|
data: std.ArrayList(u8),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -648,22 +648,22 @@ pub fn BrowserContext(comptime CDP_T: type) type {
|
|||||||
if (!gop.found_existing) {
|
if (!gop.found_existing) {
|
||||||
gop.value_ptr.* = .{
|
gop.value_ptr.* = .{
|
||||||
.data = .empty,
|
.data = .empty,
|
||||||
// Encode the data in base64 by default, but use none
|
// Encode the data in base64 by default, but don't encode
|
||||||
// encoding for well known content-type.
|
// for well known content-type.
|
||||||
.encode = blk: {
|
.must_encode = blk: {
|
||||||
const transfer = msg.transfer;
|
const transfer = msg.transfer;
|
||||||
if (transfer.response_header.?.contentType()) |ct| {
|
if (transfer.response_header.?.contentType()) |ct| {
|
||||||
const mime = try Mime.parse(ct);
|
const mime = try Mime.parse(ct);
|
||||||
|
|
||||||
if (!mime.isText()) {
|
if (!mime.isText()) {
|
||||||
break :blk .base64;
|
break :blk true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std.mem.eql(u8, "UTF-8", mime.charsetString())) {
|
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 id = msg.transfer.id;
|
||||||
const resp = self.captured_responses.getPtr(id) orelse lp.assert(false, "onHttpResponseData missinf captured response", .{});
|
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);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn onHttpRequestAuthRequired(ctx: *anyopaque, data: *const Notification.RequestAuthRequired) !void {
|
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 bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||||
const resp = bc.captured_responses.getPtr(request_id) orelse return error.RequestNotFound;
|
const resp = bc.captured_responses.getPtr(request_id) orelse return error.RequestNotFound;
|
||||||
|
|
||||||
try cmd.sendResult(.{
|
if (!resp.must_encode) {
|
||||||
.body = resp.data.items,
|
return cmd.sendResult(.{
|
||||||
.base64Encoded = resp.encode == .base64,
|
.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