Pre-size the destination buffer when we know the response content length

This commit is contained in:
Karl Seguin
2025-08-31 20:14:55 +08:00
parent f66f4d9aeb
commit 2a8e51c2d2
3 changed files with 25 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {