From 0e08533c8ebe942fde78974e78b0c570835180fc Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 8 Mar 2024 16:16:33 +0100 Subject: [PATCH] browser: write a cache file of failed fetched js --- .gitignore | 1 + src/browser/browser.zig | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/.gitignore b/.gitignore index a5e9840f..03da85f3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ zig-out /vendor/netsurf/lib/ /vendor/netsurf/include/ /vendor/libiconv/ +/*.cache diff --git a/src/browser/browser.zig b/src/browser/browser.zig index d180ef27..fcf16dc1 100644 --- a/src/browser/browser.zig +++ b/src/browser/browser.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const Types = @import("root").Types; @@ -430,6 +431,13 @@ pub const Page = struct { if (res.success) { log.debug("eval remote {s}: {s}", .{ src, res.result }); } else { + // In debug mode only, save the file in a temp file. + if (comptime builtin.mode == .Debug) { + writeCache(alloc, u.path, fetchres.body.?) catch |e| { + log.debug("cache: {any}", .{e}); + }; + } + log.info("eval remote {s}: {s}", .{ src, res.result }); return FetchError.JsErr; } @@ -448,3 +456,22 @@ pub const Page = struct { return false; } }; + +fn writeCache(alloc: std.mem.Allocator, name: []const u8, data: []const u8) !void { + const fname = try std.mem.concat(alloc, u8, &.{ name, ".cache" }); + defer alloc.free(fname); + + // clear invalid char. + for (fname, 0..) |c, i| { + if (!std.ascii.isPrint(c) or std.ascii.isWhitespace(c) or c == '/') { + fname[i] = '_'; + } + } + + log.debug("cache {s}", .{fname}); + + const f = try std.fs.cwd().createFile(fname, .{ .read = false, .truncate = true }); + defer f.close(); + + try f.writeAll(data); +}