Skip to content

Lightpanda: the headless browser designed for AI and automation

network: clamp a response status to the HTTP range at the curl boundary

Adrià Arrufat1fee085f9f5a
diff --git a/src/network/http.zig b/src/network/http.zig
index 43773c890..ab34bff33 100644
--- a/src/network/http.zig
+++ b/src/network/http.zig
@@ -613,18 +613,19 @@ pub const Connection = struct {
pub fn getConnectCode(self: *const Connection) !u16 {
var status: c_long = undefined;
try libcurl.curl_easy_getinfo(self._easy, .connect_code, &status);
- if (status < 0 or status > std.math.maxInt(u16)) {
- return 0;
- }
- return @intCast(status);
+ return inHttpRange(status);
}
pub fn getResponseCode(self: *const Connection) !u16 {
var status: c_long = undefined;
try libcurl.curl_easy_getinfo(self._easy, .response_code, &status);
- if (status < 0 or status > std.math.maxInt(u16)) {
- return 0;
- }
+ return inHttpRange(status);
+ }
+
+ /// 0 outside 100..599, which is curl's own value for having no status.
+ /// Consumers cast this into `std.http.Status`, an enum(u10).
+ fn inHttpRange(status: c_long) u16 {
+ if (status < 100 or status > 599) return 0;
return @intCast(status);
}
@@ -1114,3 +1115,15 @@ test "opensocketCallback: block_private=false allows private IP" {
try testing.expect(fd >= 0);
}
+
+test "Connection.inHttpRange: only a real status survives" {
+ const kept = [_]c_long{ 100, 200, 404, 503, 599 };
+ for (kept) |status| {
+ try std.testing.expectEqual(@as(u16, @intCast(status)), Connection.inHttpRange(status));
+ }
+ // 9999 used to survive and then panic in every std.http.Status cast.
+ const dropped = [_]c_long{ -1, 0, 99, 600, 1024, 9999, 65536 };
+ for (dropped) |status| {
+ try std.testing.expectEqual(0, Connection.inHttpRange(status));
+ }
+}
diff --git a/src/server/cdp/domains/network.zig b/src/server/cdp/domains/network.zig
index d1de19f9d..790e72915 100644
--- a/src/server/cdp/domains/network.zig
+++ b/src/server/cdp/domains/network.zig
@@ -609,7 +609,9 @@ const ResponseWriter = struct {
try jws.write(status);
try jws.objectField("statusText");
- try jws.write(@as(std.http.Status, @enumFromInt(status)).phrase() orelse "Unknown");
+ // A CDP client can fulfill a request with any u16; Status is enum(u10).
+ const phrase = if (status > 599) "Unknown" else @as(std.http.Status, @enumFromInt(status)).phrase() orelse "Unknown";
+ try jws.write(phrase);
}
{