Skip to content

Lightpanda: the headless browser designed for AI and automation

`Download`: small nits; more regression fixes!

Halil Durak62efbec093f1
diff --git a/src/browser/Download.zig b/src/browser/Download.zig
index fa05812aa..c57254f4e 100644
--- a/src/browser/Download.zig
+++ b/src/browser/Download.zig
@@ -214,17 +214,19 @@ pub fn feed(self: *Download, bytes: []const u8) !void {
/// The body was fully received.
pub fn finish(self: *Download) void {
defer self.report(.completed);
- if (self.file) |file| {
- file.close(lp.io);
- }
+ self.closeFile();
}
/// The transfer failed partway. The partial file stays on disk for now.
pub fn cancel(self: *Download) void {
defer self.report(.canceled);
- if (self.file) |file| {
- file.close(lp.io);
- }
+ self.closeFile();
+}
+
+fn closeFile(self: *Download) void {
+ const file = self.file orelse return;
+ file.close(lp.io);
+ self.file = null;
}
// Playwright only settles a download on a `completed` or `canceled`
@@ -386,8 +388,12 @@ fn urlBasename(arena: Allocator, url: []const u8) !?[]const u8 {
var ptr: [*]const u8 = undefined;
var len: usize = undefined;
sys_url.url_get_path(u, &ptr, &len);
+ const path = ptr[0..len];
+ if (std.mem.startsWith(u8, path, "/") == false) {
+ return null;
+ }
- const name = sanitizeFilename(ptr[0..len]) orelse return null;
+ const name = sanitizeFilename(path) orelse return null;
return try arena.dupe(u8, name);
}
@@ -419,4 +425,5 @@ test "Download: urlBasename" {
try testing.expectEqualSlices(u8, "report.csv", (try urlBasename(a, "http://x.com/a/b/report.csv")).?);
try testing.expectEqualSlices(u8, "report.csv", (try urlBasename(a, "http://x.com/report.csv?v=1#x")).?);
try testing.expect((try urlBasename(a, "http://x.com/")) == null);
+ try testing.expect((try urlBasename(a, "data:text/csv,name%2Cvalue")) == null);
}
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 2f9db3551..ecb722654 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -1910,10 +1910,14 @@ fn frameDataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
}
},
.raw, .image => |*buf| try buf.appendSlice(self.page_arena, data),
- // A write failure fails the transfer, so no further chunk arrives;
- // `frameErrorCallback` then reports the download canceled, as the
- // `<a download>` path does. The partial file stays on disk for now.
- .download => |*download| try download.feed(data),
+ .download => |*download| download.feed(data) catch {
+ // Like Chrome, a failed download doesn't fail the navigation:
+ // report it canceled, then let `done` commit the same empty
+ // document a finished download gets (see `.raw`). The partial
+ // file stays on disk for now.
+ download.cancel();
+ self._parse_state = .{ .raw = .empty };
+ },
.pre => unreachable,
.complete => unreachable,
.err => unreachable,
diff --git a/src/server/cdp/domains/browser.zig b/src/server/cdp/domains/browser.zig
index 36906be6f..c6e25ddc5 100644
--- a/src/server/cdp/domains/browser.zig
+++ b/src/server/cdp/domains/browser.zig
@@ -109,6 +109,11 @@ fn setDownloadBehavior(cmd: *CDP.Command) !void {
else
return error.InvalidParams;
+ // These write to disk, so they need to know where.
+ if ((behavior == .allow or behavior == .allow_and_name) and params.downloadPath == null) {
+ return error.InvalidParams;
+ }
+
// Drivers (notably Playwright) send Browser.setDownloadBehavior at the
// browser level during connection setup, before any target/context has
// been created. Chromium accepts this; we have nowhere to store the config
@@ -598,6 +603,29 @@ test "cdp.browser: setDownloadBehavior stream requires eventsEnabled" {
try testing.expect(bc.session.download_path == null);
}
+test "cdp.browser: setDownloadBehavior allow requires downloadPath" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ const bc = try ctx.loadBrowserContext(.{ .session_id = "SID-DLP" });
+
+ // With nowhere to write, every download would fail unannounced.
+ try ctx.processMessage(.{
+ .id = 54,
+ .method = "Browser.setDownloadBehavior",
+ .params = .{ .behavior = "allow", .eventsEnabled = true },
+ });
+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 54 });
+
+ try ctx.processMessage(.{
+ .id = 55,
+ .method = "Browser.setDownloadBehavior",
+ .params = .{ .behavior = "allowAndName" },
+ });
+ try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 55 });
+ try testing.expectEqual(.deny, bc.session.download_behavior);
+}
+
test "cdp.browser: setDownloadBehavior writes an <a download> to disk and emits events" {
var ctx = try testing.context();
defer ctx.deinit();