Compare commits

..

35 Commits

Author SHA1 Message Date
Muki Kiboigo
c4dde615d3 remove cache revalidation stubs 2026-03-30 15:32:51 -07:00
Muki Kiboigo
2d4cdccdf0 add Vary support 2026-03-30 15:32:51 -07:00
Muki Kiboigo
07e391382a add more FsCache tests 2026-03-30 15:32:51 -07:00
Muki Kiboigo
7aa0360788 always close file on serveFromCache 2026-03-30 15:32:50 -07:00
Muki Kiboigo
d06dd1208f better logging for cache 2026-03-30 15:32:50 -07:00
Muki Kiboigo
201fba6362 switch to single file cache 2026-03-30 15:32:50 -07:00
Muki Kiboigo
4f3d5c181e properly deinit cache 2026-03-30 15:32:50 -07:00
Muki Kiboigo
372b911f20 use wyhash for power of two lock stripes 2026-03-30 15:32:50 -07:00
Muki Kiboigo
0d0ad718a9 require timestamp passed in with cache request 2026-03-30 15:32:50 -07:00
Muki Kiboigo
094b8ed3e2 remove unused cache method on fs cache 2026-03-30 15:32:50 -07:00
Muki Kiboigo
b4eb058038 add basic fs cache get/put test 2026-03-30 15:32:50 -07:00
Muki Kiboigo
400d32b6b0 check age on fs cache get 2026-03-30 15:32:49 -07:00
Muki Kiboigo
a8f17000ff only store stuff when we know we will cache 2026-03-30 15:32:49 -07:00
Muki Kiboigo
c9a0f0011a shortcircuit a lot of caching checks 2026-03-30 15:32:49 -07:00
Muki Kiboigo
264538d911 ensure fs cache file is closed after use 2026-03-30 15:32:49 -07:00
Muki Kiboigo
59462779b8 use CacheRequest instead of key 2026-03-30 15:32:49 -07:00
Muki Kiboigo
eadfb83baa add striped lock to FsCache 2026-03-30 15:32:49 -07:00
Muki Kiboigo
74026421d4 use arena_pool for cache get 2026-03-30 15:32:49 -07:00
Muki Kiboigo
8a4aae406f use writer for fs cache body file 2026-03-30 15:32:48 -07:00
Muki Kiboigo
52310f9571 use json for fs cache metadata file 2026-03-30 15:32:48 -07:00
Muki Kiboigo
fefee36496 use sha256 instead of wyhash 2026-03-30 15:32:48 -07:00
Muki Kiboigo
026586fe1b store type_buf and sub_type_buf in Mime 2026-03-30 15:32:48 -07:00
Muki Kiboigo
75b3779a8c add more http caching rules 2026-03-30 15:32:48 -07:00
Muki Kiboigo
d52bff5bcc use CacheControl and Vary 2026-03-30 15:32:48 -07:00
Muki Kiboigo
881719cec3 cache headers along with response 2026-03-30 15:32:48 -07:00
Muki Kiboigo
9d51f23c3c add basic caching support 2026-03-30 15:32:47 -07:00
Muki Kiboigo
de08479654 create cache owned by the network struct 2026-03-30 15:32:47 -07:00
Muki Kiboigo
d555892f1e use enum approach instead of vtable 2026-03-30 15:32:47 -07:00
Muki Kiboigo
ac533c684e add basic FsCache impl 2026-03-30 15:32:47 -07:00
Muki Kiboigo
0de89a383d add CachedResponse variant to Response 2026-03-30 15:32:47 -07:00
Muki Kiboigo
604b9f270d add headerIterator to Http Response 2026-03-30 15:32:47 -07:00
Muki Kiboigo
2f97251a56 use Response instead of Transfer in callbacks 2026-03-30 15:32:45 -07:00
Muki Kiboigo
553732c1ea allow Mime parse to use []const u8 2026-03-30 15:25:33 -07:00
Muki Kiboigo
18c80dd9b4 add cache dir to configuration opts 2026-03-30 15:25:33 -07:00
Muki Kiboigo
de0989af17 create Cache interface file 2026-03-30 15:25:33 -07:00
51 changed files with 1975 additions and 1569 deletions

View File

@@ -55,7 +55,7 @@ pub fn init(allocator: Allocator, config: *const Config) !*App {
.arena_pool = undefined,
};
app.network = try Network.init(allocator, config);
app.network = try Network.init(allocator, app, config);
errdefer app.network.deinit();
app.platform = try Platform.init();

View File

