Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/browser/ScriptManager.zig
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,11 @@ 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| {
try buffer.ensureTotalCapacity(self.manager.allocator, cl);
}
self.script.source = .{ .remote = buffer };
}

fn dataCallback(self: *PendingScript, transfer: *Http.Transfer, data: []const u8) !void {
Expand Down
4 changes: 4 additions & 0 deletions src/browser/xhr/xhr.zig
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ pub const XMLHttpRequest = struct {

self.state = .loading;
self.dispatchEvt("readystatechange");

if (transfer.getContentLength()) |cl| {
try self.response_bytes.ensureTotalCapacity(self.arena, cl);
}
}

fn httpDataCallback(transfer: *Http.Transfer, data: []const u8) !void {
Expand Down
29 changes: 29 additions & 0 deletions src/http/Client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,35 @@ pub const Transfer = struct {

try req.done_callback(req.ctx);
}

// This function should be called during the dataCallback. Calling it after
// such as in the doneCallback is guaranteed to return null.
pub fn getContentLength(self: *const Transfer) ?u32 {
const cl = self.getContentLengthRawValue() orelse return null;
return std.fmt.parseInt(u32, cl, 10) catch null;
}

fn getContentLengthRawValue(self: *const Transfer) ?[]const u8 {
if (self._handle) |handle| {
// If we have a handle, than this is a normal request. We can get the
// header value from the easy handle.
const cl = getResponseHeader(handle.conn.easy, "content-length", 0) orelse return null;
return cl.value;
}

// If we have no handle, then maybe this is being called after the
// doneCallback. OR, maybe this is a "fulfilled" request. Let's check
// the injected headers (if we have any).

const rh = self.response_header orelse return null;
for (rh._injected_headers) |hdr| {
if (std.ascii.eqlIgnoreCase(hdr.name, "content-length")) {
return hdr.value;
}
}

return null;
}
};

pub const ResponseHeader = struct {
Expand Down