Lightpanda: the headless browser designed for AI and automation
Report natural dimensions of fetched images from a bounded header prefix
Scott Taylor009225ace122
diff --git a/src/browser/frame/resource_load.zig b/src/browser/frame/resource_load.zigindex 3edae3cfa..ebd74e8a2 100644--- a/src/browser/frame/resource_load.zig+++ b/src/browser/frame/resource_load.zig@@ -26,6 +26,7 @@ const URL = @import("../URL.zig");const Frame = @import("../Frame.zig");const Factory = @import("../Factory.zig");const Element = @import("../webapi/Element.zig");+const image_dimensions = @import("../image_dimensions.zig");const HttpClient = @import("../../network/HttpClient.zig");const log = lp.log;@@ -84,6 +85,7 @@ pub fn image(frame: *Frame, img: *Element.Html.Image, src: []const u8) !void {.credentials_mode = .include,.resource_type = .image,.headers_only = true,+ .headers_only_prefix_bytes = 16 * 1024,.header_callback = ImageLoad.headerCallback,.data_callback = ImageLoad.dataCallback,.done_callback = ImageLoad.doneCallback,@@ -116,6 +118,7 @@ const ImageLoad = struct {image: *Element.Html.Image,generation: u32,status: u16 = 0,+ dimensions: ?image_dimensions.Dimensions = null,fn headerCallback(transfer: *HttpClient.Transfer) !HttpClient.Transfer.HeaderResult {const self: *ImageLoad = @ptrCast(@alignCast(transfer.req.ctx));@@ -123,8 +126,9 @@ const ImageLoad = struct {return .proceed;}- fn dataCallback(_: *HttpClient.Transfer, _: []const u8) !void {- // headers_only tears the transfer down at the first body byte.+ fn dataCallback(transfer: *HttpClient.Transfer, prefix: []const u8) !void {+ const self: *ImageLoad = @ptrCast(@alignCast(transfer.req.ctx));+ self.dimensions = image_dimensions.parse(prefix);}fn doneCallback(ctx: *anyopaque) !void {@@ -173,6 +177,12 @@ const ImageLoad = struct {const current = self.generation == img._generation;if (current) {img._complete = true;+ if (kind == .load) {+ if (self.dimensions) |dimensions| {+ img._natural_width = dimensions.width;+ img._natural_height = dimensions.height;+ }+ }}// Released before anything below, because everything below can run JSdiff --git a/src/browser/image_dimensions.zig b/src/browser/image_dimensions.zignew file mode 100644index 000000000..5b6da626c--- /dev/null+++ b/src/browser/image_dimensions.zig@@ -0,0 +1,93 @@+// Copyright (C) 2026 Lightpanda (Selecy SAS)+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published+// by the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++const std = @import("std");++pub const Dimensions = struct { width: u32, height: u32 };++/// Read only the image header. The caller supplies a bounded prefix; an+/// unsupported format or a JPEG with its SOF beyond that prefix has no known+/// dimensions. No bitmap data is decoded.+pub fn parse(bytes: []const u8) ?Dimensions {+ if (bytes.len >= 24 and std.mem.eql(u8, bytes[0..8], "\x89PNG\r\n\x1a\n") and+ std.mem.eql(u8, bytes[8..12], "\x00\x00\x00\x0d") and std.mem.eql(u8, bytes[12..16], "IHDR"))+ {+ return valid(std.mem.readInt(u32, bytes[16..20], .big), std.mem.readInt(u32, bytes[20..24], .big));+ }++ if (bytes.len >= 10 and (std.mem.eql(u8, bytes[0..6], "GIF87a") or std.mem.eql(u8, bytes[0..6], "GIF89a"))) {+ return valid(std.mem.readInt(u16, bytes[6..8], .little), std.mem.readInt(u16, bytes[8..10], .little));+ }++ if (bytes.len >= 4 and bytes[0] == 0xff and bytes[1] == 0xd8) {+ var pos: usize = 2;+ while (pos < bytes.len) {+ if (bytes[pos] != 0xff) return null;+ while (pos < bytes.len and bytes[pos] == 0xff) : (pos += 1) {}+ if (pos >= bytes.len) return null;+ const marker = bytes[pos];+ pos += 1;+ if (marker == 0xd9 or marker == 0xda) return null; // EOI or scan data+ if (marker == 0x01 or (marker >= 0xd0 and marker <= 0xd7)) continue;+ if (bytes.len - pos < 2) return null;+ const len = std.mem.readInt(u16, bytes[pos..][0..2], .big);+ if (len < 2 or len > bytes.len - pos) return null;+ // SOF markers (except the non-frame DHT, JPG and DAC markers).+ if ((marker >= 0xc0 and marker <= 0xcf) and marker != 0xc4 and marker != 0xc8 and marker != 0xcc) {+ if (len < 7) return null;+ return valid(std.mem.readInt(u16, bytes[pos + 5 ..][0..2], .big), std.mem.readInt(u16, bytes[pos + 3 ..][0..2], .big));+ }+ pos += len;+ }+ }++ if (bytes.len >= 25 and std.mem.eql(u8, bytes[0..4], "RIFF") and std.mem.eql(u8, bytes[8..12], "WEBP")) {+ if (bytes.len >= 30 and std.mem.eql(u8, bytes[12..16], "VP8X")) {+ const width = @as(u32, bytes[24]) | @as(u32, bytes[25]) << 8 | @as(u32, bytes[26]) << 16;+ const height = @as(u32, bytes[27]) | @as(u32, bytes[28]) << 8 | @as(u32, bytes[29]) << 16;+ return valid(width + 1, height + 1);+ }+ if (bytes.len >= 30 and std.mem.eql(u8, bytes[12..16], "VP8 ") and std.mem.eql(u8, bytes[23..26], "\x9d\x01\x2a")) {+ return valid(std.mem.readInt(u16, bytes[26..28], .little) & 0x3fff, std.mem.readInt(u16, bytes[28..30], .little) & 0x3fff);+ }+ if (std.mem.eql(u8, bytes[12..16], "VP8L") and bytes[20] == 0x2f) {+ const width = @as(u32, bytes[21]) | (@as(u32, bytes[22]) & 0x3f) << 8;+ const height = @as(u32, bytes[22]) >> 6 | @as(u32, bytes[23]) << 2 | (@as(u32, bytes[24]) & 0x0f) << 10;+ return valid(width + 1, height + 1);+ }+ }+ return null;+}++fn valid(width: u32, height: u32) ?Dimensions {+ if (width == 0 or height == 0) return null;+ return .{ .width = width, .height = height };+}++const testing = std.testing;+test "image dimensions: PNG, GIF, JPEG, WebP" {+ try testing.expectEqual(Dimensions{ .width = 1000, .height = 750 }, parse("\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x03\xe8\x00\x00\x02\xee").?);+ try testing.expectEqual(Dimensions{ .width = 320, .height = 240 }, parse("GIF89a\x40\x01\xf0\x00").?);+ try testing.expectEqual(Dimensions{ .width = 1000, .height = 750 }, parse("\xff\xd8\xff\xe1\x00\x04\x00\x00\xff\xc0\x00\x0b\x08\x02\xee\x03\xe8\x01\x01\x11\x00").?);+ try testing.expectEqual(Dimensions{ .width = 1000, .height = 750 }, parse("RIFF\x00\x00\x00\x00WEBPVP8X\x0a\x00\x00\x00\x00\x00\x00\x00\xe7\x03\x00\xed\x02\x00").?);+ try testing.expectEqual(Dimensions{ .width = 1, .height = 1 }, parse("RIFF\x00\x00\x00\x00WEBPVP8L\x05\x00\x00\x00\x2f\x00\x00\x00\x00\x00").?);+}++test "image dimensions: truncated or invalid headers" {+ try testing.expectEqual(null, parse("\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00"));+ try testing.expectEqual(null, parse("\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x00\x00\x00\x00\x01"));+ try testing.expectEqual(null, parse("\xff\xd8\xff\xe1\x00\xff\x00"));+ try testing.expectEqual(null, parse("<svg width='10' height='20'></svg>"));+}diff --git a/src/browser/tests/element/html/image.html b/src/browser/tests/element/html/image.htmlindex a778285cb..368de087b 100644--- a/src/browser/tests/element/html/image.html+++ b/src/browser/tests/element/html/image.html@@ -135,6 +135,7 @@testing.expectEqual(false, e.composed);testing.expectEqual(true, e.isTrusted);testing.expectEqual(img, e.target);+ testing.expectEqual(0, img.naturalWidth); // Images are not fetched by default.});</script>diff --git a/src/browser/tests/element/html/image_fetch.html b/src/browser/tests/element/html/image_fetch.htmlindex 995a987bc..ca8b83bef 100644--- a/src/browser/tests/element/html/image_fetch.html+++ b/src/browser/tests/element/html/image_fetch.html@@ -26,6 +26,9 @@state.resolve();await state.done(() => {testing.expectEqual("load", evt);+ testing.expectEqual(1000, img.naturalWidth);+ testing.expectEqual(750, img.naturalHeight);+ testing.expectEqual(0, img.width); // Natural size does not set HTML attributes.});</script>@@ -44,6 +47,7 @@state.resolve();await state.done(() => {testing.expectEqual("error", evt);+ testing.expectEqual(0, img.naturalWidth);});</script>@@ -63,8 +67,9 @@state.resolve();await state.done(() => {testing.expectEqual("load", evt);- // Drained, not decoded: still no dimensions.- testing.expectEqual(0, img.naturalWidth);+ // The body is not decoded, but its header gives the natural size.+ testing.expectEqual(320, img.naturalWidth);+ testing.expectEqual(240, img.naturalHeight);});</script>@@ -103,6 +108,7 @@state.resolve();await state.done(() => {testing.expectEqual("load", evt);+ testing.expectEqual(0, img.naturalWidth); // SVG is not parsed as a bitmap header.});</script>@@ -137,12 +143,51 @@state.resolve();await state.done(() => {testing.expectEqual("load", evt);+ testing.expectEqual(1000, img.naturalWidth);+ testing.expectEqual(750, img.naturalHeight);+ });+</script>++<script id="img-jpeg-and-src-change" type=module>+ const state = await testing.async();+ const img = new Image();+ const loaded = new Promise(resolve => img.addEventListener('load', resolve, { once: true }));+ img.src = '/images/photo.jpg';+ await loaded;+ testing.expectEqual(1000, img.naturalWidth);+ testing.expectEqual(750, img.naturalHeight);++ const failed = new Promise(resolve => img.addEventListener('error', resolve, { once: true }));+ img.src = '/images/404.png';+ testing.expectEqual(0, img.naturalWidth); // Old dimensions are invalid immediately.+ await failed;++ state.resolve();+ await state.done(() => {+ testing.expectEqual(0, img.naturalWidth);+ testing.expectEqual(0, img.naturalHeight);+ });+</script>++<script id="img-removing-loaded-src-clears-dimensions" type=module>+ const state = await testing.async();+ const img = new Image();+ const loaded = new Promise(resolve => img.addEventListener('load', resolve, { once: true }));+ img.src = '/images/small.png';+ await loaded;+ testing.expectEqual(320, img.naturalWidth);+ img.removeAttribute('src');++ state.resolve();+ await state.done(() => {+ testing.expectEqual(0, img.naturalWidth);+ testing.expectEqual(0, img.naturalHeight);});</script><script id="img-complete-tracks-the-fetch" type=module>- // complete is false only while a request is in flight. naturalWidth stays- // 0 either way: we fetch images, we don't decode them.+ // complete is false while a request is in flight. Dimensions are only+ // available after the header has arrived; no bitmap is decoded.const state = await testing.async();const img = document.createElement("img");@@ -160,8 +205,8 @@await state.done(() => {testing.expectEqual(false, during);testing.expectEqual(true, after);- testing.expectEqual(0, img.naturalWidth);- testing.expectEqual(0, img.naturalHeight);+ testing.expectEqual(1000, img.naturalWidth);+ testing.expectEqual(750, img.naturalHeight);});</script>@@ -219,6 +264,7 @@// The superseded 404 is dropped; only the winning fetch reports.testing.expectEqual(1, events.length);testing.expectEqual("load", events[0]);+ testing.expectEqual(1000, img.naturalWidth);});</script>diff --git a/src/browser/webapi/element/html/Image.zig b/src/browser/webapi/element/html/Image.zigindex 946428f37..7865f2053 100644--- a/src/browser/webapi/element/html/Image.zig+++ b/src/browser/webapi/element/html/Image.zig@@ -40,6 +40,8 @@ pub const Proto = HtmlElement;_generation: u32 = 0,// Per spec, false only while a fetch is in flight._complete: bool = true,+_natural_width: u32 = 0,+_natural_height: u32 = 0,// Hash of the URL the last update selected, so a <picture> mutation that// leaves the selection unchanged doesn't restart the load._selected_hash: u64 = 0,@@ -100,16 +102,12 @@ fn setLoading(self: *Image, value: []const u8, frame: *Frame) !void {try self.asElement().setAttributeSafe(comptime .wrap("loading"), .wrap(value), frame);}-fn getNaturalWidth(_: *const Image) u32 {- // this is a valid response under a number of normal conditions, but could- // be used to detect the nature of Browser.- return 0;+fn getNaturalWidth(self: *const Image) u32 {+ return self._natural_width;}-fn getNaturalHeight(_: *const Image) u32 {- // this is a valid response under a number of normal conditions, but could- // be used to detect the nature of Browser.- return 0;+fn getNaturalHeight(self: *const Image) u32 {+ return self._natural_height;}fn getComplete(self: *const Image) bool {@@ -138,6 +136,8 @@ fn imageAddedCallback(self: *Image, frame: *Frame) !void {self._generation +%= 1;self._complete = true;+ self._natural_width = 0;+ self._natural_height = 0;const src = self.selectSource(frame);self._selected_hash = std.hash.Wyhash.hash(0, src);diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zigindex 8683add2a..bff53232e 100644--- a/src/network/HttpClient.zig+++ b/src/network/HttpClient.zig@@ -2056,10 +2056,16 @@ pub const Request = struct {// origins and CDNs see (and answer) exactly what a real browser sends;// the body is then discarded, and torn off the wire if it doesn't fit in// HEADERS_ONLY_DRAIN_MAX. The consumer still gets the usual- // start/header/done sequence, with an empty body; `data_callback` never- // fires.+ // start/header/done sequence, with an empty body unless the optional+ // headers_only_prefix_bytes is set.headers_only: bool = false,+ // Keep at most this many leading bytes while discarding the rest of a+ // headers-only response. Used to identify image dimensions without+ // downloading or retaining the entire image. The prefix is delivered to+ // data_callback before done_callback. Capped at HEADERS_ONLY_DRAIN_MAX.+ headers_only_prefix_bytes: usize = 0,+// Should only be set when they need to differ from the owner's.frame_id: u32 = 0,loader_id: u32 = 0,@@ -3959,6 +3965,14 @@ pub const Transfer = struct {res.bytes_received += chunk_len;if (transfer.req.headers_only) {+ const limit = @min(transfer.req.headers_only_prefix_bytes, Request.HEADERS_ONLY_DRAIN_MAX);+ if (res.buffer.items.len < limit) {+ const count = @min(chunk_len, limit - res.buffer.items.len);+ res.buffer.appendSlice(transfer.arena.allocator(), buffer[0..count]) catch |err| {+ res.callback_error = err;+ return http.writefunc_error;+ };+ }// Plenty of images have no Content-Length to decide this up front, so// decide it as the body arrives.if (res.bytes_received <= Request.HEADERS_ONLY_DRAIN_MAX) {diff --git a/src/testing.zig b/src/testing.zigindex 03b149f8c..0baa33751 100644--- a/src/testing.zig+++ b/src/testing.zig@@ -1109,9 +1109,11 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {// ok.png takes the abort branch, small.png the drain branch; both must// behave identically as far as the DOM is concerned.if (std.mem.eql(u8, path, "/images/ok.png")) {- // > HttpClient.Request.HEADERS_ONLY_DRAIN_MAX+ // > HttpClient.Request.HEADERS_ONLY_DRAIN_MAX. The synthetic PNG+ // header advertises 1000 x 750 pixels; no bitmap is decoded.const body = try arena_allocator.alloc(u8, 16 * 1024 + 1);@memset(body, 'x');+ @memcpy(body[0..24], "\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x03\xe8\x00\x00\x02\xee");return req.respond(body, .{.extra_headers = &.{.{ .name = "Content-Type", .value = "image/png" },@@ -1124,6 +1126,7 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {if (std.mem.startsWith(u8, path, "/images/small.png")) {const body = try arena_allocator.alloc(u8, 1024);@memset(body, 'x');+ @memcpy(body[0..24], "\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x01\x40\x00\x00\x00\xf0");return req.respond(body, .{.extra_headers = &.{.{ .name = "Content-Type", .value = "image/png" },@@ -1131,6 +1134,15 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {});}+ if (std.mem.eql(u8, path, "/images/photo.jpg")) {+ const body = "\xff\xd8\xff\xe1\x00\x04\x00\x00\xff\xc0\x00\x0b\x08\x02\xee\x03\xe8\x01\x01\x11\x00";+ return req.respond(body, .{+ .extra_headers = &.{+ .{ .name = "Content-Type", .value = "image/jpeg" },+ },+ });+ }+// No Content-Length: whether the body is small enough to drain can only// be decided as it arrives.if (std.mem.eql(u8, path, "/images/chunked.png")) {