Skip to content

Lightpanda: the headless browser designed for AI and automation

http: deliver 3xx responses without a Location header as final responses

Navid EMAD6665684abe7a
diff --git a/src/browser/HttpClient.zig b/src/browser/HttpClient.zig
index 6112e1cbe..66d03e353 100644
--- a/src/browser/HttpClient.zig
+++ b/src/browser/HttpClient.zig
@@ -1078,9 +1078,11 @@ fn processOneMessage(self: *Client, msg: http.Handles.MultiMessage, transfer: *T
}
// Handle redirects: reuse the same connection to preserve TCP state.
+ // A 3xx without a Location header is not a redirect: per RFC 9110 §15.4
+ // it's a final response and falls through so its body is delivered.
if (msg.err == null) {
const status = try msg.conn.getResponseCode();
- if (status >= 300 and status <= 399) {
+ if (status >= 300 and status <= 399 and msg.conn.getResponseHeader("location", 0) != null) {
try transfer.handleRedirect();
const conn = transfer._conn.?;
@@ -2034,7 +2036,10 @@ pub const Transfer = struct {
log.err(.http, "getResponseCode", .{ .err = err, .source = "body callback" });
return http.writefunc_error;
};
- if (status >= 300 and status <= 399) {
+ // Only skip the body when the 3xx will actually be retried as a
+ // redirect (it has a Location header). A 3xx without one is a
+ // final response whose body must be kept.
+ if (status >= 300 and status <= 399 and conn.getResponseHeader("location", 0) != null) {
res.skip_body = true;
return @intCast(chunk_len);
}
diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zig
index ec8ad40a8..72974cf77 100644
--- a/src/cdp/domains/page.zig
+++ b/src/cdp/domains/page.zig
@@ -1310,6 +1310,34 @@ test "cdp.frame: navigate inherits original fragment across redirect" {
}
}
+test "cdp.frame: navigate renders body of 3xx response without Location" {
+ // RFC 9110 §15.4: a 3xx response without a Location header is not a
+ // redirect — it's a final response whose body must be delivered, like
+ // any other status. It must not abort the navigation.
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ var bc = try ctx.loadBrowserContext(.{ .id = "BID-3XX", .url = "hi.html", .target_id = "FID-0000003XX0".* });
+
+ try ctx.processMessage(.{
+ .id = 50,
+ .method = "Page.navigate",
+ .params = .{ .url = "http://127.0.0.1:9582/303-no-location" },
+ });
+
+ var runner = try bc.session.runner(.{});
+ try runner.wait(.{ .ms = 2000 });
+
+ const frame = bc.session.currentFrame() orelse unreachable;
+ try testing.expectEqualSlices(u8, "http://127.0.0.1:9582/303-no-location", frame.url);
+
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ const v = try ls.local.exec("document.title === 'landed' && document.body.innerText.includes('see other body')", null);
+ try testing.expect(v.toBool());
+}
+
test "cdp.frame: navigate to about:blank replaces a non-blank document" {
// Regression test for #2363. Page.navigate("about:blank") issued against a
// tab that already holds a real document must replace the active document
diff --git a/src/testing.zig b/src/testing.zig
index 3085f28c1..4ed8e0028 100644
--- a/src/testing.zig
+++ b/src/testing.zig
@@ -637,6 +637,17 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {
});
}
+ if (std.mem.eql(u8, path, "/303-no-location")) {
+ // 3xx WITHOUT a Location header: not a redirect, a final response
+ // whose body must be delivered (RFC 9110 §15.4).
+ return req.respond("<!DOCTYPE html><title>landed</title><p>see other body</p>", .{
+ .status = .see_other,
+ .extra_headers = &.{
+ .{ .name = "Content-Type", .value = "text/html; charset=utf-8" },
+ },
+ });
+ }
+
if (std.mem.eql(u8, path, "/redirect-with-fragment")) {
return req.respond("", .{
.status = .found,