Skip to content

Commit 087e42a

Browse files
committed
Normalize CDP response headers
chromedb doesn't support duplicate header names. Although servers _will_ send this (e.g. Cache-Control: public\r\nCache-Control: max-age=60\r\n), Chrome seems to join them with a "\n". So we do the same. A note on curl_easy_nextheader, which this code ultimately uses to iterate and collect the headers. The documentation says: Applications must copy the data if they want it to survive subsequent API calls or the life-time of the easy handle. As-is, I'd understand this to mean that a given header name/value is only valid until any API call, including another call to curl_easy_nextheader. So, from this comment, we _should_ be duping the name/value. But we don't. Why? Because, despite the note in the documentation, this doesn't appear to be how it actually works, nor does it really make sense. If it's just a linked list, there's no reason curl_easy_nextheader should invalidate previous results. I'm guessing this is just a general lack of guarantee libcurl is willing to make re lifetimes. #966
1 parent e26d4af commit 087e42a

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/cdp/domains/network.zig

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ pub fn httpResponseHeaderDone(arena: Allocator, bc: anytype, msg: *const Notific
267267
.requestId = try std.fmt.allocPrint(arena, "REQ-{d}", .{msg.transfer.id}),
268268
.loaderId = bc.loader_id,
269269
.frameId = target_id,
270-
.response = TransferAsResponseWriter.init(msg.transfer),
270+
.response = TransferAsResponseWriter.init(arena, msg.transfer),
271271
}, .{ .session_id = session_id });
272272
}
273273

@@ -352,10 +352,12 @@ pub const TransferAsRequestWriter = struct {
352352
};
353353

354354
const TransferAsResponseWriter = struct {
355+
arena: Allocator,
355356
transfer: *Transfer,
356357

357-
fn init(transfer: *Transfer) TransferAsResponseWriter {
358+
fn init(arena: Allocator, transfer: *Transfer) TransferAsResponseWriter {
358359
return .{
360+
.arena = arena,
359361
.transfer = transfer,
360362
};
361363
}
@@ -392,14 +394,24 @@ const TransferAsResponseWriter = struct {
392394
}
393395

394396
{
395-
try writer.objectField("headers");
396-
try writer.beginObject();
397+
// chromedp doesn't like having duplicate header names. It's pretty
398+
// common to get these from a server (e.g. for Cache-Control), but
399+
// Chrome joins these. So we have to too.
400+
const arena = self.arena;
397401
var it = transfer.responseHeaderIterator();
402+
var map: std.StringArrayHashMapUnmanaged([]const u8) = .empty;
398403
while (it.next()) |hdr| {
399-
try writer.objectField(hdr.name);
400-
try writer.write(hdr.value);
404+
const gop = try map.getOrPut(arena, hdr.name);
405+
if (gop.found_existing) {
406+
// yes, chrome joins multi-value headers with a \n
407+
gop.value_ptr.* = try std.mem.join(arena, "\n", &.{ gop.value_ptr.*, hdr.value });
408+
} else {
409+
gop.value_ptr.* = hdr.value;
410+
}
401411
}
402-
try writer.endObject();
412+
413+
try writer.objectField("headers");
414+
try writer.write(std.json.ArrayHashMap([]const u8){ .map = map });
403415
}
404416
try writer.endObject();
405417
}

0 commit comments

Comments
 (0)