mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 23:23:28 +00:00
Pre-size the destination buffer when we know the response content length
This commit is contained in:
@@ -480,7 +480,14 @@ const PendingScript = struct {
|
||||
// will fail. This assertion exists to catch incorrect assumptions about
|
||||
// how libcurl works, or about how we've configured it.
|
||||
std.debug.assert(self.script.source.remote.capacity == 0);
|
||||
self.script.source = .{ .remote = self.manager.buffer_pool.get() };
|
||||
var buffer = self.manager.buffer_pool.get();
|
||||
if (transfer.getContentLength()) |cl| {
|
||||
if (cl > 100 * 1024 * 1024) {
|
||||
return error.ResponseTooLarge;
|
||||
}
|
||||
try buffer.ensureTotalCapacity(self.manager.allocator, cl);
|
||||
}
|
||||
self.script.source = .{ .remote = buffer };
|
||||
}
|
||||
|
||||
fn dataCallback(self: *PendingScript, transfer: *Http.Transfer, data: []const u8) !void {
|
||||
|
||||
@@ -438,6 +438,13 @@ pub const XMLHttpRequest = struct {
|
||||
|
||||
self.state = .loading;
|
||||
self.dispatchEvt("readystatechange");
|
||||
|
||||
if (transfer.getContentLength()) |cl| {
|
||||
if (cl > 100 * 1024 * 1024) {
|
||||
return error.ResponseTooLarge;
|
||||
}
|
||||
try self.response_bytes.ensureTotalCapacity(self.arena, cl);
|
||||
}
|
||||
}
|
||||
|
||||
fn httpDataCallback(transfer: *Http.Transfer, data: []const u8) !void {
|
||||
|
||||
@@ -1040,6 +1040,16 @@ pub const Transfer = struct {
|
||||
|
||||
try req.done_callback(req.ctx);
|
||||
}
|
||||
|
||||
pub fn getContentLength(self: *const Transfer) ?u32 {
|
||||
// It's possible for this to be null even with correct code, due to
|
||||
// request fulfillment. If transfer.fulfill is called, we won't have
|
||||
// a handle.
|
||||
const handle = self._handle orelse return null;
|
||||
|
||||
const cl = getResponseHeader(handle.conn.easy, "content-length", 0) orelse return null;
|
||||
return std.fmt.parseInt(u32, cl.value, 10) catch null;
|
||||
}
|
||||
};
|
||||
|
||||
pub const ResponseHeader = struct {
|
||||
|
||||
Reference in New Issue
Block a user