Skip to content

Lightpanda: the headless browser designed for AI and automation

bidi: add skeleton for bidi

Karl Seguin7c51dfff0551
diff --git a/src/Inbox.zig b/src/Inbox.zig
index e225243bd..d5a4092d6 100644
--- a/src/Inbox.zig
+++ b/src/Inbox.zig
@@ -28,7 +28,7 @@
const std = @import("std");
const lp = @import("lightpanda");
-const CDP = @import("cdp/CDP.zig");
+const CDP = @import("server/cdp/CDP.zig");
const DoublyLinkedList = std.DoublyLinkedList;
@@ -120,14 +120,20 @@ pub const Message = struct {
// consumer's use of `input`.
cdp: Cdp,
+ // A BiDi text/binary frame, raw (owned). Unlike CDP it isn't
+ // parsed on the Network thread — nothing on that side needs the
+ // method name yet.
+ bidi: []u8,
+
// WS ping frame body (≤125 bytes per spec). Consumer is
// expected to echo via pong on its thread.
ping: []u8,
- // Peer-initiated close frame. Consumer is expected to send a
- // close reply and tear the connection down. The peer's close
- // body is dropped — historically we always reply CLOSE_NORMAL
- // (status 1000) regardless of what the peer sent.
+ // A close frame was received from the peer, or the worker decided
+ // to close (BiDi's session.end). Consumer is expected to send the
+ // close frame and tear the connection down. A peer's close body is
+ // dropped — we always send CLOSE_NORMAL (status 1000) regardless of
+ // what the peer sent.
close: void,
// No allocation; conveys "no more messages will arrive on
diff --git a/src/NodeRegistry.zig b/src/NodeRegistry.zig
new file mode 100644
index 000000000..96359e7ae
--- /dev/null
+++ b/src/NodeRegistry.zig
@@ -0,0 +1,190 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+
+// 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/>.
+
+// Issues stable ids for DOM nodes exposed to a client (CDP node ids, the
+// agent's backendNodeIds, ...) and maintains the two-way node <-> id lookup.
+
+const std = @import("std");
+
+const Frame = @import("browser/Frame.zig");
+const DOMNode = @import("browser/webapi/Node.zig");
+
+const Allocator = std.mem.Allocator;
+
+const NodeRegistry = @This();
+
+pub const Id = u32;
+
+pub const Node = struct {
+ id: Id,
+ dom: *DOMNode,
+};
+
+node_id: Id,
+allocator: Allocator,
+node_pool: std.heap.MemoryPool(Node),
+lookup_by_id: std.AutoHashMapUnmanaged(Id, *Node),
+lookup_by_node: std.HashMapUnmanaged(*DOMNode, *Node, NodeContext, std.hash_map.default_max_load_percentage),
+
+pub fn init(allocator: Allocator) NodeRegistry {
+ return .{
+ .node_id = 1,
+ .node_pool = .empty,
+ .lookup_by_id = .{},
+ .lookup_by_node = .{},
+ .allocator = allocator,
+ };
+}
+
+pub fn deinit(self: *NodeRegistry) void {
+ const allocator = self.allocator;
+ self.lookup_by_id.deinit(allocator);
+ self.lookup_by_node.deinit(allocator);
+ self.node_pool.deinit(allocator);
+}
+
+pub fn reset(self: *NodeRegistry) void {
+ self.lookup_by_id.clearRetainingCapacity();
+ self.lookup_by_node.clearRetainingCapacity();
+ _ = self.node_pool.reset(self.allocator, .{ .retain_with_limit = 1024 });
+}
+
+/// Evict only the nodes owned by `frame`'s page, leaving sibling pages' node
+/// IDs valid. Must run before the page's arena is freed — attribution walks
+/// each node's live parent chain.
+pub fn resetFrame(self: *NodeRegistry, arena: Allocator, frame: *Frame) void {
+ const page = frame._page;
+ var doomed: std.ArrayListUnmanaged(*Node) = .empty;
+ var it = self.lookup_by_id.valueIterator();
+ while (it.next()) |node_ptr| {
+ const node = node_ptr.*;
+ if (node.dom.ownerFrame(frame)._page == page) {
+ doomed.append(arena, node) catch return;
+ }
+ }
+ for (doomed.items) |node| {
+ _ = self.lookup_by_id.remove(node.id);
+ _ = self.lookup_by_node.remove(node.dom);
+ self.node_pool.destroy(node);
+ }
+}
+
+pub fn register(self: *NodeRegistry, dom_node: *DOMNode) !*Node {
+ const node_lookup_gop = try self.lookup_by_node.getOrPut(self.allocator, dom_node);
+ if (node_lookup_gop.found_existing) {
+ return node_lookup_gop.value_ptr.*;
+ }
+
+ // on error, we're probably going to abort the entire browser context
+ // but, just in case, let's try to keep things tidy.
+ errdefer _ = self.lookup_by_node.remove(dom_node);
+
+ const node = try self.node_pool.create(self.allocator);
+ errdefer self.node_pool.destroy(node);
+
+ const id = self.node_id;
+ self.node_id = id + 1;
+
+ node.* = .{
+ .id = id,
+ .dom = dom_node,
+ };
+
+ node_lookup_gop.value_ptr.* = node;
+ try self.lookup_by_id.putNoClobber(self.allocator, id, node);
+ return node;
+}
+
+const NodeContext = struct {
+ pub fn hash(_: NodeContext, dom_node: *DOMNode) u64 {
+ return std.hash.Wyhash.hash(0, std.mem.asBytes(&@intFromPtr(dom_node)));
+ }
+
+ pub fn eql(_: NodeContext, a: *DOMNode, b: *DOMNode) bool {
+ return @intFromPtr(a) == @intFromPtr(b);
+ }
+};
+
+const testing = @import("testing.zig");
+test "NodeRegistry: register" {
+ var registry = NodeRegistry.init(testing.allocator);
+ defer registry.deinit();
+
+ 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", .{});
+ defer page.close();
+
+ const frame = page.frame().?;
+ var doc = frame.window._document;
+
+ {
+ const dom_node = (try doc.querySelector(.wrap("#a1"), frame)).?.asNode();
+ const node = try registry.register(dom_node);
+ const n1b = registry.lookup_by_id.get(1).?;
+ const n1c = registry.lookup_by_node.get(node.dom).?;
+ try testing.expectEqual(node, n1b);
+ try testing.expectEqual(node, n1c);
+
+ try testing.expectEqual(1, node.id);
+ try testing.expectEqual(dom_node, node.dom);
+ }
+
+ {
+ const dom_node = (try doc.querySelector(.wrap("p"), frame)).?.asNode();
+ const node = try registry.register(dom_node);
+ const n1b = registry.lookup_by_id.get(2).?;
+ const n1c = registry.lookup_by_node.get(node.dom).?;
+ try testing.expectEqual(node, n1b);
+ try testing.expectEqual(node, n1c);
+
+ try testing.expectEqual(2, node.id);
+ try testing.expectEqual(dom_node, node.dom);
+ }
+}
+
+test "NodeRegistry: resetFrame" {
+ var registry = NodeRegistry.init(testing.allocator);
+ defer registry.deinit();
+
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+
+ var page_a = try testing.pageTest("cdp/registry1.html", .{});
+ defer page_a.close();
+ var page_b = try testing.pageTest("cdp/registry2.html", .{});
+ defer page_b.close();
+
+ const frame_a = page_a.frame().?;
+ const frame_b = page_b.frame().?;
+
+ const a_node = (try frame_a.window._document.querySelector(.wrap("#a1"), frame_a)).?.asNode();
+ const b_node = (try frame_b.window._document.querySelector(.wrap("a"), frame_b)).?.asNode();
+ const ra = try registry.register(a_node);
+ const rb = try registry.register(b_node);
+ try testing.expectEqual(2, registry.lookup_by_id.count());
+
+ registry.resetFrame(arena.allocator(), frame_a);
+
+ try testing.expectEqual(null, registry.lookup_by_id.get(ra.id));
+ try testing.expectEqual(null, registry.lookup_by_node.get(a_node));
+ try testing.expectEqual(1, registry.lookup_by_id.count());
+ try testing.expectEqual(rb, registry.lookup_by_id.get(rb.id).?);
+ try testing.expectEqual(b_node, registry.lookup_by_node.get(b_node).?.dom);
+}
diff --git a/src/SemanticTree.zig b/src/SemanticTree.zig
index 896a1b233..63a78542c 100644
--- a/src/SemanticTree.zig
+++ b/src/SemanticTree.zig
@@ -19,7 +19,6 @@
const std = @import("std");
const lp = @import("lightpanda");
-const isAllWhitespace = @import("string.zig").isAllWhitespace;
const interactive = @import("browser/interactive.zig");
const SelectorPath = @import("browser/SelectorPath.zig");
@@ -27,8 +26,10 @@ const CData = @import("browser/webapi/CData.zig");
const Element = @import("browser/webapi/Element.zig");
const Label = @import("browser/webapi/element/html/Label.zig");
const Node = @import("browser/webapi/Node.zig");
-const AXNode = @import("cdp/AXNode.zig");
-const CDPNode = @import("cdp/Node.zig");
+const AXNode = @import("server/cdp/AXNode.zig");
+
+const NodeRegistry = @import("NodeRegistry.zig");
+const isAllWhitespace = @import("string.zig").isAllWhitespace;
const log = lp.log;
const Frame = lp.Frame;
@@ -36,7 +37,7 @@ const Frame = lp.Frame;
const Self = @This();
dom_node: *Node,
-registry: *CDPNode.Registry,
+registry: *NodeRegistry,
frame: *Frame,
arena: std.mem.Allocator,
prune: bool = true,
@@ -96,7 +97,7 @@ const OptionData = struct {
};
const NodeData = struct {
- id: CDPNode.Id,
+ id: NodeRegistry.Id,
axn: AXNode,
role: []const u8,
name: ?[]const u8,
@@ -549,7 +550,7 @@ const TextVisitor = struct {
};
pub const NodeDetails = struct {
- backendNodeId: CDPNode.Id,
+ backendNodeId: NodeRegistry.Id,
tag_name: []const u8,
role: []const u8,
name: ?[]const u8,
@@ -655,7 +656,7 @@ pub const NodeDetails = struct {
pub fn getNodeDetails(
arena: std.mem.Allocator,
node: *Node,
- registry: *CDPNode.Registry,
+ registry: *NodeRegistry,
frame: *Frame,
) !NodeDetails {
const cdp_node = try registry.register(node);
@@ -737,7 +738,7 @@ pub fn getNodeDetails(
const testing = @import("testing.zig");
test "SemanticTree backendDOMNodeId" {
- var registry: CDPNode.Registry = .init(testing.allocator);
+ var registry: NodeRegistry = .init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/registry1.html", .{});
@@ -761,7 +762,7 @@ test "SemanticTree backendDOMNodeId" {
}
test "SemanticTree max_depth" {
- var registry: CDPNode.Registry = .init(testing.allocator);
+ var registry: NodeRegistry = .init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/registry1.html", .{});
diff --git a/src/ToolSession.zig b/src/ToolSession.zig
index eca52abc2..fa0a2e397 100644
--- a/src/ToolSession.zig
+++ b/src/ToolSession.zig
@@ -24,14 +24,14 @@ const App = @import("App.zig");
const Browser = @import("browser/Browser.zig");
const Session = @import("browser/Session.zig");
const Notification = @import("Notification.zig");
-const CDPNode = @import("cdp/Node.zig");
+const NodeRegistry = @import("NodeRegistry.zig");
const ToolSession = @This();
browser: Browser,
session: *Session,
notification: *Notification,
-registry: CDPNode.Registry,
+registry: NodeRegistry,
/// Leaves the browser's isolate entered, like `Browser.init`; callers
/// sharing one thread between several isolates park it with
diff --git a/src/browser/Browser.zig b/src/browser/Browser.zig
index e27d9208b..79cad999b 100644
--- a/src/browser/Browser.zig
+++ b/src/browser/Browser.zig
@@ -20,8 +20,8 @@ const std = @import("std");
const lp = @import("lightpanda");
const App = @import("../App.zig");
-const CDP = @import("../cdp/CDP.zig");
const Watchdog = @import("../Watchdog.zig");
+const Driver = @import("../server/Driver.zig");
const Notification = @import("../Notification.zig");
const HttpClient = @import("../network/HttpClient.zig");
@@ -112,7 +112,7 @@ pub fn nextFrameId(self: *Browser) u32 {
return id;
}
-pub fn init(self: *Browser, app: *App, opts: InitOpts, cdp: ?*CDP) !void {
+pub fn init(self: *Browser, app: *App, opts: InitOpts, driver: ?Driver) !void {
const allocator = app.allocator;
var env = try js.Env.init(app, opts.env);
@@ -131,7 +131,7 @@ pub fn init(self: *Browser, app: *App, opts: InitOpts, cdp: ?*CDP) !void {
.watchdog_entry = undefined,
};
self.env.protectHeapLimit();
- try self.http_client.init(app, cdp);
+ try self.http_client.init(app, driver);
self.watchdog_entry = .{
.env = &self.env,
diff --git a/src/browser/interactive.zig b/src/browser/interactive.zig
index 5d4ac1322..ba47dc493 100644
--- a/src/browser/interactive.zig
+++ b/src/browser/interactive.zig
@@ -22,7 +22,7 @@ const Frame = @import("Frame.zig");
const URL = @import("URL.zig");
const TreeWalker = @import("webapi/TreeWalker.zig");
const Label = @import("webapi/element/html/Label.zig");
-const AXNode = @import("../cdp/AXNode.zig");
+const AXNode = @import("../server/cdp/AXNode.zig");
const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig");
const EventTarget = @import("webapi/EventTarget.zig");
diff --git a/src/browser/links.zig b/src/browser/links.zig
index fe9dd00b8..31e60c3de 100644
--- a/src/browser/links.zig
+++ b/src/browser/links.zig
@@ -18,7 +18,7 @@
const std = @import("std");
-const AXNode = @import("../cdp/AXNode.zig");
+const AXNode = @import("../server/cdp/AXNode.zig");
const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig");
const Frame = @import("Frame.zig");
diff --git a/src/browser/tools.zig b/src/browser/tools.zig
index 81d29d03f..0a4e2c1ac 100644
--- a/src/browser/tools.zig
+++ b/src/browser/tools.zig
@@ -20,16 +20,18 @@ const std = @import("std");
const lp = @import("lightpanda");
const zenai = @import("zenai");
+const NodeRegistry = @import("../NodeRegistry.zig");
+const LimitedWriter = @import("../LimitedWriter.zig");
+
+const DOMNode = @import("webapi/Node.zig");
+const Selector = @import("webapi/selector/Selector.zig");
+
const log = lp.log;
const tavily = zenai.search.tavily;
const brave = zenai.search.brave;
const exa = zenai.search.exa;
const keenable = zenai.search.keenable;
-const DOMNode = @import("webapi/Node.zig");
-const CDPNode = @import("../cdp/Node.zig");
-const Selector = @import("webapi/selector/Selector.zig");
-
/// Conventions any LLM driving Lightpanda should follow. The standalone
/// agent prepends this to its own system prompt; the MCP server returns
/// it in the `instructions` field of the `initialize` response so
@@ -834,7 +836,7 @@ pub const UrlParams = struct {
const ActionTarget = union(enum) {
selector: []const u8,
- backend_node_id: CDPNode.Id,
+ backend_node_id: NodeRegistry.Id,
pub fn format(self: ActionTarget, writer: *std.Io.Writer) std.Io.Writer.Error!void {
switch (self) {
@@ -860,7 +862,7 @@ pub const inline_image_max_height = 4096;
pub fn call(
arena: std.mem.Allocator,
session: *lp.Session,
- registry: *CDPNode.Registry,
+ registry: *NodeRegistry,
tool_name: []const u8,
arguments: ?std.json.Value,
opts: CallOpts,
@@ -894,7 +896,7 @@ pub fn call(
fn dispatch(
arena: std.mem.Allocator,
session: *lp.Session,
- registry: *CDPNode.Registry,
+ registry: *NodeRegistry,
tool: Tool,
substituted: ?std.json.Value,
opts: CallOpts,
@@ -942,7 +944,7 @@ fn formatNavigationError(arena: std.mem.Allocator, session: *lp.Session) ?[]cons
pub fn evalScript(
arena: std.mem.Allocator,
session: *lp.Session,
- registry: *CDPNode.Registry,
+ registry: *NodeRegistry,
script: []const u8,
) ToolError!ToolResult {
const z = try arena.dupeZ(u8, script);
@@ -956,7 +958,7 @@ pub fn evalScript(
pub fn extract(
arena: std.mem.Allocator,
session: *lp.Session,
- registry: *CDPNode.Registry,
+ registry: *NodeRegistry,
schema_json: []const u8,
) ToolError!ToolResult {
const trimmed = std.mem.trim(u8, schema_json, &std.ascii.whitespace);
@@ -1021,7 +1023,7 @@ const schema_walker_prefix =
;
const schema_walker_suffix = ")";
-fn execGoto(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execGoto(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const args = try parseArgs(GotoParams, arena, arguments);
return switch (try performGoto(session, registry, args.url, .{ .timeout = args.timeout, .wait_until = args.waitUntil })) {
.completed => "Navigated successfully.",
@@ -1299,10 +1301,10 @@ fn renderFrameMarkdown(arena: std.mem.Allocator, frame: *lp.Frame) ToolError![]c
return aw.written();
}
-fn execMarkdown(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execMarkdown(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
selector: ?[]const u8 = null,
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
maxBytes: ?u32 = null,
url: ?[:0]const u8 = null,
timeout: ?u32 = null,
@@ -1319,21 +1321,21 @@ fn execMarkdown(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNo
/// The node a read tool works on: the selector match, the registry node, or
/// the whole document. All three live in the current frame.
-fn resolveScope(session: *lp.Session, registry: *CDPNode.Registry, page: *lp.Frame, selector: ?[]const u8, node_id: ?CDPNode.Id) ToolError!*DOMNode {
+fn resolveScope(session: *lp.Session, registry: *NodeRegistry, page: *lp.Frame, selector: ?[]const u8, node_id: ?NodeRegistry.Id) ToolError!*DOMNode {
if (selector == null and node_id == null) return page.document.asNode();
return (try resolveTarget(session, registry, selector, node_id)).node;
}
const HtmlParams = struct {
selector: ?[]const u8 = null,
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
maxBytes: ?u32 = null,
strip: lp.dump.Opts.Strip = .{},
url: ?[:0]const u8 = null,
timeout: ?u32 = null,
};
-fn execHtml(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execHtml(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const args = try parseArgsOrDefault(HtmlParams, arena, arguments);
const page = try ensurePage(session, registry, args.url, args.timeout);
@@ -1348,11 +1350,11 @@ fn execHtml(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.R
return aw.written();
}
-fn execScreenshot(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value, inline_image: bool) ToolError!ToolResult {
+fn execScreenshot(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value, inline_image: bool) ToolError!ToolResult {
const Params = struct {
path: ?[]const u8 = null,
selector: ?[]const u8 = null,
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
fullPage: bool = false,
url: ?[:0]const u8 = null,
timeout: ?u32 = null,
@@ -1395,7 +1397,7 @@ fn writePng(prepared: *const lp.screenshot.Prepared, path: []const u8) !u32 {
return height;
}
-fn execLinks(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execLinks(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
limit: ?u32 = null,
url: ?[:0]const u8 = null,
@@ -1414,7 +1416,7 @@ fn execLinks(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.
return renderJson(arena, links_list);
}
-fn execTree(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execTree(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const TreeParams = struct {
url: ?[:0]const u8 = null,
backendNodeId: ?u32 = null,
@@ -1440,8 +1442,8 @@ fn execTree(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.R
return aw.written();
}
-fn execNodeDetails(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
- const Params = struct { backendNodeId: CDPNode.Id };
+fn execNodeDetails(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
+ const Params = struct { backendNodeId: NodeRegistry.Id };
const args = try parseArgs(Params, arena, arguments);
const page = try requireFrame(session);
@@ -1453,7 +1455,7 @@ fn execNodeDetails(arena: std.mem.Allocator, session: *lp.Session, registry: *CD
return renderJson(arena, &details);
}
-fn execInteractiveElements(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execInteractiveElements(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const args = try parseArgsOrDefault(UrlParams, arena, arguments);
const page = try ensurePage(session, registry, args.url, args.timeout);
@@ -1464,7 +1466,7 @@ fn execInteractiveElements(arena: std.mem.Allocator, session: *lp.Session, regis
return renderJson(arena, elements);
}
-fn execStructuredData(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execStructuredData(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const args = try parseArgsOrDefault(UrlParams, arena, arguments);
const page = try ensurePage(session, registry, args.url, args.timeout);
@@ -1473,7 +1475,7 @@ fn execStructuredData(arena: std.mem.Allocator, session: *lp.Session, registry:
return renderJson(arena, data);
}
-fn execDetectForms(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execDetectForms(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const args = try parseArgsOrDefault(UrlParams, arena, arguments);
const page = try ensurePage(session, registry, args.url, args.timeout);
@@ -1484,7 +1486,7 @@ fn execDetectForms(arena: std.mem.Allocator, session: *lp.Session, registry: *CD
return renderJson(arena, forms_data);
}
-fn execEvaluate(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError!ToolResult {
+fn execEvaluate(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError!ToolResult {
const Params = struct {
script: [:0]const u8,
url: ?[:0]const u8 = null,
@@ -1558,7 +1560,7 @@ fn execEvaluate(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNo
return .{ .text = text };
}
-fn execExtract(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError!ToolResult {
+fn execExtract(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError!ToolResult {
const Params = struct {
schema: []const u8,
save: ?[]const u8 = null,
@@ -1734,9 +1736,9 @@ fn bridgeStorePut(allocator: std.mem.Allocator, store: *BridgeStore, name: []con
/// Resolve a target element from either a CSS selector or a backendNodeId.
fn resolveTarget(
session: *lp.Session,
- registry: *CDPNode.Registry,
+ registry: *NodeRegistry,
selector: ?[]const u8,
- backend_node_id: ?CDPNode.Id,
+ backend_node_id: ?NodeRegistry.Id,
) ToolError!NodeAndPage {
if (selector) |sel| return resolveBySelector(session, sel);
if (backend_node_id) |nid| return resolveNodeAndPage(session, registry, nid);
@@ -1745,7 +1747,7 @@ fn resolveTarget(
/// Look up an optional DOM node by backendNodeId. Returns null when no id was
/// supplied, errors when the id doesn't resolve.
-fn resolveOptionalNode(registry: *CDPNode.Registry, backend_node_id: ?CDPNode.Id) ToolError!?*DOMNode {
+fn resolveOptionalNode(registry: *NodeRegistry, backend_node_id: ?NodeRegistry.Id) ToolError!?*DOMNode {
const id = backend_node_id orelse return null;
const node = registry.lookup_by_id.get(id) orelse return ToolError.NodeNotFound;
return node.dom;
@@ -1796,7 +1798,7 @@ fn beginAction(session: *lp.Session) ActionScope {
/// Finish a state-changing action: drain any queued navigation triggered by
/// the action, then tag `body` with the resulting page URL and title so the
/// caller (LLM, MCP client) can see whether the action triggered navigation.
-fn finalizeAction(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, scope: ActionScope, body: []const u8) ToolError![]const u8 {
+fn finalizeAction(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, scope: ActionScope, body: []const u8) ToolError![]const u8 {
const before = scope.frame;
if (before) |b| {
try awaitQueuedNavigation(session, b);
@@ -1831,9 +1833,9 @@ fn finalizeAction(arena: std.mem.Allocator, session: *lp.Session, registry: *CDP
}) catch ToolError.InternalError;
}
-fn execClick(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execClick(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
selector: ?[]const u8 = null,
};
const args = try parseArgs(Params, arena, arguments);
@@ -1847,9 +1849,9 @@ fn execClick(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.
return finalizeAction(arena, session, registry, scope, body);
}
-fn execFill(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execFill(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
selector: ?[]const u8 = null,
value: []const u8,
};
@@ -1868,9 +1870,9 @@ fn execFill(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.R
return finalizeAction(arena, session, registry, scope, body);
}
-fn execScroll(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execScroll(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
x: ?i32 = null,
y: ?i32 = null,
};
@@ -1898,7 +1900,7 @@ pub fn defaultWaitTimeout(frame: *const lp.Frame) u32 {
return default_nav_timeout_ms + default_wait_timeout_ms;
}
-fn execWaitForSelector(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execWaitForSelector(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
selector: [:0]const u8,
timeout: ?u32 = null,
@@ -1976,9 +1978,9 @@ fn execWaitForState(arena: std.mem.Allocator, session: *lp.Session, arguments: ?
return std.fmt.allocPrint(arena, "Page reached {s}.", .{@tagName(args.state)}) catch return ToolError.InternalError;
}
-fn execHover(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execHover(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
selector: ?[]const u8 = null,
};
const args = try parseArgs(Params, arena, arguments);
@@ -1992,11 +1994,11 @@ fn execHover(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.
return finalizeAction(arena, session, registry, scope, body);
}
-fn execPress(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execPress(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
key: []const u8,
selector: ?[]const u8 = null,
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
};
const args = try parseArgs(Params, arena, arguments);
@@ -2021,9 +2023,9 @@ fn execPress(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.
return finalizeAction(arena, session, registry, scope, body);
}
-fn execSelectOption(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execSelectOption(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
selector: ?[]const u8 = null,
value: []const u8,
};
@@ -2039,9 +2041,9 @@ fn execSelectOption(arena: std.mem.Allocator, session: *lp.Session, registry: *C
return finalizeAction(arena, session, registry, scope, body);
}
-fn execSetChecked(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execSetChecked(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
selector: ?[]const u8 = null,
checked: bool = true,
};
@@ -2058,7 +2060,7 @@ fn execSetChecked(arena: std.mem.Allocator, session: *lp.Session, registry: *CDP
return finalizeAction(arena, session, registry, scope, body);
}
-fn execFindElement(arena: std.mem.Allocator, session: *lp.Session, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 {
+fn execFindElement(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegistry, arguments: ?std.json.Value) ToolError![]const u8 {
const Params = struct {
role: ?[]const u8 = null,
name: ?[]const u8 = null,
@@ -2203,7 +2205,7 @@ fn renderJson(arena: std.mem.Allocator, value: anytype) ToolError![]const u8 {
return aw.written();
}
-fn ensurePage(session: *lp.Session, registry: *CDPNode.Registry, url: ?[:0]const u8, timeout: ?u32) ToolError!*lp.Frame {
+fn ensurePage(session: *lp.Session, registry: *NodeRegistry, url: ?[:0]const u8, timeout: ?u32) ToolError!*lp.Frame {
if (url) |u| {
if (session.currentFrame()) |frame| {
if (std.mem.eql(u8, frame.url, u)) return frame;
@@ -2246,7 +2248,7 @@ fn openPage(session: *lp.Session, url: [:0]const u8) ToolError!lp.Session.PageHa
pub fn startGoto(
arena: std.mem.Allocator,
session: *lp.Session,
- registry: *CDPNode.Registry,
+ registry: *NodeRegistry,
arguments: ?std.json.Value,
receiver_frame_id: ?u32,
) ToolError!StartedGoto {
@@ -2271,7 +2273,7 @@ const PerformGotoOpts = struct {
wait_until: lp.Config.WaitUntil = default_nav_wait,
};
-fn performGoto(session: *lp.Session, registry: *CDPNode.Registry, url: [:0]const u8, opts: PerformGotoOpts) ToolError!lp.Session.Runner.WaitResult {
+fn performGoto(session: *lp.Session, registry: *NodeRegistry, url: [:0]const u8, opts: PerformGotoOpts) ToolError!lp.Session.Runner.WaitResult {
if (session.primaryPage()) |old_page| {
registry.reset();
old_page.close();
@@ -2291,7 +2293,7 @@ fn performGoto(session: *lp.Session, registry: *CDPNode.Registry, url: [:0]const
return result;
}
-fn resolveNodeAndPage(session: *lp.Session, registry: *CDPNode.Registry, node_id: CDPNode.Id) ToolError!NodeAndPage {
+fn resolveNodeAndPage(session: *lp.Session, registry: *NodeRegistry, node_id: NodeRegistry.Id) ToolError!NodeAndPage {
const page = try requireFrame(session);
const node = registry.lookup_by_id.get(node_id) orelse return ToolError.NodeNotFound;
return .{ .node = node.dom, .page = page, .target = .{ .backend_node_id = node_id } };
@@ -2517,27 +2519,27 @@ test "parseValue: zero-filled optional backendNodeId treated as omitted" {
const aa = arena.allocator();
const Params = struct {
- backendNodeId: ?CDPNode.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
maxDepth: ?u32 = null,
};
const zeroed = try std.json.parseFromSliceLeaky(std.json.Value, aa,
\\{"backendNodeId":0,"maxDepth":2}
, .{});
const args = try parseValue(Params, aa, zeroed);
- try std.testing.expectEqual(@as(?CDPNode.Id, null), args.backendNodeId);
+ try std.testing.expectEqual(@as(?NodeRegistry.Id, null), args.backendNodeId);
try std.testing.expectEqual(@as(?u32, 2), args.maxDepth);
const real = try std.json.parseFromSliceLeaky(std.json.Value, aa,
\\{"backendNodeId":7}
, .{});
- try std.testing.expectEqual(@as(?CDPNode.Id, 7), (try parseValue(Params, aa, real)).backendNodeId);
+ try std.testing.expectEqual(@as(?NodeRegistry.Id, 7), (try parseValue(Params, aa, real)).backendNodeId);
// Non-optional ids (nodeDetails) pass through untouched.
- const Required = struct { backendNodeId: CDPNode.Id };
+ const Required = struct { backendNodeId: NodeRegistry.Id };
const zero_required = try std.json.parseFromSliceLeaky(std.json.Value, aa,
\\{"backendNodeId":0}
, .{});
- try std.testing.expectEqual(@as(CDPNode.Id, 0), (try parseValue(Required, aa, zero_required)).backendNodeId);
+ try std.testing.expectEqual(@as(NodeRegistry.Id, 0), (try parseValue(Required, aa, zero_required)).backendNodeId);
}
test "substituteEnvVars resolves LP_* vars" {
diff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zig
index 829f74919..19552dd2e 100644
--- a/src/browser/webapi/WebDriver.zig
+++ b/src/browser/webapi/WebDriver.zig
@@ -49,7 +49,7 @@ pub fn deleteAllCookies(_: *const WebDriver, page: *Page) void {
}
pub fn getComputedLabel(_: *const WebDriver, element: *Element, frame: *Frame) ![]const u8 {
- const AXNode = @import("../../cdp/AXNode.zig");
+ const AXNode = @import("../../server/cdp/AXNode.zig");
const axnode = AXNode.fromNode(element.asNode());
var labels: Label.LabelByForIndex = .{};
return (try axnode.getName(frame, frame.call_arena, &labels)) orelse "";
diff --git a/src/cdp/Connection.zig b/src/cdp/Connection.zig
deleted file mode 100644
index c30835f1b..000000000
--- a/src/cdp/Connection.zig
+++ /dev/null
@@ -1,638 +0,0 @@
-// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
-//
-// Francis Bouvier <[email protected]>
-// Pierre Tachoire <[email protected]>
-//
-// 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 lp = @import("lightpanda");
-
-const CDP = @import("CDP.zig");
-
-const App = @import("../App.zig");
-const Inbox = @import("../Inbox.zig");
-const ArenaPool = @import("../ArenaPool.zig");
-
-const WS = @import("../network/WS.zig");
-const sys_net = @import("../sys/net.zig");
-const header_parser = @import("../network/header_parser.zig");
-
-const log = lp.log;
-const posix = std.posix;
-const ArenaAllocator = std.heap.ArenaAllocator;
-
-pub const Connection = @This();
-
-pub const State = enum { handshaking, live };
-
-// reference to http_client.inbox
-inbox: *Inbox,
-arena_pool: *ArenaPool,
-socket: posix.socket_t,
-socket_flags: usize,
-state: State = .handshaking,
-reader: WS.Reader(true),
-send_arena: ArenaAllocator,
-metrics_enabled: bool,
-max_http_message_size: usize,
-json_version_response: []const u8,
-
-pub fn init(
- self: *Connection,
- app: *App,
- socket: posix.socket_t,
- json_version_response: []const u8,
- inbox: *Inbox,
-) !void {
- const socket_flags = try sys_net.fcntl(socket, posix.F.GETFL, 0);
- const nonblocking = @as(u32, @bitCast(posix.O{ .NONBLOCK = true }));
- if (lp.IS_TEST == false) {
- lp.assert(socket_flags & nonblocking == nonblocking, "Connection.init blocking", .{});
- }
-
- const config = app.config;
- const allocator = app.allocator;
-
- self.* = .{
- .inbox = inbox,
- .socket = socket,
- .arena_pool = &app.arena_pool,
- .socket_flags = socket_flags,
- .metrics_enabled = config.metricsEndpointEnabled(),
- .max_http_message_size = config.cdpMaxHTTPMessageSize(),
- .reader = try .init(allocator, config.cdpMaxMessageSize()),
- .send_arena = ArenaAllocator.init(allocator),
- .json_version_response = json_version_response,
- };
-}
-
-pub fn deinit(self: *Connection) void {
- self.reader.deinit();
- self.send_arena.deinit();
-}
-
-pub fn send(self: *Connection, data: []const u8) !void {
- var pos: usize = 0;
- var changed_to_blocking: bool = false;
- defer _ = self.send_arena.reset(.{ .retain_with_limit = 1024 * 32 });
-
- defer if (changed_to_blocking) {
- // We had to change our socket to blocking mode to get our write out
- // We need to change it back to non-blocking.
- _ = sys_net.fcntl(self.socket, posix.F.SETFL, self.socket_flags) catch |err| {
- log.err(.app, "ws restore nonblocking", .{ .err = err });
- };
- };
-
- LOOP: while (pos < data.len) {
- const written = sys_net.write(self.socket, data[pos..]) catch |err| switch (err) {
- error.WouldBlock => {
- // self.socket is nonblocking, because we don't want to block
- // reads. But our life is a lot easier if we block writes,
- // largely, because we don't have to maintain a queue of pending
- // writes (which would each need their own allocations). So
- // if we get a WouldBlock error, we'll switch the socket to
- // blocking and switch it back to non-blocking after the write
- // is complete. Doesn't seem particularly efficiently, but
- // this should virtually never happen.
- lp.assert(changed_to_blocking == false, "Connection.double block", .{});
- changed_to_blocking = true;
- _ = try sys_net.fcntl(self.socket, posix.F.SETFL, self.socket_flags & ~@as(u32, @bitCast(posix.O{ .NONBLOCK = true })));
- continue :LOOP;
- },
- else => return err,
- };
-
- if (written == 0) {
- return error.Closed;
- }
- pos += written;
- }
-}
-
-pub fn sendPong(self: *Connection, data: []const u8) !void {
- if (data.len == 0) {
- return self.send(&WS.EMPTY_PONG);
- }
- var header_buf: [10]u8 = undefined;
- const header = websocketHeader(&header_buf, .pong, data.len);
-
- const allocator = self.send_arena.allocator();
- const framed = try allocator.alloc(u8, header.len + data.len);
- @memcpy(framed[0..header.len], header);
- @memcpy(framed[header.len..], data);
- return self.send(framed);
-}
-
-// called by CDP
-// Websocket frames have a variable length header. For server-client,
-// it could be anywhere from 2 to 10 bytes. Our IO.Loop doesn't have
-// writev, so we need to get creative. We'll JSON serialize to a
-// buffer, where the first 10 bytes are reserved. We can then backfill
-// the header and send the slice.
-pub fn sendJSON(self: *Connection, message: anytype, opts: std.json.Stringify.Options) !void {
- const allocator = self.send_arena.allocator();
-
- var aw = try std.Io.Writer.Allocating.initCapacity(allocator, 512);
-
- // reserve space for the maximum possible header
- try aw.writer.writeAll(&[_]u8{0} ** 10);
- try std.json.Stringify.value(message, opts, &aw.writer);
- const framed = fillWebsocketHeader(aw.toArrayList());
- return self.send(framed);
-}
-
-pub fn sendJSONRaw(
- self: *Connection,
- buf: std.ArrayList(u8),
-) !void {
- // Dangerous API!. We assume the caller has reserved the first 10
- // bytes in `buf`.
- const framed = fillWebsocketHeader(buf);
- return self.send(framed);
-}
-
-pub const HttpResult = enum { more, upgraded, close };
-
-pub fn handshake(self: *Connection) !bool {
- while (true) {
- var pfds = [_]posix.pollfd{.{
- .fd = self.socket,
- .events = posix.POLL.IN,
- .revents = 0,
- }};
- const n = try posix.poll(&pfds, 5000);
- if (n == 0) {
- log.info(.cdp, "CDP handshake timeout", .{});
- return false;
- }
- const read_bytes = self.read() catch |err| {
- log.warn(.cdp, "CDP read", .{ .err = err });
- return false;
- };
- if (read_bytes == 0) {
- log.info(.cdp, "CDP disconnect", .{});
- return false;
- }
- const result = self.processHttpRequest() catch return false;
- switch (result) {
- .more => continue,
- .upgraded => return true,
- .close => return false,
- }
- }
-}
-
-pub fn read(self: *Connection) !usize {
- const n = try posix.read(self.socket, self.reader.readBuf());
- self.reader.len += n;
- return n;
-}
-
-// Append as many bytes as fit into the reader's free space. Returns
-// the number of bytes copied. Used post-handshake when the network
-// thread owns socket reads.
-//
-// Why partial: a single network read can carry more bytes than the
-// reader's current free space (e.g. one large pending frame plus the
-// start of another). The caller is expected to loop:
-//
-// while (remaining.len > 0) {
-// const n = conn.feedBytes(remaining);
-// remaining = remaining[n..];
-// _ = try conn.processMessages(); // extracts frames + compacts
-// // processMessages also grows the reader buffer if it sees a
-// // frame header bigger than the current capacity, so the next
-// // feedBytes call has somewhere to land.
-// }
-pub fn feedBytes(self: *Connection, data: []const u8) usize {
- const dst = self.reader.readBuf();
- const n = @min(data.len, dst.len);
- @memcpy(dst[0..n], data[0..n]);
- self.reader.len += n;
- return n;
-}
-
-fn processHttpRequest(self: *Connection) !HttpResult {
- lp.assert(self.reader.pos == 0, "Connection.HTTP pos", .{ .pos = self.reader.pos });
- const request = self.reader.buf[0..self.reader.len];
-
- if (request.len > self.max_http_message_size) {
- log.warn(.cdp, "CDP message too big", .{ .type = "HTTP", .len = request.len, .hint = "See the --cdp-max-http-message-size <bytes>" });
- self.sendHttpError(413, "Request too large");
- return error.RequestTooLarge;
- }
-
- // we're only expecting [body-less] GET requests.
- if (std.mem.endsWith(u8, request, "\r\n\r\n") == false) {
- // we need more data, put any more data here
- return .more;
- }
-
- // the next incoming data can go to the front of our buffer
- defer self.reader.len = 0;
- return self.handleHttpRequest(request) catch |err| {
- switch (err) {
- error.NotFound => self.sendHttpError(404, "Not found"),
- error.ForbiddenOrigin => self.sendHttpError(403, "Origin not allowed"),
- error.ForbiddenHost => self.sendHttpError(403, "Host not allowed"),
- error.InvalidRequest => self.sendHttpError(400, "Invalid request"),
- error.InvalidProtocol => self.sendHttpError(400, "Invalid HTTP protocol"),
- error.MissingHeaders => self.sendHttpError(400, "Missing required header"),
- error.InvalidUpgradeHeader => self.sendHttpError(400, "Unsupported upgrade type"),
- error.InvalidVersionHeader => self.sendHttpError(400, "Invalid websocket version"),
- error.InvalidConnectionHeader => self.sendHttpError(400, "Invalid connection header"),
- else => {
- log.err(.app, "server 500", .{ .err = err, .req = request[0..@min(100, request.len)] });
- self.sendHttpError(500, "Internal Server Error");
- },
- }
- return err;
- };
-}
-
-fn handleHttpRequest(self: *Connection, request: []u8) !HttpResult {
- if (request.len < 18) {
- // 18 is [generously] the smallest acceptable HTTP request
- return error.InvalidRequest;
- }
-
- if (std.mem.eql(u8, request[0..4], "GET ") == false) {
- return error.NotFound;
- }
-
- const url_end = std.mem.indexOfScalarPos(u8, request, 4, ' ') orelse {
- return error.InvalidRequest;
- };
-
- const url = request[4..url_end];
-
- if (std.mem.eql(u8, url, "/")) {
- try self.upgrade(request);
- return .upgraded;
- }
-
- if (std.mem.eql(u8, url, "/json/version") or std.mem.eql(u8, url, "/json/version/")) {
- try self.send(self.json_version_response);
- // Chromedp (a Go driver) does an http request to /json/version
- // then to / (websocket upgrade) using a different connection.
- // Since we only allow 1 connection at a time, the 2nd one (the
- // websocket upgrade) blocks until the first one times out.
- // We can avoid that by closing the connection. json_version_response
- // has a Connection: Close header too.
- self.shutdown();
- return .close;
- }
-
- if (std.mem.eql(u8, url, "/json/list") or std.mem.eql(u8, url, "/json/list/") or
- std.mem.eql(u8, url, "/json") or std.mem.eql(u8, url, "/json/"))
- {
- try self.send(empty_json_list_response);
- self.shutdown();
- return .close;
- }
-
- if (std.mem.eql(u8, url, "/json/protocol") or std.mem.eql(u8, url, "/json/protocol/")) {
- try self.send(protocol_response);
- self.shutdown();
- return .close;
- }
-
- if (self.metrics_enabled and std.mem.eql(u8, url, "/metrics")) {
- try self.sendMetrics();
- self.shutdown();
- return .close;
- }
-
- return error.NotFound;
-}
-
-fn sendMetrics(self: *Connection) !void {
- const allocator = self.send_arena.allocator();
-
- var aw = try std.Io.Writer.Allocating.initCapacity(allocator, 4096);
- lp.metrics.write(&aw.writer);
- const body = aw.written();
-
- const response = try std.fmt.allocPrint(allocator, "HTTP/1.1 200 OK\r\n" ++
- "Content-Length: {d}\r\n" ++
- "Connection: Close\r\n" ++
- "Content-Type: text/plain; version=0.0.4; charset=utf-8\r\n\r\n" ++
- "{s}", .{ body.len, body });
- try self.send(response);
-}
-
-const empty_json_list_response =
- "HTTP/1.1 200 OK\r\n" ++
- "Content-Length: 2\r\n" ++
- "Connection: Close\r\n" ++
- "Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
- "[]";
-
-const protocol_json = @embedFile("../data/protocol.json");
-
-const protocol_response = std.fmt.comptimePrint(
- "HTTP/1.1 200 OK\r\n" ++
- "Content-Length: {d}\r\n" ++
- "Connection: Close\r\n" ++
- "Content-Type: application/json; charset=UTF-8\r\n\r\n",
- .{protocol_json.len},
-) ++ protocol_json;
-
-// Framing-only iteration over received bytes. processMessages no
-// longer auto-replies pong/close or sends close-on-error — the Network
-// thread runs this loop and is read-only on the socket.
-//
-// Returns false if a close frame was seen (caller should drop the
-// link) or the handler asked to stop; true if the loop exited because
-// there were no more complete frames buffered.
-pub fn processMessages(self: *Connection) !bool {
- var reader = &self.reader;
- while (true) {
- const msg = (try reader.next()) orelse break;
-
- const keep = switch (msg.type) {
- .pong => true,
- .ping, .text, .binary => try self.handleMessage(msg),
- .close => blk: {
- _ = try self.handleMessage(msg);
- break :blk false;
- },
- };
-
- if (msg.cleanup_fragment) {
- reader.cleanup();
- }
-
- if (!keep) {
- return false;
- }
- }
-
- // We might have read part of the next message. Our reader potentially
- // has to move data around in its buffer to make space.
- reader.compact();
- return true;
-}
-
-fn handleMessage(self: *Connection, msg: WS.Message) !bool {
- switch (msg.type) {
- .text, .binary => return self.pushCdp(msg.data),
- .ping => {
- const arena = try self.arena_pool.acquire(.tiny, "cdp ping");
- errdefer arena.release();
- self.inbox.push(arena, .{ .ping = try arena.dupe(u8, msg.data) });
- return true;
- },
- .close => {
- const arena = try self.arena_pool.acquire(.tiny, "cdp close");
- self.inbox.push(arena, .close);
- return true;
- },
- .pong => unreachable, // processMessages skips pong
- }
-}
-
-// Parse a CDP JSON frame on the Network thread and push it onto the
-// inbox already-parsed. The consumer's allowlist check works on
-// `input.method` directly (no substring matching against raw JSON),
-// and the worker doesn't re-parse on dispatch. On parse failure we
-// push `.disconnect(error.InvalidJSON)` so the worker tears down —
-// treated the same way as a fatal WS framing error.
-fn pushCdp(self: *Connection, bytes: []const u8) !bool {
- // TODO: is it worth trying to pad this for the cost overhead of parsing?
- const arena = try self.arena_pool.acquire(bytes.len, "cdp data");
- errdefer arena.release();
-
- const raw = try arena.dupe(u8, bytes);
-
- const input = std.json.parseFromSliceLeaky(
- CDP.InputMessage,
- arena.allocator(),
- raw,
- .{ .ignore_unknown_fields = true },
- ) catch {
- self.inbox.push(arena, .{ .disconnect = error.InvalidJSON });
- return false;
- };
-
- self.inbox.push(arena, .{ .cdp = .{
- .raw = raw,
- .input = input,
- } });
- return true;
-}
-
-pub fn upgrade(self: *Connection, request: []u8) !void {
- // We need to make sure that we got all the necessary headers + values;
- // a bit per required header.
- const FOUND_UPGRADE: u8 = 1 << 0; // Upgrade: websocket
- const FOUND_VERSION: u8 = 1 << 1; // Sec-WebSocket-Version: 13
- const FOUND_CONNECTION: u8 = 1 << 2; // Connection: upgrade
- const FOUND_KEY: u8 = 1 << 3; // Sec-WebSocket-Key
- const FOUND_ALL = FOUND_UPGRADE | FOUND_VERSION | FOUND_CONNECTION | FOUND_KEY;
-
- // A malformed request line maps to a 400 in processHttpRequest.
- const method, _, const version, var header_iterator = header_parser.parseRequest(request) catch {
- return error.InvalidProtocol;
- };
- if (method != .get or version != .@"1.1") {
- return error.InvalidProtocol;
- }
-
- var found_headers: u8 = 0;
- // We need to extract the `Sec-WebSocket-Key` value.
- var sec_websocket_key: []const u8 = "";
-
- // A malformed header maps to a 400 in processHttpRequest.
- while (header_iterator.next() catch return error.InvalidRequest) |header| {
- const key = header.key;
- const value = header.value;
-
- // Header names are case-insensitive; `Header.parse` keeps their
- // original casing.
- if (std.ascii.eqlIgnoreCase(key, "upgrade")) {
- if (!std.ascii.eqlIgnoreCase("websocket", value)) {
- return error.InvalidUpgradeHeader;
- }
- found_headers |= FOUND_UPGRADE;
- } else if (std.ascii.eqlIgnoreCase(key, "sec-websocket-version")) {
- if (value.len != 2 or value[0] != '1' or value[1] != '3') {
- return error.InvalidVersionHeader;
- }
- found_headers |= FOUND_VERSION;
- } else if (std.ascii.eqlIgnoreCase(key, "connection")) {
- // find if connection header has upgrade in it, example header:
- // Connection: keep-alive, Upgrade
- if (std.ascii.indexOfIgnoreCase(value, "upgrade") == null) {
- return error.InvalidConnectionHeader;
- }
- found_headers |= FOUND_CONNECTION;
- } else if (std.ascii.eqlIgnoreCase(key, "sec-websocket-key")) {
- sec_websocket_key = value;
- found_headers |= FOUND_KEY;
- } else if (std.ascii.eqlIgnoreCase(key, "origin")) {
- // Only a browser sends `Origin`, and a browser has no business
- // driving CDP: whatever page sent this is cross-origin to us by
- // definition, including one served from loopback itself. Scripted
- // clients (Puppeteer, Playwright, chromedp, ...) never send it.
- log.warn(.cdp, "rejected websocket origin", .{
- .origin = value[0..@min(value.len, 64)],
- });
- return error.ForbiddenOrigin;
- } else if (std.ascii.eqlIgnoreCase(key, "host")) {
- const host = value;
- const is_allowed = blk: {
- // allow literal localhost
- if (std.mem.startsWith(u8, host, "localhost:")) {
- break :blk true;
- }
-
- _ = std.Io.net.IpAddress.parseLiteral(host) catch break :blk false;
- break :blk true;
- };
-
- // Defense in depth against DNS rebinding: an IP literal is the only
- // thing that can legitimately reach us, because no name has to be
- // resolved to produce one. The one name we accept is
- // `localhost:<port>`, which browsers hardwire to loopback without
- // any DNS lookup. Any other name means something answered a DNS
- // lookup with our address, which is exactly what a rebinding
- // attack looks like. A request without a Host header isn't from a
- // browser, so it can't be the vector.
- if (!is_allowed) {
- log.warn(.cdp, "rejected websocket host", .{
- .host = host[0..@min(host.len, 64)],
- .hint = "connect to the CDP endpoint by IP address or localhost",
- });
- return error.ForbiddenHost;
- }
- }
- }
-
- // Check if we've received all related headers.
- if (found_headers != FOUND_ALL) {
- return error.MissingHeaders;
- }
-
- // our caller has already made sure this request ended in \r\n\r\n
- // so it isn't something we need to check again
-
- const alloc = self.send_arena.allocator();
-
- const response = blk: {
- // Response to an upgrade request is always this, with
- // the Sec-Websocket-Accept value a spacial sha1 hash of the
- // request "sec-websocket-version" and a magic value.
-
- const template =
- "HTTP/1.1 101 Switching Protocols\r\n" ++
- "Upgrade: websocket\r\n" ++
- "Connection: upgrade\r\n" ++
- "Sec-Websocket-Accept: 0000000000000000000000000000\r\n\r\n";
-
- // The response will be sent via the IO Loop and thus has to have its
- // own lifetime.
- const res = try alloc.dupe(u8, template);
-
- // magic response
- const key_pos = res.len - 32;
- var h: [20]u8 = undefined;
- var hasher = std.crypto.hash.Sha1.init(.{});
- hasher.update(sec_websocket_key);
- // websocket spec always used this value
- hasher.update("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
- hasher.final(&h);
-
- _ = std.base64.standard.Encoder.encode(res[key_pos .. key_pos + 28], h[0..]);
-
- break :blk res;
- };
-
- return self.send(response);
-}
-
-pub fn sendHttpError(self: *Connection, comptime status: u16, comptime body: []const u8) void {
- const response = std.fmt.comptimePrint(
- "HTTP/1.1 {d} \r\nConnection: Close\r\nContent-Length: {d}\r\n\r\n{s}",
- .{ status, body.len, body },
- );
-
- // we're going to close this connection anyways, swallowing any
- // error seems safe
- self.send(response) catch {};
-}
-
-pub fn getAddress(self: *Connection) !sys_net.IpAddress {
- var storage: posix.sockaddr.storage = undefined;
- var socklen: posix.socklen_t = @sizeOf(posix.sockaddr.storage);
- try posix.getpeername(self.socket, @ptrCast(&storage), &socklen);
- return sys_net.addressFromSockaddr(@ptrCast(&storage));
-}
-
-pub fn shutdown(self: *Connection) void {
- sys_net.shutdown(self.socket, .recv) catch {};
-}
-
-fn fillWebsocketHeader(buf: std.ArrayList(u8)) []const u8 {
- // can't use buf[0..10] here, because the header length
- // is variable. If it's just 2 bytes, for example, we need the
- // framed message to be:
- // h1, h2, data
- // If we use buf[0..10], we'd get:
- // h1, h2, 0, 0, 0, 0, 0, 0, 0, 0, data
-
- var header_buf: [10]u8 = undefined;
-
- // -10 because we reserved 10 bytes for the header above
- const header = websocketHeader(&header_buf, .text, buf.items.len - 10);
- const start = 10 - header.len;
-
- const message = buf.items;
- @memcpy(message[start..10], header);
- return message[start..];
-}
-
-// makes the assumption that our caller reserved the first
-// 10 bytes for the header
-fn websocketHeader(buf: []u8, op_code: WS.OpCode, payload_len: usize) []const u8 {
- lp.assert(buf.len == 10, "Websocket.Header", .{ .len = buf.len });
-
- const len = payload_len;
- buf[0] = 128 | @intFromEnum(op_code); // fin | opcode
-
- if (len <= 125) {
- buf[1] = @intCast(len);
- return buf[0..2];
- }
-
- if (len < 65536) {
- buf[1] = 126;
- buf[2] = @intCast((len >> 8) & 0xFF);
- buf[3] = @intCast(len & 0xFF);
- return buf[0..4];
- }
-
- buf[1] = 127;
- buf[2] = 0;
- buf[3] = 0;
- buf[4] = 0;
- buf[5] = 0;
- buf[6] = @intCast((len >> 24) & 0xFF);
- buf[7] = @intCast((len >> 16) & 0xFF);
- buf[8] = @intCast((len >> 8) & 0xFF);
- buf[9] = @intCast(len & 0xFF);
- return buf[0..10];
-}
diff --git a/src/crash_handler.zig b/src/crash_handler.zig
index b3b86c41a..82eafbb5c 100644
--- a/src/crash_handler.zig
+++ b/src/crash_handler.zig
@@ -1,3 +1,20 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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 lp = @import("lightpanda");
const builtin = @import("builtin");
diff --git a/src/lightpanda.zig b/src/lightpanda.zig
index 9de75d10c..63702fb41 100644
--- a/src/lightpanda.zig
+++ b/src/lightpanda.zig
@@ -24,10 +24,10 @@ pub const App = @import("App.zig");
pub const Arena = @import("Arena.zig");
pub const ArenaPool = @import("ArenaPool.zig");
pub const Network = @import("network/Network.zig");
-pub const Server = @import("Server.zig");
pub const Config = @import("Config.zig");
pub const String = @import("string.zig").String;
pub const Notification = @import("Notification.zig");
+pub const Server = @import("server/Server.zig");
pub const URL = @import("browser/URL.zig");
pub const Page = @import("browser/Page.zig");
@@ -45,7 +45,7 @@ pub const Base64Writer = @import("Base64Writer.zig");
const Selector = @import("browser/webapi/selector/Selector.zig");
const Node = @import("browser/webapi/Node.zig");
pub const SemanticTree = @import("SemanticTree.zig");
-pub const CDPNode = @import("cdp/Node.zig");
+pub const NodeRegistry = @import("NodeRegistry.zig");
pub const interactive = @import("browser/interactive.zig");
pub const links = @import("browser/links.zig");
pub const forms = @import("browser/forms.zig");
@@ -490,7 +490,7 @@ fn dumpContent(app: *App, mode: Config.DumpFormat, opts: FetchOpts, frame: *Fram
try pdf.print(arena.allocator(), root, .{}, writer, frame);
},
.semantic_tree, .semantic_tree_text => {
- var registry = CDPNode.Registry.init(app.allocator);
+ var registry = NodeRegistry.init(app.allocator);
defer registry.deinit();
const st: SemanticTree = .{
diff --git a/src/log.zig b/src/log.zig
index aad606aed..c3234e180 100644
--- a/src/log.zig
+++ b/src/log.zig
@@ -21,6 +21,7 @@ const lp = @import("lightpanda");
pub const Scope = enum {
app,
+ bidi,
browser,
bug,
cache,
diff --git a/src/mcp/Server.zig b/src/mcp/Server.zig
index 76084450d..1ec925c66 100644
--- a/src/mcp/Server.zig
+++ b/src/mcp/Server.zig
@@ -1,13 +1,30 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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 lp = @import("lightpanda");
const App = @import("../App.zig");
const testing = @import("../testing.zig");
+
+const tools = @import("tools.zig");
+const router = @import("router.zig");
const protocol = @import("protocol.zig");
const resources = @import("resources.zig");
-const router = @import("router.zig");
-const tools = @import("tools.zig");
const Transport = @import("Transport.zig");
const Self = @This();
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index dccf42b11..344865a21 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -20,11 +20,11 @@ const std = @import("std");
const lp = @import("lightpanda");
const Inbox = @import("../Inbox.zig");
+const Watchdog = @import("../Watchdog.zig");
const ArenaPool = @import("../ArenaPool.zig");
const Notification = @import("../Notification.zig");
+const Driver = @import("../server/Driver.zig");
-const CDP = @import("../cdp/CDP.zig");
-const Watchdog = @import("../Watchdog.zig");
const URL = @import("../browser/URL.zig");
const referrer = @import("../browser/referrer.zig");
const WebSocket = @import("../browser/webapi/net/WebSocket.zig");
@@ -150,27 +150,28 @@ test_fail_submit: if (lp.IS_TEST) ?anyerror else void = if (lp.IS_TEST) null els
// Allocated from self.allocator when set, null otherwise.
user_agent_override: ?[:0]const u8 = null,
-// The CDP layer we dispatch inbox messages to. Set in CDP.init for
-// `serve` mode; null in all other modes. Since this is set early, BEFORE the
-// CDP socket is registered with the network thread, we also have the
-// `cdp_link_active` boolean.
-cdp: ?*CDP = null,
+// The protocol layer we dispatch inbox messages to. Set in CDP.init /
+// BiDi.init for `serve` mode; null in all other modes. Since this is set
+// early, BEFORE the socket is registered with the network thread, we also
+// have the `driver_link_active` boolean.
+driver: ?Driver = null,
// True iff a producer (Server.handleConnection, after the worker
-// handshake completes) has registered the CDP socket with the Network
+// handshake completes) has registered the client socket with the Network
// thread and Network will fire curl_multi_wakeup on our multi handle
-// when it pushes to the inbox. perform uses this — NOT `cdp != null`
+// when it pushes to the inbox. perform uses this — NOT `driver != null`
// — to decide whether to block in poll without any in-flight curl
-// work. cdp is set in CDP.init, well before the link is wired; tests
-// and the pre-handshake window have a cdp but no producer, so polling
+// work. driver is set in the driver's init, well before the link is
+// wired; tests and the pre-handshake window have a driver but no
+// producer, so polling
// there would just eat the timeout waiting for a wakeup that's never
// coming.
-cdp_link_active: bool = false,
+driver_link_active: bool = false,
-// CDP messages parsed off the WS socket by the Network thread land
+// Client messages read off the WS socket by the Network thread land
// here. perform drains the inbox at each safe point and dispatches
-// via cdp.onMessage / onPing / onClose / onDisconnect. Always present
-// even in non-CDP mode — the empty-queue drain is one mutex lock plus
+// via driver.onMessage / onPing / onClose / onDisconnect. Always present
+// even in non-serve mode — the empty-queue drain is one mutex lock plus
// a linked-list head check, cheaper than nullability everywhere.
inbox: Inbox,
@@ -199,7 +200,7 @@ http_version: lp.Config.HttpVersion,
robots: RobotsGate,
url_blocklist: ?UrlBlocklist,
-pub fn init(self: *Client, app: *lp.App, cdp: ?*CDP) !void {
+pub fn init(self: *Client, app: *lp.App, driver: ?Driver) !void {
const config = app.config;
const allocator = app.allocator;
@@ -229,7 +230,7 @@ pub fn init(self: *Client, app: *lp.App, cdp: ?*CDP) !void {
.handles = handles,
.network = network,
.allocator = app.allocator,
- .cdp = cdp,
+ .driver = driver,
.inbox = .{},
.cache = &network.cache,
@@ -557,7 +558,7 @@ pub fn activity(self: *const Client) Activity {
};
}
-// What CDP messages drainInbox is allowed to dispatch this tick.
+// What client messages drainInbox is allowed to dispatch this tick.
// .all — outer event loop (Runner.tick). Safe to dispatch
// everything; the JS stack is empty.
// .sync_wait — reachable from inside a JS callback (syncRequest,
@@ -682,7 +683,7 @@ pub fn _tick(self: *Client, timeout_ms: u32, mode: DrainMode) !bool {
if (dispatched == false and processed == false and self.dispatch_queue.first == null and self.ws_dispatch_queue.first == null) {
// Nothing was dispatched, no messages were processed and nothing is
// waiting for dispatch. We need to wait for I/O.
- if (running > 0 or self.cdp_link_active or self.delayed_queue.first != null) {
+ if (running > 0 or self.driver_link_active or self.delayed_queue.first != null) {
{
self.heartbeat.enterWait();
defer self.heartbeat.exitWait();
@@ -1309,14 +1310,14 @@ fn makeRequest(self: *Client, conn: *http.Connection, transfer: *Transfer) anyer
_ = try self.handles.perform();
}
-// Drain any CDP messages the Network thread pushed into our inbox
-// and dispatch them via the cdp_client callbacks. Returns
+// Drain any client messages the Network thread pushed into our inbox
+// and dispatch them via the driver callbacks. Returns
// error.ClientDisconnected if the inbox surfaced a disconnect message,
// so the worker loop can tear down the connection. Called from tick
// only — NOT from perform, because perform recurses through
// processOneMessage's redirect path.
fn drainInbox(self: *Client, mode: DrainMode) !void {
- const cdp = self.cdp orelse return;
+ const driver = &(self.driver orelse return);
while (true) {
const msg = switch (mode) {
.all => self.inbox.pop(),
@@ -1326,20 +1327,19 @@ fn drainInbox(self: *Client, mode: DrainMode) !void {
defer msg.deinit();
switch (msg.payload) {
- .cdp => |*c| cdp.onMessage(c) catch |err| {
+ .cdp, .bidi => driver.onMessage(msg) catch |err| {
// A single malformed/failed dispatch shouldn't poison
// the rest of the batch — log and continue.
- log.err(.cdp, "CDP dispatch", .{ .err = err });
+ log.err(.app, "client dispatch", .{ .err = err });
},
- .ping => |body| cdp.onPing(body),
+ .ping => |body| driver.onPing(body),
.close => {
- cdp.onClose();
- cdp.onDisconnect(null);
+ driver.onClose();
self.inbox.terminated = true;
return error.ClientDisconnected;
},
.disconnect => |err| {
- cdp.onDisconnect(err);
+ driver.onDisconnect(err);
self.inbox.terminated = true;
return error.ClientDisconnected;
},
@@ -1363,6 +1363,9 @@ fn allowDuringSyncWait(msg: *Inbox.Message) bool {
return switch (msg.payload) {
.ping, .close, .disconnect => true,
.cdp => |c| isFetchInterceptionMethod(c.input.method),
+ // BiDi has no request interception yet, so nothing it can send is
+ // safe to dispatch from inside a JS callback.
+ .bidi => false,
};
}
@@ -1382,6 +1385,9 @@ fn isSyncWaitInterrupt(msg: *Inbox.Message) bool {
.close, .disconnect => true,
.ping => false,
.cdp => |c| isTeardownMethod(c.input.method),
+ // Frames aren't parsed on the Network thread for BiDi, so we
+ // can't spot a teardown command without re-parsing here.
+ .bidi => false,
};
}
diff --git a/src/network/WS.zig b/src/network/WS.zig
index 62c69b7b4..05cc736ee 100644
--- a/src/network/WS.zig
+++ b/src/network/WS.zig
@@ -56,6 +56,50 @@ pub const OpCode = enum(u8) {
pong = 128 | 10,
};
+// Server->client frames have a variable-length header: 2 to 10 bytes
+// depending on the payload length. Writes the header into `buf` and returns
+// the used prefix.
+pub fn frameHeader(buf: *[10]u8, op_code: OpCode, payload_len: usize) []const u8 {
+ const len = payload_len;
+ buf[0] = @intFromEnum(op_code);
+
+ if (len <= 125) {
+ buf[1] = @intCast(len);
+ return buf[0..2];
+ }
+
+ if (len < 65536) {
+ buf[1] = 126;
+ buf[2] = @intCast((len >> 8) & 0xFF);
+ buf[3] = @intCast(len & 0xFF);
+ return buf[0..4];
+ }
+
+ buf[1] = 127;
+ buf[2] = 0;
+ buf[3] = 0;
+ buf[4] = 0;
+ buf[5] = 0;
+ buf[6] = @intCast((len >> 24) & 0xFF);
+ buf[7] = @intCast((len >> 16) & 0xFF);
+ buf[8] = @intCast((len >> 8) & 0xFF);
+ buf[9] = @intCast(len & 0xFF);
+ return buf[0..10];
+}
+
+// Frames a text message serialized with its first 10 bytes reserved for the
+// header. Since the header is variable-length, it's written right-aligned
+// against the payload — the returned slice may not start at buf.items[0].
+pub fn fillHeader(buf: std.ArrayList(u8)) []const u8 {
+ var header_buf: [10]u8 = undefined;
+ const header = frameHeader(&header_buf, .text, buf.items.len - 10);
+ const start = 10 - header.len;
+
+ const message = buf.items;
+ @memcpy(message[start..10], header);
+ return message[start..];
+}
+
// We'll grow our buffer up to cdp-max-message-size (default 1MB), but should
// try to reclaim some of that space. A lot of drivers send large messages
// upfront (e.g. page.addScriptToEvaluateOnNewDocument) and then settle into
diff --git a/src/script/Runtime.zig b/src/script/Runtime.zig
index ff3587858..d4017665a 100644
--- a/src/script/Runtime.zig
+++ b/src/script/Runtime.zig
@@ -19,19 +19,19 @@
const std = @import("std");
const lp = @import("lightpanda");
-const browser_tools = lp.tools;
-const BrowserTool = browser_tools.Tool;
-const CDPNode = @import("../cdp/Node.zig");
const Schema = @import("Schema.zig");
+const NodeRegistry = @import("../NodeRegistry.zig");
const v8 = lp.js.v8;
+const browser_tools = lp.tools;
+const BrowserTool = browser_tools.Tool;
const Runtime = @This();
allocator: std.mem.Allocator,
app: *lp.App,
session: *lp.Session,
-registry: *CDPNode.Registry,
+registry: *NodeRegistry,
env: lp.js.Env,
context: v8.Global,
has_context: bool,
@@ -121,7 +121,7 @@ pub fn init(
allocator: std.mem.Allocator,
app: *lp.App,
session: *lp.Session,
- registry: *CDPNode.Registry,
+ registry: *NodeRegistry,
) InitError!*Runtime {
const self = try allocator.create(Runtime);
errdefer allocator.destroy(self);
@@ -1001,7 +1001,7 @@ fn terminateRuntimeSoon(runtime: *Runtime) void {
test "agent script runtime: goto and evaluate dispatch through browser tools" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1020,7 +1020,7 @@ test "agent script runtime: goto and evaluate dispatch through browser tools" {
}
test "agent script runtime: Page must be called with new" {
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1031,7 +1031,7 @@ test "agent script runtime: Page must be called with new" {
}
test "agent script runtime: a method on an un-navigated page errors" {
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1047,7 +1047,7 @@ test "agent script runtime: a method on an un-navigated page errors" {
test "agent script runtime: page.close stales the handle" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1067,7 +1067,7 @@ test "agent script runtime: page.close stales the handle" {
test "agent script runtime: parallel gotos coexist and route per page" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1091,7 +1091,7 @@ test "agent script runtime: parallel gotos coexist and route per page" {
test "agent script runtime: goto resolves $LP_* placeholders" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1114,7 +1114,7 @@ extern fn unsetenv(name: [*:0]u8) c_int;
test "agent script runtime: goto with invalid arguments rejects instead of crashing" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1133,7 +1133,7 @@ test "agent script runtime: goto with invalid arguments rejects instead of crash
test "agent script runtime: a tool-triggered navigation keeps the handle routable" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1153,7 +1153,7 @@ test "agent script runtime: a tool-triggered navigation keeps the handle routabl
test "agent script runtime: re-goto on the same page object replaces its page" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1173,7 +1173,7 @@ test "agent script runtime: a failed navigation rejects the goto promise" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1194,7 +1194,7 @@ test "agent script runtime: a failed navigation rejects the goto promise" {
test "agent script runtime: extract returns a JavaScript object" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1248,7 +1248,7 @@ test "agent script runtime: extract returns a JavaScript object" {
test "agent script runtime: extract tolerates list selectors that match nothing" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1276,7 +1276,7 @@ test "agent script runtime: extract tolerates list selectors that match nothing"
test "agent script runtime: strict-mode scripts can call primitives" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1292,7 +1292,7 @@ test "agent script runtime: strict-mode scripts can call primitives" {
}
test "agent script runtime: promise microtasks run to completion" {
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1312,7 +1312,7 @@ test "agent script runtime: promise microtasks run to completion" {
test "agent script runtime: primitives re-entered from argument callbacks stay isolated" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1339,7 +1339,7 @@ test "agent script runtime: primitives re-entered from argument callbacks stay i
test "agent script runtime: terminate interrupts local JavaScript" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1356,7 +1356,7 @@ test "agent script runtime: terminate interrupts local JavaScript" {
test "agent script runtime: agent variables persist and page globals are isolated" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1380,7 +1380,7 @@ test "agent script runtime: agent variables persist and page globals are isolate
test "agent script runtime: page evaluate cannot see agent primitives or bindings" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1397,7 +1397,7 @@ test "agent script runtime: page evaluate cannot see agent primitives or binding
}
test "agent script runtime: console is available in agent context" {
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1418,7 +1418,7 @@ test "agent script runtime: console is available in agent context" {
test "agent script runtime: tool errors throw and stop execution" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1444,7 +1444,7 @@ test "agent script runtime: tool errors throw and stop execution" {
test "agent script runtime: builtin argument marshalling (positional + options)" {
defer testing.test_session.closeAllPages();
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
@@ -1500,7 +1500,7 @@ test "agent script runtime: builtin argument marshalling (positional + options)"
}
test "agent script runtime: top-level await runs in an async wrapper" {
- var registry = CDPNode.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
const runtime = try Runtime.init(testing.allocator, testing.test_app, testing.test_session, &registry);
diff --git a/src/server/Connection.zig b/src/server/Connection.zig
new file mode 100644
index 000000000..691625cef
--- /dev/null
+++ b/src/server/Connection.zig
@@ -0,0 +1,274 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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 lp = @import("lightpanda");
+
+const App = @import("../App.zig");
+const Inbox = @import("../Inbox.zig");
+const WS = @import("../network/WS.zig");
+const sys_net = @import("../sys/net.zig");
+const ArenaPool = @import("../ArenaPool.zig");
+
+const CDP = @import("cdp/CDP.zig");
+
+const log = lp.log;
+const posix = std.posix;
+const ArenaAllocator = std.heap.ArenaAllocator;
+
+pub const Connection = @This();
+
+// is .starting until server.track is called
+const State = enum { starting, live };
+
+const Protocol = enum { cdp, bidi };
+
+// reference to http_client.inbox
+inbox: *Inbox,
+arena_pool: *ArenaPool,
+socket: posix.socket_t,
+socket_flags: usize,
+state: State = .starting,
+protocol: Protocol,
+reader: WS.Reader(true),
+send_arena: ArenaAllocator,
+
+pub fn init(
+ self: *Connection,
+ app: *App,
+ socket: posix.socket_t,
+ protocol: Protocol,
+ inbox: *Inbox,
+) !void {
+ const socket_flags = try sys_net.fcntl(socket, posix.F.GETFL, 0);
+ const nonblocking = @as(u32, @bitCast(posix.O{ .NONBLOCK = true }));
+ if (lp.IS_TEST == false) {
+ lp.assert(socket_flags & nonblocking == nonblocking, "Connection.init blocking", .{});
+ }
+
+ const config = app.config;
+ const allocator = app.allocator;
+
+ self.* = .{
+ .inbox = inbox,
+ .socket = socket,
+ .protocol = protocol,
+ .arena_pool = &app.arena_pool,
+ .socket_flags = socket_flags,
+ .reader = try .init(allocator, config.cdpMaxMessageSize()),
+ .send_arena = ArenaAllocator.init(allocator),
+ };
+}
+
+pub fn deinit(self: *Connection) void {
+ self.reader.deinit();
+ self.send_arena.deinit();
+}
+
+pub fn send(self: *Connection, data: []const u8) !void {
+ var pos: usize = 0;
+ var changed_to_blocking: bool = false;
+ defer _ = self.send_arena.reset(.{ .retain_with_limit = 1024 * 32 });
+
+ defer if (changed_to_blocking) {
+ // We had to change our socket to blocking mode to get our write out
+ // We need to change it back to non-blocking.
+ _ = sys_net.fcntl(self.socket, posix.F.SETFL, self.socket_flags) catch |err| {
+ log.err(.app, "ws restore nonblocking", .{ .err = err });
+ };
+ };
+
+ LOOP: while (pos < data.len) {
+ const written = sys_net.write(self.socket, data[pos..]) catch |err| switch (err) {
+ error.WouldBlock => {
+ // self.socket is nonblocking, because we don't want to block
+ // reads. But our life is a lot easier if we block writes,
+ // largely, because we don't have to maintain a queue of pending
+ // writes (which would each need their own allocations). So
+ // if we get a WouldBlock error, we'll switch the socket to
+ // blocking and switch it back to non-blocking after the write
+ // is complete. Doesn't seem particularly efficiently, but
+ // this should virtually never happen.
+ lp.assert(changed_to_blocking == false, "Connection.double block", .{});
+ changed_to_blocking = true;
+ _ = try sys_net.fcntl(self.socket, posix.F.SETFL, self.socket_flags & ~@as(u32, @bitCast(posix.O{ .NONBLOCK = true })));
+ continue :LOOP;
+ },
+ else => return err,
+ };
+
+ if (written == 0) {
+ return error.Closed;
+ }
+ pos += written;
+ }
+}
+
+pub fn sendPong(self: *Connection, data: []const u8) !void {
+ if (data.len == 0) {
+ return self.send(&WS.EMPTY_PONG);
+ }
+ var header_buf: [10]u8 = undefined;
+ const header = WS.frameHeader(&header_buf, .pong, data.len);
+
+ const allocator = self.send_arena.allocator();
+ const framed = try allocator.alloc(u8, header.len + data.len);
+ @memcpy(framed[0..header.len], header);
+ @memcpy(framed[header.len..], data);
+ return self.send(framed);
+}
+
+// Websocket frames have a variable length header. For server-client,
+// it could be anywhere from 2 to 10 bytes. Our IO.Loop doesn't have
+// writev, so we need to get creative. We'll JSON serialize to a
+// buffer, where the first 10 bytes are reserved. We can then backfill
+// the header and send the slice.
+pub fn sendJSON(self: *Connection, message: anytype, opts: std.json.Stringify.Options) !void {
+ const allocator = self.send_arena.allocator();
+
+ var aw = try std.Io.Writer.Allocating.initCapacity(allocator, 512);
+
+ // reserve space for the maximum possible header
+ try aw.writer.writeAll(&[_]u8{0} ** 10);
+ try std.json.Stringify.value(message, opts, &aw.writer);
+ const framed = WS.fillHeader(aw.toArrayList());
+ return self.send(framed);
+}
+
+pub fn sendJSONRaw(self: *Connection, buf: std.ArrayList(u8)) !void {
+ // Dangerous API!. We assume the caller has reserved the first 10
+ // bytes in `buf`.
+ const framed = WS.fillHeader(buf);
+ return self.send(framed);
+}
+
+pub fn feed(self: *Connection, data: []const u8) !bool {
+ var remaining = data;
+ while (remaining.len > 0) {
+ // we copy what will fit into our read buffer
+ const dst = self.reader.readBuf();
+ const used = @min(remaining.len, dst.len);
+ @memcpy(dst[0..used], remaining[0..used]);
+ self.reader.len += used;
+
+ // If we copied 1+ valid messages, this will process it.
+ if ((try self.processMessages()) == false) {
+ return false;
+ }
+
+ remaining = remaining[used..];
+ }
+ return true;
+}
+
+// Framing-only iteration over received bytes. Will process as many messages
+// as are buffered.
+fn processMessages(self: *Connection) !bool {
+ var reader = &self.reader;
+ while (true) {
+ const msg = (try reader.next()) orelse break;
+
+ const keep = switch (msg.type) {
+ .pong => true,
+ .ping, .text, .binary => try self.handleMessage(msg),
+ .close => blk: {
+ _ = try self.handleMessage(msg);
+ break :blk false;
+ },
+ };
+
+ if (msg.cleanup_fragment) {
+ reader.cleanup();
+ }
+
+ if (!keep) {
+ return false;
+ }
+ }
+
+ // We might have read part of the next message. Our reader potentially
+ // has to move data around in its buffer to make space.
+ reader.compact();
+ return true;
+}
+
+fn handleMessage(self: *Connection, msg: WS.Message) !bool {
+ switch (msg.type) {
+ .text, .binary => return switch (self.protocol) {
+ .cdp => self.pushCdp(msg.data),
+ .bidi => self.pushBiDi(msg.data),
+ },
+ .ping => {
+ const arena = try self.arena_pool.acquire(.tiny, "ws ping");
+ errdefer arena.release();
+ self.inbox.push(arena, .{ .ping = try arena.dupe(u8, msg.data) });
+ return true;
+ },
+ .close => {
+ const arena = try self.arena_pool.acquire(.tiny, "ws close");
+ self.inbox.push(arena, .close);
+ return true;
+ },
+ .pong => unreachable, // processMessages skips pong
+ }
+}
+
+// Parse a CDP JSON frame on the Network thread and push it onto the
+// inbox already-parsed. The consumer's allowlist check works on
+// `input.method` directly (no substring matching against raw JSON),
+// and the worker doesn't re-parse on dispatch. On parse failure we
+// push `.disconnect(error.InvalidJSON)` so the worker tears down —
+// treated the same way as a fatal WS framing error.
+fn pushCdp(self: *Connection, bytes: []const u8) !bool {
+ // TODO: is it worth trying to pad this for the cost overhead of parsing?
+ const arena = try self.arena_pool.acquire(bytes.len, "cdp data");
+ errdefer arena.release();
+
+ const raw = try arena.dupe(u8, bytes);
+
+ const input = std.json.parseFromSliceLeaky(
+ CDP.InputMessage,
+ arena.allocator(),
+ raw,
+ .{ .ignore_unknown_fields = true },
+ ) catch {
+ self.inbox.push(arena, .{ .disconnect = error.InvalidJSON });
+ return false;
+ };
+
+ self.inbox.push(arena, .{ .cdp = .{
+ .raw = raw,
+ .input = input,
+ } });
+ return true;
+}
+
+// BiDi frames are pushed raw; the worker parses them. Unlike CDP there's
+// no allowlist that needs the method name on this thread yet — when BiDi
+// grows request interception, this is where that parse would go.
+fn pushBiDi(self: *Connection, bytes: []const u8) !bool {
+ const arena = try self.arena_pool.acquire(bytes.len, "bidi data");
+ errdefer arena.release();
+
+ self.inbox.push(arena, .{ .bidi = try arena.dupe(u8, bytes) });
+ return true;
+}
+
+pub fn shutdown(self: *Connection) void {
+ sys_net.shutdown(self.socket, .recv) catch {};
+}
diff --git a/src/server/Driver.zig b/src/server/Driver.zig
new file mode 100644
index 000000000..b2399ec4f
--- /dev/null
+++ b/src/server/Driver.zig
@@ -0,0 +1,202 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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 lp = @import("lightpanda");
+
+const WS = @import("../network/WS.zig");
+const Inbox = @import("../Inbox.zig");
+
+const CDP = @import("cdp/CDP.zig");
+const Server = @import("Server.zig");
+const BiDi = @import("bidi/BiDi.zig");
+const Connection = @import("Connection.zig");
+const Browser = @import("../browser/Browser.zig");
+const Session = @import("../browser/Session.zig");
+
+const log = lp.log;
+
+// Parts of the driver are owned by the server run loop, parts are owned by
+// the worker thread. The run loop reads messages and pushes to the inbox,
+// the worker mostly just writes to the socket.
+//
+// What every protocol has - a connection, a browser, a link to the network
+// thread - lives here rather than behind `impl`, so the shared paths are plain
+// field access. Only what genuinely differs switches on `impl`.
+const Driver = @This();
+
+pub const Impl = union(enum) {
+ cdp: *CDP,
+ bidi: *BiDi,
+};
+
+impl: Impl,
+conn: *Connection,
+browser: *Browser,
+link: *Server.Link,
+
+// The protocol's log scope, so shared code still logs as .cdp / .bidi.
+scope: log.Scope,
+
+// Called from CDP.init / BiDi.init, where conn, link and browser are all
+// still undefined: we only take their addresses, which the impl's own
+// allocation already fixed.
+pub fn init(impl: Impl) Driver {
+ return switch (impl) {
+ // The tag names line up with the log scopes of the same name.
+ inline else => |d, tag| .{
+ .impl = impl,
+ .conn = &d.conn,
+ .link = &d.link,
+ .browser = &d.browser,
+ .scope = @field(log.Scope, @tagName(tag)),
+ },
+ };
+}
+
+// Server run loop. Received data, driver returns false to signal it should
+// disconnect.
+pub fn onData(self: *const Driver, data: []const u8) anyerror!bool {
+ return self.conn.feed(data);
+}
+
+// Server run loop. Called when it drops the link unsolicited (peer EOF, ...)
+pub fn onLinkDisconnect(self: *const Driver, err: ?anyerror) void {
+ self.browser.env.requestTerminate();
+ const arena = self.browser.arena_pool.acquire(.tiny, "driver disconnect") catch |e| switch (e) {
+ error.OutOfMemory => @panic("OOM"),
+ };
+ self.browser.http_client.inbox.push(arena, .{ .disconnect = err });
+}
+
+// Worker thread. We're processing messages from the inbox.
+pub fn onMessage(self: *const Driver, msg: *Inbox.Message) anyerror!void {
+ return switch (self.impl) {
+ .cdp => |cdp| cdp.onMessage(&msg.payload.cdp),
+ .bidi => |bidi| bidi.onMessage(msg.payload.bidi),
+ };
+}
+
+// Worker Thread. We're processing messages from the inbox.
+pub fn onPing(self: *const Driver, body: []const u8) void {
+ self.conn.sendPong(body) catch |err| {
+ log.warn(self.scope, "pong", .{ .err = err });
+ };
+}
+
+// Worker Thread. We're processing messages from the inbox.
+pub fn onClose(self: *const Driver) void {
+ self.conn.send(&WS.CLOSE_NORMAL) catch |err| {
+ log.warn(self.scope, "close reply", .{ .err = err });
+ };
+ self.onDisconnect(null);
+}
+
+// Worker Thread. We're processing messages from the inbox.
+pub fn onDisconnect(self: *const Driver, err: ?anyerror) void {
+ if (err) |e| {
+ if (WS.errorReply(e)) |close_frame| {
+ self.conn.send(close_frame) catch {};
+ }
+ }
+ log.info(self.scope, "disconnect", .{ .err = err });
+}
+
+// Worker thread. Once the websocket connection is established, the server
+// calls this (from the worker thread) and it becomes the driving loop.
+pub fn run(self: *const Driver) void {
+ while (true) {
+ const alive = self.tick() catch |err| {
+ log.err(self.scope, "tick", .{ .err = err });
+ return;
+ };
+ if (alive == false) {
+ return;
+ }
+ }
+}
+
+// One iteration of the worker loop. Returns false to disconnect.
+fn tick(self: *const Driver) !bool {
+ if (self.browser.env.terminatePending()) {
+ // terminatePending means someone decided this browser must die
+ // (e.g. the heap limit was reached).
+ log.warn(self.scope, "closing connection", .{ .reason = "pending terminate" });
+ // The worker thread is the sole writer of this socket, so sending
+ // the close frame here can't interleave with another write.
+ self.conn.send(&WS.CLOSE_GOING_AWAY) catch |err| {
+ log.warn(self.scope, "terminate close", .{ .err = err });
+ };
+ return false;
+ }
+
+ // Liveness is enforced by TCP keepalive configured in
+ // Server.configureSocket; the wakeup lets V8 run or terminate.
+ const wait_ms: u32 = 1000; // 1s
+
+ if (self.pageWait()) |wait| {
+ var runner = wait.session.runner(.{});
+ runner.waitForFrameCDP(wait.frame_id, wait_ms, .done) catch |err| switch (err) {
+ error.ClientDisconnected => return false,
+ else => return err,
+ };
+ return true;
+ }
+
+ // No active page yet (or a teardown is in flight). Fall back to ticking
+ // the http client directly so commands still get dispatched.
+ _ = self.browser.http_client.tick(wait_ms) catch |err| switch (err) {
+ error.ClientDisconnected => return false,
+ else => {
+ log.err(self.scope, "http tick", .{ .err = err });
+ return false;
+ },
+ };
+ return true;
+}
+
+const PageWait = struct {
+ session: *Session,
+ frame_id: u32,
+};
+
+// The page this connection's tick should wait on, null when there isn't one.
+fn pageWait(self: *const Driver) ?PageWait {
+ switch (self.impl) {
+ .cdp => |cdp| {
+ const bc = &(cdp.browser_context orelse return null);
+ const page = bc.page_handle orelse return null;
+ return .{ .session = bc.session, .frame_id = page.frame_id };
+ },
+ .bidi => |bidi| {
+ const context = bidi.context orelse return null;
+ return .{ .session = bidi.session, .frame_id = context.frame_id };
+ },
+ }
+}
+
+// signal handler thread
+pub fn shutdown(self: *const Driver) void {
+ if (self.conn.state == .live) {
+ self.browser.env.terminate();
+ // We use to send a nice WS close frame here but (a) it isn't
+ // strictly required and (b) we'd have to protect against an interleaved
+ // write from the worker thread.
+ }
+ self.conn.shutdown();
+}
diff --git a/src/server/Handshake.zig b/src/server/Handshake.zig
new file mode 100644
index 000000000..76ad05e6b
--- /dev/null
+++ b/src/server/Handshake.zig
@@ -0,0 +1,383 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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/>.
+
+// The pre-upgrade HTTP phase of a connection. Owns the socket until it
+// either serves a plain HTTP request (/json/*, /metrics) and closes, or
+// completes a websocket upgrade — at which point the request path decides
+// which protocol driver the connection is handed to.
+
+const std = @import("std");
+const lp = @import("lightpanda");
+
+const App = @import("../App.zig");
+const sys_net = @import("../sys/net.zig");
+const header_parser = @import("../network/header_parser.zig");
+
+const log = lp.log;
+const posix = std.posix;
+
+const Handshake = @This();
+
+pub const Driver = enum { cdp, bidi };
+
+app: *App,
+len: usize = 0,
+socket: posix.socket_t,
+// cdpMaxHTTPMessageSize is a u14, so this covers any configured limit.
+buf: [std.math.maxInt(u14) + 1]u8 = undefined,
+json_version_response: []const u8,
+
+const Result = union(enum) {
+ more,
+ close,
+ upgrade: Driver,
+};
+
+// Runs the HTTP phase to completion. Returns the route to hand the
+// upgraded socket to, or null if the connection is done (plain HTTP
+// request served, error, timeout or disconnect).
+pub fn run(app: *App, socket: posix.socket_t, json_version_response: []const u8) ?Driver {
+ var self = Handshake{
+ .app = app,
+ .socket = socket,
+ .json_version_response = json_version_response,
+ };
+
+ while (true) {
+ var pfds = [_]posix.pollfd{.{
+ .fd = self.socket,
+ .events = posix.POLL.IN,
+ .revents = 0,
+ }};
+ const n = posix.poll(&pfds, 5000) catch return null;
+ if (n == 0) {
+ log.info(.cdp, "handshake timeout", .{});
+ return null;
+ }
+ const read_bytes = posix.read(self.socket, self.buf[self.len..]) catch |err| {
+ log.warn(.cdp, "handshake read", .{ .err = err });
+ return null;
+ };
+ if (read_bytes == 0) {
+ log.info(.cdp, "handshake disconnect", .{});
+ return null;
+ }
+ self.len += read_bytes;
+
+ const result = self.processHttpRequest() catch return null;
+ switch (result) {
+ .more => continue,
+ .close => return null,
+ .upgrade => |route| return route,
+ }
+ }
+}
+
+fn processHttpRequest(self: *Handshake) !Result {
+ const request = self.buf[0..self.len];
+
+ if (request.len > self.app.config.cdpMaxHTTPMessageSize()) {
+ log.warn(.cdp, "message too big", .{ .type = "HTTP", .len = request.len, .hint = "See the --cdp-max-http-message-size <bytes>" });
+ self.sendHttpError(413, "Request too large");
+ return error.RequestTooLarge;
+ }
+
+ // we're only expecting [body-less] GET requests.
+ if (std.mem.endsWith(u8, request, "\r\n\r\n") == false) {
+ // we need more data, put any more data here
+ return .more;
+ }
+
+ return self.handleHttpRequest(request) catch |err| {
+ switch (err) {
+ error.NotFound => self.sendHttpError(404, "Not found"),
+ error.ForbiddenOrigin => self.sendHttpError(403, "Origin not allowed"),
+ error.ForbiddenHost => self.sendHttpError(403, "Host not allowed"),
+ error.InvalidRequest => self.sendHttpError(400, "Invalid request"),
+ error.InvalidProtocol => self.sendHttpError(400, "Invalid HTTP protocol"),
+ error.MissingHeaders => self.sendHttpError(400, "Missing required header"),
+ error.InvalidUpgradeHeader => self.sendHttpError(400, "Unsupported upgrade type"),
+ error.InvalidVersionHeader => self.sendHttpError(400, "Invalid websocket version"),
+ error.InvalidConnectionHeader => self.sendHttpError(400, "Invalid connection header"),
+ else => {
+ log.err(.app, "server 500", .{ .err = err, .req = request[0..@min(100, request.len)] });
+ self.sendHttpError(500, "Internal Server Error");
+ },
+ }
+ return err;
+ };
+}
+
+fn handleHttpRequest(self: *Handshake, request: []u8) !Result {
+ if (request.len < 18) {
+ // 18 is [generously] the smallest acceptable HTTP request
+ return error.InvalidRequest;
+ }
+
+ if (std.mem.eql(u8, request[0..4], "GET ") == false) {
+ return error.NotFound;
+ }
+
+ const url_end = std.mem.indexOfScalarPos(u8, request, 4, ' ') orelse {
+ return error.InvalidRequest;
+ };
+
+ const url = request[4..url_end];
+
+ if (std.mem.eql(u8, url, "/")) {
+ try self.upgrade(request);
+ return .{ .upgrade = .cdp };
+ }
+
+ if (std.mem.eql(u8, url, "/session")) {
+ // /session is the path Firefox advertises its BiDi endpoint on
+ try self.upgrade(request);
+ return .{ .upgrade = .bidi };
+ }
+
+ if (std.mem.eql(u8, url, "/json/version") or std.mem.eql(u8, url, "/json/version/")) {
+ try self.send(self.json_version_response);
+ // Chromedp (a Go driver) does an http request to /json/version
+ // then to / (websocket upgrade) using a different connection.
+ // Since we only allow 1 connection at a time, the 2nd one (the
+ // websocket upgrade) blocks until the first one times out.
+ // We can avoid that by closing the connection. json_version_response
+ // has a Connection: Close header too.
+ self.shutdown();
+ return .close;
+ }
+
+ if (std.mem.eql(u8, url, "/json/list") or std.mem.eql(u8, url, "/json/list/") or
+ std.mem.eql(u8, url, "/json") or std.mem.eql(u8, url, "/json/"))
+ {
+ try self.send(empty_json_list_response);
+ self.shutdown();
+ return .close;
+ }
+
+ if (std.mem.eql(u8, url, "/json/protocol") or std.mem.eql(u8, url, "/json/protocol/")) {
+ try self.send(protocol_response);
+ self.shutdown();
+ return .close;
+ }
+
+ if (std.mem.eql(u8, url, "/metrics") and self.app.config.metricsEndpointEnabled()) {
+ try self.sendMetrics();
+ self.shutdown();
+ return .close;
+ }
+
+ return error.NotFound;
+}
+
+fn upgrade(self: *Handshake, request: []u8) !void {
+ // We need to make sure that we got all the necessary headers + values;
+ // a bit per required header.
+ const FOUND_UPGRADE: u8 = 1 << 0; // Upgrade: websocket
+ const FOUND_VERSION: u8 = 1 << 1; // Sec-WebSocket-Version: 13
+ const FOUND_CONNECTION: u8 = 1 << 2; // Connection: upgrade
+ const FOUND_KEY: u8 = 1 << 3; // Sec-WebSocket-Key
+ const FOUND_ALL = FOUND_UPGRADE | FOUND_VERSION | FOUND_CONNECTION | FOUND_KEY;
+
+ // A malformed request line maps to a 400 in processHttpRequest.
+ const method, _, const version, var header_iterator = header_parser.parseRequest(request) catch {
+ return error.InvalidProtocol;
+ };
+ if (method != .get or version != .@"1.1") {
+ return error.InvalidProtocol;
+ }
+
+ var found_headers: u8 = 0;
+ // We need to extract the `Sec-WebSocket-Key` value.
+ var sec_websocket_key: []const u8 = "";
+
+ // A malformed header maps to a 400 in processHttpRequest.
+ while (header_iterator.next() catch return error.InvalidRequest) |header| {
+ const key = header.key;
+ const value = header.value;
+
+ // Header names are case-insensitive; `Header.parse` keeps their
+ // original casing.
+ if (std.ascii.eqlIgnoreCase(key, "upgrade")) {
+ if (!std.ascii.eqlIgnoreCase("websocket", value)) {
+ return error.InvalidUpgradeHeader;
+ }
+ found_headers |= FOUND_UPGRADE;
+ } else if (std.ascii.eqlIgnoreCase(key, "sec-websocket-version")) {
+ if (value.len != 2 or value[0] != '1' or value[1] != '3') {
+ return error.InvalidVersionHeader;
+ }
+ found_headers |= FOUND_VERSION;
+ } else if (std.ascii.eqlIgnoreCase(key, "connection")) {
+ // find if connection header has upgrade in it, example header:
+ // Connection: keep-alive, Upgrade
+ if (std.ascii.indexOfIgnoreCase(value, "upgrade") == null) {
+ return error.InvalidConnectionHeader;
+ }
+ found_headers |= FOUND_CONNECTION;
+ } else if (std.ascii.eqlIgnoreCase(key, "sec-websocket-key")) {
+ sec_websocket_key = value;
+ found_headers |= FOUND_KEY;
+ } else if (std.ascii.eqlIgnoreCase(key, "origin")) {
+ // Only a browser sends `Origin`, and a browser has no business
+ // driving CDP: whatever page sent this is cross-origin to us by
+ // definition, including one served from loopback itself. Scripted
+ // clients (Puppeteer, Playwright, chromedp, ...) never send it.
+ log.warn(.cdp, "rejected websocket origin", .{
+ .origin = value[0..@min(value.len, 64)],
+ });
+ return error.ForbiddenOrigin;
+ } else if (std.ascii.eqlIgnoreCase(key, "host")) {
+ const host = value;
+ const is_allowed = blk: {
+ // allow literal localhost
+ if (std.mem.startsWith(u8, host, "localhost:")) {
+ break :blk true;
+ }
+
+ _ = std.Io.net.IpAddress.parseLiteral(host) catch break :blk false;
+ break :blk true;
+ };
+
+ // Defense in depth against DNS rebinding: an IP literal is the only
+ // thing that can legitimately reach us, because no name has to be
+ // resolved to produce one. The one name we accept is
+ // `localhost:<port>`, which browsers hardwire to loopback without
+ // any DNS lookup. Any other name means something answered a DNS
+ // lookup with our address, which is exactly what a rebinding
+ // attack looks like. A request without a Host header isn't from a
+ // browser, so it can't be the vector.
+ if (!is_allowed) {
+ log.warn(.cdp, "rejected websocket host", .{
+ .host = host[0..@min(host.len, 64)],
+ .hint = "connect to the CDP endpoint by IP address or localhost",
+ });
+ return error.ForbiddenHost;
+ }
+ }
+ }
+
+ // Check if we've received all related headers.
+ if (found_headers != FOUND_ALL) {
+ return error.MissingHeaders;
+ }
+
+ // our caller has already made sure this request ended in \r\n\r\n
+ // so it isn't something we need to check again
+
+ // Response to an upgrade request is always this, with the
+ // Sec-Websocket-Accept value a special sha1 hash of the request
+ // "sec-websocket-key" and a magic value.
+ const template =
+ "HTTP/1.1 101 Switching Protocols\r\n" ++
+ "Upgrade: websocket\r\n" ++
+ "Connection: upgrade\r\n" ++
+ "Sec-Websocket-Accept: 0000000000000000000000000000\r\n\r\n";
+
+ var res: [template.len]u8 = template.*;
+
+ const key_pos = res.len - 32;
+ var h: [20]u8 = undefined;
+ var hasher = std.crypto.hash.Sha1.init(.{});
+ hasher.update(sec_websocket_key);
+ // websocket spec always used this value
+ hasher.update("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
+ hasher.final(&h);
+
+ _ = std.base64.standard.Encoder.encode(res[key_pos .. key_pos + 28], h[0..]);
+
+ return self.send(&res);
+}
+
+fn sendMetrics(self: *Handshake) !void {
+ const allocator = self.app.allocator;
+
+ var aw = try std.Io.Writer.Allocating.initCapacity(allocator, 4096);
+ defer aw.deinit();
+ lp.metrics.write(&aw.writer);
+ const body = aw.written();
+
+ const response = try std.fmt.allocPrint(allocator, "HTTP/1.1 200 OK\r\n" ++
+ "Content-Length: {d}\r\n" ++
+ "Connection: Close\r\n" ++
+ "Content-Type: text/plain; version=0.0.4; charset=utf-8\r\n\r\n" ++
+ "{s}", .{ body.len, body });
+ defer allocator.free(response);
+ try self.send(response);
+}
+
+fn sendHttpError(self: *Handshake, comptime status: u16, comptime body: []const u8) void {
+ const response = std.fmt.comptimePrint(
+ "HTTP/1.1 {d} \r\nConnection: Close\r\nContent-Length: {d}\r\n\r\n{s}",
+ .{ status, body.len, body },
+ );
+
+ // we're going to close this connection anyways, swallowing any
+ // error seems safe
+ self.send(response) catch {};
+}
+
+// The socket is non-blocking (reads must never block once the network
+// thread owns them), but our responses are small one-shot writes, so on
+// WouldBlock we just wait for writability rather than queueing.
+fn send(self: *Handshake, data: []const u8) !void {
+ var pos: usize = 0;
+ while (pos < data.len) {
+ const written = sys_net.write(self.socket, data[pos..]) catch |err| switch (err) {
+ error.WouldBlock => {
+ var pfds = [_]posix.pollfd{.{
+ .fd = self.socket,
+ .events = posix.POLL.OUT,
+ .revents = 0,
+ }};
+ const n = try posix.poll(&pfds, 5000);
+ if (n == 0) {
+ return error.Timeout;
+ }
+ continue;
+ },
+ else => return err,
+ };
+
+ if (written == 0) {
+ return error.Closed;
+ }
+ pos += written;
+ }
+}
+
+fn shutdown(self: *Handshake) void {
+ sys_net.shutdown(self.socket, .recv) catch {};
+}
+
+const empty_json_list_response =
+ "HTTP/1.1 200 OK\r\n" ++
+ "Content-Length: 2\r\n" ++
+ "Connection: Close\r\n" ++
+ "Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
+ "[]";
+
+const protocol_json = @embedFile("../data/protocol.json");
+
+const protocol_response = std.fmt.comptimePrint(
+ "HTTP/1.1 200 OK\r\n" ++
+ "Content-Length: {d}\r\n" ++
+ "Connection: Close\r\n" ++
+ "Content-Type: application/json; charset=UTF-8\r\n\r\n",
+ .{protocol_json.len},
+) ++ protocol_json;
diff --git a/src/Server.zig b/src/server/Server.zig
similarity index 72%
rename from src/Server.zig
rename to src/server/Server.zig
index 27ad2c8f6..6ba28752c 100644
--- a/src/Server.zig
+++ b/src/server/Server.zig
@@ -20,12 +20,16 @@ const std = @import("std");
const lp = @import("lightpanda");
const builtin = @import("builtin");
-const App = @import("App.zig");
-const Config = @import("Config.zig");
+const App = @import("../App.zig");
+const Config = @import("../Config.zig");
+const sys_net = @import("../sys/net.zig");
const CDP = @import("cdp/CDP.zig");
-const http = @import("network/http.zig");
-const sys_net = @import("sys/net.zig");
+const http = @import("../network/http.zig");
+
+const BiDi = @import("bidi/BiDi.zig");
+const Handshake = @import("Handshake.zig");
+const Driver = @import("Driver.zig");
const log = lp.log;
const posix = std.posix;
@@ -34,15 +38,16 @@ const DoublyLinkedList = std.DoublyLinkedList;
const Server = @This();
-// Read side of a CDP WebSocket, registered with the server's run loop so
-// bytes are read off the socket there and dispatched into the CDP layer
-// via direct method calls on `cdp`. The loop never sends on the socket —
-// the worker is the sole writer. After registerLink returns, the worker
-// must not call posix.read on this socket directly. unregisterLink is
-// synchronous: it blocks until the loop confirms the link has been
-// dropped from its poll set and won't touch it again.
+// Read side of a client (CDP / BiDi) WebSocket, registered with the
+// server's run loop so bytes are read off the socket there and dispatched
+// into the protocol layer via direct method calls on `driver`. The loop
+// never sends on the socket — the worker is the sole writer. After
+// registerLink returns, the worker must not call posix.read on this socket
+// directly. unregisterLink is synchronous: it blocks until the loop
+// confirms the link has been dropped from its poll set and won't touch it
+// again.
pub const Link = struct {
- cdp: *CDP,
+ driver: Driver,
state: State,
socket: posix.socket_t,
// The worker's HttpClient.Handles (by value — it's one pointer wide).
@@ -69,14 +74,15 @@ app: *App,
max_connections: usize,
json_version_response: []const u8,
-// Number of active CDP conns, used to enforce the cdp-max-connections limit.
+driver_mutex: std.Io.Mutex = .init,
+drivers: std.ArrayList(Driver) = .empty,
+
+// Number of active conns, used to enforce the cdp-max-connections limit.
active_conns: std.atomic.Value(u32) = .init(0),
// Number of existing threads, used to deinit correctly.
// It can be higher than active_conns b/c we free conn slots early.
active_threads: std.atomic.Value(u32) = .init(0),
-cdps: std.ArrayList(*CDP) = .empty,
-cdp_mutex: std.Io.Mutex = .init,
cdp_pool: std.heap.MemoryPool(CDP),
listener: posix.socket_t,
@@ -92,7 +98,7 @@ wakeup_pipe: [2]posix.fd_t,
shutting_down: std.atomic.Value(bool) = .init(false),
-// Registered CDP read endpoints. Producer-side (the worker doing
+// Registered client read endpoints. Producer-side (the worker doing
// register/unregister) and consumer-side (the run loop) are serialized
// by link_mutex. link_removed signals when a link transitions to
// .removed so unregisterLink can return.
@@ -180,23 +186,17 @@ fn bindListener(config: *const Config, address: *sys_net.IpAddress) !posix.socke
return listener;
}
-// Stop accepting, make run() return, and terminate every live CDP worker.
+// Stop accepting, make run() return, and terminate every live worker.
// Idempotent: the signal handler calls it, and so does deinit.
pub fn shutdown(self: *Server) void {
self.shutting_down.store(true, .release);
self.wakeup();
- self.cdp_mutex.lockUncancelable(lp.io);
- defer self.cdp_mutex.unlock(lp.io);
+ self.driver_mutex.lockUncancelable(lp.io);
+ defer self.driver_mutex.unlock(lp.io);
- for (self.cdps.items) |cdp| {
- if (cdp.conn.state == .live) {
- cdp.browser.env.terminate();
- // We use to send a nice WS close frame here but (a) it isn't
- // strictly required and (b) we'd have to protect against an interleaved
- // write from the worker thread.
- }
- cdp.conn.shutdown();
+ for (self.drivers.items) |*driver| {
+ driver.shutdown();
}
}
@@ -208,7 +208,7 @@ pub fn deinit(self: *Server) void {
}
const allocator = self.app.allocator;
- self.cdps.deinit(allocator);
+ self.drivers.deinit(allocator);
self.cdp_pool.deinit(allocator);
allocator.free(self.json_version_response);
allocator.free(self.pollfds);
@@ -223,7 +223,7 @@ pub fn deinit(self: *Server) void {
allocator.destroy(self);
}
-// Blocks the calling thread servicing the listener and the registered CDP
+// Blocks the calling thread servicing the listener and the registered client
// read sockets until shutdown(). Page fetches run on per-worker HttpClient
// multis and telemetry on its own thread, so nothing here drives libcurl.
pub fn run(self: *Server) void {
@@ -235,7 +235,7 @@ pub fn run(self: *Server) void {
while (true) {
self.preparePollFds();
- // wait until we get a CDP message or a signal on the wakeup pipe
+ // wait until we get a client message or a signal on the wakeup pipe
_ = posix.poll(self.pollfds, -1) catch |err| {
log.err(.app, "poll", .{ .err = err });
continue;
@@ -325,7 +325,7 @@ fn acceptConnections(self: *Server) void {
}
}
-// Hand a CDP WebSocket's read side over to the run loop. The caller owns
+// Hand a client WebSocket's read side over to the run loop. The caller owns
// the link and must keep it alive until unregisterLink is called. The
// caller must not read from the socket.
pub fn registerLink(self: *Server, link: *Link) void {
@@ -375,16 +375,14 @@ fn dropLink(self: *Server, link: *Link, err: ?anyerror, opts: DropLinkOpts) void
}
if (opts.notify) {
- link.cdp.terminateFromServer();
-
// notify=true means the worker hasn't been told yet — push the
// disconnect into the inbox and break it out of curl_multi_poll.
// notify=false paths have already woken the worker (close frame
// case) or are about to be unblocked via link_removed.broadcast
// (unregister case); no extra wakeup needed.
- link.cdp.onLinkDisconnect(err);
+ link.driver.onLinkDisconnect(err);
link.handles.wakeup() catch |e| {
- log.warn(.cdp, "CDP link wakeup", .{ .err = e });
+ log.warn(.app, "client link wakeup", .{ .err = e });
};
}
}
@@ -476,7 +474,7 @@ fn processLinks(self: *Server) void {
const n = posix.read(link.socket, &buf) catch |err| switch (err) {
error.WouldBlock => continue,
else => {
- log.warn(.cdp, "CDP read", .{ .err = err });
+ log.warn(.app, "client read", .{ .err = err });
self.dropLink(link, err, .{ .notify = true, .shutdown_socket = true });
any_removed = true;
continue;
@@ -490,14 +488,14 @@ fn processLinks(self: *Server) void {
continue;
}
- const keep = link.cdp.onData(buf[0..n]) catch |err| {
+ const keep = link.driver.onData(buf[0..n]) catch |err| {
// Fatal frame/feed error. Whatever messages on_bytes
// managed to push are still in the inbox; the failing
// frame was NOT pushed, and the worker has no way to
// know it should exit. Drop with notify=true so
// on_disconnect surfaces a .disconnect into the inbox.
// dropLink wakes the worker.
- log.info(.cdp, "CDP onData", .{ .err = err });
+ log.info(.app, "client onData", .{ .err = err });
self.dropLink(link, err, .{ .notify = true });
any_removed = true;
continue;
@@ -506,7 +504,7 @@ fn processLinks(self: *Server) void {
// on_bytes succeeded — wake the worker so it observes anything
// new in the inbox (data / ping / close).
link.handles.wakeup() catch |err| {
- log.warn(.cdp, "CDP link wakeup", .{ .err = err });
+ log.warn(.app, "client link wakeup", .{ .err = err });
};
if (!keep) {
@@ -625,79 +623,89 @@ fn handleConnection(self: *Server, socket: posix.socket_t) void {
defer _ = self.active_threads.fetchSub(1, .monotonic);
defer _ = std.c.close(socket);
+ const route = Handshake.run(self.app, socket, self.json_version_response) orelse return;
+ switch (route) {
+ .cdp => self.serveCDP(socket, &active_conns_early_release),
+ .bidi => self.serveBiDi(socket, &active_conns_early_release),
+ }
+}
+
+// The socket is an upgraded websocket speaking CDP.
+fn serveCDP(self: *Server, socket: posix.socket_t, active_conns_early_release: *bool) void {
const cdp = blk: {
const allocator = self.app.allocator;
- self.cdp_mutex.lockUncancelable(lp.io);
- defer self.cdp_mutex.unlock(lp.io);
+ self.driver_mutex.lockUncancelable(lp.io);
+ defer self.driver_mutex.unlock(lp.io);
break :blk self.cdp_pool.create(allocator) catch @panic("OOM");
};
defer {
- self.cdp_mutex.lockUncancelable(lp.io);
- defer self.cdp_mutex.unlock(lp.io);
+ self.driver_mutex.lockUncancelable(lp.io);
+ defer self.driver_mutex.unlock(lp.io);
self.cdp_pool.destroy(cdp);
}
- cdp.init(self.app, socket, self.json_version_response) catch |err| {
+ cdp.init(self.app, socket) catch |err| {
log.err(.app, "CDP init", .{ .err = err });
return;
};
defer cdp.deinit();
- if (log.enabled(.app, .info)) {
- const client_address = cdp.conn.getAddress() catch null;
- log.info(.app, "client connected", .{ .ip = client_address });
- }
+ // Only CDP connections are counted: the metric names are part of the
+ // scrape contract, and there are no bidi_* equivalents yet.
+ lp.metrics.cdp_connections.incr();
+ lp.metrics.cdp_active_connections.incr();
+ defer lp.metrics.cdp_active_connections.decr();
- {
- // track the connection
- self.cdp_mutex.lockUncancelable(lp.io);
- defer self.cdp_mutex.unlock(lp.io);
- self.cdps.append(self.app.allocator, cdp) catch {};
- }
+ self.serve(.init(.{ .cdp = cdp }), active_conns_early_release);
+}
- defer {
- // untrack the connection
- self.cdp_mutex.lockUncancelable(lp.io);
- defer self.cdp_mutex.unlock(lp.io);
- for (self.cdps.items, 0..) |c, i| {
- if (c == cdp) {
- _ = self.cdps.swapRemove(i);
- break;
- }
- }
- }
+// The socket is an upgraded websocket speaking WebDriver BiDi.
+fn serveBiDi(self: *Server, socket: posix.socket_t, active_conns_early_release: *bool) void {
+ const allocator = self.app.allocator;
+
+ // heap-allocated: BiDi embeds a Browser
+ const bidi = allocator.create(BiDi) catch @panic("OOM");
+ defer allocator.destroy(bidi);
- const upgraded = cdp.conn.handshake() catch |err| {
- log.err(.app, "CDP handshake", .{ .err = err });
+ bidi.init(self.app, socket) catch |err| {
+ log.err(.app, "BiDi init", .{ .err = err });
return;
};
+ defer bidi.deinit();
- if (!upgraded) {
- return;
+ self.serve(.init(.{ .bidi = bidi }), active_conns_early_release);
+}
+
+// Everything a live connection needs regardless of which protocol it
+// speaks: tracking (so shutdown can reach it), handing the read side to
+// the run loop, and running the worker loop.
+fn serve(self: *Server, driver: Driver, active_conns_early_release: *bool) void {
+ const conn = driver.conn;
+
+ if (log.enabled(.app, .info)) {
+ const client_address = getClientAddress(conn.socket) catch null;
+ log.info(.app, "client connected", .{ .ip = client_address });
}
- // only count websocket (i.e. CDP) connections, not HTTP requests like
- // /json/version probes or /metrics scrapes
- lp.metrics.cdp_connections.incr();
- lp.metrics.cdp_active_connections.incr();
- defer lp.metrics.cdp_active_connections.decr();
+ self.track(driver);
+ defer self.untrack(driver);
{
- // Transition from .handshake state to .live
+ // Transition from .starting state to .live
// Lock needed even though the main thread hasn't seen this yet because
// shutdown could access this from the sighandler thread.
- self.cdp_mutex.lockUncancelable(lp.io);
- defer self.cdp_mutex.unlock(lp.io);
- cdp.conn.state = .live;
+ self.driver_mutex.lockUncancelable(lp.io);
+ defer self.driver_mutex.unlock(lp.io);
+ conn.state = .live;
}
- // Hand the read side of the CDP socket over to the run loop.
+ // Hand the read side of the socket over to the run loop.
// From here until the matching unregisterLink, the worker must NOT
// read from the socket directly — bytes arrive via the inbox.
// unregisterLink is synchronous, so by the time it returns the loop
// is guaranteed to be done with this link.
//
- // cdp_link_active gates HttpClient.perform's block in
+ // driver_link_active gates HttpClient.perform's block in
// curl_multi_poll: with it false (tests, pre-handshake), perform
// skips the poll when there's no in-flight curl work — sleeping
// would just eat the timeout waiting for a wakeup that won't
@@ -705,11 +713,11 @@ fn handleConnection(self: *Server, socket: posix.socket_t) void {
// accepting wakeups by the time the worker might poll, and clear
// it *after* unregisterLink returns (the loop is guaranteed done
// with us by then).
- self.registerLink(&cdp.link);
- cdp.browser.http_client.cdp_link_active = true;
+ self.registerLink(driver.link);
+ driver.browser.http_client.driver_link_active = true;
defer {
- self.unregisterLink(&cdp.link);
- cdp.browser.http_client.cdp_link_active = false;
+ self.unregisterLink(driver.link);
+ driver.browser.http_client.driver_link_active = false;
}
// Check shutdown after markLive so that a concurrent shutdown either
@@ -719,19 +727,42 @@ fn handleConnection(self: *Server, socket: posix.socket_t) void {
return;
}
- while (true) {
- const next = cdp.tick() catch |err| {
- log.err(.app, "cdp tick", .{ .err = err });
- return;
- };
- if (!next) break;
- }
+ driver.run();
- // try to release the connection as soon as possible.
- active_conns_early_release = true;
+ // Try to release the connection as soon as possible: the browser/V8
+ // teardown in our callers' defers can take milliseconds, and a client
+ // that disconnects and immediately reconnects shouldn't be rejected
+ // for a slot we're merely unwinding.
+ active_conns_early_release.* = true;
_ = self.active_conns.fetchSub(1, .monotonic);
}
+fn track(self: *Server, driver: Driver) void {
+ self.driver_mutex.lockUncancelable(lp.io);
+ defer self.driver_mutex.unlock(lp.io);
+ self.drivers.append(self.app.allocator, driver) catch {};
+}
+
+fn untrack(self: *Server, driver: Driver) void {
+ self.driver_mutex.lockUncancelable(lp.io);
+ defer self.driver_mutex.unlock(lp.io);
+
+ for (self.drivers.items, 0..) |*d, i| {
+ if (d.conn == driver.conn) {
+ _ = self.drivers.swapRemove(i);
+ break;
+ }
+ }
+}
+
+fn getClientAddress(socket: posix.socket_t) !sys_net.IpAddress {
+ var storage: posix.sockaddr.storage = undefined;
+ var socklen: posix.socklen_t = @sizeOf(posix.sockaddr.storage);
+ try posix.getpeername(socket, @ptrCast(&storage), &socklen);
+ return sys_net.addressFromSockaddr(@ptrCast(&storage));
+}
+
+// The pointed-to driver is owned by its worker thread
fn buildJSONVersionResponse(app: *const App, port: u16) ![]const u8 {
const host = app.config.advertiseHost();
if (app.config.bindIsWildcard()) {
@@ -768,7 +799,7 @@ fn buildJSONVersionResponse(app: *const App, port: u16) ![]const u8 {
return try std.fmt.allocPrint(app.allocator, response_format, .{ body_len, host, port });
}
-const testing = @import("testing.zig");
+const testing = @import("../testing.zig");
test "server: buildJSONVersionResponse" {
const res = try buildJSONVersionResponse(testing.test_app, testing.test_app.config.port());
defer testing.test_app.allocator.free(res);
@@ -1074,6 +1105,216 @@ test "Client: close message" {
);
}
+test "server: bidi session lifecycle" {
+ var c = try createTestClient();
+ defer c.deinit();
+ try c.handshake("/session");
+
+ try c.bidiCommand("{\"id\":1,\"method\":\"session.status\"}");
+ try assertBidiMessage(&c, .{ .type = "success", .id = 1, .result = .{ .ready = true, .message = "" } });
+
+ // capture the sessionId from session.new by hand — it's random
+ try c.bidiCommand("{\"id\":2,\"method\":\"session.new\",\"params\":{\"capabilities\":{}}}");
+ {
+ const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
+ defer if (msg.cleanup_fragment) c.reader.cleanup();
+
+ const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, msg.data, .{});
+ defer parsed.deinit();
+
+ const obj = parsed.value.object;
+ try testing.expectEqual("success", obj.get("type").?.string);
+ try testing.expectEqual(2, obj.get("id").?.integer);
+
+ const result = obj.get("result").?.object;
+ try testing.expectEqual(36, result.get("sessionId").?.string.len);
+
+ const capabilities = result.get("capabilities").?.object;
+ try testing.expectEqual("Lightpanda", capabilities.get("browserName").?.string);
+ try testing.expectEqual(lp.build_config.version, capabilities.get("browserVersion").?.string);
+ try testing.expectEqual(false, capabilities.get("acceptInsecureCerts").?.bool);
+ }
+
+ try c.bidiCommand("{\"id\":3,\"method\":\"session.status\"}");
+ try assertBidiMessage(&c, .{ .type = "success", .id = 3, .result = .{ .ready = false, .message = "session already started" } });
+
+ try c.bidiCommand("{\"id\":4,\"method\":\"session.new\"}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 4, .@"error" = "session not created", .message = "session already exists" });
+
+ try c.bidiCommand("{\"id\":5,\"method\":\"session.subscribe\",\"params\":{\"events\":[\"log.entryAdded\"]}}");
+ {
+ // the subscription id is random
+ const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
+ defer if (msg.cleanup_fragment) c.reader.cleanup();
+
+ const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, msg.data, .{});
+ defer parsed.deinit();
+ try testing.expectEqual("success", parsed.value.object.get("type").?.string);
+ try testing.expectEqual(36, parsed.value.object.get("result").?.object.get("subscription").?.string.len);
+ }
+
+ try c.bidiCommand("{\"id\":6,\"method\":\"session.unsubscribe\",\"params\":{\"events\":[\"log.entryAdded\"]}}");
+ // an empty result must serialize as {}, not [] — struct {}{} on the
+ // expected side asserts the object-ness
+ try assertBidiMessage(&c, .{ .type = "success", .id = 6, .result = struct {}{} });
+
+ try c.bidiCommand("{\"id\":7,\"method\":\"session.end\"}");
+ try assertBidiMessage(&c, .{ .type = "success", .id = 7, .result = struct {}{} });
+
+ // ending the session closes the connection
+ {
+ const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
+ defer if (msg.cleanup_fragment) c.reader.cleanup();
+ try testing.expectEqual(.close, msg.type);
+ }
+}
+
+test "server: bidi errors" {
+ var c = try createTestClient();
+ defer c.deinit();
+ try c.handshake("/session");
+
+ try c.bidiCommand("this is not json");
+ try assertBidiMessage(&c, .{ .type = "error", .id = null, .@"error" = "invalid argument", .message = "invalid JSON message" });
+
+ try c.bidiCommand("{\"method\":\"session.status\"}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = null, .@"error" = "invalid argument", .message = "missing command id" });
+
+ try c.bidiCommand("{\"id\":1}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 1, .@"error" = "invalid argument", .message = "missing command method" });
+
+ try c.bidiCommand("{\"id\":2,\"method\":\"browsingContext.getTree\"}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 2, .@"error" = "invalid session id", .message = "no active session" });
+
+ // session-scoped commands before session.new
+ try c.bidiCommand("{\"id\":3,\"method\":\"session.subscribe\"}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 3, .@"error" = "invalid session id", .message = "no active session" });
+
+ try c.bidiCommand("{\"id\":4,\"method\":\"session.end\"}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 4, .@"error" = "invalid session id", .message = "no active session" });
+
+ // an unknown command is reported as such even before session.new; only
+ // known commands in known modules reach the session gate
+ try c.bidiCommand("{\"id\":5,\"method\":\"session.over9000\"}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 5, .@"error" = "unknown command", .message = "session.over9000" });
+
+ try c.bidiCommand("{\"id\":6,\"method\":\"storage.getCookies\"}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 6, .@"error" = "unknown command", .message = "storage.getCookies" });
+
+ try c.bidiCommand("{\"id\":7,\"method\":\"nodothere\"}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 7, .@"error" = "unknown command", .message = "nodothere" });
+}
+
+test "server: bidi browsingContext" {
+ var c = try createTestClient();
+ defer c.deinit();
+ try c.handshake("/session");
+
+ try c.bidiCommand("{\"id\":1,\"method\":\"session.new\",\"params\":{\"capabilities\":{}}}");
+ try discardBidiMessage(&c); // response shape covered by the lifecycle test
+
+ var context_id: [36]u8 = undefined;
+ try c.bidiCommand("{\"id\":2,\"method\":\"browsingContext.create\",\"params\":{\"type\":\"tab\"}}");
+ {
+ const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
+ defer if (msg.cleanup_fragment) c.reader.cleanup();
+
+ const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, msg.data, .{});
+ defer parsed.deinit();
+ const obj = parsed.value.object;
+ try testing.expectEqual("success", obj.get("type").?.string);
+ const context = obj.get("result").?.object.get("context").?.string;
+ try testing.expectEqual(36, context.len);
+ @memcpy(&context_id, context);
+ }
+
+ try c.bidiCommand("{\"id\":3,\"method\":\"browsingContext.getTree\"}");
+ try assertBidiMessage(&c, .{ .type = "success", .id = 3, .result = .{ .contexts = .{.{
+ .context = &context_id,
+ .url = "about:blank",
+ .userContext = "default",
+ .originalOpener = null,
+ .children = .{},
+ }} } });
+
+ try c.bidiCommand("{\"id\":4,\"method\":\"session.subscribe\",\"params\":{\"events\":[\"browsingContext.domContentLoaded\",\"browsingContext.load\"]}}");
+ try discardBidiMessage(&c);
+
+ const url = "http://127.0.0.1:9582/src/browser/tests/cdp/dom2.html";
+ var buf: [256]u8 = undefined;
+ try c.bidiCommand(try std.fmt.bufPrint(
+ &buf,
+ "{{\"id\":5,\"method\":\"browsingContext.navigate\",\"params\":{{\"context\":\"{s}\",\"url\":\"" ++ url ++ "\",\"wait\":\"complete\"}}}}",
+ .{&context_id},
+ ));
+ try assertBidiEvent(&c, "browsingContext.domContentLoaded", &context_id, url);
+ try assertBidiEvent(&c, "browsingContext.load", &context_id, url);
+ {
+ const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
+ defer if (msg.cleanup_fragment) c.reader.cleanup();
+
+ const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, msg.data, .{});
+ defer parsed.deinit();
+ const obj = parsed.value.object;
+ try testing.expectEqual("success", obj.get("type").?.string);
+ try testing.expectEqual(5, obj.get("id").?.integer);
+ const result = obj.get("result").?.object;
+ try testing.expectEqual(url, result.get("url").?.string);
+ try testing.expectEqual(36, result.get("navigation").?.string.len);
+ }
+
+ try c.bidiCommand("{\"id\":6,\"method\":\"browsingContext.navigate\",\"params\":{\"context\":\"00000000-0000-0000-0000-000000000000\",\"url\":\"about:blank\"}}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 6, .@"error" = "no such frame", .message = "unknown context" });
+
+ try c.bidiCommand("{\"id\":7,\"method\":\"browsingContext.create\",\"params\":{\"type\":\"tab\"}}");
+ try assertBidiMessage(&c, .{ .type = "error", .id = 7, .@"error" = "unsupported operation" });
+
+ try c.bidiCommand(try std.fmt.bufPrint(
+ &buf,
+ "{{\"id\":8,\"method\":\"browsingContext.close\",\"params\":{{\"context\":\"{s}\"}}}}",
+ .{&context_id},
+ ));
+ try assertBidiMessage(&c, .{ .type = "success", .id = 8, .result = struct {}{} });
+
+ try c.bidiCommand("{\"id\":9,\"method\":\"browsingContext.getTree\"}");
+ try assertBidiMessage(&c, .{ .type = "success", .id = 9, .result = .{ .contexts = .{} } });
+}
+
+fn discardBidiMessage(c: *TestClient) !void {
+ const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
+ if (msg.cleanup_fragment) {
+ c.reader.cleanup();
+ }
+}
+
+fn assertBidiEvent(c: *TestClient, method: []const u8, context: []const u8, url: []const u8) !void {
+ const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
+ defer if (msg.cleanup_fragment) {
+ c.reader.cleanup();
+ };
+
+ const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, msg.data, .{});
+ defer parsed.deinit();
+ const obj = parsed.value.object;
+ try testing.expectEqual("event", obj.get("type").?.string);
+ try testing.expectEqual(method, obj.get("method").?.string);
+
+ const p = obj.get("params").?.object;
+ try testing.expectEqual(context, p.get("context").?.string);
+ try testing.expectEqual(url, p.get("url").?.string);
+ try testing.expectEqual(36, p.get("navigation").?.string.len);
+}
+
+fn assertBidiMessage(c: *TestClient, expected: anytype) !void {
+ const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
+ defer if (msg.cleanup_fragment) {
+ c.reader.cleanup();
+ };
+
+ try testing.expectEqual(.text, msg.type);
+ try testing.expectJson(expected, msg.data);
+}
+
test "server: 404" {
var c = try createTestClient();
defer c.deinit();
@@ -1169,7 +1410,7 @@ fn assertWebSocketError(close_code: u16, input: []const u8) !void {
var c = try createTestClient();
defer c.deinit();
- try c.handshake();
+ try c.handshake("/");
try sys_net.writeAll(c.socket, input);
const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
@@ -1186,7 +1427,7 @@ fn assertWebSocketMessage(expected: []const u8, input: []const u8) !void {
var c = try createTestClient();
defer c.deinit();
- try c.handshake();
+ try c.handshake("/");
try sys_net.writeAll(c.socket, input);
const msg = try c.readWebsocketMessage() orelse return error.NoMessage;
@@ -1248,7 +1489,7 @@ const TestClient = struct {
buf: [8192]u8 = undefined,
reader: WS.Reader(false),
- const WS = @import("network/WS.zig");
+ const WS = @import("../network/WS.zig");
fn deinit(self: *TestClient) void {
_ = std.c.close(self.socket);
@@ -1316,9 +1557,9 @@ const TestClient = struct {
}
}
- fn handshake(self: *TestClient) !void {
+ fn handshake(self: *TestClient, comptime path: []const u8) !void {
const request =
- "GET / HTTP/1.1\r\n" ++
+ "GET " ++ path ++ " HTTP/1.1\r\n" ++
"Connection: upgrade\r\n" ++
"Upgrade: websocket\r\n" ++
"sec-websocket-version:13\r\n" ++
@@ -1332,16 +1573,35 @@ const TestClient = struct {
"Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res);
}
+ // client->server frames must be masked; a zero mask leaves the payload
+ // bytes unchanged.
+ fn bidiCommand(self: *TestClient, payload: []const u8) !void {
+ var frame: [1024]u8 = undefined;
+ frame[0] = 129; // fin | text
+ var header_len: usize = 2;
+ if (payload.len <= 125) {
+ frame[1] = 128 | @as(u8, @intCast(payload.len));
+ } else {
+ frame[1] = 128 | 126;
+ std.mem.writeInt(u16, frame[2..4], @intCast(payload.len), .big);
+ header_len = 4;
+ }
+ @memset(frame[header_len..][0..4], 0);
+ @memcpy(frame[header_len + 4 ..][0..payload.len], payload);
+ try sys_net.writeAll(self.socket, frame[0 .. header_len + 4 + payload.len]);
+ }
+
fn readWebsocketMessage(self: *TestClient) !?WS.Message {
while (true) {
+ // two frames can arrive in one read; drain buffered ones first
+ if (try self.reader.next()) |msg| {
+ return msg;
+ }
const n = try posix.read(self.socket, self.reader.readBuf());
if (n == 0) {
return error.Closed;
}
self.reader.len += n;
- if (try self.reader.next()) |msg| {
- return msg;
- }
}
}
};
diff --git a/src/server/bidi/BiDi.zig b/src/server/bidi/BiDi.zig
new file mode 100644
index 000000000..69af4a11a
--- /dev/null
+++ b/src/server/bidi/BiDi.zig
@@ -0,0 +1,251 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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 App = @import("../../App.zig");
+const Server = @import("../Server.zig");
+const Browser = @import("../../browser/Browser.zig");
+const Session = @import("../../browser/Session.zig");
+const Notification = @import("../../Notification.zig");
+
+const Driver = @import("../Driver.zig");
+const Connection = @import("../Connection.zig");
+
+const browsing_context = @import("browsing_context.zig");
+
+const posix = std.posix;
+
+const BiDi = @This();
+
+app: *App,
+conn: Connection,
+
+// Server run-loop read-side handle for the socket. Server registers it
+// after the handshake and unregisters before teardown; see CDP.zig.
+link: Server.Link,
+
+// Re-used arena for processing a message. Works because we strictly process
+// one message at a time.
+message_arena: std.heap.ArenaAllocator,
+
+// Allocations that live for the whole session (subscriptions).
+session_arena: std.heap.ArenaAllocator,
+
+// The active session, created by session.new
+session_id: ?[36]u8 = null,
+
+browser: Browser,
+session: *Session,
+notification: *Notification,
+context: ?browsing_context.Context = null,
+
+subscriptions: std.ArrayList(Subscription) = .empty,
+
+const Subscription = struct {
+ id: [36]u8,
+ event: []const u8,
+};
+
+const InputMessage = struct {
+ id: ?u64 = null,
+ method: ?[]const u8 = null,
+};
+
+pub fn init(self: *BiDi, app: *App, socket: posix.socket_t) !void {
+ const allocator = app.allocator;
+ self.* = .{
+ .app = app,
+ .link = undefined,
+ .conn = undefined,
+ .browser = undefined,
+ .session = undefined,
+ .notification = undefined,
+ .message_arena = std.heap.ArenaAllocator.init(allocator),
+ .session_arena = std.heap.ArenaAllocator.init(allocator),
+ };
+
+ const driver: Driver = .init(.{ .bidi = self });
+
+ try self.browser.init(app, .{}, driver);
+ errdefer self.browser.deinit();
+
+ const http_client = &self.browser.http_client;
+ try self.conn.init(app, socket, .bidi, &http_client.inbox);
+ errdefer self.conn.deinit();
+
+ self.link = .{
+ .driver = driver,
+ .state = .live,
+ .socket = socket,
+ .handles = http_client.handles,
+ };
+
+ // the remaining failures are OOM-class; no point cleaning up
+ self.notification = try Notification.init(allocator);
+ self.session = try self.browser.newSession(self.notification);
+ try browsing_context.registerNotifications(self);
+}
+
+pub fn deinit(self: *BiDi) void {
+ self.notification.unregisterAll(self);
+ self.browser.closeSession();
+ self.notification.deinit();
+ self.browser.deinit();
+ self.conn.deinit();
+ self.message_arena.deinit();
+ self.session_arena.deinit();
+}
+
+// Dispatch a single BiDi frame from the inbox. Unlike CDP, frames aren't
+// parsed on the Network thread, so `data` is the raw JSON.
+pub fn onMessage(self: *BiDi, data: []const u8) anyerror!void {
+ if (self.browser.env.terminatePending()) {
+ return;
+ }
+
+ defer _ = self.message_arena.reset(.{ .retain_with_limit = 4096 });
+ const arena = self.message_arena.allocator();
+
+ const input = std.json.parseFromSliceLeaky(InputMessage, arena, data, .{
+ .ignore_unknown_fields = true,
+ }) catch {
+ return self.sendError(null, "invalid argument", "invalid JSON message");
+ };
+
+ const id = input.id orelse {
+ return self.sendError(null, "invalid argument", "missing command id");
+ };
+ const method = input.method orelse {
+ return self.sendError(id, "invalid argument", "missing command method");
+ };
+
+ self.dispatch(id, method, data) catch |err| switch (err) {
+ error.UnknownCommand => try self.sendError(id, "unknown command", method),
+ // Command.params already answered the client.
+ error.InvalidParams => {},
+ else => return err,
+ };
+}
+
+// A BiDi method is always "<module>.<command>".
+fn dispatch(self: *BiDi, id: u64, method: []const u8, data: []const u8) !void {
+ const i = std.mem.indexOfScalar(u8, method, '.') orelse {
+ return error.UnknownCommand;
+ };
+ const module = std.meta.stringToEnum(enum {
+ session,
+ browser,
+ browsingContext,
+ }, method[0..i]) orelse return error.UnknownCommand;
+
+ // Only the session module is reachable without a session (it's what
+ // creates one); it gates its own commands.
+ if (self.session_id == null and module != .session) {
+ return self.sendError(id, "invalid session id", "no active session");
+ }
+
+ const cmd: Command = .{
+ .id = id,
+ .bidi = self,
+ .json = data,
+ .action = method[i + 1 ..],
+ };
+
+ switch (module) {
+ .session => return @import("session.zig").processMessage(&cmd),
+ .browser => return @import("browser.zig").processMessage(&cmd),
+ .browsingContext => return browsing_context.processMessage(&cmd),
+ }
+}
+
+// One command being processed. Handlers answer through it so they don't
+// have to thread the id (and the raw message) around.
+pub const Command = struct {
+ bidi: *BiDi,
+
+ // Echoed back in the response.
+ id: u64,
+
+ // The "<command>" half of "<module>.<command>".
+ action: []const u8,
+
+ // The full raw message; `params` is parsed out of it on demand.
+ json: []const u8,
+
+ // Parses the command's params object. Answers the client and returns
+ // error.InvalidParams when the message has no params or they don't
+ // match T, so callers can just `try`. onMessage swallows that error.
+ pub fn params(self: *const Command, comptime T: type) !T {
+ const arena = self.bidi.message_arena.allocator();
+ const wrapper = std.json.parseFromSliceLeaky(struct { params: T }, arena, self.json, .{
+ .ignore_unknown_fields = true,
+ }) catch {
+ try self.sendError("invalid argument", "invalid params");
+ return error.InvalidParams;
+ };
+ return wrapper.params;
+ }
+
+ pub fn sendResult(self: *const Command, result: anytype) !void {
+ return self.bidi.sendResult(self.id, result);
+ }
+
+ pub fn sendError(self: *const Command, code: []const u8, message: []const u8) !void {
+ return self.bidi.sendError(self.id, code, message);
+ }
+
+ // Events aren't tied to the command, but handlers that emit one always
+ // have a cmd on hand.
+ pub fn sendEvent(self: *const Command, method: []const u8, p: anytype) !void {
+ return self.bidi.sendEvent(method, p);
+ }
+};
+
+// An event is delivered when its name is subscribed exactly, or its module
+// is ("browsingContext" covers "browsingContext.load").
+fn subscribed(self: *const BiDi, name: []const u8) bool {
+ for (self.subscriptions.items) |sub| {
+ if (std.mem.eql(u8, sub.event, name)) {
+ return true;
+ }
+ if (sub.event.len < name.len and name[sub.event.len] == '.' and std.mem.startsWith(u8, name, sub.event)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+pub fn sendEvent(self: *BiDi, method: []const u8, p: anytype) !void {
+ if (!self.subscribed(method)) {
+ return;
+ }
+ return self.sendJSON(.{ .type = "event", .method = method, .params = p });
+}
+
+pub fn sendResult(self: *BiDi, id: u64, result: anytype) !void {
+ return self.sendJSON(.{ .type = "success", .id = id, .result = result });
+}
+
+pub fn sendError(self: *BiDi, id: ?u64, code: []const u8, message: []const u8) !void {
+ return self.sendJSON(.{ .type = "error", .id = id, .@"error" = code, .message = message });
+}
+
+fn sendJSON(self: *BiDi, message: anytype) !void {
+ return self.conn.sendJSON(message, .{});
+}
diff --git a/src/server/bidi/browser.zig b/src/server/bidi/browser.zig
new file mode 100644
index 000000000..b60078593
--- /dev/null
+++ b/src/server/bidi/browser.zig
@@ -0,0 +1,39 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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 BiDi = @import("BiDi.zig");
+
+pub fn processMessage(cmd: *const BiDi.Command) !void {
+ const command = std.meta.stringToEnum(enum {
+ getUserContexts,
+ }, cmd.action) orelse return error.UnknownCommand;
+
+ switch (command) {
+ .getUserContexts => return getUserContexts(cmd),
+ }
+}
+
+fn getUserContexts(cmd: *const BiDi.Command) !void {
+ return cmd.sendResult(.{
+ .userContexts = &[_]struct { userContext: []const u8 }{
+ .{ .userContext = "default" },
+ },
+ });
+}
diff --git a/src/server/bidi/browsing_context.zig b/src/server/bidi/browsing_context.zig
new file mode 100644
index 000000000..1be63fe05
--- /dev/null
+++ b/src/server/bidi/browsing_context.zig
@@ -0,0 +1,320 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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 lp = @import("lightpanda");
+
+const URL = @import("../../browser/URL.zig");
+const uuidv4 = @import("../../id.zig").uuidv4;
+const Session = @import("../../browser/Session.zig");
+const Notification = @import("../../Notification.zig");
+
+const BiDi = @import("BiDi.zig");
+
+const log = lp.log;
+
+pub const Context = struct {
+ // The BiDi navigable id. Stable across navigations.
+ id: [36]u8,
+
+ frame_id: u32,
+
+ // Regenerated by onFrameNavigate for every root navigation, including
+ // ones the page itself initiates.
+ navigation_id: [36]u8,
+
+ // A navigate command whose `wait` hasn't been satisfied yet.
+ pending_navigate: ?PendingNavigate = null,
+};
+
+const PendingNavigate = struct {
+ command_id: u64,
+ until: enum { interactive, complete },
+};
+
+pub fn processMessage(cmd: *const BiDi.Command) !void {
+ const command = std.meta.stringToEnum(enum {
+ getTree,
+ create,
+ navigate,
+ close,
+ }, cmd.action) orelse return error.UnknownCommand;
+
+ switch (command) {
+ .getTree => return getTree(cmd),
+ .create => return create(cmd),
+ .navigate => return navigate(cmd),
+ .close => return close(cmd),
+ }
+}
+
+fn getTree(cmd: *const BiDi.Command) !void {
+ const bidi = cmd.bidi;
+ const ctx = &(bidi.context orelse {
+ return cmd.sendResult(.{ .contexts = &[_]Info{} });
+ });
+
+ const url: []const u8 = if (bidi.session.currentFrame()) |frame| frame.url else "about:blank";
+ return cmd.sendResult(.{
+ .contexts = &[_]Info{.{ .context = &ctx.id, .url = url }},
+ });
+}
+
+// {type: "tab" | "window"} ignored
+fn create(cmd: *const BiDi.Command) !void {
+ const bidi = cmd.bidi;
+ if (bidi.context != null) {
+ return cmd.sendError("unsupported operation", "only a single browsing context is supported");
+ }
+
+ const page = bidi.session.createPage() catch |err| {
+ log.err(.bidi, "create page", .{ .err = err });
+ return cmd.sendError("unknown error", "failed to create page");
+ };
+
+ var ctx = Context{
+ .id = undefined,
+ .frame_id = page.frame_id,
+ .navigation_id = undefined,
+ };
+ uuidv4(&ctx.id);
+ uuidv4(&ctx.navigation_id);
+ bidi.context = ctx;
+
+ try cmd.sendEvent("browsingContext.contextCreated", .{
+ .context = &ctx.id,
+ .url = "about:blank",
+ .userContext = "default",
+ .originalOpener = null,
+ .children = null,
+ });
+
+ // A fresh page has no document; give it one so evaluation and getTree
+ // have something to work against before the first navigate.
+ if (page.frame()) |frame| {
+ frame.navigate("about:blank", .{
+ .reason = .address_bar,
+ .kind = .{ .push = null },
+ }) catch |err| {
+ log.warn(.bidi, "blank navigate", .{ .err = err });
+ };
+ }
+
+ return cmd.sendResult(.{ .context = &ctx.id });
+}
+
+fn navigate(cmd: *const BiDi.Command) !void {
+ const p = try cmd.params(struct {
+ url: [:0]const u8,
+ context: []const u8,
+ wait: enum { none, interactive, complete } = .none,
+ });
+
+ const bidi = cmd.bidi;
+ const ctx = (try requireContext(cmd, p.context)) orelse return;
+
+ const frame = bidi.session.currentFrame() orelse {
+ return cmd.sendError("unknown error", "no frame");
+ };
+ const encoded_url = URL.resolveNavigation(frame.call_arena, p.url, .{}) catch {
+ return cmd.sendError("invalid argument", "invalid url");
+ };
+
+ // A second navigate supersedes an in-flight one; answer the old command
+ // so the client isn't left waiting on its id forever.
+ try rejectPending(bidi, ctx, "navigation superseded");
+
+ // Set before starting: a navigation can reach its wait condition
+ // synchronously (about:blank), which would fire the lifecycle callback
+ // before we got a chance to record the pending command.
+ switch (p.wait) {
+ .none => {},
+ .interactive => ctx.pending_navigate = .{ .command_id = cmd.id, .until = .interactive },
+ .complete => ctx.pending_navigate = .{ .command_id = cmd.id, .until = .complete },
+ }
+
+ // Same fast path as CDP's Page.navigate: a root frame that never
+ // navigated has nothing to preserve, so it navigates in place; a live
+ // page goes through the pending-Page replacement machinery.
+ const nav_result = if (frame._load_state == .waiting)
+ frame.navigate(encoded_url, .{ .reason = .address_bar, .kind = .{ .push = null } })
+ else
+ bidi.session.initiateRootNavigation(frame._frame_id, encoded_url, .{ .reason = .address_bar, .kind = .{ .push = null } });
+
+ nav_result catch |err| {
+ log.warn(.bidi, "navigate", .{ .err = err });
+ ctx.pending_navigate = null;
+ return cmd.sendError("unknown error", "navigation failed");
+ };
+
+ if (p.wait == .none) {
+ return cmd.sendResult(.{ .navigation = &ctx.navigation_id, .url = encoded_url });
+ }
+}
+
+fn close(cmd: *const BiDi.Command) !void {
+ const p = try cmd.params(struct {
+ context: []const u8,
+ });
+
+ const bidi = cmd.bidi;
+ const ctx = (try requireContext(cmd, p.context)) orelse return;
+ try rejectPending(bidi, ctx, "browsing context closed");
+ const ctx_id = ctx.id;
+
+ // the frame's url doesn't survive closePage
+ const arena = bidi.message_arena.allocator();
+ const url: []const u8 = if (bidi.session.currentFrame()) |frame|
+ try arena.dupe(u8, frame.url)
+ else
+ "about:blank";
+
+ bidi.session.closePage(ctx.frame_id);
+ bidi.context = null;
+
+ try cmd.sendEvent("browsingContext.contextDestroyed", .{
+ .context = &ctx_id,
+ .url = url,
+ .userContext = "default",
+ .originalOpener = null,
+ .children = null,
+ });
+
+ return cmd.sendResult(struct {}{});
+}
+
+fn requireContext(cmd: *const BiDi.Command, context: []const u8) !?*Context {
+ if (cmd.bidi.context) |*ctx| {
+ if (std.mem.eql(u8, &ctx.id, context)) {
+ return ctx;
+ }
+ }
+ try cmd.sendError("no such frame", "unknown context");
+ return null;
+}
+
+// Answers `ctx`'s pending navigate command, if any, and clears it.
+fn answerPending(bidi: *BiDi, ctx: *Context, url: []const u8) !void {
+ const pending = ctx.pending_navigate orelse return;
+ ctx.pending_navigate = null;
+ return bidi.sendResult(pending.command_id, .{
+ .url = url,
+ .navigation = &ctx.navigation_id,
+ });
+}
+
+// Fails `ctx`'s pending navigate command, if any, and clears it. BiDi has no
+// navigation-specific error code, so every reason is an "unknown error" and
+// only the message distinguishes them.
+fn rejectPending(bidi: *BiDi, ctx: *Context, message: []const u8) !void {
+ const pending = ctx.pending_navigate orelse return;
+ ctx.pending_navigate = null;
+ return bidi.sendError(pending.command_id, "unknown error", message);
+}
+
+pub fn registerNotifications(bidi: *BiDi) !void {
+ const notification = bidi.notification;
+ try notification.register(.frame_loaded, bidi, onFrameLoaded);
+ try notification.register(.frame_navigate, bidi, onFrameNavigate);
+ try notification.register(.frame_navigate_failed, bidi, onFrameNavigateFailed);
+ try notification.register(.frame_dom_content_loaded, bidi, onFrameDOMContentLoaded);
+}
+
+fn onFrameNavigate(ptr: *anyopaque, msg: *const Notification.FrameNavigate) !void {
+ const bidi: *BiDi = @ptrCast(@alignCast(ptr));
+ const ctx = rootContext(bidi, msg.frame_id) orelse return;
+
+ uuidv4(&ctx.navigation_id);
+ try bidi.sendEvent("browsingContext.navigationStarted", .{
+ .context = &ctx.id,
+ .navigation = &ctx.navigation_id,
+ .timestamp = msg.timestamp,
+ .url = msg.url,
+ });
+}
+
+fn onFrameNavigateFailed(ptr: *anyopaque, msg: *const Notification.FrameNavigateFailed) !void {
+ const bidi: *BiDi = @ptrCast(@alignCast(ptr));
+ const ctx = rootContext(bidi, msg.frame_id) orelse return;
+
+ try bidi.sendEvent("browsingContext.navigationFailed", .{
+ .context = &ctx.id,
+ .navigation = &ctx.navigation_id,
+ .timestamp = msg.timestamp,
+ .url = msg.url,
+ });
+
+ try rejectPending(bidi, ctx, "navigation failed");
+}
+
+fn onFrameDOMContentLoaded(ptr: *anyopaque, msg: *const Notification.FrameDOMContentLoaded) !void {
+ return frameLifecycleEvent(ptr, "browsingContext.domContentLoaded", .interactive, msg.frame_id, msg.timestamp);
+}
+
+fn onFrameLoaded(ptr: *anyopaque, msg: *const Notification.FrameLoaded) !void {
+ return frameLifecycleEvent(ptr, "browsingContext.load", .complete, msg.frame_id, msg.timestamp);
+}
+
+fn frameLifecycleEvent(
+ ptr: *anyopaque,
+ comptime method: []const u8,
+ reached: @FieldType(PendingNavigate, "until"),
+ frame_id: u32,
+ timestamp: u64,
+) !void {
+ const bidi: *BiDi = @ptrCast(@alignCast(ptr));
+ const ctx = rootContext(bidi, frame_id) orelse return;
+
+ const url: []const u8 = if (bidi.session.currentFrame()) |frame| frame.url else "";
+ try bidi.sendEvent(method, .{
+ .context = &ctx.id,
+ .navigation = &ctx.navigation_id,
+ .timestamp = timestamp,
+ .url = url,
+ });
+
+ // The response comes after the matching event — drivers key off the
+ // event, and answering first would let a client see the command
+ // complete before the lifecycle it waited for.
+ if (ctx.pending_navigate) |pending| {
+ if (pending.until == reached) {
+ try answerPending(bidi, ctx, url);
+ }
+ }
+}
+
+// Only the root frame maps to the BiDi context; child-frame events are
+// dropped until child navigables are supported.
+fn rootContext(bidi: *BiDi, frame_id: u32) ?*Context {
+ if (bidi.context) |*ctx| {
+ if (ctx.frame_id == frame_id) {
+ return ctx;
+ }
+ }
+ return null;
+}
+
+// BrowsingContextInfo for getTree. Events send children: null instead of a
+// (recursive) list, so they use ad-hoc structs.
+const Info = struct {
+ context: []const u8,
+ url: []const u8,
+ userContext: []const u8 = "default",
+ originalOpener: ?[]const u8 = null,
+ children: []const Info = &.{},
+};
diff --git a/src/server/bidi/session.zig b/src/server/bidi/session.zig
new file mode 100644
index 000000000..99af6fcce
--- /dev/null
+++ b/src/server/bidi/session.zig
@@ -0,0 +1,147 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// 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 lp = @import("lightpanda");
+const builtin = @import("builtin");
+
+const uuidv4 = @import("../../id.zig").uuidv4;
+
+const BiDi = @import("BiDi.zig");
+
+pub fn processMessage(cmd: *const BiDi.Command) !void {
+ const command = std.meta.stringToEnum(enum {
+ status,
+ new,
+ end,
+ subscribe,
+ unsubscribe,
+ }, cmd.action) orelse return error.UnknownCommand;
+
+ // The only two commands that work without a session; `new` is what
+ // creates one.
+ if (command != .status and command != .new and cmd.bidi.session_id == null) {
+ return cmd.sendError("invalid session id", "no active session");
+ }
+
+ switch (command) {
+ .status => return status(cmd),
+ .new => return new(cmd),
+ .end => return end(cmd),
+ .subscribe => return subscribe(cmd),
+ .unsubscribe => return unsubscribe(cmd),
+ }
+}
+
+fn status(cmd: *const BiDi.Command) !void {
+ // `ready` reflects whether session.new can succeed on this connection.
+ if (cmd.bidi.session_id == null) {
+ return cmd.sendResult(.{ .ready = true, .message = "" });
+ }
+ return cmd.sendResult(.{ .ready = false, .message = "session already started" });
+}
+
+fn new(cmd: *const BiDi.Command) !void {
+ const bidi = cmd.bidi;
+ if (bidi.session_id != null) {
+ return cmd.sendError("session not created", "session already exists");
+ }
+
+ bidi.session_id = @as([36]u8, undefined);
+ uuidv4(&bidi.session_id.?);
+
+ return cmd.sendResult(.{
+ .sessionId = &bidi.session_id.?,
+ .capabilities = .{
+ .acceptInsecureCerts = false,
+ .browserName = "Lightpanda",
+ .browserVersion = lp.build_config.version,
+ .platformName = platform_name,
+ .setWindowRect = false,
+ .userAgent = cmd.bidi.app.config.http_headers.user_agent,
+ .proxy = struct {}{},
+ },
+ });
+}
+
+fn end(cmd: *const BiDi.Command) !void {
+ try cmd.sendResult(struct {}{});
+
+ const browser = &cmd.bidi.browser;
+ const arena = try browser.arena_pool.acquire(.tiny, "bidi session end");
+ browser.http_client.inbox.push(arena, .close);
+}
+
+// Subscriptions are global (per-context filtering is not supported yet).
+// Event names aren't validated against a known list.
+fn subscribe(cmd: *const BiDi.Command) !void {
+ const p = try cmd.params(struct {
+ events: []const []const u8,
+ });
+ if (p.events.len == 0) {
+ return cmd.sendError("invalid argument", "no events");
+ }
+
+ var sub_id: [36]u8 = undefined;
+ uuidv4(&sub_id);
+
+ const bidi = cmd.bidi;
+ const arena = bidi.session_arena.allocator();
+ for (p.events) |event| {
+ try bidi.subscriptions.append(arena, .{
+ .id = sub_id,
+ .event = try arena.dupe(u8, event),
+ });
+ }
+ return cmd.sendResult(.{ .subscription = &sub_id });
+}
+
+fn unsubscribe(cmd: *const BiDi.Command) !void {
+ const p = try cmd.params(struct {
+ events: []const []const u8 = &.{},
+ subscriptions: []const []const u8 = &.{},
+ });
+
+ const subscriptions = &cmd.bidi.subscriptions;
+ var i: usize = 0;
+ while (i < subscriptions.items.len) {
+ const sub = subscriptions.items[i];
+ if (matchesAny(p.events, sub.event) or matchesAny(p.subscriptions, &sub.id)) {
+ _ = subscriptions.swapRemove(i);
+ } else {
+ i += 1;
+ }
+ }
+ return cmd.sendResult(struct {}{});
+}
+
+fn matchesAny(haystack: []const []const u8, needle: []const u8) bool {
+ for (haystack) |candidate| {
+ if (std.mem.eql(u8, candidate, needle)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+const platform_name = switch (builtin.os.tag) {
+ .macos, .ios => "mac",
+ .windows => "windows",
+ .linux => "linux",
+ else => @tagName(builtin.os.tag),
+};
diff --git a/src/cdp/AXNode.zig b/src/server/cdp/AXNode.zig
similarity index 99%
rename from src/cdp/AXNode.zig
rename to src/server/cdp/AXNode.zig
index abcb69b3d..c4492b11a 100644
--- a/src/cdp/AXNode.zig
+++ b/src/server/cdp/AXNode.zig
@@ -19,12 +19,12 @@
const std = @import("std");
const lp = @import("lightpanda");
-const Frame = @import("../browser/Frame.zig");
-const DOMNode = @import("../browser/webapi/Node.zig");
-const Label = @import("../browser/webapi/element/html/Label.zig");
-const interactive = @import("../browser/interactive.zig");
+const Frame = @import("../../browser/Frame.zig");
+const DOMNode = @import("../../browser/webapi/Node.zig");
+const Label = @import("../../browser/webapi/element/html/Label.zig");
+const interactive = @import("../../browser/interactive.zig");
-const Node = @import("Node.zig");
+const NodeRegistry = @import("../../NodeRegistry.zig");
const log = lp.log;
const jsonStringify = std.json.Stringify;
@@ -40,8 +40,8 @@ const scratch_retain_limit = 64 * 1024;
// its direct children, and sometimes the entire tree.
// (For now, we only support direct children)
pub const Writer = struct {
- root: *const Node,
- registry: *Node.Registry,
+ root: *const NodeRegistry.Node,
+ registry: *NodeRegistry,
frame: *Frame,
visibility_cache: *DOMNode.Element.VisibilityCache,
label_index: *Label.LabelByForIndex,
@@ -1514,7 +1514,7 @@ test "AXnode: stripWhitespaces" {
const testing = @import("testing.zig");
test "AXNode: writer" {
- var registry = Node.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/dom3.html", .{});
@@ -1606,7 +1606,7 @@ test "AXNode: writer" {
}
test "AXNode: writer prunes hidden and resolves labels" {
- var registry = Node.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/ax_tree.html", .{});
@@ -1741,7 +1741,7 @@ test "AXNode: writer prunes hidden and resolves labels" {
}
test "AXNode: Writer query filters by role" {
- var registry = Node.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/ax_tree.html", .{});
@@ -1800,7 +1800,7 @@ test "AXNode: role attribute token list" {
}
test "AXNode: writer maps password input to textbox" {
- var registry = Node.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/ax_tree.html", .{});
@@ -1877,7 +1877,7 @@ test "AXNode: writer maps password input to textbox" {
}
test "AXNode: Writer query filters by accessible name" {
- var registry = Node.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/ax_tree.html", .{});
@@ -1918,7 +1918,7 @@ test "AXNode: Writer query filters by accessible name" {
}
test "AXNode: Writer query combined role+name filter promotes hidden-input labels" {
- var registry = Node.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/ax_tree.html", .{});
@@ -1971,7 +1971,7 @@ test "AXNode: Writer query combined role+name filter promotes hidden-input label
}
test "AXNode: Writer query no match returns empty array" {
- var registry = Node.Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/ax_tree.html", .{});
diff --git a/src/cdp/CDP.zig b/src/server/cdp/CDP.zig
similarity index 90%
rename from src/cdp/CDP.zig
rename to src/server/cdp/CDP.zig
index b2e29c49b..e8c40df4c 100644
--- a/src/cdp/CDP.zig
+++ b/src/server/cdp/CDP.zig
@@ -19,25 +19,26 @@
const std = @import("std");
const lp = @import("lightpanda");
-const App = @import("../App.zig");
-const Inbox = @import("../Inbox.zig");
-const Notification = @import("../Notification.zig");
+const App = @import("../../App.zig");
+const Inbox = @import("../../Inbox.zig");
+const Notification = @import("../../Notification.zig");
-const WS = @import("../network/WS.zig");
-const http = @import("../network/http.zig");
+const WS = @import("../../network/WS.zig");
+const http = @import("../../network/http.zig");
const Server = @import("../Server.zig");
-const Transfer = @import("../network/HttpClient.zig").Transfer;
-
-const js = @import("../browser/js/js.zig");
-const Browser = @import("../browser/Browser.zig");
-const Session = @import("../browser/Session.zig");
-const Frame = @import("../browser/Frame.zig");
-const Page = @import("../browser/Page.zig");
-const Mime = @import("../browser/Mime.zig");
-const Element = @import("../browser/webapi/Element.zig");
-const Label = @import("../browser/webapi/element/html/Label.zig");
-
-const Connection = @import("Connection.zig");
+const HttpClient = @import("../../network/HttpClient.zig");
+
+const js = @import("../../browser/js/js.zig");
+const Browser = @import("../../browser/Browser.zig");
+const Session = @import("../../browser/Session.zig");
+const Frame = @import("../../browser/Frame.zig");
+const Page = @import("../../browser/Page.zig");
+const Mime = @import("../../browser/Mime.zig");
+const Element = @import("../../browser/webapi/Element.zig");
+const Label = @import("../../browser/webapi/element/html/Label.zig");
+
+const Connection = @import("../Connection.zig");
+const Driver = @import("../Driver.zig");
const Incrementing = @import("id.zig").Incrementing;
const network_domain = @import("domains/network.zig");
const InterceptState = @import("domains/fetch.zig").InterceptState;
@@ -64,7 +65,7 @@ browser: Browser,
allocator: Allocator,
// Server run-loop read-side handle for the CDP socket. Populated in
-// init; Server.handleConnection calls registerLink(&cdp.link) after the
+// init; Server.serve calls registerLink(&cdp.link) after the
// worker-side handshake completes, and unregisterLink before teardown.
link: Server.Link,
@@ -83,8 +84,8 @@ browser_session_id: ?[]const u8 = null,
browser_context: ?BrowserContext,
disable_set_cache_disabled: bool = false,
-// Re-used arena for processing a message. We're assuming that we're getting
-// 1 message at a time.
+// Re-used arena for processing a message. Works because we strictly process
+// one message at a time.
message_arena: std.heap.ArenaAllocator,
// Used for processing notifications within a browser context.
@@ -104,7 +105,6 @@ pub fn init(
self: *CDP,
app: *App,
socket: posix.socket_t,
- json_version_response: []const u8,
) !void {
const allocator = app.allocator;
@@ -122,14 +122,16 @@ pub fn init(
.streams = .{ .allocator = allocator },
};
- try self.browser.init(app, .{ .env = .{ .with_inspector = true } }, self);
+ const driver: Driver = .init(.{ .cdp = self });
+
+ try self.browser.init(app, .{ .env = .{ .with_inspector = true } }, driver);
const http_client = &self.browser.http_client;
- try self.conn.init(app, socket, json_version_response, &http_client.inbox);
+ try self.conn.init(app, socket, .cdp, &http_client.inbox);
errdefer self.conn.deinit();
self.link = .{
- .cdp = self,
+ .driver = driver,
.state = .live,
.socket = socket,
.handles = http_client.handles,
@@ -153,42 +155,6 @@ pub fn deinit(self: *CDP) void {
// into the worker's inbox. Returns false if a close frame was seen (or a
// fatal frame error) so the loop drops the link from its poll set.
//
-// One network read can carry more bytes than the reader's current
-// free space — large CDP messages (Page.addScriptToEvaluateOnNewDocument,
-// Runtime evaluation results, etc.) routinely exceed 16 KB, and a
-// single read can contain multiple messages, or part of messages. We loop: feed
-// what fits, run processMessages (which extracts complete frames, compacts the
-// reader, and grows the buffer if it sees a frame header larger than current
-// capacity), repeat until the chunk is drained.
-pub fn onData(self: *CDP, data: []const u8) anyerror!bool {
- var remaining = data;
- while (remaining.len > 0) {
- const n = self.conn.feedBytes(remaining);
- remaining = remaining[n..];
- if ((try self.conn.processMessages()) == false) {
- return false;
- }
- }
- return true;
-}
-
-// Called by the Server run loop when it drops the link unsolicited (peer
-// EOF, read error, poll HUP/ERR). Push a disconnect into the inbox so the
-// worker's drainInbox surfaces error.ClientDisconnected.
-pub fn onLinkDisconnect(self: *CDP, err: ?anyerror) void {
- const arena = self.browser.arena_pool.acquire(.tiny, "cdp disconnect") catch |e| switch (e) {
- error.OutOfMemory => @panic("OOM"),
- };
-
- self.browser.http_client.inbox.push(arena, .{ .disconnect = err });
-}
-
-// Called by the Server run loop to try to force the Worker to shutdown.
-// Protects against a stuck worker.
-pub fn terminateFromServer(self: *CDP) void {
- self.browser.env.requestTerminate();
-}
-
// Called in the Worker to dispatch a single CDP message bubbled up by
// HttpClient.drainInbox. The Server run loop already parsed the JSON
// when it pushed the message to the inbox, so we skip straight to
@@ -216,86 +182,10 @@ pub fn processMessage(self: *CDP, msg: []const u8) !void {
return self.dispatch(arena.allocator(), .{ .cdp = self }, msg);
}
-// Called in the worker when a PING message is received
-pub fn onPing(self: *CDP, body: []const u8) void {
- self.conn.sendPong(body) catch |err| {
- log.warn(.app, "CDP pong", .{ .err = err });
- };
-}
-
-// Called in the Worker when a peer-initiated close with CLOSE_NORMAL. The worker
-// loop tears down immediately after; drainInbox returns
-// error.ClientDisconnected once we return.
-pub fn onClose(self: *CDP) void {
- self.conn.send(&WS.CLOSE_NORMAL) catch |err| {
- log.warn(.app, "CDP close reply", .{ .err = err });
- };
-}
-
-// Called in the Worker when the peer disconnected (peer EOF, fatal frame error,
-// or right after a peer close was replied to). drainInbox returns
-// error.ClientDisconnected, which the worker loop catches to tear down.
-//
-// If `err` is a recognized WS framing error, send the matching close
-// frame back to the peer before tearing down — that's how clients
-// observe protocol violations (close code 1002 / 1009 / etc.).
-pub fn onDisconnect(self: *CDP, err: ?anyerror) void {
- if (err) |e| {
- if (WS.errorReply(e)) |close_frame| {
- self.conn.send(close_frame) catch {};
- }
- }
- log.info(.cdp, "CDP disconnect", .{ .err = err });
-}
-
pub fn sendJSON(self: *CDP, message: anytype) !void {
try self.conn.sendJSON(message, .{ .emit_null_optional_fields = false });
}
-pub fn tick(self: *CDP) !bool {
- // terminatePending means someone decided this browser must die (e.g. the
- // heap limit was reached). Nothing in the CDP path ever calls cancelTerminate
- // so the flag can't be a stale leftover here. Exit.
- if (self.browser.env.terminatePending()) {
- log.warn(.cdp, "closing connection", .{ .reason = "pending terminate" });
- // The worker thread is the sole writer of this socket, so sending the
- // close frame here can't interleave with another write.
- self.conn.send(&WS.CLOSE_GOING_AWAY) catch |err| {
- log.warn(.app, "CDP terminate close", .{ .err = err });
- };
- return false;
- }
-
- // Liveness is enforced by TCP keepalive configured in
- // Server.configureSocket; the wakeup lets V8 run or terminate.
- const wait_ms: u32 = 1000; // 1s
-
- self.pageWait(wait_ms) catch |wait_err| switch (wait_err) {
- error.NoPage => {
- // No active page yet (or a teardown is in flight). Fall
- // back to ticking the http client directly so CDP messages
- // still get dispatched.
- _ = self.browser.http_client.tick(wait_ms) catch |err| switch (err) {
- error.ClientDisconnected => return false,
- else => {
- log.err(.app, "http tick", .{ .err = err });
- return false;
- },
- };
- },
- error.ClientDisconnected => return false,
- else => return wait_err,
- };
- return true;
-}
-
-fn pageWait(self: *CDP, ms: u32) !void {
- const bc = &(self.browser_context orelse return error.NoPage);
- const page = bc.page_handle orelse return error.NoPage;
- var runner = bc.session.runner(.{});
- return runner.waitForFrameCDP(page.frame_id, ms, .done);
-}
-
// Parse-then-dispatch entry point. Used by:
// - CDP.processMessage (tests, and any other caller that hands us
// raw JSON bytes).
@@ -513,6 +403,7 @@ pub fn sendEvent(self: *CDP, method: []const u8, p: anytype, opts: SendEventOpts
pub const BrowserContext = struct {
const Node = @import("Node.zig");
const AXNode = @import("AXNode.zig");
+ const NodeRegistry = @import("../../NodeRegistry.zig");
const CapturedResponse = struct {
must_encode: bool,
@@ -584,9 +475,14 @@ pub const BrowserContext = struct {
security_origin: []const u8,
page_life_cycle_events: bool,
secure_context_type: []const u8,
- node_registry: Node.Registry,
+ node_registry: NodeRegistry,
node_search_list: Node.Search.List,
+ // Node ids a DOM.setChildNodes event was already sent for. CDP protocol
+ // bookkeeping; ids are never reused within a registry's lifetime, so
+ // entries evicted by resetFrame can linger harmlessly until reset.
+ set_child_nodes_sent: std.AutoHashMapUnmanaged(NodeRegistry.Id, void) = .empty,
+
inspector_session: *js.Inspector.Session,
isolated_worlds: std.ArrayList(*IsolatedWorld),
@@ -654,7 +550,7 @@ pub const BrowserContext = struct {
const inspector_session = browser.env.inspector.?.startSession(self);
errdefer browser.env.inspector.?.stopSession();
- var registry = Node.Registry.init(allocator);
+ var registry = NodeRegistry.init(allocator);
errdefer registry.deinit();
self.* = .{
@@ -737,6 +633,7 @@ pub const BrowserContext = struct {
self.node_registry.deinit();
self.node_search_list.deinit();
+ self.set_child_nodes_sent.deinit(self.cdp.allocator);
self.notification.deinit();
if (self.http_proxy_changed) {
@@ -755,6 +652,7 @@ pub const BrowserContext = struct {
pub fn reset(self: *BrowserContext) void {
self.node_registry.reset();
self.node_search_list.reset();
+ self.set_child_nodes_sent.clearRetainingCapacity();
}
pub fn createIsolatedWorld(self: *BrowserContext, world_name: []const u8, grant_universal_access: bool) !*IsolatedWorld {
@@ -796,7 +694,7 @@ pub const BrowserContext = struct {
return world;
}
- pub fn nodeWriter(self: *BrowserContext, root: *const Node, opts: Node.Writer.Opts) Node.Writer {
+ pub fn nodeWriter(self: *BrowserContext, root: *const NodeRegistry.Node, opts: Node.Writer.Opts) Node.Writer {
return .{
.root = root,
.depth = opts.depth,
@@ -805,7 +703,7 @@ pub const BrowserContext = struct {
};
}
- pub fn axnodeWriter(self: *BrowserContext, temp_arena: *lp.Arena, root: *const Node, opts: AXNode.Writer.Opts) !AXNode.Writer {
+ pub fn axnodeWriter(self: *BrowserContext, temp_arena: *lp.Arena, root: *const NodeRegistry.Node, opts: AXNode.Writer.Opts) !AXNode.Writer {
// Bind the writer to the frame that owns the root node. Name resolution
// (`Label.findLabelByFor` against `ownerDocument`) and visibility
// checks (`frame._style_manager`) are per-frame; getting this wrong on
@@ -1108,7 +1006,7 @@ pub const BrowserContext = struct {
return @import("domains/page.zig").javascriptDialogOpening(self, msg);
}
- fn keyFromTransfer(transfer: *const Transfer) CDP.BrowserContext.CapturedKey {
+ fn keyFromTransfer(transfer: *const HttpClient.Transfer) CDP.BrowserContext.CapturedKey {
return if (transfer.req.resource_type == .document)
.{ .kind = .loader, .id = transfer.req.loader_id }
else
@@ -1591,7 +1489,7 @@ test "cdp: disconnect latches so the worker keeps exiting" {
try testing.expectError(error.ClientDisconnected, client.tick(0));
}
-test "cdp: tick sends a close frame on pending terminate" {
+test "cdp: run sends a close frame on pending terminate" {
testing.expectLog(&.{.cdp});
var ctx = try testing.context();
@@ -1603,7 +1501,9 @@ test "cdp: tick sends a close frame on pending terminate" {
// terminating-state asserts.
defer cdp.browser.env.cancelTerminate();
- try testing.expectEqual(false, try cdp.tick());
+ // The pending terminate makes the first tick the last one, so run returns.
+ const driver: Driver = .init(.{ .cdp = cdp });
+ driver.run();
// The client should receive a close frame (code 1001, going away), not
// just an abrupt socket close.
@@ -1639,7 +1539,7 @@ test "cdp: syncRequest short-circuits after disconnect" {
.cookie_origin = "",
.resource_type = .fetch,
.notification = undefined,
- .shutdown_callback = @import("../network/HttpClient.zig").noopShutdown,
+ .shutdown_callback = HttpClient.noopShutdown,
}, null);
try testing.expectError(error.ClientDisconnected, transfer.submitSync());
}
diff --git a/src/cdp/Node.zig b/src/server/cdp/Node.zig
similarity index 69%
rename from src/cdp/Node.zig
rename to src/server/cdp/Node.zig
index 8af5f3b23..b53201ad5 100644
--- a/src/cdp/Node.zig
+++ b/src/server/cdp/Node.zig
@@ -16,117 +16,19 @@
// 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/>.
+// The CDP-specific views over the NodeRegistry: DOM.performSearch state and
+// the DOM.Node wire-format serializer.
+
const std = @import("std");
const lp = @import("lightpanda");
-const Frame = @import("../browser/Frame.zig");
-const DOMNode = @import("../browser/webapi/Node.zig");
+const Frame = @import("../../browser/Frame.zig");
+const DOMNode = @import("../../browser/webapi/Node.zig");
+const NodeRegistry = @import("../../NodeRegistry.zig");
const log = lp.log;
const Allocator = std.mem.Allocator;
-pub const Id = u32;
-
-const Node = @This();
-
-id: Id,
-dom: *DOMNode,
-set_child_nodes_event: bool,
-
-// Whenever we send a node to the client, we register it here for future lookup.
-// We maintain a node -> id and id -> node lookup.
-pub const Registry = struct {
- node_id: u32,
- allocator: Allocator,
- arena: std.heap.ArenaAllocator,
- node_pool: std.heap.MemoryPool(Node),
- lookup_by_id: std.AutoHashMapUnmanaged(Id, *Node),
- lookup_by_node: std.HashMapUnmanaged(*DOMNode, *Node, NodeContext, std.hash_map.default_max_load_percentage),
-
- pub fn init(allocator: Allocator) Registry {
- return .{
- .node_id = 1,
- .node_pool = .empty,
- .lookup_by_id = .{},
- .lookup_by_node = .{},
- .allocator = allocator,
- .arena = .init(allocator),
- };
- }
-
- pub fn deinit(self: *Registry) void {
- const allocator = self.allocator;
- self.lookup_by_id.deinit(allocator);
- self.lookup_by_node.deinit(allocator);
- self.node_pool.deinit(allocator);
- self.arena.deinit();
- }
-
- pub fn reset(self: *Registry) void {
- self.lookup_by_id.clearRetainingCapacity();
- self.lookup_by_node.clearRetainingCapacity();
- _ = self.arena.reset(.{ .retain_with_limit = 1024 });
- _ = self.node_pool.reset(self.allocator, .{ .retain_with_limit = 1024 });
- }
-
- /// Evict only the nodes owned by `frame`'s page, leaving sibling pages' node
- /// IDs valid. Must run before the page's arena is freed — attribution walks
- /// each node's live parent chain.
- pub fn resetFrame(self: *Registry, arena: Allocator, frame: *Frame) void {
- const page = frame._page;
- var doomed: std.ArrayListUnmanaged(*Node) = .empty;
- var it = self.lookup_by_id.valueIterator();
- while (it.next()) |node_ptr| {
- const node = node_ptr.*;
- if (node.dom.ownerFrame(frame)._page == page) {
- doomed.append(arena, node) catch return;
- }
- }
- for (doomed.items) |node| {
- _ = self.lookup_by_id.remove(node.id);
- _ = self.lookup_by_node.remove(node.dom);
- self.node_pool.destroy(node);
- }
- }
-
- pub fn register(self: *Registry, dom_node: *DOMNode) !*Node {
- const node_lookup_gop = try self.lookup_by_node.getOrPut(self.allocator, dom_node);
- if (node_lookup_gop.found_existing) {
- return node_lookup_gop.value_ptr.*;
- }
-
- // on error, we're probably going to abort the entire browser context
- // but, just in case, let's try to keep things tidy.
- errdefer _ = self.lookup_by_node.remove(dom_node);
-
- const node = try self.node_pool.create(self.allocator);
- errdefer self.node_pool.destroy(node);
-
- const id = self.node_id;
- self.node_id = id + 1;
-
- node.* = .{
- .id = id,
- .dom = dom_node,
- .set_child_nodes_event = false,
- };
-
- node_lookup_gop.value_ptr.* = node;
- try self.lookup_by_id.putNoClobber(self.allocator, id, node);
- return node;
- }
-};
-
-const NodeContext = struct {
- pub fn hash(_: NodeContext, dom_node: *DOMNode) u64 {
- return std.hash.Wyhash.hash(0, std.mem.asBytes(&@intFromPtr(dom_node)));
- }
-
- pub fn eql(_: NodeContext, a: *DOMNode, b: *DOMNode) bool {
- return @intFromPtr(a) == @intFromPtr(b);
- }
-};
-
// Searches are a 3 step process:
// 1 - Dom.performSearch
// 2 - Dom.getSearchResults
@@ -139,15 +41,15 @@ const NodeContext = struct {
// expect many active searches (mostly just 1), a list is fine to scan through.
pub const Search = struct {
name: []const u8,
- node_ids: []const Id,
+ node_ids: []const NodeRegistry.Id,
pub const List = struct {
search_id: u16 = 0,
- registry: *Registry,
+ registry: *NodeRegistry,
arena: std.heap.ArenaAllocator,
searches: std.ArrayList(Search) = .empty,
- pub fn init(allocator: Allocator, registry: *Registry) List {
+ pub fn init(allocator: Allocator, registry: *NodeRegistry) List {
return .{
.registry = registry,
.arena = std.heap.ArenaAllocator.init(allocator),
@@ -185,7 +87,7 @@ pub const Search = struct {
};
var registry = self.registry;
- const node_ids = try arena.alloc(Id, nodes.len);
+ const node_ids = try arena.alloc(NodeRegistry.Id, nodes.len);
for (nodes, node_ids) |node, *node_id| {
node_id.* = (try registry.register(node)).id;
}
@@ -226,8 +128,8 @@ pub const Search = struct {
pub const Writer = struct {
depth: i32,
exclude_root: bool,
- root: *const Node,
- registry: *Registry,
+ root: *const NodeRegistry.Node,
+ registry: *NodeRegistry,
pub const Opts = struct {
depth: i32 = 0,
@@ -251,7 +153,7 @@ pub const Writer = struct {
}
}
- fn toJSON(self: *const Writer, node: *const Node, depth: usize, w: anytype) !void {
+ fn toJSON(self: *const Writer, node: *const NodeRegistry.Node, depth: usize, w: anytype) !void {
try w.beginObject();
try self.writeCommon(node, false, w);
@@ -263,7 +165,7 @@ pub const Writer = struct {
try w.endObject();
}
- fn writeChildren(self: *const Writer, node: *const Node, depth: usize, w: anytype) anyerror!usize {
+ fn writeChildren(self: *const Writer, node: *const NodeRegistry.Node, depth: usize, w: anytype) anyerror!usize {
var count: usize = 0;
var it = node.dom.childrenIterator();
@@ -287,7 +189,7 @@ pub const Writer = struct {
return count;
}
- fn writeCommon(self: *const Writer, node: *const Node, include_child_count: bool, w: anytype) !void {
+ fn writeCommon(self: *const Writer, node: *const NodeRegistry.Node, include_child_count: bool, w: anytype) !void {
try w.objectField("nodeId");
try w.write(node.id);
@@ -357,76 +259,8 @@ pub const Writer = struct {
};
const testing = @import("testing.zig");
-test "cdp Node: Registry register" {
- var registry = Registry.init(testing.allocator);
- defer registry.deinit();
-
- 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", .{});
- defer page.close();
-
- const frame = page.frame().?;
- var doc = frame.window._document;
-
- {
- const dom_node = (try doc.querySelector(.wrap("#a1"), frame)).?.asNode();
- const node = try registry.register(dom_node);
- const n1b = registry.lookup_by_id.get(1).?;
- const n1c = registry.lookup_by_node.get(node.dom).?;
- try testing.expectEqual(node, n1b);
- try testing.expectEqual(node, n1c);
-
- try testing.expectEqual(1, node.id);
- try testing.expectEqual(dom_node, node.dom);
- }
-
- {
- const dom_node = (try doc.querySelector(.wrap("p"), frame)).?.asNode();
- const node = try registry.register(dom_node);
- const n1b = registry.lookup_by_id.get(2).?;
- const n1c = registry.lookup_by_node.get(node.dom).?;
- try testing.expectEqual(node, n1b);
- try testing.expectEqual(node, n1c);
-
- try testing.expectEqual(2, node.id);
- try testing.expectEqual(dom_node, node.dom);
- }
-}
-
-test "cdp Node: Registry resetFrame" {
- var registry = Registry.init(testing.allocator);
- defer registry.deinit();
-
- var arena = std.heap.ArenaAllocator.init(testing.allocator);
- defer arena.deinit();
-
- var page_a = try testing.pageTest("cdp/registry1.html", .{});
- defer page_a.close();
- var page_b = try testing.pageTest("cdp/registry2.html", .{});
- defer page_b.close();
-
- const frame_a = page_a.frame().?;
- const frame_b = page_b.frame().?;
-
- const a_node = (try frame_a.window._document.querySelector(.wrap("#a1"), frame_a)).?.asNode();
- const b_node = (try frame_b.window._document.querySelector(.wrap("a"), frame_b)).?.asNode();
- const ra = try registry.register(a_node);
- const rb = try registry.register(b_node);
- try testing.expectEqual(2, registry.lookup_by_id.count());
-
- registry.resetFrame(arena.allocator(), frame_a);
-
- try testing.expectEqual(null, registry.lookup_by_id.get(ra.id));
- try testing.expectEqual(null, registry.lookup_by_node.get(a_node));
- try testing.expectEqual(1, registry.lookup_by_id.count());
- try testing.expectEqual(rb, registry.lookup_by_id.get(rb.id).?);
- try testing.expectEqual(b_node, registry.lookup_by_node.get(b_node).?.dom);
-}
-
test "cdp Node: search list" {
- var registry = Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
var search_list = Search.List.init(testing.allocator, &registry);
@@ -492,7 +326,7 @@ test "cdp Node: search list" {
}
test "cdp Node: Writer" {
- var registry = Registry.init(testing.allocator);
+ var registry = NodeRegistry.init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/registry3.html", .{});
diff --git a/src/cdp/SafeString.zig b/src/server/cdp/SafeString.zig
similarity index 100%
rename from src/cdp/SafeString.zig
rename to src/server/cdp/SafeString.zig
diff --git a/src/cdp/domains/accessibility.zig b/src/server/cdp/domains/accessibility.zig
similarity index 100%
rename from src/cdp/domains/accessibility.zig
rename to src/server/cdp/domains/accessibility.zig
diff --git a/src/cdp/domains/audits.zig b/src/server/cdp/domains/audits.zig
similarity index 100%
rename from src/cdp/domains/audits.zig
rename to src/server/cdp/domains/audits.zig
diff --git a/src/cdp/domains/browser.zig b/src/server/cdp/domains/browser.zig
similarity index 98%
rename from src/cdp/domains/browser.zig
rename to src/server/cdp/domains/browser.zig
index c05a4a5a8..cd39bb2d8 100644
--- a/src/cdp/domains/browser.zig
+++ b/src/server/cdp/domains/browser.zig
@@ -20,11 +20,12 @@ const std = @import("std");
const lp = @import("lightpanda");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
-const Session = @import("../../browser/Session.zig");
-const Notification = @import("../../Notification.zig");
+
+const Session = @import("../../../browser/Session.zig");
+const Notification = @import("../../../Notification.zig");
+const PermissionState = @import("../../../browser/webapi/Permissions.zig").State;
const log = lp.log;
-const PermissionState = @import("../../browser/webapi/Permissions.zig").State;
// TODO: hard coded data
const PROTOCOL_VERSION = "1.3";
diff --git a/src/cdp/domains/console.zig b/src/server/cdp/domains/console.zig
similarity index 97%
rename from src/cdp/domains/console.zig
rename to src/server/cdp/domains/console.zig
index 30da94729..3dae85090 100644
--- a/src/cdp/domains/console.zig
+++ b/src/server/cdp/domains/console.zig
@@ -19,7 +19,7 @@
const std = @import("std");
const CDP = @import("../CDP.zig");
-const Notification = @import("../../Notification.zig");
+const Notification = @import("../../../Notification.zig");
const Allocator = std.mem.Allocator;
diff --git a/src/cdp/domains/css.zig b/src/server/cdp/domains/css.zig
similarity index 100%
rename from src/cdp/domains/css.zig
rename to src/server/cdp/domains/css.zig
diff --git a/src/cdp/domains/dom.zig b/src/server/cdp/domains/dom.zig
similarity index 96%
rename from src/cdp/domains/dom.zig
rename to src/server/cdp/domains/dom.zig
index 438f5a28a..238f0be56 100644
--- a/src/cdp/domains/dom.zig
+++ b/src/server/cdp/domains/dom.zig
@@ -21,18 +21,18 @@ const lp = @import("lightpanda");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
-const Node = @import("../Node.zig");
-
-const dump = @import("../../browser/dump.zig");
-const js = @import("../../browser/js/js.zig");
-const DOMNode = @import("../../browser/webapi/Node.zig");
-const Selector = @import("../../browser/webapi/selector/Selector.zig");
-const xpath = @import("../../browser/xpath/Evaluator.zig");
-const Input = @import("../../browser/webapi/element/html/Input.zig");
-const File = @import("../../browser/webapi/File.zig");
-const Blob = @import("../../browser/webapi/Blob.zig");
-const Factory = @import("../../browser/Factory.zig");
-const Page = @import("../../browser/Page.zig");
+const NodeRegistry = @import("../../../NodeRegistry.zig");
+
+const dump = @import("../../../browser/dump.zig");
+const js = @import("../../../browser/js/js.zig");
+const DOMNode = @import("../../../browser/webapi/Node.zig");
+const Selector = @import("../../../browser/webapi/selector/Selector.zig");
+const xpath = @import("../../../browser/xpath/Evaluator.zig");
+const Input = @import("../../../browser/webapi/element/html/Input.zig");
+const File = @import("../../../browser/webapi/File.zig");
+const Blob = @import("../../../browser/webapi/Blob.zig");
+const Factory = @import("../../../browser/Factory.zig");
+const Page = @import("../../../browser/Page.zig");
const log = lp.log;
const Allocator = std.mem.Allocator;
@@ -190,14 +190,14 @@ fn dispatchSetChildNodes(cmd: *CDP.Command, dom_nodes: []const *DOMNode) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const session_id = bc.session_id orelse return error.SessionIdNotLoaded;
- var parents: std.ArrayList(*Node) = .empty;
+ var parents: std.ArrayList(*NodeRegistry.Node) = .empty;
for (dom_nodes) |dom_node| {
var current = dom_node;
while (true) {
const parent_node = current._parent orelse break;
const node = try bc.node_registry.register(parent_node);
- if (node.set_child_nodes_event) {
+ if (bc.set_child_nodes_sent.contains(node.id)) {
break;
}
try parents.append(arena, node);
@@ -219,9 +219,8 @@ fn dispatchSetChildNodes(cmd: *CDP.Command, dom_nodes: []const *DOMNode) !void {
// Although our above loop won't add an already-sent node to `parents`
// this can still be true because two nodes can share the same parent node
// so we might have just sent the node a previous iteration of this loop
- if (node.set_child_nodes_event) continue;
-
- node.set_child_nodes_event = true;
+ const gop = try bc.set_child_nodes_sent.getOrPut(bc.cdp.allocator, node.id);
+ if (gop.found_existing) continue;
// If the node has no parent, it's the root node.
// We don't dispatch event for it because we assume the root node is
@@ -280,7 +279,7 @@ fn getSearchResults(cmd: *CDP.Command) !void {
fn querySelector(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
- nodeId: Node.Id,
+ nodeId: NodeRegistry.Id,
selector: []const u8,
})) orelse return error.InvalidParams;
@@ -306,7 +305,7 @@ fn querySelector(cmd: *CDP.Command) !void {
fn querySelectorAll(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
- nodeId: Node.Id,
+ nodeId: NodeRegistry.Id,
selector: []const u8,
})) orelse return error.InvalidParams;
@@ -322,7 +321,7 @@ fn querySelectorAll(cmd: *CDP.Command) !void {
const nodes = selected_nodes._nodes;
- const node_ids = try cmd.arena.alloc(Node.Id, nodes.len);
+ const node_ids = try cmd.arena.alloc(NodeRegistry.Id, nodes.len);
for (nodes, node_ids) |selected_node, *node_id| {
node_id.* = (try bc.node_registry.register(selected_node)).id;
}
@@ -337,7 +336,7 @@ fn querySelectorAll(cmd: *CDP.Command) !void {
fn resolveNode(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
- nodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
backendNodeId: ?u32 = null,
objectGroup: ?[]const u8 = null,
executionContextId: ?u32 = null,
@@ -399,8 +398,8 @@ fn resolveNode(cmd: *CDP.Command) !void {
fn describeNode(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
- nodeId: ?Node.Id = null,
- backendNodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
objectId: ?[]const u8 = null,
depth: i32 = 1,
pierce: bool = false,
@@ -446,7 +445,7 @@ fn rectToQuad(rect: DOMNode.Element.DOMRect.Data) Quad {
fn scrollIntoViewIfNeeded(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
- nodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
backendNodeId: ?u32 = null,
objectId: ?[]const u8 = null,
rect: ?DOMNode.Element.DOMRect.Data = null,
@@ -467,7 +466,7 @@ fn scrollIntoViewIfNeeded(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
-pub fn getNode(arena: Allocator, bc: *CDP.BrowserContext, node_id: ?Node.Id, backend_node_id: ?Node.Id, object_id: ?[]const u8) !*Node {
+pub fn getNode(arena: Allocator, bc: *CDP.BrowserContext, node_id: ?NodeRegistry.Id, backend_node_id: ?NodeRegistry.Id, object_id: ?[]const u8) !*NodeRegistry.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;
@@ -489,8 +488,8 @@ pub fn getNode(arena: Allocator, bc: *CDP.BrowserContext, node_id: ?Node.Id, bac
// Related to: https://drafts.csswg.org/cssom-view/#the-geometryutils-interface
fn getContentQuads(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
- nodeId: ?Node.Id = null,
- backendNodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
objectId: ?[]const u8 = null,
})) orelse return error.InvalidParams;
@@ -515,7 +514,7 @@ fn getContentQuads(cmd: *CDP.Command) !void {
fn getBoxModel(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
- nodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
backendNodeId: ?u32 = null,
objectId: ?[]const u8 = null,
})) orelse return error.InvalidParams;
@@ -545,7 +544,7 @@ fn getBoxModel(cmd: *CDP.Command) !void {
fn requestChildNodes(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
- nodeId: Node.Id,
+ nodeId: NodeRegistry.Id,
depth: i32 = 1,
pierce: bool = false,
})) orelse return error.InvalidParams;
@@ -585,8 +584,8 @@ fn getFrameOwner(cmd: *CDP.Command) !void {
fn getOuterHTML(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
- nodeId: ?Node.Id = null,
- backendNodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
objectId: ?[]const u8 = null,
includeShadowDOM: bool = false,
})) orelse return error.InvalidParams;
@@ -624,8 +623,8 @@ const MAX_FILE_BYTES: usize = 100 * 1024 * 1024;
fn setFileInputFiles(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
files: []const []const u8,
- nodeId: ?Node.Id = null,
- backendNodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
objectId: ?[]const u8 = null,
})) orelse return error.InvalidParams;
diff --git a/src/cdp/domains/emulation.zig b/src/server/cdp/domains/emulation.zig
similarity index 99%
rename from src/cdp/domains/emulation.zig
rename to src/server/cdp/domains/emulation.zig
index 820e63a57..93ae674fc 100644
--- a/src/cdp/domains/emulation.zig
+++ b/src/server/cdp/domains/emulation.zig
@@ -20,8 +20,8 @@ const std = @import("std");
const lp = @import("lightpanda");
const CDP = @import("../CDP.zig");
-const Config = @import("../../Config.zig");
-const js = @import("../../browser/js/js.zig");
+const Config = @import("../../../Config.zig");
+const js = @import("../../../browser/js/js.zig");
const log = lp.log;
diff --git a/src/cdp/domains/fetch.zig b/src/server/cdp/domains/fetch.zig
similarity index 99%
rename from src/cdp/domains/fetch.zig
rename to src/server/cdp/domains/fetch.zig
index b5a7179c1..5ada67fa5 100644
--- a/src/cdp/domains/fetch.zig
+++ b/src/server/cdp/domains/fetch.zig
@@ -19,8 +19,8 @@
const std = @import("std");
const lp = @import("lightpanda");
-const http = @import("../../network/http.zig");
-const Notification = @import("../../Notification.zig");
+const http = @import("../../../network/http.zig");
+const Notification = @import("../../../Notification.zig");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
diff --git a/src/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
similarity index 99%
rename from src/cdp/domains/input.zig
rename to src/server/cdp/domains/input.zig
index 88f808302..a71b80d51 100644
--- a/src/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -18,7 +18,7 @@
const std = @import("std");
const CDP = @import("../CDP.zig");
-const Frame = @import("../../browser/Frame.zig");
+const Frame = @import("../../../browser/Frame.zig");
const dom_button = Frame.user_input.mouse_button;
@@ -61,7 +61,7 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return;
const frame = bc.mainFrame() orelse return;
- const KeyboardEvent = @import("../../browser/webapi/event/KeyboardEvent.zig");
+ const KeyboardEvent = @import("../../../browser/webapi/event/KeyboardEvent.zig");
const keyboard_event = try KeyboardEvent.initTrusted(switch (params.type) {
.keyDown => comptime .wrap("keydown"),
.keyUp => comptime .wrap("keyup"),
diff --git a/src/cdp/domains/inspector.zig b/src/server/cdp/domains/inspector.zig
similarity index 100%
rename from src/cdp/domains/inspector.zig
rename to src/server/cdp/domains/inspector.zig
diff --git a/src/cdp/domains/io.zig b/src/server/cdp/domains/io.zig
similarity index 100%
rename from src/cdp/domains/io.zig
rename to src/server/cdp/domains/io.zig
diff --git a/src/cdp/domains/log.zig b/src/server/cdp/domains/log.zig
similarity index 100%
rename from src/cdp/domains/log.zig
rename to src/server/cdp/domains/log.zig
diff --git a/src/cdp/domains/lp.zig b/src/server/cdp/domains/lp.zig
similarity index 98%
rename from src/cdp/domains/lp.zig
rename to src/server/cdp/domains/lp.zig
index d89a55dd5..01df370d7 100644
--- a/src/cdp/domains/lp.zig
+++ b/src/server/cdp/domains/lp.zig
@@ -21,9 +21,9 @@ const lp = @import("lightpanda");
const CDP = @import("../CDP.zig");
-const Node = @import("../Node.zig");
-const DOMNode = @import("../../browser/webapi/Node.zig");
-const Robots = @import("../../network/Robots.zig");
+const Robots = @import("../../../network/Robots.zig");
+const DOMNode = @import("../../../browser/webapi/Node.zig");
+const NodeRegistry = @import("../../../NodeRegistry.zig");
const markdown = lp.markdown;
const SemanticTree = lp.SemanticTree;
@@ -118,7 +118,7 @@ fn getSemanticTree(cmd: anytype) !void {
format: ?enum { text } = null,
prune: ?bool = null,
interactiveOnly: ?bool = null,
- backendNodeId: ?Node.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
maxDepth: ?u32 = null,
};
const params = (try cmd.params(Params)) orelse Params{};
@@ -160,7 +160,7 @@ fn getSemanticTree(cmd: anytype) !void {
fn getMarkdown(cmd: anytype) !void {
const Params = struct {
- nodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
};
const params = (try cmd.params(Params)) orelse Params{};
@@ -183,7 +183,7 @@ fn getMarkdown(cmd: anytype) !void {
fn getInteractiveElements(cmd: anytype) !void {
const Params = struct {
- nodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
};
const params = (try cmd.params(Params)) orelse Params{};
@@ -205,7 +205,7 @@ fn getInteractiveElements(cmd: anytype) !void {
fn getNodeDetails(cmd: anytype) !void {
const Params = struct {
- backendNodeId: Node.Id,
+ backendNodeId: NodeRegistry.Id,
};
const params = (try cmd.params(Params)) orelse return error.InvalidParam;
@@ -276,8 +276,8 @@ fn detectForms(cmd: anytype) !void {
fn clickNode(cmd: anytype) !void {
const Params = struct {
- nodeId: ?Node.Id = null,
- backendNodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
};
const params = (try cmd.params(Params)) orelse return error.InvalidParam;
@@ -297,8 +297,8 @@ fn clickNode(cmd: anytype) !void {
fn fillNode(cmd: anytype) !void {
const Params = struct {
- nodeId: ?Node.Id = null,
- backendNodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
text: []const u8,
};
const params = (try cmd.params(Params)) orelse return error.InvalidParam;
@@ -319,8 +319,8 @@ fn fillNode(cmd: anytype) !void {
fn scrollNode(cmd: anytype) !void {
const Params = struct {
- nodeId: ?Node.Id = null,
- backendNodeId: ?Node.Id = null,
+ nodeId: ?NodeRegistry.Id = null,
+ backendNodeId: ?NodeRegistry.Id = null,
x: ?i32 = null,
y: ?i32 = null,
};
diff --git a/src/cdp/domains/network.zig b/src/server/cdp/domains/network.zig
similarity index 99%
rename from src/cdp/domains/network.zig
rename to src/server/cdp/domains/network.zig
index 113e6de0e..4f26ec077 100644
--- a/src/cdp/domains/network.zig
+++ b/src/server/cdp/domains/network.zig
@@ -23,15 +23,15 @@ const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const SafeString = @import("../SafeString.zig");
-const Config = @import("../../Config.zig");
-const URL = @import("../../browser/URL.zig");
-const Mime = @import("../../browser/Mime.zig");
-const Notification = @import("../../Notification.zig");
+const Config = @import("../../../Config.zig");
+const URL = @import("../../../browser/URL.zig");
+const Mime = @import("../../../browser/Mime.zig");
+const Notification = @import("../../../Notification.zig");
-const HttpClient = @import("../../network/HttpClient.zig");
-const Cache = @import("../../network/cache/Cache.zig");
-const Headers = @import("../../network/HttpClient.zig").Headers;
-const Transfer = @import("../../network/HttpClient.zig").Transfer;
+const HttpClient = @import("../../../network/HttpClient.zig");
+const Cache = @import("../../../network/cache/Cache.zig");
+const Headers = @import("../../../network/HttpClient.zig").Headers;
+const Transfer = @import("../../../network/HttpClient.zig").Transfer;
const CdpStorage = @import("storage.zig");
@@ -208,7 +208,7 @@ fn setExtraHTTPHeaders(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
-const Cookie = @import("../../browser/webapi/storage/storage.zig").Cookie;
+const Cookie = @import("../../../browser/webapi/storage/storage.zig").Cookie;
// Only matches the cookie on provided parameters
fn cookieMatches(cookie: *const Cookie, name: []const u8, domain: ?[]const u8, path: ?[]const u8) bool {
diff --git a/src/cdp/domains/page.zig b/src/server/cdp/domains/page.zig
similarity index 99%
rename from src/cdp/domains/page.zig
rename to src/server/cdp/domains/page.zig
index 248bc2972..4f44c731b 100644
--- a/src/cdp/domains/page.zig
+++ b/src/server/cdp/domains/page.zig
@@ -23,10 +23,10 @@ const lp = @import("lightpanda");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
-const js = @import("../../browser/js/js.zig");
-const URL = @import("../../browser/URL.zig");
-const Frame = @import("../../browser/Frame.zig");
-const Notification = @import("../../Notification.zig");
+const js = @import("../../../browser/js/js.zig");
+const URL = @import("../../../browser/URL.zig");
+const Frame = @import("../../../browser/Frame.zig");
+const Notification = @import("../../../Notification.zig");
const log = lp.log;
const Allocator = std.mem.Allocator;
diff --git a/src/cdp/domains/performance.zig b/src/server/cdp/domains/performance.zig
similarity index 100%
rename from src/cdp/domains/performance.zig
rename to src/server/cdp/domains/performance.zig
diff --git a/src/cdp/domains/runtime.zig b/src/server/cdp/domains/runtime.zig
similarity index 98%
rename from src/cdp/domains/runtime.zig
rename to src/server/cdp/domains/runtime.zig
index 663309313..15570bbcb 100644
--- a/src/cdp/domains/runtime.zig
+++ b/src/server/cdp/domains/runtime.zig
@@ -19,9 +19,9 @@
const std = @import("std");
const lp = @import("lightpanda");
-const js = @import("../../browser/js/js.zig");
const CDP = @import("../CDP.zig");
-const Notification = @import("../../Notification.zig");
+const js = @import("../../../browser/js/js.zig");
+const Notification = @import("../../../Notification.zig");
const Allocator = std.mem.Allocator;
diff --git a/src/cdp/domains/security.zig b/src/server/cdp/domains/security.zig
similarity index 100%
rename from src/cdp/domains/security.zig
rename to src/server/cdp/domains/security.zig
diff --git a/src/cdp/domains/storage.zig b/src/server/cdp/domains/storage.zig
similarity index 98%
rename from src/cdp/domains/storage.zig
rename to src/server/cdp/domains/storage.zig
index c21faa3bd..5cbe674ee 100644
--- a/src/cdp/domains/storage.zig
+++ b/src/server/cdp/domains/storage.zig
@@ -20,8 +20,8 @@ const std = @import("std");
const lp = @import("lightpanda");
const CDP = @import("../CDP.zig");
-const URL = @import("../../browser/URL.zig");
-const Cookie = @import("../../browser/webapi/storage/storage.zig").Cookie;
+const URL = @import("../../../browser/URL.zig");
+const Cookie = @import("../../../browser/webapi/storage/storage.zig").Cookie;
const log = lp.log;
const CookieJar = Cookie.Jar;
diff --git a/src/cdp/domains/target.zig b/src/server/cdp/domains/target.zig
similarity index 99%
rename from src/cdp/domains/target.zig
rename to src/server/cdp/domains/target.zig
index e764e9c7f..11470a5d2 100644
--- a/src/cdp/domains/target.zig
+++ b/src/server/cdp/domains/target.zig
@@ -22,8 +22,8 @@ const lp = @import("lightpanda");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
-const URL = @import("../../browser/URL.zig");
-const js = @import("../../browser/js/js.zig");
+const URL = @import("../../../browser/URL.zig");
+const js = @import("../../../browser/js/js.zig");
const log = lp.log;
diff --git a/src/cdp/domains/webmcp.zig b/src/server/cdp/domains/webmcp.zig
similarity index 98%
rename from src/cdp/domains/webmcp.zig
rename to src/server/cdp/domains/webmcp.zig
index 5995648c0..6a233ec1c 100644
--- a/src/cdp/domains/webmcp.zig
+++ b/src/server/cdp/domains/webmcp.zig
@@ -24,12 +24,12 @@ const lp = @import("lightpanda");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
-const ModelContext = @import("../../browser/webapi/ModelContext.zig");
-const Notification = @import("../../Notification.zig");
-const js = @import("../../browser/js/js.zig");
-const ModelContextClient = ModelContext.ModelContextClient;
+const js = @import("../../../browser/js/js.zig");
+const Notification = @import("../../../Notification.zig");
+const ModelContext = @import("../../../browser/webapi/ModelContext.zig");
const log = lp.log;
+const ModelContextClient = ModelContext.ModelContextClient;
pub const Invocation = struct {
id: u32,
@@ -345,7 +345,7 @@ test "cdp.WebMCP: register fires toolsAdded after enable" {
}, .{ .session_id = "SID-M" });
// Register a fresh tool from JS, expect a new toolsAdded event.
- var ls: @import("../../browser/js/js.zig").Local.Scope = undefined;
+ var ls: js.Local.Scope = undefined;
bc.mainFrame().?.js.localScope(&ls);
defer ls.deinit();
_ = try ls.local.exec(
@@ -446,7 +446,7 @@ test "cdp.WebMCP: cancelInvocation" {
try ctx.expectSentEvent("WebMCP.toolsAdded", null, .{ .session_id = "SID-M" });
// Register a never-settling tool so we have an invocation to cancel.
- var ls: @import("../../browser/js/js.zig").Local.Scope = undefined;
+ var ls: js.Local.Scope = undefined;
bc.mainFrame().?.js.localScope(&ls);
defer ls.deinit();
_ = try ls.local.exec(
diff --git a/src/cdp/id.zig b/src/server/cdp/id.zig
similarity index 98%
rename from src/cdp/id.zig
rename to src/server/cdp/id.zig
index 52a2b5403..2a75f448c 100644
--- a/src/cdp/id.zig
+++ b/src/server/cdp/id.zig
@@ -40,7 +40,7 @@ pub fn toLoaderId(id: u32) [14]u8 {
// requestId has special requirements. If it's the main document navigation,
// then it should match the loader id.
-const Transfer = @import("../network/HttpClient.zig").Transfer;
+const Transfer = @import("../../network/HttpClient.zig").Transfer;
pub fn toRequestId(transfer: *const Transfer) [14]u8 {
if (transfer.req.resource_type == .document) {
return toLoaderId(transfer.req.loader_id);
@@ -131,7 +131,7 @@ pub fn Incrementing(comptime T: type, comptime prefix: []const u8) type {
};
}
-const testing = @import("../testing.zig");
+const testing = @import("../../testing.zig");
test "id: Incrementing.next" {
var id = Incrementing(u16, "IDX"){};
try testing.expectEqual("IDX-1", id.next());
diff --git a/src/cdp/testing.zig b/src/server/cdp/testing.zig
similarity index 98%
rename from src/cdp/testing.zig
rename to src/server/cdp/testing.zig
index 3409ec293..f8cb9822d 100644
--- a/src/cdp/testing.zig
+++ b/src/server/cdp/testing.zig
@@ -20,7 +20,7 @@ const std = @import("std");
const CDP = @import("CDP.zig");
-pub const base = @import("../testing.zig");
+pub const base = @import("../../testing.zig");
const json = std.json;
const posix = std.posix;
@@ -57,7 +57,7 @@ pub const TestContext = struct {
pub fn cdp(self: *TestContext) *CDP {
if (!self.cdp_initialized) {
- self.cdp_.init(base.test_app, self.cdp_socket, "json-version") catch |err| @panic(@errorName(err));
+ self.cdp_.init(base.test_app, self.cdp_socket) catch |err| @panic(@errorName(err));
self.cdp_initialized = true;
}
return &self.cdp_;
diff --git a/src/testing.zig b/src/testing.zig
index b5c1bc136..8df8e8b4c 100644
--- a/src/testing.zig
+++ b/src/testing.zig
@@ -507,10 +507,10 @@ pub fn pageTest(comptime test_file: []const u8, opts: PageTestOpts) !Session.Pag
return page;
}
-const TestHTTPServer = @import("TestHTTPServer.zig");
+const Server = @import("server/Server.zig");
const TestWSServer = @import("TestWSServer.zig");
+const TestHTTPServer = @import("TestHTTPServer.zig");
-const Server = @import("Server.zig");
var test_cdp_server: ?*Server = null;
var test_cdp_server_thread: ?std.Thread = null;
var test_http_server: ?TestHTTPServer = null;