Skip to content

Commit 5100e06

Browse files
committed
fix header done callback
1 parent 35e2fa5 commit 5100e06

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/cdp/domains/network.zig

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,36 @@ pub fn processMessage(cmd: anytype) !void {
5353

5454
const Response = struct {
5555
status: u16,
56-
headers: std.StringArrayHashMapUnmanaged([]const u8) = .empty, // These may not be complete yet, but we only tell the client Network.responseReceived when all the headers are in
57-
// Later should store body as well to support getResponseBody which should only work once Network.loadingFinished is sent
58-
// but the body itself would be loaded with each chunks as Network.dataReceiveds are coming in.
56+
headers: std.StringArrayHashMapUnmanaged([]const u8) = .empty,
57+
// These may not be complete yet, but we only tell the client
58+
// Network.responseReceived when all the headers are in.
59+
// Later should store body as well to support getResponseBody which should
60+
// only work once Network.loadingFinished is sent but the body itself would
61+
// be loaded with each chunks as Network.dataReceiveds are coming in.
5962
};
6063

6164
// Stored in CDP
6265
pub const NetworkState = struct {
63-
const Self = @This();
64-
received: std.AutoArrayHashMap(u64, Response),
6566
arena: std.heap.ArenaAllocator,
67+
received: std.AutoArrayHashMap(u64, Response),
6668

6769
pub fn init(allocator: Allocator) !NetworkState {
6870
return .{
69-
.received = std.AutoArrayHashMap(u64, Response).init(allocator),
7071
.arena = std.heap.ArenaAllocator.init(allocator),
72+
.received = std.AutoArrayHashMap(u64, Response).init(allocator),
7173
};
7274
}
7375

74-
pub fn deinit(self: *Self) void {
76+
pub fn deinit(self: *NetworkState) void {
7577
self.received.deinit();
7678
self.arena.deinit();
7779
}
7880

7981
pub fn putOrAppendReceivedHeader(self: *NetworkState, request_id: u64, status: u16, header: std.http.Header) !void {
8082
const kv = try self.received.getOrPut(request_id);
81-
if (!kv.found_existing) kv.value_ptr.* = .{ .status = status };
83+
if (!kv.found_existing) {
84+
kv.value_ptr.* = .{ .status = status };
85+
}
8286

8387
const a = self.arena.allocator();
8488
const name = try a.dupe(u8, header.name);

src/http/Client.zig

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,12 @@ fn makeTransfer(self: *Client, req: Request) !*Transfer {
247247
.req = req,
248248
.ctx = req.ctx,
249249
.client = self,
250-
.notification = &self.notification,
251250
};
252251
return transfer;
253252
}
254253

255254
fn requestFailed(self: *Client, transfer: *Transfer, err: anyerror) void {
256-
// this shoudln't happen, we'll crash in debug mode. But in release, we'll
255+
// this shouldn't happen, we'll crash in debug mode. But in release, we'll
257256
// just noop this state.
258257
std.debug.assert(transfer._notified_fail == false);
259258
if (transfer._notified_fail) {
@@ -550,8 +549,6 @@ pub const Transfer = struct {
550549

551550
_redirecting: bool = false,
552551

553-
notification: *?*Notification, // Points to the Client's notification. TBD if a Browser can remove the notification before all Transfers are gone.
554-
555552
fn deinit(self: *Transfer) void {
556553
self.req.headers.deinit();
557554
if (self._handle) |handle| {
@@ -676,7 +673,8 @@ pub const Transfer = struct {
676673
// returning < buf_len terminates the request
677674
return 0;
678675
};
679-
if (transfer.notification.*) |notification| { // TBD before or after callback?
676+
677+
if (transfer.client.notification) |notification| {
680678
notification.dispatch(.http_headers_done_receiving, &.{
681679
.transfer = transfer,
682680
});
@@ -687,15 +685,16 @@ pub const Transfer = struct {
687685
log.err(.http, "header_callback", .{ .err = err, .req = transfer });
688686
return 0;
689687
};
690-
if (transfer.notification.*) |notification| { // TBD before or after callback?
691-
if (Http.Headers.parseHeader(header)) |hdr_name_value| {
692-
notification.dispatch(.http_header_received, &.{
693-
.request_id = transfer.id,
694-
.status = hdr.status,
695-
.header = hdr_name_value,
696-
});
697-
} else log.err(.http, "invalid header", .{ .line = header });
698-
}
688+
}
689+
690+
if (transfer.client.notification) |notification| {
691+
if (Http.Headers.parseHeader(header)) |hdr_name_value| {
692+
notification.dispatch(.http_header_received, &.{
693+
.request_id = transfer.id,
694+
.status = hdr.status,
695+
.header = hdr_name_value,
696+
});
697+
} else log.err(.http, "invalid header", .{ .line = header });
699698
}
700699
}
701700
return buf_len;

0 commit comments

Comments
 (0)