@@ -157,6 +157,13 @@ pub fn userAgentSuffix(self: *const Config) ?[]const u8 {
};
}
pub fn cacheDir(self: *const Config) ?[]const u8 {
return switch (self.mode) {
inline .serve, .fetch, .mcp => |opts| opts.common.cache_dir,
else => null,
};
}
pub fn cdpTimeout(self: *const Config) usize {
return switch (self.mode) {
.serve => |opts| if (opts.timeout > 604_800) 604_800_000 else @as(usize, opts.timeout) * 1000,
@@ -249,9 +256,7 @@ pub const Fetch = struct {
with_frames: bool = false,
strip: dump.Opts.Strip = .{},
wait_ms: u32 = 5000,
wait_until: ?WaitUntil = null,
wait_script: ?[:0]const u8 = null,
wait_selector: ?[:0]const u8 = null,
wait_until: WaitUntil = .done,
};
pub const Common = struct {
@@ -268,6 +273,7 @@ pub const Common = struct {
log_format: ?log.Format = null,
log_filter_scopes: ?[]log.Scope = null,
user_agent_suffix: ?[]const u8 = null,
cache_dir: ?[]const u8 = null,
web_bot_auth_key_file: ?[]const u8 = null,
web_bot_auth_keyid: ?[]const u8 = null,
@@ -415,24 +421,12 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
\\
\\--with-frames Includes the contents of iframes. Defaults to false.
\\
\\--wait-ms Wait time in milliseconds. Supersedes all other --wait
\\ parameters.
\\--wait-ms Wait time in milliseconds.
\\ Defaults to 5000.
\\
\\--wait-until Wait until the specified event. Checked before the other
\\ --wait- options. Supported events: load, domcontentloaded,
\\ networkidle, done.
\\ Defaults to 'done'. If --wait-selector, --wait-script or
\\ --wait-script-file are specified, defaults to none.
\\
\\--wait-selector Wait for an element matching the CSS selector to appear.
\\ Checked after --wait-until condition is met.
\\
\\--wait-script Wait for a JavaScript expression to return truthy.
\\ Checked after --wait-until condition is met.
\\
\\--wait-script-file
\\ Like --wait-script, but reads the script from a file.
\\--wait-until Wait until the specified event.
\\ Supported events: load, domcontentloaded, networkidle, done.
\\ Defaults to 'done'.
\\
++ common_options ++
\\
@@ -699,9 +693,7 @@ fn parseFetchArgs(
var common: Common = .{};
var strip: dump.Opts.Strip = .{};
var wait_ms: u32 = 5000;
var wait_until: ?WaitUntil = null;
var wait_script: ?[:0]const u8 = null;
var wait_selector: ?[:0]const u8 = null;
var wait_until: WaitUntil = .done;
while (args.next()) |opt| {
if (std.mem.eql(u8, "--wait-ms", opt) or std.mem.eql(u8, "--wait_ms", opt)) {
@@ -728,36 +720,6 @@ fn parseFetchArgs(
continue;
}
if (std.mem.eql(u8, "--wait-selector", opt) or std.mem.eql(u8, "--wait_selector", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
wait_selector = try allocator.dupeZ(u8, str);
continue;
}
if (std.mem.eql(u8, "--wait-script", opt) or std.mem.eql(u8, "--wait_script", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
wait_script = try allocator.dupeZ(u8, str);
continue;
}
if (std.mem.eql(u8, "--wait-script-file", opt) or std.mem.eql(u8, "--wait_script_file", opt)) {
const path = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
wait_script = std.fs.cwd().readFileAllocOptions(allocator, path, 1024 * 1024, null, .of(u8), 0) catch |err| {
log.fatal(.app, "failed to read file", .{ .arg = opt, .path = path, .err = err });
return error.InvalidArgument;
};
continue;
}
if (std.mem.eql(u8, "--dump", opt)) {
var peek_args = args.*;
if (peek_args.next()) |next_arg| {
@@ -848,8 +810,6 @@ fn parseFetchArgs(
.with_frames = with_frames,
.wait_ms = wait_ms,
.wait_until = wait_until,
.wait_selector = wait_selector,
.wait_script = wait_script,
};
}
@@ -1048,5 +1008,14 @@ fn parseCommonArg(
return true;
}
if (std.mem.eql(u8, "--cache_dir", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--cache_dir" });
return error.InvalidArgument;
};
common.cache_dir = try allocator.dupe(u8, str);
return true;
}
return false;
}

View File

@@ -74,8 +74,6 @@ const EventListeners = struct {
page_network_idle: List = .{},
page_network_almost_idle: List = .{},
page_frame_created: List = .{},
page_dom_content_loaded: List = .{},
page_loaded: List = .{},
http_request_fail: List = .{},
http_request_start: List = .{},
http_request_intercept: List = .{},
@@ -93,8 +91,6 @@ const Events = union(enum) {
page_network_idle: *const PageNetworkIdle,
page_network_almost_idle: *const PageNetworkAlmostIdle,
page_frame_created: *const PageFrameCreated,
page_dom_content_loaded: *const PageDOMContentLoaded,
page_loaded: *const PageLoaded,
http_request_fail: *const RequestFail,
http_request_start: *const RequestStart,
http_request_intercept: *const RequestIntercept,
@@ -141,18 +137,6 @@ pub const PageFrameCreated = struct {
timestamp: u64,
};
pub const PageDOMContentLoaded = struct {
req_id: u32,
frame_id: u32,
timestamp: u64,
};
pub const PageLoaded = struct {
req_id: u32,
frame_id: u32,
timestamp: u64,
};
pub const RequestStart = struct {
transfer: *Transfer,
};

View File

@@ -95,11 +95,9 @@ const NodeData = struct {
name: ?[]const u8,
value: ?[]const u8,
options: ?[]OptionData = null,
checked: ?bool = null,
xpath: []const u8,
interactive: bool,
disabled: bool,
tag_name: []const u8,
is_interactive: bool,
node_name: []const u8,
};
const WalkContext = struct {
@@ -150,21 +148,16 @@ fn walk(
const role = try axn.getRole();
var is_interactive = false;
var is_disabled = false;
var value: ?[]const u8 = null;
var options: ?[]OptionData = null;
var checked: ?bool = null;
var tag_name: []const u8 = "text";
var node_name: []const u8 = "text";
if (node.is(Element)) |el| {
tag_name = el.getTagNameLower();
node_name = el.getTagNameLower();
if (el.is(Element.Html.Input)) |input| {
value = input.getValue();
if (input._input_type == .checkbox or input._input_type == .radio) {
checked = input.getChecked();
}
if (el.getAttributeSafe(comptime .wrap("list"))) |list_id| {
if (el.getAttributeSafe(comptime lp.String.wrap("list"))) |list_id| {
options = try extractDataListOptions(list_id, self.page, self.arena);
}
} else if (el.is(Element.Html.TextArea)) |textarea| {
@@ -179,10 +172,8 @@ fn walk(
is_interactive = true;
}
}
is_disabled = el.isDisabled();
} else if (node._type == .document or node._type == .document_fragment) {
tag_name = "root";
node_name = "root";
}
const initial_xpath_len = ctx.xpath_buffer.items.len;
@@ -243,11 +234,9 @@ fn walk(
.name = name,
.value = value,
.options = options,
.checked = checked,
.xpath = xpath,
.interactive = is_interactive,
.disabled = is_disabled,
.tag_name = tag_name,
.is_interactive = is_interactive,
.node_name = node_name,
};
if (should_visit) {
@@ -346,7 +335,7 @@ const JsonVisitor = struct {
try self.jw.write(data.id);
try self.jw.objectField("nodeName");
try self.jw.write(data.tag_name);
try self.jw.write(data.node_name);
try self.jw.objectField("xpath");
try self.jw.write(data.xpath);
@@ -356,12 +345,7 @@ const JsonVisitor = struct {
try self.jw.write(1);
try self.jw.objectField("isInteractive");
try self.jw.write(data.interactive);
if (data.disabled) {
try self.jw.objectField("isDisabled");
try self.jw.write(true);
}
try self.jw.write(data.is_interactive);
try self.jw.objectField("role");
try self.jw.write(data.role);
@@ -389,11 +373,6 @@ const JsonVisitor = struct {
try self.jw.endObject();
}
if (data.checked) |checked| {
try self.jw.objectField("checked");
try self.jw.write(checked);
}
if (data.options) |options| {
try self.jw.objectField("options");
try self.jw.beginArray();
@@ -480,9 +459,6 @@ const TextVisitor = struct {
const is_text_only = std.mem.eql(u8, data.role, "StaticText") or std.mem.eql(u8, data.role, "none") or std.mem.eql(u8, data.role, "generic");
try self.writer.print("{d}", .{data.id});
if (data.interactive) {
try self.writer.writeAll(if (data.disabled) " [i:disabled]" else " [i]");
}
if (!is_text_only) {
try self.writer.print(" {s}", .{data.role});
}
@@ -496,14 +472,6 @@ const TextVisitor = struct {
}
}
if (data.checked) |c| {
if (c) {
try self.writer.writeAll(" [checked]");
} else {
try self.writer.writeAll(" [unchecked]");
}
}
if (data.options) |options| {
try self.writer.writeAll(" options=[");
for (options, 0..) |opt, i| {
@@ -541,189 +509,13 @@ const TextVisitor = struct {
}
};
pub const NodeDetails = struct {
backendNodeId: CDPNode.Id,
tag_name: []const u8,
role: []const u8,
name: ?[]const u8,
interactive: bool,
disabled: bool,
value: ?[]const u8 = null,
input_type: ?[]const u8 = null,
placeholder: ?[]const u8 = null,
href: ?[]const u8 = null,
id: ?[]const u8 = null,
class: ?[]const u8 = null,
checked: ?bool = null,
options: ?[]OptionData = null,
pub fn jsonStringify(self: *const NodeDetails, jw: anytype) !void {
try jw.beginObject();
try jw.objectField("backendNodeId");
try jw.write(self.backendNodeId);
try jw.objectField("tagName");
try jw.write(self.tag_name);
try jw.objectField("role");
try jw.write(self.role);
if (self.name) |n| {
try jw.objectField("name");
try jw.write(n);
}
try jw.objectField("isInteractive");
try jw.write(self.interactive);
if (self.disabled) {
try jw.objectField("isDisabled");
try jw.write(true);
}
if (self.value) |v| {
try jw.objectField("value");
try jw.write(v);
}
if (self.input_type) |v| {
try jw.objectField("inputType");
try jw.write(v);
}
if (self.placeholder) |v| {
try jw.objectField("placeholder");
try jw.write(v);
}
if (self.href) |v| {
try jw.objectField("href");
try jw.write(v);
}
if (self.id) |v| {
try jw.objectField("id");
try jw.write(v);
}
if (self.class) |v| {
try jw.objectField("class");
try jw.write(v);
}
if (self.checked) |c| {
try jw.objectField("checked");
try jw.write(c);
}
if (self.options) |opts| {
try jw.objectField("options");
try jw.beginArray();
for (opts) |opt| {
try jw.beginObject();
try jw.objectField("value");
try jw.write(opt.value);
try jw.objectField("text");
try jw.write(opt.text);
if (opt.selected) {
try jw.objectField("selected");
try jw.write(true);
}
try jw.endObject();
}
try jw.endArray();
}
try jw.endObject();
}
};
pub fn getNodeDetails(
arena: std.mem.Allocator,
node: *Node,
registry: *CDPNode.Registry,
page: *Page,
) !NodeDetails {
const cdp_node = try registry.register(node);
const axn = AXNode.fromNode(node);
const role = try axn.getRole();
const name = try axn.getName(page, arena);
var is_interactive = false;
var is_disabled = false;
var tag_name: []const u8 = "text";
var value: ?[]const u8 = null;
var input_type: ?[]const u8 = null;
var placeholder: ?[]const u8 = null;
var href: ?[]const u8 = null;
var id_attr: ?[]const u8 = null;
var class_attr: ?[]const u8 = null;
var checked: ?bool = null;
var options: ?[]OptionData = null;
if (node.is(Element)) |el| {
tag_name = el.getTagNameLower();
is_disabled = el.isDisabled();
id_attr = el.getAttributeSafe(comptime .wrap("id"));
class_attr = el.getAttributeSafe(comptime .wrap("class"));
placeholder = el.getAttributeSafe(comptime .wrap("placeholder"));
if (el.getAttributeSafe(comptime .wrap("href"))) |h| {
const URL = lp.URL;
href = URL.resolve(arena, page.base(), h, .{ .encode = true }) catch h;
}
if (el.is(Element.Html.Input)) |input| {
value = input.getValue();
input_type = input._input_type.toString();
if (input._input_type == .checkbox or input._input_type == .radio) {
checked = input.getChecked();
}
if (el.getAttributeSafe(comptime .wrap("list"))) |list_id| {
options = try extractDataListOptions(list_id, page, arena);
}
} else if (el.is(Element.Html.TextArea)) |textarea| {
value = textarea.getValue();
} else if (el.is(Element.Html.Select)) |select| {
value = select.getValue(page);
options = try extractSelectOptions(el.asNode(), page, arena);
}
if (el.is(Element.Html)) |html_el| {
const listener_targets = try interactive.buildListenerTargetMap(page, arena);
var pointer_events_cache: Element.PointerEventsCache = .empty;
if (interactive.classifyInteractivity(page, el, html_el, listener_targets, &pointer_events_cache) != null) {
is_interactive = true;
}
}
}
return .{
.backendNodeId = cdp_node.id,
.tag_name = tag_name,
.role = role,
.name = name,
.interactive = is_interactive,
.disabled = is_disabled,
.value = value,
.input_type = input_type,
.placeholder = placeholder,
.href = href,
.id = id_attr,
.class = class_attr,
.checked = checked,
.options = options,
};
}
const testing = @import("testing.zig");
test "SemanticTree backendDOMNodeId" {
var registry: CDPNode.Registry = .init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/registry1.html", .{});
var page = try testing.pageTest("cdp/registry1.html");
defer testing.reset();
defer page._session.removePage();
@@ -747,7 +539,7 @@ test "SemanticTree max_depth" {
var registry: CDPNode.Registry = .init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/registry1.html", .{});
var page = try testing.pageTest("cdp/registry1.html");
defer testing.reset();
defer page._session.removePage();

View File

@@ -32,6 +32,9 @@ const CookieJar = @import("webapi/storage/Cookie.zig").Jar;
const http = @import("../network/http.zig");
const Runtime = @import("../network/Runtime.zig");
const Robots = @import("../network/Robots.zig");
const Cache = @import("../network/cache/Cache.zig");
const CacheMetadata = Cache.CachedMetadata;
const CachedResponse = Cache.CachedResponse;
const IS_DEBUG = builtin.mode == .Debug;
@@ -311,7 +314,81 @@ pub fn request(self: *Client, req: Request) !void {
return self.fetchRobotsThenProcessRequest(robots_url, req);
}
fn serveFromCache(req: Request, cached: *const CachedResponse) !void {
const response = Response.fromCached(req.ctx, cached);
defer switch (cached.data) {
.buffer => |_| {},
.file => |f| f.file.close(),
};
if (req.start_callback) |cb| {
try cb(response);
}
const proceed = try req.header_callback(response);
if (!proceed) {
req.error_callback(req.ctx, error.Abort);
return;
}
switch (cached.data) {
.buffer => |data| {
if (data.len > 0) {
try req.data_callback(response, data);
}
},
.file => |f| {
const file = f.file;
var buf: [1024]u8 = undefined;
var file_reader = file.reader(&buf);
try file_reader.seekTo(f.offset);
const reader = &file_reader.interface;
var read_buf: [1024]u8 = undefined;
var remaining = f.len;
while (remaining > 0) {
const read_len = @min(read_buf.len, remaining);
const n = try reader.readSliceShort(read_buf[0..read_len]);
if (n == 0) break;
remaining -= n;
try req.data_callback(response, read_buf[0..n]);
}
},
}
try req.done_callback(req.ctx);
}
fn processRequest(self: *Client, req: Request) !void {
if (self.network.cache) |*cache| {
if (req.method == .GET) {
const arena = try self.network.app.arena_pool.acquire(.{ .debug = "HttpClient.processRequest.cache" });
defer self.network.app.arena_pool.release(arena);
var iter = req.headers.iterator();
const req_header_list = try iter.collect(arena);
if (cache.get(arena, .{
.url = req.url,
.timestamp = std.time.timestamp(),
.request_headers = req_header_list.items,
})) |cached| {
log.debug(.browser, "http.cache.get", .{
.url = req.url,
.found = true,
.metadata = cached.metadata,
});
defer req.headers.deinit();
return serveFromCache(req, &cached);
} else {
log.debug(.browser, "http.cache.get", .{ .url = req.url, .found = false });
}
}
}
const transfer = try self.makeTransfer(req);
transfer.req.notification.dispatch(.http_request_start, &.{ .transfer = transfer });
@@ -399,8 +476,10 @@ fn fetchRobotsThenProcessRequest(self: *Client, robots_url: [:0]const u8, req: R
try entry.value_ptr.append(self.allocator, req);
}
fn robotsHeaderCallback(transfer: *Transfer) !bool {
const ctx: *RobotsRequestContext = @ptrCast(@alignCast(transfer.ctx));
fn robotsHeaderCallback(response: Response) !bool {
const ctx: *RobotsRequestContext = @ptrCast(@alignCast(response.ctx));
// Robots callbacks only happen on real live requests.
const transfer = response.inner.transfer;
if (transfer.response_header) |hdr| {
log.debug(.browser, "robots status", .{ .status = hdr.status, .robots_url = ctx.robots_url });
@@ -414,8 +493,8 @@ fn robotsHeaderCallback(transfer: *Transfer) !bool {
return true;
}
fn robotsDataCallback(transfer: *Transfer, data: []const u8) !void {
const ctx: *RobotsRequestContext = @ptrCast(@alignCast(transfer.ctx));
fn robotsDataCallback(response: Response, data: []const u8) !void {
const ctx: *RobotsRequestContext = @ptrCast(@alignCast(response.ctx));
try ctx.buffer.appendSlice(ctx.client.allocator, data);
}
@@ -634,13 +713,43 @@ fn makeTransfer(self: *Client, req: Request) !*Transfer {
.id = id,
.url = req.url,
.req = req,
.ctx = req.ctx,
.client = self,
.max_response_size = self.network.config.httpMaxResponseSize(),
};
return transfer;
}
fn requestFailed(transfer: *Transfer, err: anyerror, comptime execute_callback: bool) void {
if (transfer._notified_fail) {
// we can force a failed request within a callback, which will eventually
// result in this being called again in the more general loop. We do this
// because we can raise a more specific error inside a callback in some cases
return;
}
transfer._notified_fail = true;
transfer.req.notification.dispatch(.http_request_fail, &.{
.transfer = transfer,
.err = err,
});
if (execute_callback) {
transfer.req.error_callback(transfer.req.ctx, err);
} else if (transfer.req.shutdown_callback) |cb| {
cb(transfer.req.ctx);
}
}
// Same restriction as changeProxy. Should be ok since this is only called on
// BrowserContext deinit.
pub fn restoreOriginalProxy(self: *Client) !void {
try self.ensureNoActiveConnection();
self.http_proxy = self.network.config.httpProxy();
self.use_proxy = self.http_proxy != null;
}
fn makeRequest(self: *Client, conn: *http.Connection, transfer: *Transfer) anyerror!void {
{
// Reset per-response state for retries (auth challenge, queue).
@@ -674,7 +783,7 @@ fn makeRequest(self: *Client, conn: *http.Connection, transfer: *Transfer) anyer
self.active += 1;
if (transfer.req.start_callback) |cb| {
cb(transfer) catch |err| {
cb(Response.fromTransfer(transfer)) catch |err| {
transfer.deinit();
return err;
};
@@ -742,7 +851,10 @@ fn processOneMessage(self: *Client, msg: http.Handles.MultiMessage, transfer: *T
// TODO give a way to configure the number of auth retries.
if (transfer._auth_challenge != null and transfer._tries < 10) {
var wait_for_interception = false;
transfer.req.notification.dispatch(.http_request_auth_required, &.{ .transfer = transfer, .wait_for_interception = &wait_for_interception });
transfer.req.notification.dispatch(
.http_request_auth_required,
&.{ .transfer = transfer, .wait_for_interception = &wait_for_interception },
);
if (wait_for_interception) {
self.intercepted += 1;
if (comptime IS_DEBUG) {
@@ -841,10 +953,11 @@ fn processOneMessage(self: *Client, msg: http.Handles.MultiMessage, transfer: *T
}
}
const body = transfer._stream_buffer.items;
// Replay buffered body through user's data_callback.
if (transfer._stream_buffer.items.len > 0) {
const body = transfer._stream_buffer.items;
try transfer.req.data_callback(transfer, body);
try transfer.req.data_callback(Response.fromTransfer(transfer), body);
transfer.req.notification.dispatch(.http_response_data, &.{
.data = body,
@@ -861,7 +974,21 @@ fn processOneMessage(self: *Client, msg: http.Handles.MultiMessage, transfer: *T
// will load more resources.
transfer.releaseConn();
try transfer.req.done_callback(transfer.ctx);
try transfer.req.done_callback(transfer.req.ctx);
if (transfer._pending_cache_metadata) |metadata| {
const cache = &self.network.cache.?;
// TODO: Support Vary Keying
const cache_key = transfer.req.url;
log.debug(.browser, "http cache", .{ .key = cache_key, .metadata = metadata });
cache.put(metadata, body) catch |err| {
log.warn(.http, "cache put failed", .{ .err = err });
};
log.debug(.browser, "http.cache.put", .{ .url = transfer.req.url });
}
transfer.req.notification.dispatch(.http_request_done, &.{
.transfer = transfer,
});
@@ -939,9 +1066,9 @@ pub const Request = struct {
// arbitrary data that can be associated with this request
ctx: *anyopaque = undefined,
start_callback: ?*const fn (transfer: *Transfer) anyerror!void = null,
header_callback: *const fn (transfer: *Transfer) anyerror!bool,
data_callback: *const fn (transfer: *Transfer, data: []const u8) anyerror!void,
start_callback: ?*const fn (response: Response) anyerror!void = null,
header_callback: *const fn (response: Response) anyerror!bool,
data_callback: *const fn (response: Response, data: []const u8) anyerror!void,
done_callback: *const fn (ctx: *anyopaque) anyerror!void,
error_callback: *const fn (ctx: *anyopaque, err: anyerror) void,
shutdown_callback: ?*const fn (ctx: *anyopaque) void = null,
@@ -967,16 +1094,84 @@ pub const Request = struct {
};
};
pub const Response = struct {
ctx: *anyopaque,
inner: union(enum) {
transfer: *Transfer,
cached: *const CachedResponse,
},
pub fn fromTransfer(transfer: *Transfer) Response {
return .{ .ctx = transfer.req.ctx, .inner = .{ .transfer = transfer } };
}
pub fn fromCached(ctx: *anyopaque, resp: *const CachedResponse) Response {
return .{ .ctx = ctx, .inner = .{ .cached = resp } };
}
pub fn status(self: Response) ?u16 {
return switch (self.inner) {
.transfer => |t| if (t.response_header) |rh| rh.status else null,
.cached => |c| c.metadata.status,
};
}
pub fn contentType(self: Response) ?[]const u8 {
return switch (self.inner) {
.transfer => |t| if (t.response_header) |*rh| rh.contentType() else null,
.cached => |c| c.metadata.content_type,
};
}
pub fn contentLength(self: Response) ?u32 {
return switch (self.inner) {
.transfer => |t| t.getContentLength(),
.cached => |c| switch (c.data) {
.buffer => |buf| @intCast(buf.len),
.file => |f| @intCast(f.len),
},
};
}
pub fn redirectCount(self: Response) ?u32 {
return switch (self.inner) {
.transfer => |t| if (t.response_header) |rh| rh.redirect_count else null,
.cached => 0,
};
}
pub fn url(self: Response) [:0]const u8 {
return switch (self.inner) {
.transfer => |t| t.url,
.cached => |c| c.metadata.url,
};
}
pub fn headerIterator(self: Response) HeaderIterator {
return switch (self.inner) {
.transfer => |t| t.responseHeaderIterator(),
.cached => |c| HeaderIterator{ .list = .{ .list = c.metadata.headers } },
};
}
pub fn abort(self: Response, err: anyerror) void {
switch (self.inner) {
.transfer => |t| t.abort(err),
.cached => {},
}
}
};
pub const Transfer = struct {
arena: ArenaAllocator,
id: u32 = 0,
req: Request,
url: [:0]const u8,
ctx: *anyopaque, // copied from req.ctx to make it easier for callback handlers
client: *Client,
// total bytes received in the response, including the response status line,
// the headers, and the [encoded] body.
bytes_received: usize = 0,
_pending_cache_metadata: ?CacheMetadata = null,
aborted: bool = false,
@@ -1065,7 +1260,7 @@ pub const Transfer = struct {
// as abort (doesn't send a notification, doesn't invoke an error callback)
fn kill(self: *Transfer) void {
if (self.req.shutdown_callback) |cb| {
cb(self.ctx);
cb(self.req.ctx);
}
if (self._performing or self.client.performing) {
@@ -1101,7 +1296,7 @@ pub const Transfer = struct {
});
if (execute_callback) {
self.req.error_callback(self.ctx, err);
self.req.error_callback(self.req.ctx, err);
} else if (self.req.shutdown_callback) |cb| {
cb(self.ctx);
}
@@ -1348,15 +1543,64 @@ pub const Transfer = struct {
}
}
transfer.req.notification.dispatch(.http_response_header_done, &.{
.transfer = transfer,
});
const proceed = transfer.req.header_callback(transfer) catch |err| {
const proceed = transfer.req.header_callback(Response.fromTransfer(transfer)) catch |err| {
log.err(.http, "header_callback", .{ .err = err, .req = transfer });
return err;
};
if (transfer.client.network.cache != null and transfer.req.method == .GET) {
const rh = &transfer.response_header.?;
const allocator = transfer.arena.allocator();
const vary = if (conn.getResponseHeader("vary", 0)) |h| h.value else null;
const maybe_cm = try Cache.tryCache(
allocator,
std.time.timestamp(),
transfer.url,
rh.status,
rh.contentType(),
if (conn.getResponseHeader("cache-control", 0)) |h| h.value else null,
vary,
if (conn.getResponseHeader("age", 0)) |h| h.value else null,
conn.getResponseHeader("set-cookie", 0) != null,
conn.getResponseHeader("authorization", 0) != null,
);
if (maybe_cm) |cm| {
transfer._pending_cache_metadata = cm;
var iter = transfer.responseHeaderIterator();
var header_list = try iter.collect(allocator);
const end_of_response = header_list.items.len;
transfer._pending_cache_metadata.?.headers = header_list.items[0..end_of_response];
if (vary) |vary_str| {
var req_it = transfer.req.headers.iterator();
while (req_it.next()) |hdr| {
var vary_iter = std.mem.splitScalar(u8, vary_str, ',');
while (vary_iter.next()) |part| {
const name = std.mem.trim(u8, part, &std.ascii.whitespace);
if (std.ascii.eqlIgnoreCase(hdr.name, name)) {
try header_list.append(allocator, .{
.name = try allocator.dupe(u8, hdr.name),
.value = try allocator.dupe(u8, hdr.value),
});
}
}
}
transfer._pending_cache_metadata.?.vary_headers = header_list.items[end_of_response..];
}
}
}
transfer.req.notification.dispatch(.http_response_header_done, &.{
.transfer = transfer,
});
return proceed and transfer.aborted == false;
}
@@ -1455,7 +1699,7 @@ pub const Transfer = struct {
fn _fulfill(transfer: *Transfer, status: u16, headers: []const http.Header, body: ?[]const u8) !void {
const req = &transfer.req;
if (req.start_callback) |cb| {
try cb(transfer);
try cb(Response.fromTransfer(transfer));
}
transfer.response_header = .{
@@ -1474,13 +1718,13 @@ pub const Transfer = struct {
}
lp.assert(transfer._header_done_called == false, "Transfer.fulfill header_done_called", .{});
if (try req.header_callback(transfer) == false) {
if (try req.header_callback(Response.fromTransfer(transfer)) == false) {
transfer.abort(error.Abort);
return;
}
if (body) |b| {
try req.data_callback(transfer, b);
try req.data_callback(Response.fromTransfer(transfer), b);
}
try req.done_callback(req.ctx);
@@ -1517,10 +1761,10 @@ pub const Transfer = struct {
};
const Noop = struct {
fn headerCallback(_: *Transfer) !bool {
fn headerCallback(_: Response) !bool {
return true;
}
fn dataCallback(_: *Transfer, _: []const u8) !void {}
fn dataCallback(_: Response, _: []const u8) !void {}
fn doneCallback(_: *anyopaque) !void {}
fn errorCallback(_: *anyopaque, _: anyerror) void {}
};

View File

@@ -27,6 +27,9 @@ charset: [41]u8 = default_charset,
charset_len: usize = default_charset_len,
is_default_charset: bool = true,
type_buf: [127]u8 = @splat(0),
sub_type_buf: [127]u8 = @splat(0),
/// String "UTF-8" continued by null characters.
const default_charset = .{ 'U', 'T', 'F', '-', '8' } ++ .{0} ** 36;
const default_charset_len = 5;
@@ -61,7 +64,10 @@ pub const ContentType = union(ContentTypeEnum) {
image_webp: void,
application_json: void,
unknown: void,
other: struct { type: []const u8, sub_type: []const u8 },
other: struct {
type: []const u8,
sub_type: []const u8,
},
};
pub fn contentTypeString(mime: *const Mime) []const u8 {
@@ -112,17 +118,18 @@ fn parseCharset(value: []const u8) error{ CharsetTooBig, Invalid }![]const u8 {
return value;
}
pub fn parse(input: []u8) !Mime {
pub fn parse(input: []const u8) !Mime {
if (input.len > 255) {
return error.TooBig;
}
// Zig's trim API is broken. The return type is always `[]const u8`,
// even if the input type is `[]u8`. @constCast is safe here.
var normalized = @constCast(std.mem.trim(u8, input, &std.ascii.whitespace));
var buf: [255]u8 = undefined;
const normalized = std.ascii.lowerString(&buf, std.mem.trim(u8, input, &std.ascii.whitespace));
_ = std.ascii.lowerString(normalized, normalized);
const content_type, const type_len = try parseContentType(normalized);
var mime = Mime{ .content_type = undefined };
const content_type, const type_len = try parseContentType(normalized, &mime.type_buf, &mime.sub_type_buf);
if (type_len >= normalized.len) {
return .{ .content_type = content_type };
}
@@ -163,13 +170,12 @@ pub fn parse(input: []u8) !Mime {
}
}
return .{
.params = params,
.charset = charset,
.charset_len = charset_len,
.content_type = content_type,
.is_default_charset = !has_explicit_charset,
};
mime.params = params;
mime.charset = charset;
mime.charset_len = charset_len;
mime.content_type = content_type;
mime.is_default_charset = !has_explicit_charset;
return mime;
}
/// Prescan the first 1024 bytes of an HTML document for a charset declaration.
@@ -395,7 +401,7 @@ pub fn isText(mime: *const Mime) bool {
}
// we expect value to be lowercase
fn parseContentType(value: []const u8) !struct { ContentType, usize } {
fn parseContentType(value: []const u8, type_buf: []u8, sub_type_buf: []u8) !struct { ContentType, usize } {
const end = std.mem.indexOfScalarPos(u8, value, 0, ';') orelse value.len;
const type_name = trimRight(value[0..end]);
const attribute_start = end + 1;
@@ -444,10 +450,18 @@ fn parseContentType(value: []const u8) !struct { ContentType, usize } {
return error.Invalid;
}
return .{ .{ .other = .{
.type = main_type,
.sub_type = sub_type,
} }, attribute_start };
@memcpy(type_buf[0..main_type.len], main_type);
@memcpy(sub_type_buf[0..sub_type.len], sub_type);
return .{
.{
.other = .{
.type = type_buf[0..main_type.len],
.sub_type = sub_type_buf[0..sub_type.len],
},
},
attribute_start,
};
}
const VALID_CODEPOINTS = blk: {
@@ -461,6 +475,13 @@ const VALID_CODEPOINTS = blk: {
break :blk v;
};
pub fn typeString(self: *const Mime) []const u8 {
return switch (self.content_type) {
.other => |o| o.type[0..o.type_len],
else => "",
};
}
fn validType(value: []const u8) bool {
for (value) |b| {
if (VALID_CODEPOINTS[b] == false) {

View File

@@ -487,6 +487,7 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
return error.InjectBlankFailed;
};
}
self.documentIsComplete();
session.notification.dispatch(.page_navigate, &.{
.frame_id = self._frame_id,
@@ -518,8 +519,6 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
// force next request id manually b/c we won't create a real req.
_ = session.browser.http_client.incrReqId();
self.documentIsComplete();
return;
}
@@ -739,12 +738,6 @@ pub fn _documentIsLoaded(self: *Page) !void {
self.document.asEventTarget(),
event,
);
self._session.notification.dispatch(.page_dom_content_loaded, &.{
.frame_id = self._frame_id,
.req_id = self._req_id,
.timestamp = timestamp(.monotonic),
});
}
pub fn scriptsCompletedLoading(self: *Page) void {
@@ -803,6 +796,19 @@ pub fn documentIsComplete(self: *Page) void {
self._documentIsComplete() catch |err| {
log.err(.page, "document is complete", .{ .err = err, .type = self._type, .url = self.url });
};
if (self._navigated_options) |no| {
// _navigated_options will be null in special short-circuit cases, like
// "navigating" to about:blank, in which case this notification has
// already been sent
self._session.notification.dispatch(.page_navigated, &.{
.frame_id = self._frame_id,
.req_id = self._req_id,
.opts = no,
.url = self.url,
.timestamp = timestamp(.monotonic),
});
}
}
fn _documentIsComplete(self: *Page) !void {
@@ -821,12 +827,6 @@ fn _documentIsComplete(self: *Page) !void {
try self._event_manager.dispatchDirect(window_target, event, self.window._on_load, .{ .inject_target = false, .context = "page load" });
}
self._session.notification.dispatch(.page_loaded, &.{
.frame_id = self._frame_id,
.req_id = self._req_id,
.timestamp = timestamp(.monotonic),
});
if (self._event_manager.hasDirectListeners(window_target, "pageshow", self.window._on_pageshow)) {
const pageshow_event = (try PageTransitionEvent.initTrusted(comptime .wrap("pageshow"), .{}, self)).asEvent();
try self._event_manager.dispatchDirect(window_target, pageshow_event, self.window._on_pageshow, .{ .context = "page show" });
@@ -854,12 +854,10 @@ fn notifyParentLoadComplete(self: *Page) void {
parent.iframeCompletedLoading(self.iframe.?);
}
fn pageHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
var self: *Page = @ptrCast(@alignCast(transfer.ctx));
fn pageHeaderDoneCallback(response: HttpClient.Response) !bool {
var self: *Page = @ptrCast(@alignCast(response.ctx));
const header = &transfer.response_header.?;
const response_url = std.mem.span(header.url);
const response_url = response.url();
if (std.mem.eql(u8, response_url, self.url) == false) {
// would be different than self.url in the case of a redirect
self.url = try self.arena.dupeZ(u8, response_url);
@@ -873,36 +871,23 @@ fn pageHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
if (comptime IS_DEBUG) {
log.debug(.page, "navigate header", .{
.url = self.url,
.status = header.status,
.content_type = header.contentType(),
.status = response.status(),
.content_type = response.contentType(),
.type = self._type,
});
}
if (self._navigated_options) |no| {
// _navigated_options will be null in special short-circuit cases, like
// "navigating" to about:blank, in which case this notification has
// already been sent
self._session.notification.dispatch(.page_navigated, &.{
.frame_id = self._frame_id,
.req_id = self._req_id,
.opts = no,
.url = self.url,
.timestamp = timestamp(.monotonic),
});
}
return true;
}
fn pageDataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
var self: *Page = @ptrCast(@alignCast(transfer.ctx));
fn pageDataCallback(response: HttpClient.Response, data: []const u8) !void {
var self: *Page = @ptrCast(@alignCast(response.ctx));
if (self._parse_state == .pre) {
// we lazily do this, because we might need the first chunk of data
// to sniff the content type
var mime: Mime = blk: {
if (transfer.response_header.?.contentType()) |ct| {
if (response.contentType()) |ct| {
break :blk try Mime.parse(ct);
}
break :blk Mime.sniff(data);

View File

@@ -22,14 +22,10 @@ const builtin = @import("builtin");
const log = @import("../log.zig");
const js = @import("js/js.zig");
const Page = @import("Page.zig");
const Session = @import("Session.zig");
const HttpClient = @import("HttpClient.zig");
const Node = @import("webapi/Node.zig");
const Selector = @import("webapi/selector/Selector.zig");
const IS_DEBUG = builtin.mode == .Debug;
const Runner = @This();
@@ -240,111 +236,3 @@ fn _tick(self: *Runner, comptime is_cdp: bool, opts: TickOpts) !CDPTickResult {
.raw_done => return .done,
}
}
pub fn waitForSelector(self: *Runner, selector: [:0]const u8, timeout_ms: u32) !*Node.Element {
const arena = try self.session.getArena(.{ .debug = "Runner.waitForSelector" });
defer self.session.releaseArena(arena);
var timer = try std.time.Timer.start();
const parsed_selector = try Selector.parseLeaky(arena, selector);
while (true) {
// self.page can change between ticks
const page = self.page;
if (try parsed_selector.query(page.document.asNode(), page)) |el| {
return el;
}
const elapsed: u32 = @intCast(timer.read() / std.time.ns_per_ms);
if (elapsed >= timeout_ms) {
return error.Timeout;
}
switch (try self.tick(.{ .ms = timeout_ms - elapsed })) {
.done => return error.Timeout,
.ok => |recommended_sleep_ms| {
if (recommended_sleep_ms > 0) {
std.Thread.sleep(std.time.ns_per_ms * recommended_sleep_ms);
}
},
}
}
}
pub fn waitForScript(runner: *Runner, script: [:0]const u8, timeout_ms: u32) !void {
var timer = try std.time.Timer.start();
while (true) {
const page = runner.page;
// Execute the script and check if it returns truthy
var ls: js.Local.Scope = undefined;
page.js.localScope(&ls);
defer ls.deinit();
var try_catch: js.TryCatch = undefined;
try_catch.init(&ls.local);
defer try_catch.deinit();
const value = ls.local.exec(script, "wait_script") catch |err| {
const caught = try_catch.caughtOrError(page.call_arena, err);
log.err(.app, "wait script error", .{ .err = caught });
return error.ScriptError;
};
if (value.toBool()) {
return;
}
const elapsed: u32 = @intCast(timer.read() / std.time.ns_per_ms);
if (elapsed >= timeout_ms) {
return error.Timeout;
}
switch (try runner.tick(.{ .ms = timeout_ms - elapsed })) {
.done => return error.Timeout,
.ok => |recommended_sleep_ms| {
if (recommended_sleep_ms > 0) {
std.Thread.sleep(std.time.ns_per_ms * recommended_sleep_ms);
}
},
}
}
}
const testing = @import("../testing.zig");
test "Runner: no page" {
try testing.expectError(error.NoPage, Runner.init(testing.test_session, .{}));
}
test "Runner: waitForSelector timeout" {
const page = try testing.pageTest("runner/runner1.html", .{});
defer page._session.removePage();
var runner = try page._session.runner(.{});
try testing.expectError(error.Timeout, runner.waitForSelector("#nope", 10));
}
test "Runner: waitForSelector" {
defer testing.reset();
const page = try testing.pageTest("runner/runner1.html", .{});
defer page._session.removePage();
var runner = try page._session.runner(.{});
const el = try runner.waitForSelector("#sel1", 10);
try testing.expectEqual("selector-1-content", try el.asNode().getTextContentAlloc(testing.arena_allocator));
}
test "Runner: waitForScript timeout" {
const page = try testing.pageTest("runner/runner1.html", .{});
defer page._session.removePage();
var runner = try page._session.runner(.{});
try testing.expectError(error.Timeout, runner.waitForScript("document.querySelector('#nope')", 10));
}
test "Runner: waitForScript" {
const page = try testing.pageTest("runner/runner1.html", .{});
defer page._session.removePage();
var runner = try page._session.runner(.{});
try runner.waitForScript("document.querySelector('#sel1')", 10);
}

View File

@@ -694,82 +694,86 @@ pub const Script = struct {
self.manager.page.releaseArena(self.arena);
}
fn startCallback(transfer: *HttpClient.Transfer) !void {
log.debug(.http, "script fetch start", .{ .req = transfer });
fn startCallback(response: HttpClient.Response) !void {
log.debug(.http, "script fetch start", .{ .req = response });
}
fn headerCallback(transfer: *HttpClient.Transfer) !bool {
const self: *Script = @ptrCast(@alignCast(transfer.ctx));
const header = &transfer.response_header.?;
self.status = header.status;
if (header.status != 200) {
fn headerCallback(response: HttpClient.Response) !bool {
const self: *Script = @ptrCast(@alignCast(response.ctx));
self.status = response.status().?;
if (response.status() != 200) {
log.info(.http, "script header", .{
.req = transfer,
.status = header.status,
.content_type = header.contentType(),
.req = response,
.status = response.status(),
.content_type = response.contentType(),
});
return false;
}
if (comptime IS_DEBUG) {
log.debug(.http, "script header", .{
.req = transfer,
.status = header.status,
.content_type = header.contentType(),
.req = response,
.status = response.status(),
.content_type = response.contentType(),
});
}
{
// temp debug, trying to figure out why the next assert sometimes
// fails. Is the buffer just corrupt or is headerCallback really
// being called twice?
lp.assert(self.header_callback_called == false, "ScriptManager.Header recall", .{
.m = @tagName(std.meta.activeTag(self.mode)),
.a1 = self.debug_transfer_id,
.a2 = self.debug_transfer_tries,
.a3 = self.debug_transfer_aborted,
.a4 = self.debug_transfer_bytes_received,
.a5 = self.debug_transfer_notified_fail,
.a7 = self.debug_transfer_intercept_state,
.a8 = self.debug_transfer_auth_challenge,
.a9 = self.debug_transfer_easy_id,
.b1 = transfer.id,
.b2 = transfer._tries,
.b3 = transfer.aborted,
.b4 = transfer.bytes_received,
.b5 = transfer._notified_fail,
.b7 = @intFromEnum(transfer._intercept_state),
.b8 = transfer._auth_challenge != null,
.b9 = if (transfer._conn) |c| @intFromPtr(c._easy) else 0,
});
self.header_callback_called = true;
self.debug_transfer_id = transfer.id;
self.debug_transfer_tries = transfer._tries;
self.debug_transfer_aborted = transfer.aborted;
self.debug_transfer_bytes_received = transfer.bytes_received;
self.debug_transfer_notified_fail = transfer._notified_fail;
self.debug_transfer_intercept_state = @intFromEnum(transfer._intercept_state);
self.debug_transfer_auth_challenge = transfer._auth_challenge != null;
self.debug_transfer_easy_id = if (transfer._conn) |c| @intFromPtr(c._easy) else 0;
switch (response.inner) {
.transfer => |transfer| {
// temp debug, trying to figure out why the next assert sometimes
// fails. Is the buffer just corrupt or is headerCallback really
// being called twice?
lp.assert(self.header_callback_called == false, "ScriptManager.Header recall", .{
.m = @tagName(std.meta.activeTag(self.mode)),
.a1 = self.debug_transfer_id,
.a2 = self.debug_transfer_tries,
.a3 = self.debug_transfer_aborted,
.a4 = self.debug_transfer_bytes_received,
.a5 = self.debug_transfer_notified_fail,
.a7 = self.debug_transfer_intercept_state,
.a8 = self.debug_transfer_auth_challenge,
.a9 = self.debug_transfer_easy_id,
.b1 = transfer.id,
.b2 = transfer._tries,
.b3 = transfer.aborted,
.b4 = transfer.bytes_received,
.b5 = transfer._notified_fail,
.b7 = @intFromEnum(transfer._intercept_state),
.b8 = transfer._auth_challenge != null,
.b9 = if (transfer._conn) |c| @intFromPtr(c._easy) else 0,
});
self.header_callback_called = true;
self.debug_transfer_id = transfer.id;
self.debug_transfer_tries = transfer._tries;
self.debug_transfer_aborted = transfer.aborted;
self.debug_transfer_bytes_received = transfer.bytes_received;
self.debug_transfer_notified_fail = transfer._notified_fail;
self.debug_transfer_intercept_state = @intFromEnum(transfer._intercept_state);
self.debug_transfer_auth_challenge = transfer._auth_challenge != null;
self.debug_transfer_easy_id = if (transfer._conn) |c| @intFromPtr(c._easy) else 0;
},
else => {},
}
lp.assert(self.source.remote.capacity == 0, "ScriptManager.Header buffer", .{ .capacity = self.source.remote.capacity });
var buffer: std.ArrayList(u8) = .empty;
if (transfer.getContentLength()) |cl| {
if (response.contentLength()) |cl| {
try buffer.ensureTotalCapacity(self.arena, cl);
}
self.source = .{ .remote = buffer };
return true;
}
fn dataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
const self: *Script = @ptrCast(@alignCast(transfer.ctx));
self._dataCallback(transfer, data) catch |err| {
log.err(.http, "SM.dataCallback", .{ .err = err, .transfer = transfer, .len = data.len });
fn dataCallback(response: HttpClient.Response, data: []const u8) !void {
const self: *Script = @ptrCast(@alignCast(response.ctx));
self._dataCallback(response, data) catch |err| {
log.err(.http, "SM.dataCallback", .{ .err = err, .transfer = response, .len = data.len });
return err;
};
}
fn _dataCallback(self: *Script, _: *HttpClient.Transfer, data: []const u8) !void {
fn _dataCallback(self: *Script, _: HttpClient.Response, data: []const u8) !void {
try self.source.remote.appendSlice(self.arena, data);
}

View File

@@ -114,7 +114,7 @@ fn addRawRule(self: *StyleManager, selector_text: []const u8, block_text: []cons
if (!props.isRelevant()) return;
const selectors = SelectorParser.parseList(self.arena, selector_text) catch return;
const selectors = SelectorParser.parseList(self.arena, selector_text, self.page) catch return;
for (selectors) |selector| {
const rightmost = if (selector.segments.len > 0) selector.segments[selector.segments.len - 1].compound else selector.first;
const bucket_key = getBucketKey(rightmost) orelse continue;
@@ -484,7 +484,7 @@ fn addRule(self: *StyleManager, style_rule: *CSSStyleRule) !void {
}
// Parse the selector list
const selectors = SelectorParser.parseList(self.arena, selector_text) catch return;
const selectors = SelectorParser.parseList(self.arena, selector_text, self.page) catch return;
if (selectors.len == 0) {
return;
}

View File

@@ -110,10 +110,28 @@ pub fn waitForSelector(selector: [:0]const u8, timeout_ms: u32, session: *Sessio
var runner = try session.runner(.{});
try runner.wait(.{ .ms = timeout_ms, .until = .load });
const elapsed: u32 = @intCast(timer.read() / std.time.ns_per_ms);
const remaining = timeout_ms -| elapsed;
if (remaining == 0) return error.Timeout;
while (true) {
const page = runner.page;
const element = Selector.querySelector(page.document.asNode(), selector, page) catch {
return error.InvalidSelector;
};
const el = try runner.waitForSelector(selector, remaining);
return el.asNode();
if (element) |el| {
return el.asNode();
}
const elapsed: u32 = @intCast(timer.read() / std.time.ns_per_ms);
if (elapsed >= timeout_ms) {
return error.Timeout;
}
switch (try runner.tick(.{ .ms = timeout_ms - elapsed })) {
.done => return error.Timeout,
.ok => |recommended_sleep_ms| {
if (recommended_sleep_ms > 0) {
// guanrateed to be <= 20ms
std.Thread.sleep(std.time.ns_per_ms * recommended_sleep_ms);
}
},
}
}
}

View File

@@ -104,6 +104,18 @@ pub fn Builder(comptime T: type) type {
};
}
fn releaseRef(comptime T: type, ptr_id: usize, session: *Session) void {
if (@hasDecl(T, "releaseRef")) {
T.releaseRef(@ptrFromInt(ptr_id), session);
return;
}
if (@hasField(T, "_proto")) {
releaseRef(Struct(std.meta.fieldInfo(T, ._proto).type), ptr_id, session);
return;
}
@compileError(@typeName(T) ++ " marked with finalizer without an acquireRef in its prototype chain");
}
pub const Constructor = struct {
func: *const fn (?*const v8.FunctionCallbackInfo) callconv(.c) void,

View File

@@ -20,8 +20,8 @@
<script id=urlSearchParams>
const inputs = [
[["over", "9000!!"], ["abc", "123"], ["key1", ""], ["key2", ""]],
{over: "9000!!", abc: 123, key1: "", key2: ""},
// @ZIGDOM [["over", "9000!!"], ["abc", 123], ["key1", ""], ["key2", ""]],
{over: "9000!!", abc: 123, key1: "", key2: ""},
"over=9000!!&abc=123&key1&key2=",
"?over=9000!!&abc=123&key1&key2=",
]
@@ -367,49 +367,3 @@
testing.expectEqual(['3'], ups.getAll('b'));
}
</script>
<script id=arrayOfArrays>
{
const usp = new URLSearchParams([["a", "1"], ["b", "2"], ["a", "3"]]);
testing.expectEqual(3, usp.size);
testing.expectEqual('1', usp.get('a'));
testing.expectEqual(['1', '3'], usp.getAll('a'));
testing.expectEqual('2', usp.get('b'));
testing.expectEqual('a=1&b=2&a=3', usp.toString());
}
</script>
<script id=arrayOfArraysEmpty>
{
const usp = new URLSearchParams([]);
testing.expectEqual(0, usp.size);
testing.expectEqual('', usp.toString());
}
</script>
<script id=arrayOfArraysDuplicateKeys>
{
const usp = new URLSearchParams([["key", "first"], ["key", "second"], ["key", "third"]]);
testing.expectEqual(3, usp.size);
testing.expectEqual('first', usp.get('key'));
testing.expectEqual(['first', 'second', 'third'], usp.getAll('key'));
}
</script>
<script id=arrayOfArraysSpecialChars>
{
const usp = new URLSearchParams([["q", "hello world"], ["url", "https://example.com/?a=1&b=2"]]);
testing.expectEqual(2, usp.size);
testing.expectEqual('hello world', usp.get('q'));
testing.expectEqual('https://example.com/?a=1&b=2', usp.get('url'));
}
</script>
<script id=arrayOfArraysNumericValues>
{
const usp = new URLSearchParams([["count", 42], ["pi", 3.14]]);
testing.expectEqual(2, usp.size);
testing.expectEqual('42', usp.get('count'));
testing.expectEqual('3.14', usp.get('pi'));
}
</script>

View File

@@ -1,4 +0,0 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<div id=sel0>selector-0-content</div>
<div id=sel1>selector-1-content</div>

View File

@@ -26,8 +26,6 @@ const GenericIterator = @import("../collections/iterator.zig").Entry;
const Page = @import("../../Page.zig");
const String = @import("../../../string.zig").String;
const Allocator = std.mem.Allocator;
const IS_DEBUG = @import("builtin").mode == .Debug;
pub fn registerTypes() []const type {
@@ -421,7 +419,7 @@ pub fn validateAttributeName(name: String) !void {
}
}
fn normalizeNameForLookup(name: String, page: *Page) !String {
pub fn normalizeNameForLookup(name: String, page: *Page) !String {
if (!needsLowerCasing(name.str())) {
return name;
}
@@ -433,14 +431,6 @@ fn normalizeNameForLookup(name: String, page: *Page) !String {
return .wrap(normalized);
}
pub fn normalizeNameForLookupAlloc(allocator: Allocator, name: String) !String {
if (!needsLowerCasing(name.str())) {
return name.dupe(allocator);
}
const normalized = try std.ascii.allocLowerString(allocator, name.str());
return .wrap(normalized);
}
fn needsLowerCasing(name: []const u8) bool {
var remaining = name;
if (comptime std.simd.suggestVectorLength(u8)) |vector_len| {

View File

@@ -127,16 +127,16 @@ fn handleBlobUrl(url: []const u8, resolver: js.PromiseResolver, page: *Page) !js
return resolver.promise();
}
fn httpStartCallback(transfer: *HttpClient.Transfer) !void {
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
fn httpStartCallback(response: HttpClient.Response) !void {
const self: *Fetch = @ptrCast(@alignCast(response.ctx));
if (comptime IS_DEBUG) {
log.debug(.http, "request start", .{ .url = self._url, .source = "fetch" });
}
self._response._transfer = transfer;
self._response._http_response = response;
}
fn httpHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
fn httpHeaderDoneCallback(response: HttpClient.Response) !bool {
const self: *Fetch = @ptrCast(@alignCast(response.ctx));
if (self._signal) |signal| {
if (signal._aborted) {
@@ -145,25 +145,24 @@ fn httpHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
}
const arena = self._response._arena;
if (transfer.getContentLength()) |cl| {
if (response.contentLength()) |cl| {
try self._buf.ensureTotalCapacity(arena, cl);
}
const res = self._response;
const header = transfer.response_header.?;
if (comptime IS_DEBUG) {
log.debug(.http, "request header", .{
.source = "fetch",
.url = self._url,
.status = header.status,
.status = response.status(),
});
}
res._status = header.status;
res._status_text = std.http.Status.phrase(@enumFromInt(header.status)) orelse "";
res._url = try arena.dupeZ(u8, std.mem.span(header.url));
res._is_redirected = header.redirect_count > 0;
res._status = response.status().?;
res._status_text = std.http.Status.phrase(@enumFromInt(response.status().?)) orelse "";
res._url = try arena.dupeZ(u8, response.url());
res._is_redirected = response.redirectCount().? > 0;
// Determine response type based on origin comparison
const page_origin = URL.getOrigin(arena, self._page.url) catch null;
@@ -183,7 +182,7 @@ fn httpHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
res._type = .basic;
}
var it = transfer.responseHeaderIterator();
var it = response.headerIterator();
while (it.next()) |hdr| {
try res._headers.append(hdr.name, hdr.value, self._page);
}
@@ -191,8 +190,8 @@ fn httpHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
return true;
}
fn httpDataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
fn httpDataCallback(response: HttpClient.Response, data: []const u8) !void {
const self: *Fetch = @ptrCast(@alignCast(response.ctx));
// Check if aborted
if (self._signal) |signal| {
@@ -207,7 +206,7 @@ fn httpDataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
fn httpDoneCallback(ctx: *anyopaque) !void {
const self: *Fetch = @ptrCast(@alignCast(ctx));
var response = self._response;
response._transfer = null;
response._http_response = null;
response._body = self._buf.items;
log.info(.http, "request complete", .{
@@ -230,7 +229,7 @@ fn httpErrorCallback(ctx: *anyopaque, _: anyerror) void {
const self: *Fetch = @ptrCast(@alignCast(ctx));
var response = self._response;
response._transfer = null;
response._http_response = null;
// the response is only passed on v8 on success, if we're here, it's safe to
// clear this. (defer since `self is in the response's arena).
@@ -256,7 +255,7 @@ fn httpShutdownCallback(ctx: *anyopaque) void {
if (self._owns_response) {
var response = self._response;
response._transfer = null;
response._http_response = null;
response.deinit(self._page._session);
// Do not access `self` after this point: the Fetch struct was
// allocated from response._arena which has been released.

View File

@@ -48,7 +48,7 @@ _type: Type,
_status_text: []const u8,
_url: [:0]const u8,
_is_redirected: bool,
_transfer: ?*HttpClient.Transfer = null,
_http_response: ?HttpClient.Response = null,
const InitOpts = struct {
status: u16 = 200,
@@ -81,9 +81,9 @@ pub fn init(body_: ?[]const u8, opts_: ?InitOpts, page: *Page) !*Response {
}
pub fn deinit(self: *Response, session: *Session) void {
if (self._transfer) |transfer| {
transfer.abort(error.Abort);
self._transfer = null;
if (self._http_response) |resp| {
resp.abort(error.Abort);
self._http_response = null;
}
session.releaseArena(self._arena);
}
@@ -191,7 +191,7 @@ pub fn clone(self: *const Response, page: *Page) !*Response {
._type = self._type,
._is_redirected = self._is_redirected,
._headers = try Headers.init(.{ .obj = self._headers }, page),
._transfer = null,
._http_response = null,
};
return cloned;
}

View File

@@ -46,10 +46,6 @@ pub fn init(opts_: ?InitOpts, page: *Page) !*URLSearchParams {
.query_string => |qs| break :blk try paramsFromString(arena, qs, &page.buf),
.form_data => |fd| break :blk try KeyValueList.copy(arena, fd._list),
.value => |js_val| {
// Order matters here; Array is also an Object.
if (js_val.isArray()) {
break :blk try paramsFromArray(arena, js_val.toArray());
}
if (js_val.isObject()) {
break :blk try KeyValueList.fromJsObject(arena, js_val.toObject(), null, page);
}
@@ -138,37 +134,6 @@ pub fn sort(self: *URLSearchParams) void {
}.cmp);
}
fn paramsFromArray(allocator: Allocator, array: js.Array) !KeyValueList {
const array_len = array.len();
if (array_len == 0) {
return .empty;
}
var params = KeyValueList.init();
try params.ensureTotalCapacity(allocator, array_len);
// TODO: Release `params` on error.
var i: u32 = 0;
while (i < array_len) : (i += 1) {
const item = try array.get(i);
if (!item.isArray()) return error.InvalidArgument;
const as_array = item.toArray();
// Need 2 items for KV.
if (as_array.len() != 2) return error.InvalidArgument;
const name_val = try as_array.get(0);
const value_val = try as_array.get(1);
params._entries.appendAssumeCapacity(.{
.name = try name_val.toSSOWithAlloc(allocator),
.value = try value_val.toSSOWithAlloc(allocator),
});
}
return params;
}
fn paramsFromString(allocator: Allocator, input_: []const u8, buf: []u8) !KeyValueList {
if (input_.len == 0) {
return .empty;

View File

@@ -43,8 +43,7 @@ _rc: lp.RC(u8) = .{},
_page: *Page,
_proto: *XMLHttpRequestEventTarget,
_arena: Allocator,
_transfer: ?*HttpClient.Transfer = null,
_active_request: bool = false,
_http_response: ?HttpClient.Response = null,
_url: [:0]const u8 = "",
_method: net_http.Method = .GET,
@@ -100,9 +99,9 @@ pub fn init(page: *Page) !*XMLHttpRequest {
}
pub fn deinit(self: *XMLHttpRequest, session: *Session) void {
if (self._transfer) |transfer| {
transfer.abort(error.Abort);
self._transfer = null;
if (self._http_response) |resp| {
resp.abort(error.Abort);
self._http_response = null;
}
if (self._on_ready_state_change) |func| {
@@ -137,14 +136,6 @@ pub fn deinit(self: *XMLHttpRequest, session: *Session) void {
session.releaseArena(self._arena);
}
fn releaseSelfRef(self: *XMLHttpRequest) void {
if (self._active_request == false) {
return;
}
self.releaseRef(self._page._session);
self._active_request = false;
}
pub fn releaseRef(self: *XMLHttpRequest, session: *Session) void {
self._rc.release(self, session);
}
@@ -184,9 +175,9 @@ pub fn setWithCredentials(self: *XMLHttpRequest, value: bool) !void {
// TODO: url should be a union, as it can be multiple things
pub fn open(self: *XMLHttpRequest, method_: []const u8, url: [:0]const u8) !void {
// Abort any in-progress request
if (self._transfer) |transfer| {
if (self._http_response) |transfer| {
transfer.abort(error.Abort);
self._transfer = null;
self._http_response = null;
}
// Reset internal state
@@ -261,8 +252,6 @@ pub fn send(self: *XMLHttpRequest, body_: ?[]const u8) !void {
.error_callback = httpErrorCallback,
.shutdown_callback = httpShutdownCallback,
});
self.acquireRef();
self._active_request = true;
}
fn handleBlobUrl(self: *XMLHttpRequest, page: *Page) !void {
@@ -398,34 +387,33 @@ pub fn getResponseXML(self: *XMLHttpRequest, page: *Page) !?*Node.Document {
};
}
fn httpStartCallback(transfer: *HttpClient.Transfer) !void {
const self: *XMLHttpRequest = @ptrCast(@alignCast(transfer.ctx));
fn httpStartCallback(response: HttpClient.Response) !void {
const self: *XMLHttpRequest = @ptrCast(@alignCast(response.ctx));
if (comptime IS_DEBUG) {
log.debug(.http, "request start", .{ .method = self._method, .url = self._url, .source = "xhr" });
}
self._transfer = transfer;
self._http_response = response;
self.acquireRef();
}
fn httpHeaderCallback(transfer: *HttpClient.Transfer, header: net_http.Header) !void {
const self: *XMLHttpRequest = @ptrCast(@alignCast(transfer.ctx));
fn httpHeaderCallback(response: HttpClient.Response, header: net_http.Header) !void {
const self: *XMLHttpRequest = @ptrCast(@alignCast(response.ctx));
const joined = try std.fmt.allocPrint(self._arena, "{s}: {s}", .{ header.name, header.value });
try self._response_headers.append(self._arena, joined);
}
fn httpHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
const self: *XMLHttpRequest = @ptrCast(@alignCast(transfer.ctx));
const header = &transfer.response_header.?;
fn httpHeaderDoneCallback(response: HttpClient.Response) !bool {
const self: *XMLHttpRequest = @ptrCast(@alignCast(response.ctx));
if (comptime IS_DEBUG) {
log.debug(.http, "request header", .{
.source = "xhr",
.url = self._url,
.status = header.status,
.status = response.status(),
});
}
if (header.contentType()) |ct| {
if (response.contentType()) |ct| {
self._response_mime = Mime.parse(ct) catch |e| {
log.info(.http, "invalid content type", .{
.content_Type = ct,
@@ -436,18 +424,18 @@ fn httpHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
};
}
var it = transfer.responseHeaderIterator();
var it = response.headerIterator();
while (it.next()) |hdr| {
const joined = try std.fmt.allocPrint(self._arena, "{s}: {s}", .{ hdr.name, hdr.value });
try self._response_headers.append(self._arena, joined);
}
self._response_status = header.status;
if (transfer.getContentLength()) |cl| {
self._response_status = response.status().?;
if (response.contentLength()) |cl| {
self._response_len = cl;
try self._response_data.ensureTotalCapacity(self._arena, cl);
}
self._response_url = try self._arena.dupeZ(u8, std.mem.span(header.url));
self._response_url = try self._arena.dupeZ(u8, response.url());
const page = self._page;
@@ -462,8 +450,8 @@ fn httpHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
return true;
}
fn httpDataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
const self: *XMLHttpRequest = @ptrCast(@alignCast(transfer.ctx));
fn httpDataCallback(response: HttpClient.Response, data: []const u8) !void {
const self: *XMLHttpRequest = @ptrCast(@alignCast(response.ctx));
try self._response_data.appendSlice(self._arena, data);
const page = self._page;
@@ -486,7 +474,7 @@ fn httpDoneCallback(ctx: *anyopaque) !void {
// Not that the request is done, the http/client will free the transfer
// object. It isn't safe to keep it around.
self._transfer = null;
self._http_response = null;
const page = self._page;
@@ -502,31 +490,31 @@ fn httpDoneCallback(ctx: *anyopaque) !void {
.loaded = loaded,
}, page);
self.releaseSelfRef();
self.releaseRef(page._session);
}
fn httpErrorCallback(ctx: *anyopaque, err: anyerror) void {
const self: *XMLHttpRequest = @ptrCast(@alignCast(ctx));
// http client will close it after an error, it isn't safe to keep around
self.handleError(err);
if (self._transfer != null) {
self._transfer = null;
if (self._http_response != null) {
self._http_response = null;
self.releaseRef(self._page._session);
}
self.releaseSelfRef();
}
fn httpShutdownCallback(ctx: *anyopaque) void {
const self: *XMLHttpRequest = @ptrCast(@alignCast(ctx));
self._transfer = null;
self._http_response = null;
}
pub fn abort(self: *XMLHttpRequest) void {
self.handleError(error.Abort);
if (self._transfer) |transfer| {
self._transfer = null;
transfer.abort(error.Abort);
if (self._http_response) |resp| {
self._http_response = null;
resp.abort(error.Abort);
self.releaseRef(self._page._session);
}
self.releaseSelfRef();
}
fn handleError(self: *XMLHttpRequest, err: anyerror) void {

View File

@@ -74,7 +74,7 @@ fn preprocessInput(arena: Allocator, input: []const u8) ![]const u8 {
return result.items;
}
pub fn parseList(arena: Allocator, input: []const u8) ParseError![]const Selector.Selector {
pub fn parseList(arena: Allocator, input: []const u8, page: *Page) ParseError![]const Selector.Selector {
// Preprocess input to normalize line endings
const preprocessed = try preprocessInput(arena, input);
@@ -140,7 +140,7 @@ pub fn parseList(arena: Allocator, input: []const u8) ParseError![]const Selecto
const selector_input = std.mem.trimRight(u8, trimmed[0..comma_pos], &std.ascii.whitespace);
if (selector_input.len > 0) {
const selector = try parse(arena, selector_input);
const selector = try parse(arena, selector_input, page);
try selectors.append(arena, selector);
}
@@ -155,7 +155,7 @@ pub fn parseList(arena: Allocator, input: []const u8) ParseError![]const Selecto
return selectors.items;
}
pub fn parse(arena: Allocator, input: []const u8) ParseError!Selector.Selector {
pub fn parse(arena: Allocator, input: []const u8, page: *Page) ParseError!Selector.Selector {
var parser = Parser{ .input = input };
var segments: std.ArrayList(Segment) = .empty;
var current_compound: std.ArrayList(Part) = .empty;
@@ -164,7 +164,7 @@ pub fn parse(arena: Allocator, input: []const u8) ParseError!Selector.Selector {
while (parser.skipSpaces()) {
if (parser.peek() == 0) break;
const part = try parser.parsePart(arena);
const part = try parser.parsePart(arena, page);
try current_compound.append(arena, part);
// Check what comes after this part
@@ -238,7 +238,7 @@ pub fn parse(arena: Allocator, input: []const u8) ParseError!Selector.Selector {
while (parser.skipSpaces()) {
if (parser.peek() == 0) break;
const part = try parser.parsePart(arena);
const part = try parser.parsePart(arena, page);
try current_compound.append(arena, part);
// Check what comes after this part
@@ -289,7 +289,7 @@ pub fn parse(arena: Allocator, input: []const u8) ParseError!Selector.Selector {
};
}
fn parsePart(self: *Parser, arena: Allocator) !Part {
fn parsePart(self: *Parser, arena: Allocator, page: *Page) !Part {
return switch (self.peek()) {
'#' => .{ .id = try self.id(arena) },
'.' => .{ .class = try self.class(arena) },
@@ -297,17 +297,16 @@ fn parsePart(self: *Parser, arena: Allocator) !Part {
self.input = self.input[1..];
break :blk .universal;
},
'[' => .{ .attribute = try self.attribute(arena) },
':' => .{ .pseudo_class = try self.pseudoClass(arena) },
'[' => .{ .attribute = try self.attribute(arena, page) },
':' => .{ .pseudo_class = try self.pseudoClass(arena, page) },
'a'...'z', 'A'...'Z', '_', '\\', 0x80...0xFF => blk: {
// Use parseIdentifier for full escape support
const tag_name = try self.parseIdentifier(arena, error.InvalidTagSelector);
if (tag_name.len > 256) {
return error.InvalidTagSelector;
}
var buf: [256]u8 = undefined;
// Try to match as a known tag enum for optimization
const lower = std.ascii.lowerString(&buf, tag_name);
const lower = std.ascii.lowerString(&page.buf, tag_name);
if (Node.Element.Tag.parseForMatch(lower)) |known_tag| {
break :blk .{ .tag = known_tag };
}
@@ -374,7 +373,7 @@ fn consumeUntilCommaOrParen(self: *Parser) []const u8 {
return result;
}
fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {
fn pseudoClass(self: *Parser, arena: Allocator, page: *Page) !Selector.PseudoClass {
if (comptime IS_DEBUG) {
// Should have been verified by caller
std.debug.assert(self.peek() == ':');
@@ -446,7 +445,7 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {
if (self.peek() == 0) return error.InvalidPseudoClass;
// Parse a full selector (with potential combinators and compounds)
const selector = try parse(arena, self.consumeUntilCommaOrParen());
const selector = try parse(arena, self.consumeUntilCommaOrParen(), page);
try selectors.append(arena, selector);
_ = self.skipSpaces();
@@ -473,7 +472,7 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {
if (self.peek() == ')') break;
if (self.peek() == 0) return error.InvalidPseudoClass;
const selector = try parse(arena, self.consumeUntilCommaOrParen());
const selector = try parse(arena, self.consumeUntilCommaOrParen(), page);
try selectors.append(arena, selector);
_ = self.skipSpaces();
@@ -500,7 +499,7 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {
if (self.peek() == ')') break;
if (self.peek() == 0) return error.InvalidPseudoClass;
const selector = try parse(arena, self.consumeUntilCommaOrParen());
const selector = try parse(arena, self.consumeUntilCommaOrParen(), page);
try selectors.append(arena, selector);
_ = self.skipSpaces();
@@ -527,7 +526,7 @@ fn pseudoClass(self: *Parser, arena: Allocator) !Selector.PseudoClass {
if (self.peek() == ')') break;
if (self.peek() == 0) return error.InvalidPseudoClass;
const selector = try parse(arena, self.consumeUntilCommaOrParen());
const selector = try parse(arena, self.consumeUntilCommaOrParen(), page);
try selectors.append(arena, selector);
_ = self.skipSpaces();
@@ -899,7 +898,7 @@ fn tag(self: *Parser) ![]const u8 {
return input[0..i];
}
fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {
fn attribute(self: *Parser, arena: Allocator, page: *Page) !Selector.Attribute {
if (comptime IS_DEBUG) {
// should have been verified by caller
std.debug.assert(self.peek() == '[');
@@ -911,7 +910,8 @@ fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {
const attr_name = try self.attributeName();
// Normalize the name to lowercase for fast matching (consistent with Attribute.normalizeNameForLookup)
const name = try Attribute.normalizeNameForLookupAlloc(arena, .wrap(attr_name));
const normalized = try Attribute.normalizeNameForLookup(.wrap(attr_name), page);
const name = try normalized.dupe(arena);
var case_insensitive = false;
_ = self.skipSpaces();

View File

@@ -20,24 +20,41 @@ const std = @import("std");
const String = @import("../../../string.zig").String;
const Parser = @import("Parser.zig");
const Node = @import("../Node.zig");
const Page = @import("../../Page.zig");
const Parser = @import("Parser.zig");
pub const List = @import("List.zig");
const Allocator = std.mem.Allocator;
pub fn parseLeaky(arena: Allocator, input: []const u8) !Parsed {
pub fn querySelector(root: *Node, input: []const u8, page: *Page) !?*Node.Element {
if (input.len == 0) {
return error.SyntaxError;
}
return .{ .selectors = try Parser.parseList(arena, input) };
}
pub fn querySelector(root: *Node, input: []const u8, page: *Page) !?*Node.Element {
const parsed = try parseLeaky(page.call_arena, input);
return parsed.query(root, page);
const arena = page.call_arena;
const selectors = try Parser.parseList(arena, input, page);
for (selectors) |selector| {
// Fast path: single compound with only an ID selector
if (selector.segments.len == 0 and selector.first.parts.len == 1) {
const first = selector.first.parts[0];
if (first == .id) {
const el = page.getElementByIdFromNode(root, first.id) orelse continue;
// Check if the element is within the root subtree
const node = el.asNode();
if (node != root and root.contains(node)) {
return el;
}
continue;
}
}
if (List.initOne(root, selector, page)) |node| {
if (node.is(Node.Element)) |el| {
return el;
}
}
}
return null;
}
pub fn querySelectorAll(root: *Node, input: []const u8, page: *Page) !*List {
@@ -50,7 +67,7 @@ pub fn querySelectorAll(root: *Node, input: []const u8, page: *Page) !*List {
var nodes: std.AutoArrayHashMapUnmanaged(*Node, void) = .empty;
const selectors = try Parser.parseList(arena, input);
const selectors = try Parser.parseList(arena, input, page);
for (selectors) |selector| {
try List.collect(arena, root, selector, &nodes, page);
}
@@ -69,7 +86,7 @@ pub fn matches(el: *Node.Element, input: []const u8, page: *Page) !bool {
}
const arena = page.call_arena;
const selectors = try Parser.parseList(arena, input);
const selectors = try Parser.parseList(arena, input, page);
for (selectors) |selector| {
if (List.matches(el.asNode(), selector, el.asNode(), page)) {
@@ -87,7 +104,7 @@ pub fn matchesWithScope(el: *Node.Element, input: []const u8, scope: *Node.Eleme
}
const arena = page.call_arena;
const selectors = try Parser.parseList(arena, input);
const selectors = try Parser.parseList(arena, input, page);
for (selectors) |selector| {
if (List.matches(el.asNode(), selector, scope.asNode(), page)) {
@@ -275,32 +292,3 @@ pub const Selector = struct {
}
}
};
pub const Parsed = struct {
selectors: []const Selector,
pub fn query(self: Parsed, root: *Node, page: *Page) !?*Node.Element {
for (self.selectors) |selector| {
// Fast path: single compound with only an ID selector
if (selector.segments.len == 0 and selector.first.parts.len == 1) {
const first = selector.first.parts[0];
if (first == .id) {
const el = page.getElementByIdFromNode(root, first.id) orelse continue;
// Check if the element is within the root subtree
const node = el.asNode();
if (node != root and root.contains(node)) {
return el;
}
continue;
}
}
if (List.initOne(root, selector, page)) |node| {
if (node.is(Node.Element)) |el| {
return el;
}
}
}
return null;
}
};

View File

@@ -1179,7 +1179,7 @@ test "AXNode: writer" {
var registry = Node.Registry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/dom3.html", .{});
var page = try testing.pageTest("cdp/dom3.html");
defer page._session.removePage();
var doc = page.window._document;

File diff suppressed because it is too large Load Diff

View File

@@ -343,7 +343,7 @@ test "cdp Node: Registry register" {
try testing.expectEqual(0, registry.lookup_by_id.count());
try testing.expectEqual(0, registry.lookup_by_node.count());
var page = try testing.pageTest("cdp/registry1.html", .{});
var page = try testing.pageTest("cdp/registry1.html");
defer page._session.removePage();
var doc = page.window._document;
@@ -400,7 +400,7 @@ test "cdp Node: search list" {
}
{
var page = try testing.pageTest("cdp/registry2.html", .{});
var page = try testing.pageTest("cdp/registry2.html");
defer page._session.removePage();
var doc = page.window._document;
@@ -440,7 +440,7 @@ test "cdp Node: Writer" {
var registry = Registry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/registry3.html", .{});
var page = try testing.pageTest("cdp/registry3.html");
defer page._session.removePage();
var doc = page.window._document;

View File

@@ -18,9 +18,8 @@
const std = @import("std");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,
@@ -33,15 +32,15 @@ pub fn processMessage(cmd: *CDP.Command) !void {
.getFullAXTree => return getFullAXTree(cmd),
}
}
fn enable(cmd: *CDP.Command) !void {
fn enable(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn disable(cmd: *CDP.Command) !void {
fn disable(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn getFullAXTree(cmd: *CDP.Command) !void {
fn getFullAXTree(cmd: anytype) !void {
const params = (try cmd.params(struct {
depth: ?i32 = null,
frameId: ?[]const u8 = null,

View File

@@ -17,7 +17,6 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
// TODO: hard coded data
const PROTOCOL_VERSION = "1.3";
@@ -36,7 +35,7 @@ const PRODUCT = "Chrome/124.0.6367.29";
const JS_VERSION = "12.4.254.8";
const DEV_TOOLS_WINDOW_ID = 1923710101;
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
getVersion,
setPermission,
@@ -58,7 +57,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
}
}
fn getVersion(cmd: *CDP.Command) !void {
fn getVersion(cmd: anytype) !void {
// TODO: pre-serialize?
return cmd.sendResult(.{
.protocolVersion = PROTOCOL_VERSION,
@@ -70,7 +69,7 @@ fn getVersion(cmd: *CDP.Command) !void {
}
// TODO: noop method
fn setDownloadBehavior(cmd: *CDP.Command) !void {
fn setDownloadBehavior(cmd: anytype) !void {
// const params = (try cmd.params(struct {
// behavior: []const u8,
// browserContextId: ?[]const u8 = null,
@@ -81,7 +80,7 @@ fn setDownloadBehavior(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{ .include_session_id = false });
}
fn getWindowForTarget(cmd: *CDP.Command) !void {
fn getWindowForTarget(cmd: anytype) !void {
// const params = (try cmd.params(struct {
// targetId: ?[]const u8 = null,
// })) orelse return error.InvalidParams;
@@ -92,22 +91,22 @@ fn getWindowForTarget(cmd: *CDP.Command) !void {
}
// TODO: noop method
fn setWindowBounds(cmd: *CDP.Command) !void {
fn setWindowBounds(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
// TODO: noop method
fn grantPermissions(cmd: *CDP.Command) !void {
fn grantPermissions(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
// TODO: noop method
fn setPermission(cmd: *CDP.Command) !void {
fn setPermission(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
// TODO: noop method
fn resetPermissions(cmd: *CDP.Command) !void {
fn resetPermissions(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}

View File

@@ -17,9 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
}, cmd.input.action) orelse return error.UnknownMethod;

View File

@@ -18,18 +18,17 @@
const std = @import("std");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const Node = @import("../Node.zig");
const log = @import("../../log.zig");
const dump = @import("../../browser/dump.zig");
const js = @import("../../browser/js/js.zig");
const Node = @import("../Node.zig");
const DOMNode = @import("../../browser/webapi/Node.zig");
const Selector = @import("../../browser/webapi/selector/Selector.zig");
const dump = @import("../../browser/dump.zig");
const js = @import("../../browser/js/js.zig");
const Allocator = std.mem.Allocator;
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
getDocument,
@@ -70,7 +69,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getDocument
fn getDocument(cmd: *CDP.Command) !void {
fn getDocument(cmd: anytype) !void {
const Params = struct {
// CDP documentation implies that 0 isn't valid, but it _does_ work in Chrome
depth: i32 = 3,
@@ -90,7 +89,7 @@ fn getDocument(cmd: *CDP.Command) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-performSearch
fn performSearch(cmd: *CDP.Command) !void {
fn performSearch(cmd: anytype) !void {
const params = (try cmd.params(struct {
query: []const u8,
includeUserAgentShadowDOM: ?bool = null,
@@ -117,7 +116,7 @@ fn performSearch(cmd: *CDP.Command) !void {
// hierarchy of each nodes.
// We dispatch event in the reverse order: from the top level to the direct parents.
// We should dispatch a node only if it has never been sent.
fn dispatchSetChildNodes(cmd: *CDP.Command, dom_nodes: []const *DOMNode) !void {
fn dispatchSetChildNodes(cmd: anytype, dom_nodes: []const *DOMNode) !void {
const arena = cmd.arena;
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const session_id = bc.session_id orelse return error.SessionIdNotLoaded;
@@ -173,7 +172,7 @@ fn dispatchSetChildNodes(cmd: *CDP.Command, dom_nodes: []const *DOMNode) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-discardSearchResults
fn discardSearchResults(cmd: *CDP.Command) !void {
fn discardSearchResults(cmd: anytype) !void {
const params = (try cmd.params(struct {
searchId: []const u8,
})) orelse return error.InvalidParams;
@@ -185,7 +184,7 @@ fn discardSearchResults(cmd: *CDP.Command) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getSearchResults
fn getSearchResults(cmd: *CDP.Command) !void {
fn getSearchResults(cmd: anytype) !void {
const params = (try cmd.params(struct {
searchId: []const u8,
fromIndex: u32,
@@ -210,7 +209,7 @@ fn getSearchResults(cmd: *CDP.Command) !void {
return cmd.sendResult(.{ .nodeIds = node_ids[params.fromIndex..params.toIndex] }, .{});
}
fn querySelector(cmd: *CDP.Command) !void {
fn querySelector(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: Node.Id,
selector: []const u8,
@@ -236,7 +235,7 @@ fn querySelector(cmd: *CDP.Command) !void {
}, .{});
}
fn querySelectorAll(cmd: *CDP.Command) !void {
fn querySelectorAll(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: Node.Id,
selector: []const u8,
@@ -267,7 +266,7 @@ fn querySelectorAll(cmd: *CDP.Command) !void {
}, .{});
}
fn resolveNode(cmd: *CDP.Command) !void {
fn resolveNode(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?u32 = null,
@@ -328,7 +327,7 @@ fn resolveNode(cmd: *CDP.Command) !void {
} }, .{});
}
fn describeNode(cmd: *CDP.Command) !void {
fn describeNode(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?Node.Id = null,
@@ -375,7 +374,7 @@ fn rectToQuad(rect: DOMNode.Element.DOMRect) Quad {
};
}
fn scrollIntoViewIfNeeded(cmd: *CDP.Command) !void {
fn scrollIntoViewIfNeeded(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?u32 = null,
@@ -398,7 +397,7 @@ fn scrollIntoViewIfNeeded(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn getNode(arena: Allocator, bc: *CDP.BrowserContext, node_id: ?Node.Id, backend_node_id: ?Node.Id, object_id: ?[]const u8) !*Node {
fn getNode(arena: Allocator, bc: anytype, node_id: ?Node.Id, backend_node_id: ?Node.Id, object_id: ?[]const u8) !*Node {
const input_node_id = node_id orelse backend_node_id;
if (input_node_id) |input_node_id_| {
return bc.node_registry.lookup_by_id.get(input_node_id_) orelse return error.NodeNotFound;
@@ -418,7 +417,7 @@ fn getNode(arena: Allocator, bc: *CDP.BrowserContext, node_id: ?Node.Id, backend
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getContentQuads
// Related to: https://drafts.csswg.org/cssom-view/#the-geometryutils-interface
fn getContentQuads(cmd: *CDP.Command) !void {
fn getContentQuads(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?Node.Id = null,
@@ -444,7 +443,7 @@ fn getContentQuads(cmd: *CDP.Command) !void {
return cmd.sendResult(.{ .quads = &.{quad} }, .{});
}
fn getBoxModel(cmd: *CDP.Command) !void {
fn getBoxModel(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?u32 = null,
@@ -473,7 +472,7 @@ fn getBoxModel(cmd: *CDP.Command) !void {
} }, .{});
}
fn requestChildNodes(cmd: *CDP.Command) !void {
fn requestChildNodes(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: Node.Id,
depth: i32 = 1,
@@ -497,7 +496,7 @@ fn requestChildNodes(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn getFrameOwner(cmd: *CDP.Command) !void {
fn getFrameOwner(cmd: anytype) !void {
const params = (try cmd.params(struct {
frameId: []const u8,
})) orelse return error.InvalidParams;
@@ -513,7 +512,7 @@ fn getFrameOwner(cmd: *CDP.Command) !void {
return cmd.sendResult(.{ .nodeId = node.id, .backendNodeId = node.id }, .{});
}
fn getOuterHTML(cmd: *CDP.Command) !void {
fn getOuterHTML(cmd: anytype) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?Node.Id = null,
@@ -535,7 +534,7 @@ fn getOuterHTML(cmd: *CDP.Command) !void {
return cmd.sendResult(.{ .outerHTML = aw.written() }, .{});
}
fn requestNode(cmd: *CDP.Command) !void {
fn requestNode(cmd: anytype) !void {
const params = (try cmd.params(struct {
objectId: []const u8,
})) orelse return error.InvalidParams;

View File

@@ -17,10 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
setEmulatedMedia,
setFocusEmulationEnabled,
@@ -39,7 +38,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
}
// TODO: noop method
fn setEmulatedMedia(cmd: *CDP.Command) !void {
fn setEmulatedMedia(cmd: anytype) !void {
// const input = (try const incoming.params(struct {
// media: ?[]const u8 = null,
// features: ?[]struct{
@@ -52,7 +51,7 @@ fn setEmulatedMedia(cmd: *CDP.Command) !void {
}
// TODO: noop method
fn setFocusEmulationEnabled(cmd: *CDP.Command) !void {
fn setFocusEmulationEnabled(cmd: anytype) !void {
// const input = (try const incoming.params(struct {
// enabled: bool,
// })) orelse return error.InvalidParams;
@@ -60,16 +59,16 @@ fn setFocusEmulationEnabled(cmd: *CDP.Command) !void {
}
// TODO: noop method
fn setDeviceMetricsOverride(cmd: *CDP.Command) !void {
fn setDeviceMetricsOverride(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
// TODO: noop method
fn setTouchEmulationEnabled(cmd: *CDP.Command) !void {
fn setTouchEmulationEnabled(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn setUserAgentOverride(cmd: *CDP.Command) !void {
fn setUserAgentOverride(cmd: anytype) !void {
log.info(.app, "setUserAgentOverride ignored", .{});
return cmd.sendResult(null, .{});
}

View File

@@ -17,19 +17,17 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Allocator = std.mem.Allocator;
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
const network = @import("network.zig");
const HttpClient = @import("../../browser/HttpClient.zig");
const net_http = @import("../../network/http.zig");
const Notification = @import("../../Notification.zig");
const network = @import("network.zig");
const Allocator = std.mem.Allocator;
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
disable,
enable,
@@ -137,13 +135,13 @@ const ErrorReason = enum {
BlockedByResponse,
};
fn disable(cmd: *CDP.Command) !void {
fn disable(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
bc.fetchDisable();
return cmd.sendResult(null, .{});
}
fn enable(cmd: *CDP.Command) !void {
fn enable(cmd: anytype) !void {
const params = (try cmd.params(EnableParam)) orelse EnableParam{};
if (!arePatternsSupported(params.patterns)) {
log.warn(.not_implemented, "Fetch.enable", .{ .params = "pattern" });
@@ -182,7 +180,7 @@ fn arePatternsSupported(patterns: []RequestPattern) bool {
return true;
}
pub fn requestIntercept(bc: *CDP.BrowserContext, intercept: *const Notification.RequestIntercept) !void {
pub fn requestIntercept(bc: anytype, intercept: *const Notification.RequestIntercept) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -217,7 +215,7 @@ pub fn requestIntercept(bc: *CDP.BrowserContext, intercept: *const Notification.
intercept.wait_for_interception.* = true;
}
fn continueRequest(cmd: *CDP.Command) !void {
fn continueRequest(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
requestId: []const u8, // INT-{d}"
@@ -277,7 +275,7 @@ const AuthChallengeResponse = enum {
ProvideCredentials,
};
fn continueWithAuth(cmd: *CDP.Command) !void {
fn continueWithAuth(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
requestId: []const u8, // "INT-{d}"
@@ -320,7 +318,7 @@ fn continueWithAuth(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn fulfillRequest(cmd: *CDP.Command) !void {
fn fulfillRequest(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
@@ -362,7 +360,7 @@ fn fulfillRequest(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn failRequest(cmd: *CDP.Command) !void {
fn failRequest(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
requestId: []const u8, // "INT-{d}"
@@ -384,7 +382,7 @@ fn failRequest(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
pub fn requestAuthRequired(bc: *CDP.BrowserContext, intercept: *const Notification.RequestAuthRequired) !void {
pub fn requestAuthRequired(bc: anytype, intercept: *const Notification.RequestAuthRequired) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;

View File

@@ -17,9 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
dispatchKeyEvent,
dispatchMouseEvent,
@@ -34,7 +33,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchKeyEvent
fn dispatchKeyEvent(cmd: *CDP.Command) !void {
fn dispatchKeyEvent(cmd: anytype) !void {
const params = (try cmd.params(struct {
type: Type,
key: []const u8 = "",
@@ -75,7 +74,7 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchMouseEvent
fn dispatchMouseEvent(cmd: *CDP.Command) !void {
fn dispatchMouseEvent(cmd: anytype) !void {
const params = (try cmd.params(struct {
x: f64,
y: f64,
@@ -105,7 +104,7 @@ fn dispatchMouseEvent(cmd: *CDP.Command) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-insertText
fn insertText(cmd: *CDP.Command) !void {
fn insertText(cmd: anytype) !void {
const params = (try cmd.params(struct {
text: []const u8, // The text to insert
})) orelse return error.InvalidParams;

View File

@@ -17,9 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,

View File

@@ -17,9 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,

View File

@@ -18,23 +18,18 @@
const std = @import("std");
const lp = @import("lightpanda");
const CDP = @import("../CDP.zig");
const Node = @import("../Node.zig");
const DOMNode = @import("../../browser/webapi/Node.zig");
const markdown = lp.markdown;
const SemanticTree = lp.SemanticTree;
const interactive = lp.interactive;
const structured_data = lp.structured_data;
const Node = @import("../Node.zig");
const DOMNode = @import("../../browser/webapi/Node.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
getMarkdown,
getSemanticTree,
getInteractiveElements,
getNodeDetails,
getStructuredData,
detectForms,
clickNode,
@@ -47,7 +42,6 @@ pub fn processMessage(cmd: *CDP.Command) !void {
.getMarkdown => return getMarkdown(cmd),
.getSemanticTree => return getSemanticTree(cmd),
.getInteractiveElements => return getInteractiveElements(cmd),
.getNodeDetails => return getNodeDetails(cmd),
.getStructuredData => return getStructuredData(cmd),
.detectForms => return detectForms(cmd),
.clickNode => return clickNode(cmd),
@@ -147,24 +141,6 @@ fn getInteractiveElements(cmd: anytype) !void {
}, .{});
}
fn getNodeDetails(cmd: anytype) !void {
const Params = struct {
backendNodeId: Node.Id,
};
const params = (try cmd.params(Params)) orelse return error.InvalidParam;
const bc = cmd.browser_context orelse return error.NoBrowserContext;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
const node = (bc.node_registry.lookup_by_id.get(params.backendNodeId) orelse return error.InvalidNodeId).dom;
const details = SemanticTree.getNodeDetails(cmd.arena, node, &bc.node_registry, page) catch return error.InternalError;
return cmd.sendResult(.{
.nodeDetails = details,
}, .{});
}
fn getStructuredData(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.NoBrowserContext;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;

View File

@@ -18,21 +18,18 @@
const std = @import("std");
const lp = @import("lightpanda");
const Allocator = std.mem.Allocator;
const log = @import("../../log.zig");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const CdpStorage = @import("storage.zig");
const id = @import("../id.zig");
const URL = @import("../../browser/URL.zig");
const Transfer = @import("../../browser/HttpClient.zig").Transfer;
const Notification = @import("../../Notification.zig");
const Mime = @import("../../browser/Mime.zig");
const CdpStorage = @import("storage.zig");
const Allocator = std.mem.Allocator;
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,
@@ -62,19 +59,19 @@ pub fn processMessage(cmd: *CDP.Command) !void {
}
}
fn enable(cmd: *CDP.Command) !void {
fn enable(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
try bc.networkEnable();
return cmd.sendResult(null, .{});
}
fn disable(cmd: *CDP.Command) !void {
fn disable(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
bc.networkDisable();
return cmd.sendResult(null, .{});
}
fn setExtraHTTPHeaders(cmd: *CDP.Command) !void {
fn setExtraHTTPHeaders(cmd: anytype) !void {
const params = (try cmd.params(struct {
headers: std.json.ArrayHashMap([]const u8),
})) orelse return error.InvalidParams;
@@ -113,7 +110,7 @@ fn cookieMatches(cookie: *const Cookie, name: []const u8, domain: ?[]const u8, p
return true;
}
fn deleteCookies(cmd: *CDP.Command) !void {
fn deleteCookies(cmd: anytype) !void {
const params = (try cmd.params(struct {
name: []const u8,
url: ?[:0]const u8 = null,
@@ -147,14 +144,14 @@ fn deleteCookies(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn clearBrowserCookies(cmd: *CDP.Command) !void {
fn clearBrowserCookies(cmd: anytype) !void {
if (try cmd.params(struct {}) != null) return error.InvalidParams;
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
bc.session.cookie_jar.clearRetainingCapacity();
return cmd.sendResult(null, .{});
}
fn setCookie(cmd: *CDP.Command) !void {
fn setCookie(cmd: anytype) !void {
const params = (try cmd.params(
CdpStorage.CdpCookie,
)) orelse return error.InvalidParams;
@@ -165,7 +162,7 @@ fn setCookie(cmd: *CDP.Command) !void {
try cmd.sendResult(.{ .success = true }, .{});
}
fn setCookies(cmd: *CDP.Command) !void {
fn setCookies(cmd: anytype) !void {
const params = (try cmd.params(struct {
cookies: []const CdpStorage.CdpCookie,
})) orelse return error.InvalidParams;
@@ -181,7 +178,7 @@ fn setCookies(cmd: *CDP.Command) !void {
const GetCookiesParam = struct {
urls: ?[]const [:0]const u8 = null,
};
fn getCookies(cmd: *CDP.Command) !void {
fn getCookies(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(GetCookiesParam)) orelse GetCookiesParam{};
@@ -204,7 +201,7 @@ fn getCookies(cmd: *CDP.Command) !void {
try cmd.sendResult(.{ .cookies = writer }, .{});
}
fn getResponseBody(cmd: *CDP.Command) !void {
fn getResponseBody(cmd: anytype) !void {
const params = (try cmd.params(struct {
requestId: []const u8, // "REQ-{d}"
})) orelse return error.InvalidParams;
@@ -230,7 +227,7 @@ fn getResponseBody(cmd: *CDP.Command) !void {
}, .{});
}
pub fn httpRequestFail(bc: *CDP.BrowserContext, msg: *const Notification.RequestFail) !void {
pub fn httpRequestFail(bc: anytype, msg: *const Notification.RequestFail) !void {
// It's possible that the request failed because we aborted when the client
// sent Target.closeTarget. In that case, bc.session_id will be cleared
// already, and we can skip sending these messages to the client.
@@ -250,7 +247,7 @@ pub fn httpRequestFail(bc: *CDP.BrowserContext, msg: *const Notification.Request
}, .{ .session_id = session_id });
}
pub fn httpRequestStart(bc: *CDP.BrowserContext, msg: *const Notification.RequestStart) !void {
pub fn httpRequestStart(bc: anytype, msg: *const Notification.RequestStart) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -279,7 +276,7 @@ pub fn httpRequestStart(bc: *CDP.BrowserContext, msg: *const Notification.Reques
}, .{ .session_id = session_id });
}
pub fn httpResponseHeaderDone(arena: Allocator, bc: *CDP.BrowserContext, msg: *const Notification.ResponseHeaderDone) !void {
pub fn httpResponseHeaderDone(arena: Allocator, bc: anytype, msg: *const Notification.ResponseHeaderDone) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -296,7 +293,7 @@ pub fn httpResponseHeaderDone(arena: Allocator, bc: *CDP.BrowserContext, msg: *c
}, .{ .session_id = session_id });
}
pub fn httpRequestDone(bc: *CDP.BrowserContext, msg: *const Notification.RequestDone) !void {
pub fn httpRequestDone(bc: anytype, msg: *const Notification.RequestDone) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;

View File

@@ -1,5 +1,4 @@
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
@@ -23,8 +22,6 @@ const lp = @import("lightpanda");
const screenshot_png = @embedFile("screenshot.png");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
const js = @import("../../browser/js/js.zig");
const URL = @import("../../browser/URL.zig");
@@ -34,7 +31,7 @@ const Notification = @import("../../Notification.zig");
const Allocator = std.mem.Allocator;
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
getFrameTree,
@@ -81,7 +78,7 @@ const Frame = struct {
gatedAPIFeatures: [][]const u8 = &[0][]const u8{},
};
fn getFrameTree(cmd: *CDP.Command) !void {
fn getFrameTree(cmd: anytype) !void {
// Stagehand parses the response and error if we don't return a
// correct one for this call when browser context or target id are missing.
const startup = .{
@@ -111,7 +108,7 @@ fn getFrameTree(cmd: *CDP.Command) !void {
}, .{});
}
fn setLifecycleEventsEnabled(cmd: *CDP.Command) !void {
fn setLifecycleEventsEnabled(cmd: anytype) !void {
const params = (try cmd.params(struct {
enabled: bool,
})) orelse return error.InvalidParams;
@@ -152,7 +149,7 @@ fn setLifecycleEventsEnabled(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn addScriptToEvaluateOnNewDocument(cmd: *CDP.Command) !void {
fn addScriptToEvaluateOnNewDocument(cmd: anytype) !void {
const params = (try cmd.params(struct {
source: []const u8,
worldName: ?[]const u8 = null,
@@ -182,7 +179,7 @@ fn addScriptToEvaluateOnNewDocument(cmd: *CDP.Command) !void {
}, .{});
}
fn removeScriptToEvaluateOnNewDocument(cmd: *CDP.Command) !void {
fn removeScriptToEvaluateOnNewDocument(cmd: anytype) !void {
const params = (try cmd.params(struct {
identifier: []const u8,
})) orelse return error.InvalidParams;
@@ -201,7 +198,7 @@ fn removeScriptToEvaluateOnNewDocument(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn close(cmd: *CDP.Command) !void {
fn close(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const target_id = bc.target_id orelse return error.TargetNotLoaded;
@@ -238,7 +235,7 @@ fn close(cmd: *CDP.Command) !void {
bc.target_id = null;
}
fn createIsolatedWorld(cmd: *CDP.Command) !void {
fn createIsolatedWorld(cmd: anytype) !void {
const params = (try cmd.params(struct {
frameId: []const u8,
worldName: []const u8,
@@ -258,7 +255,7 @@ fn createIsolatedWorld(cmd: *CDP.Command) !void {
return cmd.sendResult(.{ .executionContextId = js_context.id }, .{});
}
fn navigate(cmd: *CDP.Command) !void {
fn navigate(cmd: anytype) !void {
const params = (try cmd.params(struct {
url: [:0]const u8,
// referrer: ?[]const u8 = null,
@@ -292,7 +289,7 @@ fn navigate(cmd: *CDP.Command) !void {
});
}
fn doReload(cmd: *CDP.Command) !void {
fn doReload(cmd: anytype) !void {
const params = try cmd.params(struct {
ignoreCache: ?bool = null,
scriptToEvaluateOnLoad: ?[]const u8 = null,
@@ -322,7 +319,7 @@ fn doReload(cmd: *CDP.Command) !void {
});
}
pub fn pageNavigate(bc: *CDP.BrowserContext, event: *const Notification.PageNavigate) !void {
pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -374,7 +371,7 @@ pub fn pageNavigate(bc: *CDP.BrowserContext, event: *const Notification.PageNavi
}, .{ .session_id = session_id });
}
pub fn pageRemove(bc: *CDP.BrowserContext) !void {
pub fn pageRemove(bc: anytype) !void {
// Clear all remote object mappings to prevent stale objectIds from being used
// after the context is destroy
bc.inspector_session.inspector.resetContextGroup();
@@ -385,7 +382,7 @@ pub fn pageRemove(bc: *CDP.BrowserContext) !void {
}
}
pub fn pageCreated(bc: *CDP.BrowserContext, page: *Page) !void {
pub fn pageCreated(bc: anytype, page: *Page) !void {
_ = bc.cdp.page_arena.reset(.{ .retain_with_limit = 1024 * 512 });
for (bc.isolated_worlds.items) |isolated_world| {
@@ -397,7 +394,7 @@ pub fn pageCreated(bc: *CDP.BrowserContext, page: *Page) !void {
bc.captured_responses = .empty;
}
pub fn pageFrameCreated(bc: *CDP.BrowserContext, event: *const Notification.PageFrameCreated) !void {
pub fn pageFrameCreated(bc: anytype, event: *const Notification.PageFrameCreated) !void {
const session_id = bc.session_id orelse return;
const cdp = bc.cdp;
@@ -418,11 +415,12 @@ pub fn pageFrameCreated(bc: *CDP.BrowserContext, event: *const Notification.Page
}
}
pub fn pageNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const Notification.PageNavigated) !void {
pub fn pageNavigated(arena: Allocator, bc: anytype, event: *const Notification.PageNavigated) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
const timestamp = event.timestamp;
const frame_id = &id.toFrameId(event.frame_id);
const loader_id = &id.toLoaderId(event.req_id);
@@ -474,9 +472,9 @@ pub fn pageNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const No
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
// When we actually recreated the context we should have the inspector send
// this event, see: resetContextGroup. Sending this event will tell the
// client that the context ids they had are invalid and the context should
// be dropped. The client will expect us to send new contextCreated events,
// this event, see: resetContextGroup Sending this event will tell the
// client that the context ids they had are invalid and the context shouls
// be dropped The client will expect us to send new contextCreated events,
// such that the client has new id's for the active contexts.
// Only send executionContextsCleared for main frame navigations. For child
// frames (iframes), clearing all contexts would destroy the main frame's
@@ -486,18 +484,6 @@ pub fn pageNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const No
try cdp.sendEvent("Runtime.executionContextsCleared", null, .{ .session_id = session_id });
}
// frameNavigated event
try cdp.sendEvent("Page.frameNavigated", .{
.type = "Navigation",
.frame = Frame{
.id = frame_id,
.url = event.url,
.loaderId = loader_id,
.securityOrigin = bc.security_origin,
.secureContextType = bc.secure_context_type,
},
}, .{ .session_id = session_id });
{
const aux_data = try std.fmt.allocPrint(arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\",\"loaderId\":\"{s}\"}}", .{ frame_id, loader_id });
@@ -568,22 +554,18 @@ pub fn pageNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const No
// chromedp client expects to receive the events is this order.
// see https://github.com/chromedp/chromedp/issues/1558
try cdp.sendEvent("DOM.documentUpdated", null, .{ .session_id = session_id });
}
pub fn pageDOMContentLoaded(bc: anytype, event: *const Notification.PageDOMContentLoaded) !void {
const session_id = bc.session_id orelse return;
const timestamp = event.timestamp;
var cdp = bc.cdp;
// domContentEventFired event
// TODO: partially hard coded
try cdp.sendEvent(
"Page.domContentEventFired",
.{ .timestamp = timestamp },
.{ .session_id = session_id },
);
// lifecycle DOMContentLoaded event
// TODO: partially hard coded
if (bc.page_life_cycle_events) {
const frame_id = &id.toFrameId(event.frame_id);
const loader_id = &id.toLoaderId(event.req_id);
try cdp.sendEvent("Page.lifecycleEvent", LifecycleEvent{
.timestamp = timestamp,
.name = "DOMContentLoaded",
@@ -591,23 +573,16 @@ pub fn pageDOMContentLoaded(bc: anytype, event: *const Notification.PageDOMConte
.loaderId = loader_id,
}, .{ .session_id = session_id });
}
}
pub fn pageLoaded(bc: anytype, event: *const Notification.PageLoaded) !void {
const session_id = bc.session_id orelse return;
const timestamp = event.timestamp;
var cdp = bc.cdp;
const frame_id = &id.toFrameId(event.frame_id);
// loadEventFired event
try cdp.sendEvent(
"Page.loadEventFired",
.{ .timestamp = timestamp },
.{ .session_id = session_id },
);
// lifecycle DOMContentLoaded event
if (bc.page_life_cycle_events) {
const loader_id = &id.toLoaderId(event.req_id);
try cdp.sendEvent("Page.lifecycleEvent", LifecycleEvent{
.timestamp = timestamp,
.name = "load",
@@ -616,20 +591,21 @@ pub fn pageLoaded(bc: anytype, event: *const Notification.PageLoaded) !void {
}, .{ .session_id = session_id });
}
// frameStoppedLoading
return cdp.sendEvent("Page.frameStoppedLoading", .{
.frameId = frame_id,
}, .{ .session_id = session_id });
}
pub fn pageNetworkIdle(bc: *CDP.BrowserContext, event: *const Notification.PageNetworkIdle) !void {
pub fn pageNetworkIdle(bc: anytype, event: *const Notification.PageNetworkIdle) !void {
return sendPageLifecycle(bc, "networkIdle", event.timestamp, &id.toFrameId(event.frame_id), &id.toLoaderId(event.req_id));
}
pub fn pageNetworkAlmostIdle(bc: *CDP.BrowserContext, event: *const Notification.PageNetworkAlmostIdle) !void {
pub fn pageNetworkAlmostIdle(bc: anytype, event: *const Notification.PageNetworkAlmostIdle) !void {
return sendPageLifecycle(bc, "networkAlmostIdle", event.timestamp, &id.toFrameId(event.frame_id), &id.toLoaderId(event.req_id));
}
fn sendPageLifecycle(bc: *CDP.BrowserContext, name: []const u8, timestamp: u64, frame_id: []const u8, loader_id: []const u8) !void {
fn sendPageLifecycle(bc: anytype, name: []const u8, timestamp: u64, frame_id: []const u8, loader_id: []const u8) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -664,7 +640,7 @@ fn base64Encode(comptime input: []const u8) [std.base64.standard.Encoder.calcSiz
return buf;
}
fn captureScreenshot(cmd: *CDP.Command) !void {
fn captureScreenshot(cmd: anytype) !void {
const Params = struct {
format: ?[]const u8 = "png",
quality: ?u8 = null,
@@ -700,7 +676,7 @@ fn captureScreenshot(cmd: *CDP.Command) !void {
}, .{});
}
fn getLayoutMetrics(cmd: *CDP.Command) !void {
fn getLayoutMetrics(cmd: anytype) !void {
const width = 1920;
const height = 1080;

View File

@@ -17,9 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,

View File

@@ -19,9 +19,7 @@
const std = @import("std");
const builtin = @import("builtin");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
runIfWaitingForDebugger,
@@ -38,7 +36,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
}
}
fn sendInspector(cmd: *CDP.Command, action: anytype) !void {
fn sendInspector(cmd: anytype, action: anytype) !void {
// save script in file at debug mode
if (builtin.mode == .Debug) {
try logInspector(cmd, action);
@@ -50,7 +48,7 @@ fn sendInspector(cmd: *CDP.Command, action: anytype) !void {
bc.callInspector(cmd.input.json);
}
fn logInspector(cmd: *CDP.Command, action: anytype) !void {
fn logInspector(cmd: anytype, action: anytype) !void {
const script = switch (action) {
.evaluate => blk: {
const params = (try cmd.params(struct {

View File

@@ -17,9 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,
@@ -33,7 +32,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
}
}
fn setIgnoreCertificateErrors(cmd: *CDP.Command) !void {
fn setIgnoreCertificateErrors(cmd: anytype) !void {
const params = (try cmd.params(struct {
ignore: bool,
})) orelse return error.InvalidParams;

View File

@@ -18,16 +18,13 @@
const std = @import("std");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
const URL = @import("../../browser/URL.zig");
const Cookie = @import("../../browser/webapi/storage/storage.zig").Cookie;
const CookieJar = Cookie.Jar;
pub const PreparedUri = Cookie.PreparedUri;
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
clearCookies,
setCookies,
@@ -43,7 +40,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
const BrowserContextParam = struct { browserContextId: ?[]const u8 = null };
fn clearCookies(cmd: *CDP.Command) !void {
fn clearCookies(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(BrowserContextParam)) orelse BrowserContextParam{};
@@ -58,7 +55,7 @@ fn clearCookies(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn getCookies(cmd: *CDP.Command) !void {
fn getCookies(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(BrowserContextParam)) orelse BrowserContextParam{};
@@ -72,7 +69,7 @@ fn getCookies(cmd: *CDP.Command) !void {
try cmd.sendResult(.{ .cookies = writer }, .{});
}
fn setCookies(cmd: *CDP.Command) !void {
fn setCookies(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
cookies: []const CdpCookie,

View File

@@ -20,13 +20,11 @@ const std = @import("std");
const lp = @import("lightpanda");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
const URL = @import("../../browser/URL.zig");
const js = @import("../../browser/js/js.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
getTargets,
attachToTarget,
@@ -62,7 +60,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
}
}
fn getTargets(cmd: *CDP.Command) !void {
fn getTargets(cmd: anytype) !void {
// If no context available, return an empty array.
const bc = cmd.browser_context orelse {
return cmd.sendResult(.{
@@ -88,7 +86,7 @@ fn getTargets(cmd: *CDP.Command) !void {
}, .{ .include_session_id = false });
}
fn getBrowserContexts(cmd: *CDP.Command) !void {
fn getBrowserContexts(cmd: anytype) !void {
var browser_context_ids: []const []const u8 = undefined;
if (cmd.browser_context) |bc| {
browser_context_ids = &.{bc.id};
@@ -101,7 +99,7 @@ fn getBrowserContexts(cmd: *CDP.Command) !void {
}, .{ .include_session_id = false });
}
fn createBrowserContext(cmd: *CDP.Command) !void {
fn createBrowserContext(cmd: anytype) !void {
const params = try cmd.params(struct {
disposeOnDetach: bool = false,
proxyServer: ?[:0]const u8 = null,
@@ -132,7 +130,7 @@ fn createBrowserContext(cmd: *CDP.Command) !void {
}, .{});
}
fn disposeBrowserContext(cmd: *CDP.Command) !void {
fn disposeBrowserContext(cmd: anytype) !void {
const params = (try cmd.params(struct {
browserContextId: []const u8,
})) orelse return error.InvalidParams;
@@ -143,7 +141,7 @@ fn disposeBrowserContext(cmd: *CDP.Command) !void {
try cmd.sendResult(null, .{});
}
fn createTarget(cmd: *CDP.Command) !void {
fn createTarget(cmd: anytype) !void {
const params = (try cmd.params(struct {
url: [:0]const u8 = "about:blank",
// width: ?u64 = null,
@@ -232,7 +230,7 @@ fn createTarget(cmd: *CDP.Command) !void {
}, .{});
}
fn attachToTarget(cmd: *CDP.Command) !void {
fn attachToTarget(cmd: anytype) !void {
const params = (try cmd.params(struct {
targetId: []const u8,
flatten: bool = true,
@@ -249,7 +247,7 @@ fn attachToTarget(cmd: *CDP.Command) !void {
return cmd.sendResult(.{ .sessionId = bc.session_id }, .{});
}
fn attachToBrowserTarget(cmd: *CDP.Command) !void {
fn attachToBrowserTarget(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const session_id = bc.session_id orelse cmd.cdp.session_id_gen.next();
@@ -271,7 +269,7 @@ fn attachToBrowserTarget(cmd: *CDP.Command) !void {
return cmd.sendResult(.{ .sessionId = bc.session_id }, .{});
}
fn closeTarget(cmd: *CDP.Command) !void {
fn closeTarget(cmd: anytype) !void {
const params = (try cmd.params(struct {
targetId: []const u8,
})) orelse return error.InvalidParams;
@@ -312,7 +310,7 @@ fn closeTarget(cmd: *CDP.Command) !void {
bc.target_id = null;
}
fn getTargetInfo(cmd: *CDP.Command) !void {
fn getTargetInfo(cmd: anytype) !void {
const Params = struct {
targetId: ?[]const u8 = null,
};
@@ -349,7 +347,7 @@ fn getTargetInfo(cmd: *CDP.Command) !void {
}, .{ .include_session_id = false });
}
fn sendMessageToTarget(cmd: *CDP.Command) !void {
fn sendMessageToTarget(cmd: anytype) !void {
const params = (try cmd.params(struct {
message: []const u8,
sessionId: []const u8,
@@ -367,19 +365,32 @@ fn sendMessageToTarget(cmd: *CDP.Command) !void {
return error.UnknownSessionId;
}
var aw = std.Io.Writer.Allocating.init(cmd.arena);
cmd.cdp.dispatch(cmd.arena, .{ .capture = &aw.writer }, params.message) catch |err| {
const Capture = struct {
aw: std.Io.Writer.Allocating,
pub fn sendJSON(self: *@This(), message: anytype) !void {
return std.json.Stringify.value(message, .{
.emit_null_optional_fields = false,
}, &self.aw.writer);
}
};
var capture = Capture{
.aw = .init(cmd.arena),
};
cmd.cdp.dispatch(cmd.arena, &capture, params.message) catch |err| {
log.err(.cdp, "internal dispatch error", .{ .err = err, .id = cmd.input.id, .message = params.message });
return err;
};
try cmd.sendEvent("Target.receivedMessageFromTarget", .{
.message = aw.written(),
.message = capture.aw.written(),
.sessionId = params.sessionId,
}, .{});
}
fn detachFromTarget(cmd: *CDP.Command) !void {
fn detachFromTarget(cmd: anytype) !void {
if (cmd.browser_context) |bc| {
if (bc.session_id) |session_id| {
try cmd.sendEvent("Target.detachedFromTarget", .{
@@ -393,11 +404,11 @@ fn detachFromTarget(cmd: *CDP.Command) !void {
}
// TODO: noop method
fn setDiscoverTargets(cmd: *CDP.Command) !void {
fn setDiscoverTargets(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn setAutoAttach(cmd: *CDP.Command) !void {
fn setAutoAttach(cmd: anytype) !void {
const params = (try cmd.params(struct {
autoAttach: bool,
waitForDebuggerOnStart: bool,
@@ -457,7 +468,7 @@ fn setAutoAttach(cmd: *CDP.Command) !void {
try cmd.sendResult(null, .{});
}
fn doAttachtoTarget(cmd: *CDP.Command, target_id: []const u8) !void {
fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void {
const bc = cmd.browser_context.?;
const session_id = bc.session_id orelse cmd.cdp.session_id_gen.next();

View File

@@ -64,7 +64,7 @@ const TestContext = struct {
session_id: ?[]const u8 = null,
url: ?[:0]const u8 = null,
};
pub fn loadBrowserContext(self: *TestContext, opts: BrowserContextOpts) !*CDP.BrowserContext {
pub fn loadBrowserContext(self: *TestContext, opts: BrowserContextOpts) !*CDP.BrowserContext(CDP) {
var c = self.cdp();
if (c.browser_context) |bc| {
_ = c.disposeBrowserContext(bc.id);

View File

@@ -48,9 +48,7 @@ const IS_DEBUG = @import("builtin").mode == .Debug;
pub const FetchOpts = struct {
wait_ms: u32 = 5000,
wait_until: ?Config.WaitUntil = null,
wait_script: ?[:0]const u8 = null,
wait_selector: ?[:0]const u8 = null,
wait_until: Config.WaitUntil = .load,
dump: dump.Opts,
dump_mode: ?Config.DumpFormat = null,
writer: ?*std.Io.Writer = null,
@@ -113,31 +111,7 @@ pub fn fetch(app: *App, url: [:0]const u8, opts: FetchOpts) !void {
.kind = .{ .push = null },
});
var runner = try session.runner(.{});
var timer = try std.time.Timer.start();
if (opts.wait_until) |wu| {
try runner.wait(.{ .ms = opts.wait_ms, .until = wu });
} else if (opts.wait_selector == null and opts.wait_script == null) {
// We default to .done if both wait_selector and wait_script are null
// This allows the caller to ONLY --wait-selector or ONLY --wait-script
// or combine --wait-until WITH --wait-selector/script
try runner.wait(.{ .ms = opts.wait_ms, .until = .done });
}
if (opts.wait_selector) |selector| {
const elapsed: u32 = @intCast(timer.read() / std.time.ns_per_ms);
const remaining = opts.wait_ms -| elapsed;
if (remaining == 0) return error.Timeout;
_ = try runner.waitForSelector(selector, remaining);
}
if (opts.wait_script) |script| {
const elapsed: u32 = @intCast(timer.read() / std.time.ns_per_ms);
const remaining = opts.wait_ms -| elapsed;
if (remaining == 0) return error.Timeout;
try runner.waitForScript(script, remaining);
}
try runner.wait(.{ .ms = opts.wait_ms, .until = opts.wait_until });
const writer = opts.writer orelse return;
if (opts.dump_mode) |mode| {
@@ -263,10 +237,6 @@ pub fn RC(comptime T: type) type {
session.releaseArena(kv.value.arena);
}
}
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
return writer.print("{d}", .{self._refs});
}
};
}

View File

@@ -39,6 +39,7 @@ pub const Scope = enum {
telemetry,
unknown_prop,
mcp,
cache,
};
const Opts = struct {

View File

@@ -123,8 +123,6 @@ fn run(allocator: Allocator, main_arena: Allocator) !void {
var fetch_opts = lp.FetchOpts{
.wait_ms = opts.wait_ms,
.wait_until = opts.wait_until,
.wait_script = opts.wait_script,
.wait_selector = opts.wait_selector,
.dump_mode = opts.dump_mode,
.dump = .{
.strip = opts.strip,

View File

@@ -75,19 +75,6 @@ pub const tool_list = [_]protocol.Tool{
\\}
),
},
.{
.name = "nodeDetails",
.description = "Get detailed information about a specific node by its backend node ID. Returns tag, role, name, interactivity, disabled state, value, input type, placeholder, href, checked state, and select options.",
.inputSchema = protocol.minify(
\\{
\\ "type": "object",
\\ "properties": {
\\ "backendNodeId": { "type": "integer", "description": "The backend node ID of the element to inspect." }
\\ },
\\ "required": ["backendNodeId"]
\\}
),
},
.{
.name = "interactiveElements",
.description = "Extract interactive elements from the opened page. If a url is provided, it navigates to that url first.",
@@ -269,7 +256,6 @@ const ToolAction = enum {
navigate,
markdown,
links,
nodeDetails,
interactiveElements,
structuredData,
detectForms,
@@ -286,7 +272,6 @@ const tool_map = std.StaticStringMap(ToolAction).initComptime(.{
.{ "navigate", .navigate },
.{ "markdown", .markdown },
.{ "links", .links },
.{ "nodeDetails", .nodeDetails },
.{ "interactiveElements", .interactiveElements },
.{ "structuredData", .structuredData },
.{ "detectForms", .detectForms },
@@ -320,7 +305,6 @@ pub fn handleCall(server: *Server, arena: std.mem.Allocator, req: protocol.Reque
.goto, .navigate => try handleGoto(server, arena, req.id.?, call_params.arguments),
.markdown => try handleMarkdown(server, arena, req.id.?, call_params.arguments),
.links => try handleLinks(server, arena, req.id.?, call_params.arguments),
.nodeDetails => try handleNodeDetails(server, arena, req.id.?, call_params.arguments),
.interactiveElements => try handleInteractiveElements(server, arena, req.id.?, call_params.arguments),
.structuredData => try handleStructuredData(server, arena, req.id.?, call_params.arguments),
.detectForms => try handleDetectForms(server, arena, req.id.?, call_params.arguments),
@@ -389,32 +373,6 @@ fn handleSemanticTree(server: *Server, arena: std.mem.Allocator, id: std.json.Va
};
}
fn handleNodeDetails(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
const Params = struct {
backendNodeId: CDPNode.Id,
};
const args = try parseArgs(Params, arena, arguments, server, id, "nodeDetails");
_ = server.session.currentPage() orelse {
return server.sendError(id, .PageNotLoaded, "Page not loaded");
};
const node = server.node_registry.lookup_by_id.get(args.backendNodeId) orelse {
return server.sendError(id, .InvalidParams, "Node not found");
};
const page = server.session.currentPage().?;
const details = lp.SemanticTree.getNodeDetails(arena, node.dom, &server.node_registry, page) catch {
return server.sendError(id, .InternalError, "Failed to get node details");
};
var aw: std.Io.Writer.Allocating = .init(arena);
try std.json.Stringify.value(&details, .{}, &aw.writer);
const content = [_]protocol.TextContent([]const u8){.{ .text = aw.written() }};
try server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content });
}
fn handleInteractiveElements(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
const args = try parseArgsOrDefault(UrlParams, arena, arguments, server, id);
const page = try ensurePage(server, id, args.url);

View File

@@ -29,7 +29,9 @@ const libcurl = @import("../sys/libcurl.zig");
const net_http = @import("http.zig");
const RobotStore = @import("Robots.zig").RobotStore;
const WebBotAuth = @import("WebBotAuth.zig");
const Cache = @import("cache/Cache.zig");
const App = @import("../App.zig");
const Runtime = @This();
const Listener = struct {
@@ -45,10 +47,12 @@ const MAX_TICK_CALLBACKS = 16;
allocator: Allocator,
app: *App,
config: *const Config,
ca_blob: ?net_http.Blob,
robot_store: RobotStore,
web_bot_auth: ?WebBotAuth,
cache: ?Cache,
connections: []net_http.Connection,
available: std.DoublyLinkedList = .{},
@@ -200,7 +204,7 @@ fn globalDeinit() void {
libcurl.curl_global_cleanup();
}
pub fn init(allocator: Allocator, config: *const Config) !Runtime {
pub fn init(allocator: Allocator, app: *App, config: *const Config) !Runtime {
globalInit(allocator);
errdefer globalDeinit();
@@ -233,6 +237,11 @@ pub fn init(allocator: Allocator, config: *const Config) !Runtime {
else
null;
const cache = if (config.cacheDir()) |cache_dir_path|
Cache{ .kind = .{ .fs = try .init(cache_dir_path) } }
else
null;
return .{
.allocator = allocator,
.config = config,
@@ -244,8 +253,10 @@ pub fn init(allocator: Allocator, config: *const Config) !Runtime {
.available = available,
.connections = connections,
.app = app,
.robot_store = RobotStore.init(allocator),
.web_bot_auth = web_bot_auth,
.cache = cache,
};
}
@@ -278,6 +289,8 @@ pub fn deinit(self: *Runtime) void {
wba.deinit(self.allocator);
}
if (self.cache) |*cache| cache.deinit();
globalDeinit();
}

156
src/network/cache/Cache.zig vendored Normal file
View File

@@ -0,0 +1,156 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// 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");
const Http = @import("../http.zig");
const FsCache = @import("FsCache.zig");
/// A browser-wide cache for resources across the network.
/// This mostly conforms to RFC9111 with regards to caching behavior.
pub const Cache = @This();
kind: union(enum) {
fs: FsCache,
},
pub fn deinit(self: *Cache) void {
return switch (self.kind) {
inline else => |*c| c.deinit(),
};
}
pub fn get(self: *Cache, arena: std.mem.Allocator, req: CacheRequest) ?CachedResponse {
return switch (self.kind) {
inline else => |*c| c.get(arena, req),
};
}
pub fn put(self: *Cache, metadata: CachedMetadata, body: []const u8) !void {
return switch (self.kind) {
inline else => |*c| c.put(metadata, body),
};
}
pub const CacheControl = struct {
max_age: u64,
pub fn parse(value: []const u8) ?CacheControl {
var cc: CacheControl = .{ .max_age = undefined };
var max_age_set = false;
var max_s_age_set = false;
var is_public = false;
var iter = std.mem.splitScalar(u8, value, ',');
while (iter.next()) |part| {
const directive = std.mem.trim(u8, part, &std.ascii.whitespace);
if (std.ascii.eqlIgnoreCase(directive, "no-store")) {
return null;
} else if (std.ascii.eqlIgnoreCase(directive, "no-cache")) {
return null;
} else if (std.ascii.eqlIgnoreCase(directive, "public")) {
is_public = true;
} else if (std.ascii.startsWithIgnoreCase(directive, "max-age=")) {
if (!max_s_age_set) {
if (std.fmt.parseInt(u64, directive[8..], 10) catch null) |max_age| {
cc.max_age = max_age;
max_age_set = true;
}
}
} else if (std.ascii.startsWithIgnoreCase(directive, "s-maxage=")) {
if (std.fmt.parseInt(u64, directive[9..], 10) catch null) |max_age| {
cc.max_age = max_age;
max_age_set = true;
max_s_age_set = true;
}
}
}
if (!max_age_set) return null;
if (!is_public) return null;
if (cc.max_age == 0) return null;
return cc;
}
};
pub const CachedMetadata = struct {
url: [:0]const u8,
content_type: []const u8,
status: u16,
stored_at: i64,
age_at_store: u64,
cache_control: CacheControl,
/// Response Headers
headers: []const Http.Header,
/// These are Request Headers used by Vary.
vary_headers: []const Http.Header,
};
pub const CacheRequest = struct {
url: []const u8,
timestamp: i64,
request_headers: []const Http.Header,
};
pub const CachedData = union(enum) {
buffer: []const u8,
file: struct {
file: std.fs.File,
offset: usize,
len: usize,
},
};
pub const CachedResponse = struct {
metadata: CachedMetadata,
data: CachedData,
};
pub fn tryCache(
arena: std.mem.Allocator,
timestamp: i64,
url: [:0]const u8,
status: u16,
content_type: ?[]const u8,
cache_control: ?[]const u8,
vary: ?[]const u8,
age: ?[]const u8,
has_set_cookie: bool,
has_authorization: bool,
) !?CachedMetadata {
if (status != 200) return null;
if (has_set_cookie) return null;
if (has_authorization) return null;
if (vary) |v| if (std.mem.eql(u8, v, "*")) return null;
const cc = CacheControl.parse(cache_control orelse return null) orelse return null;
return .{
.url = url,
.content_type = if (content_type) |ct| try arena.dupe(u8, ct) else "application/octet-stream",
.status = status,
.stored_at = timestamp,
.age_at_store = if (age) |a| std.fmt.parseInt(u64, a, 10) catch 0 else 0,
.cache_control = cc,
.headers = &.{},
.vary_headers = &.{},
};
}

580
src/network/cache/FsCache.zig vendored Normal file
View File

@@ -0,0 +1,580 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// 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");
const log = @import("../../log.zig");
const Cache = @import("Cache.zig");
const Http = @import("../http.zig");
const CacheRequest = Cache.CacheRequest;
const CachedMetadata = Cache.CachedMetadata;
const CachedResponse = Cache.CachedResponse;
const CACHE_VERSION: usize = 1;
const LOCK_STRIPES = 16;
comptime {
std.debug.assert(std.math.isPowerOfTwo(LOCK_STRIPES));
}
pub const FsCache = @This();
dir: std.fs.Dir,
locks: [LOCK_STRIPES]std.Thread.Mutex = .{std.Thread.Mutex{}} ** LOCK_STRIPES,
const CacheMetadataJson = struct {
version: usize,
metadata: CachedMetadata,
};
fn getLockPtr(self: *FsCache, key: *const [HASHED_KEY_LEN]u8) *std.Thread.Mutex {
const lock_idx = std.hash.Wyhash.hash(0, key[0..]) & (LOCK_STRIPES - 1);
return &self.locks[lock_idx];
}
const BODY_LEN_HEADER_LEN = 8;
const HASHED_KEY_LEN = 64;
const HASHED_PATH_LEN = HASHED_KEY_LEN + 6;
const HASHED_TMP_PATH_LEN = HASHED_PATH_LEN + 4;
fn hashKey(key: []const u8) [HASHED_KEY_LEN]u8 {
var digest: [std.crypto.hash.sha2.Sha256.digest_length]u8 = undefined;
std.crypto.hash.sha2.Sha256.hash(key, &digest, .{});
var hex: [HASHED_KEY_LEN]u8 = undefined;
_ = std.fmt.bufPrint(&hex, "{s}", .{std.fmt.bytesToHex(&digest, .lower)}) catch unreachable;
return hex;
}
fn cachePath(hashed_key: *const [HASHED_KEY_LEN]u8) [HASHED_PATH_LEN]u8 {
var path: [HASHED_PATH_LEN]u8 = undefined;
_ = std.fmt.bufPrint(&path, "{s}.cache", .{hashed_key}) catch unreachable;
return path;
}
fn cacheTmpPath(hashed_key: *const [HASHED_KEY_LEN]u8) [HASHED_TMP_PATH_LEN]u8 {
var path: [HASHED_TMP_PATH_LEN]u8 = undefined;
_ = std.fmt.bufPrint(&path, "{s}.cache.tmp", .{hashed_key}) catch unreachable;
return path;
}
pub fn init(path: []const u8) !FsCache {
const cwd = std.fs.cwd();
cwd.makeDir(path) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => return err,
};
const dir = try cwd.openDir(path, .{ .iterate = true });
return .{ .dir = dir };
}
pub fn deinit(self: *FsCache) void {
self.dir.close();
}
pub fn get(self: *FsCache, arena: std.mem.Allocator, req: CacheRequest) ?Cache.CachedResponse {
const hashed_key = hashKey(req.url);
const cache_p = cachePath(&hashed_key);
const lock = self.getLockPtr(&hashed_key);
lock.lock();
defer lock.unlock();
const file = self.dir.openFile(&cache_p, .{ .mode = .read_only }) catch |e| {
switch (e) {
std.fs.File.OpenError.FileNotFound => {
log.debug(.cache, "miss", .{ .url = req.url, .hash = &hashed_key });
},
else => |err| {
log.warn(.cache, "open file err", .{ .url = req.url, .err = err });
},
}
return null;
};
var cleanup = false;
defer if (cleanup) {
file.close();
self.dir.deleteFile(&cache_p) catch |e| {
log.err(.cache, "clean fail", .{ .url = req.url, .file = &cache_p, .err = e });
};
};
var file_buf: [1024]u8 = undefined;
var len_buf: [BODY_LEN_HEADER_LEN]u8 = undefined;
var file_reader = file.reader(&file_buf);
const file_reader_iface = &file_reader.interface;
file_reader_iface.readSliceAll(&len_buf) catch |e| {
log.warn(.cache, "read header", .{ .url = req.url, .err = e });
cleanup = true;
return null;
};
const body_len = std.mem.readInt(u64, &len_buf, .little);
// Now we read metadata.
file_reader.seekTo(body_len + BODY_LEN_HEADER_LEN) catch |e| {
log.warn(.cache, "seek metadata", .{ .url = req.url, .err = e });
cleanup = true;
return null;
};
var json_reader = std.json.Reader.init(arena, file_reader_iface);
const cache_file: CacheMetadataJson = std.json.parseFromTokenSourceLeaky(
CacheMetadataJson,
arena,
&json_reader,
.{ .allocate = .alloc_always },
) catch |e| {
log.warn(.cache, "metadata parse", .{ .url = req.url, .err = e });
cleanup = true;
return null;
};
if (cache_file.version != CACHE_VERSION) {
log.warn(.cache, "version", .{ .url = req.url, .expected = CACHE_VERSION, .got = cache_file.version });
cleanup = true;
return null;
}
const metadata = cache_file.metadata;
// Check entry expiration.
const now = req.timestamp;
const age = (now - metadata.stored_at) + @as(i64, @intCast(metadata.age_at_store));
if (age < 0 or @as(u64, @intCast(age)) >= metadata.cache_control.max_age) {
log.debug(.cache, "expired", .{ .url = req.url });
cleanup = true;
return null;
}
// If we have Vary headers, ensure they are present & matching.
for (metadata.vary_headers) |vary_hdr| {
const name = vary_hdr.name;
const value = vary_hdr.value;
const incoming = for (req.request_headers) |h| {
if (std.ascii.eqlIgnoreCase(h.name, name)) break h.value;
} else "";
if (!std.ascii.eqlIgnoreCase(value, incoming)) {
log.debug(.cache, "vary mismatch", .{ .url = req.url, .header = name });
return null;
}
}
// On the case of a hash collision.
if (!std.ascii.eqlIgnoreCase(metadata.url, req.url)) {
log.warn(.cache, "collision", .{ .url = req.url, .expected = metadata.url, .got = req.url });
cleanup = true;
return null;
}
return .{
.metadata = metadata,
.data = .{
.file = .{
.file = file,
.offset = BODY_LEN_HEADER_LEN,
.len = body_len,
},
},
};
}
pub fn put(self: *FsCache, meta: CachedMetadata, body: []const u8) !void {
const hashed_key = hashKey(meta.url);
const cache_p = cachePath(&hashed_key);
const cache_tmp_p = cacheTmpPath(&hashed_key);
const lock = self.getLockPtr(&hashed_key);
lock.lock();
defer lock.unlock();
const file = try self.dir.createFile(&cache_tmp_p, .{ .truncate = true });
defer file.close();
var writer_buf: [1024]u8 = undefined;
var file_writer = file.writer(&writer_buf);
var file_writer_iface = &file_writer.interface;
var len_buf: [8]u8 = undefined;
std.mem.writeInt(u64, &len_buf, body.len, .little);
try file_writer_iface.writeAll(&len_buf);
try file_writer_iface.writeAll(body);
try std.json.Stringify.value(
CacheMetadataJson{ .version = CACHE_VERSION, .metadata = meta },
.{ .whitespace = .minified },
file_writer_iface,
);
try file_writer_iface.flush();
try self.dir.rename(&cache_tmp_p, &cache_p);
}
const testing = std.testing;
fn setupCache() !struct { tmp: testing.TmpDir, cache: Cache } {
var tmp = testing.tmpDir(.{});
errdefer tmp.cleanup();
const path = try tmp.dir.realpathAlloc(testing.allocator, ".");
defer testing.allocator.free(path);
return .{
.tmp = tmp,
.cache = Cache{ .kind = .{ .fs = try FsCache.init(path) } },
};
}
test "FsCache: basic put and get" {
var setup = try setupCache();
defer {
setup.cache.deinit();
setup.tmp.cleanup();
}
const cache = &setup.cache;
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const now = std.time.timestamp();
const meta = CachedMetadata{
.url = "https://example.com",
.content_type = "text/html",
.status = 200,
.stored_at = now,
.age_at_store = 0,
.cache_control = .{ .max_age = 600 },
.headers = &.{},
.vary_headers = &.{},
};
const body = "hello world";
try cache.put(meta, body);
const result = cache.get(
arena.allocator(),
.{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{},
},
) orelse return error.CacheMiss;
const f = result.data.file;
const file = f.file;
defer file.close();
var buf: [64]u8 = undefined;
var file_reader = file.reader(&buf);
try file_reader.seekTo(f.offset);
const read_buf = try file_reader.interface.readAlloc(testing.allocator, f.len);
defer testing.allocator.free(read_buf);
try testing.expectEqualStrings(body, read_buf);
}
test "FsCache: get expiration" {
var setup = try setupCache();
defer {
setup.cache.deinit();
setup.tmp.cleanup();
}
const cache = &setup.cache;
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const now = 5000;
const max_age = 1000;
const meta = CachedMetadata{
.url = "https://example.com",
.content_type = "text/html",
.status = 200,
.stored_at = now,
.age_at_store = 900,
.cache_control = .{ .max_age = max_age },
.headers = &.{},
.vary_headers = &.{},
};
const body = "hello world";
try cache.put(meta, body);
const result = cache.get(
arena.allocator(),
.{
.url = "https://example.com",
.timestamp = now + 50,
.request_headers = &.{},
},
) orelse return error.CacheMiss;
result.data.file.file.close();
try testing.expectEqual(null, cache.get(
arena.allocator(),
.{
.url = "https://example.com",
.timestamp = now + 200,
.request_headers = &.{},
},
));
try testing.expectEqual(null, cache.get(
arena.allocator(),
.{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{},
},
));
}
test "FsCache: put override" {
var setup = try setupCache();
defer {
setup.cache.deinit();
setup.tmp.cleanup();
}
const cache = &setup.cache;
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
{
const now = 5000;
const max_age = 1000;
const meta = CachedMetadata{
.url = "https://example.com",
.content_type = "text/html",
.status = 200,
.stored_at = now,
.age_at_store = 900,
.cache_control = .{ .max_age = max_age },
.headers = &.{},
.vary_headers = &.{},
};
const body = "hello world";
try cache.put(meta, body);
const result = cache.get(
arena.allocator(),
.{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{},
},
) orelse return error.CacheMiss;
const f = result.data.file;
const file = f.file;
defer file.close();
var buf: [64]u8 = undefined;
var file_reader = file.reader(&buf);
try file_reader.seekTo(f.offset);
const read_buf = try file_reader.interface.readAlloc(testing.allocator, f.len);
defer testing.allocator.free(read_buf);
try testing.expectEqualStrings(body, read_buf);
}
{
const now = 10000;
const max_age = 2000;
const meta = CachedMetadata{
.url = "https://example.com",
.content_type = "text/html",
.status = 200,
.stored_at = now,
.age_at_store = 0,
.cache_control = .{ .max_age = max_age },
.headers = &.{},
.vary_headers = &.{},
};
const body = "goodbye world";
try cache.put(meta, body);
const result = cache.get(
arena.allocator(),
.{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{},
},
) orelse return error.CacheMiss;
const f = result.data.file;
const file = f.file;
defer file.close();
var buf: [64]u8 = undefined;
var file_reader = file.reader(&buf);
try file_reader.seekTo(f.offset);
const read_buf = try file_reader.interface.readAlloc(testing.allocator, f.len);
defer testing.allocator.free(read_buf);
try testing.expectEqualStrings(body, read_buf);
}
}
test "FsCache: garbage file" {
var setup = try setupCache();
defer {
setup.cache.deinit();
setup.tmp.cleanup();
}
const hashed_key = hashKey("https://example.com");
const cache_p = cachePath(&hashed_key);
const file = try setup.cache.kind.fs.dir.createFile(&cache_p, .{});
try file.writeAll("this is not a valid cache file !@#$%");
file.close();
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
try testing.expectEqual(
null,
setup.cache.get(arena.allocator(), .{
.url = "https://example.com",
.timestamp = 5000,
.request_headers = &.{},
}),
);
}
test "FsCache: vary hit and miss" {
var setup = try setupCache();
defer {
setup.cache.deinit();
setup.tmp.cleanup();
}
const cache = &setup.cache;
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const now = std.time.timestamp();
const meta = CachedMetadata{
.url = "https://example.com",
.content_type = "text/html",
.status = 200,
.stored_at = now,
.age_at_store = 0,
.cache_control = .{ .max_age = 600 },
.headers = &.{},
.vary_headers = &.{
.{ .name = "Accept-Encoding", .value = "gzip" },
},
};
try cache.put(meta, "hello world");
const result = cache.get(arena.allocator(), .{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{
.{ .name = "Accept-Encoding", .value = "gzip" },
},
}) orelse return error.CacheMiss;
result.data.file.file.close();
try testing.expectEqual(null, cache.get(arena.allocator(), .{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{
.{ .name = "Accept-Encoding", .value = "br" },
},
}));
try testing.expectEqual(null, cache.get(arena.allocator(), .{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{},
}));
const result2 = cache.get(arena.allocator(), .{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{
.{ .name = "Accept-Encoding", .value = "gzip" },
},
}) orelse return error.CacheMiss;
result2.data.file.file.close();
}
test "FsCache: vary multiple headers" {
var setup = try setupCache();
defer {
setup.cache.deinit();
setup.tmp.cleanup();
}
const cache = &setup.cache;
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const now = std.time.timestamp();
const meta = CachedMetadata{
.url = "https://example.com",
.content_type = "text/html",
.status = 200,
.stored_at = now,
.age_at_store = 0,
.cache_control = .{ .max_age = 600 },
.headers = &.{},
.vary_headers = &.{
.{ .name = "Accept-Encoding", .value = "gzip" },
.{ .name = "Accept-Language", .value = "en" },
},
};
try cache.put(meta, "hello world");
const result = cache.get(arena.allocator(), .{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{
.{ .name = "Accept-Encoding", .value = "gzip" },
.{ .name = "Accept-Language", .value = "en" },
},
}) orelse return error.CacheMiss;
result.data.file.file.close();
try testing.expectEqual(null, cache.get(arena.allocator(), .{
.url = "https://example.com",
.timestamp = now,
.request_headers = &.{
.{ .name = "Accept-Encoding", .value = "gzip" },
.{ .name = "Accept-Language", .value = "fr" },
},
}));
}

View File

@@ -79,7 +79,7 @@ pub const Headers = struct {
self.headers = updated_headers;
}
fn parseHeader(header_str: []const u8) ?Header {
pub fn parseHeader(header_str: []const u8) ?Header {
const colon_pos = std.mem.indexOfScalar(u8, header_str, ':') orelse return null;
const name = std.mem.trim(u8, header_str[0..colon_pos], " \t");
@@ -88,22 +88,9 @@ pub const Headers = struct {
return .{ .name = name, .value = value };
}
pub fn iterator(self: *Headers) Iterator {
return .{
.header = self.headers,
};
pub fn iterator(self: Headers) HeaderIterator {
return .{ .curl_slist = .{ .header = self.headers } };
}
const Iterator = struct {
header: [*c]libcurl.CurlSList,
pub fn next(self: *Iterator) ?Header {
const h = self.header orelse return null;
self.header = h.*.next;
return parseHeader(std.mem.span(@as([*:0]const u8, @ptrCast(h.*.data))));
}
};
};
// In normal cases, the header iterator comes from the curl linked list.
@@ -112,6 +99,7 @@ pub const Headers = struct {
// This union, is an iterator that exposes the same API for either case.
pub const HeaderIterator = union(enum) {
curl: CurlHeaderIterator,
curl_slist: CurlSListIterator,
list: ListHeaderIterator,
pub fn next(self: *HeaderIterator) ?Header {
@@ -120,6 +108,19 @@ pub const HeaderIterator = union(enum) {
}
}
pub fn collect(self: *HeaderIterator, allocator: std.mem.Allocator) !std.ArrayList(Header) {
var list: std.ArrayList(Header) = .empty;
while (self.next()) |hdr| {
try list.append(allocator, .{
.name = try allocator.dupe(u8, hdr.name),
.value = try allocator.dupe(u8, hdr.value),
});
}
return list;
}
const CurlHeaderIterator = struct {
conn: *const Connection,
prev: ?*libcurl.CurlHeader = null,
@@ -136,6 +137,16 @@ pub const HeaderIterator = union(enum) {
}
};
const CurlSListIterator = struct {
header: [*c]libcurl.CurlSList,
pub fn next(self: *CurlSListIterator) ?Header {
const h = self.header orelse return null;
self.header = h.*.next;
return Headers.parseHeader(std.mem.span(@as([*:0]const u8, @ptrCast(h.*.data))));
}
};
const ListHeaderIterator = struct {
index: usize = 0,
list: []const Header,

View File

@@ -452,10 +452,8 @@ fn runWebApiTest(test_file: [:0]const u8) !void {
}
}
const PageTestOpts = struct {
wait_until_done: bool = true,
};
pub fn pageTest(comptime test_file: []const u8, opts: PageTestOpts) !*Page {
// Used by a few CDP tests - wouldn't be sad to see this go.
pub fn pageTest(comptime test_file: []const u8) !*Page {
const page = try test_session.createPage();
errdefer test_session.removePage();
@@ -468,9 +466,7 @@ pub fn pageTest(comptime test_file: []const u8, opts: PageTestOpts) !*Page {
try page.navigate(url, .{});
var runner = try test_session.runner(.{});
if (opts.wait_until_done) {
try runner.wait(.{ .ms = 2000 });
}
try runner.wait(.{ .ms = 2000 });
return page;
}