mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-30 17:18:57 +00:00
Compare commits
1 Commits
remove_cdp
...
build-chec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3aeba97fc9 |
2
.github/workflows/nightly.yml
vendored
2
.github/workflows/nightly.yml
vendored
@@ -7,7 +7,7 @@ env:
|
||||
AWS_REGION: ${{ vars.NIGHTLY_BUILD_AWS_REGION }}
|
||||
|
||||
RELEASE: ${{ github.ref_type == 'tag' && github.ref_name || 'nightly' }}
|
||||
VERSION_FLAG: ${{ github.ref_type == 'tag' && format('-Dversion={0}', github.ref_name) || '-Dversion=nightly' }}
|
||||
VERSION_FLAG: ${{ github.ref_type == 'tag' && format('-Dversion_string={0}', github.ref_name) || format('-Dpre_version={0}', 'nightly') }}
|
||||
|
||||
on:
|
||||
push:
|
||||
|
||||
@@ -170,7 +170,6 @@ You may still encounter errors or crashes. Please open an issue with specifics i
|
||||
|
||||
Here are the key features we have implemented:
|
||||
|
||||
- [ ] CORS [#2015](https://github.com/lightpanda-io/browser/issues/2015)
|
||||
- [x] HTTP loader ([Libcurl](https://curl.se/libcurl/))
|
||||
- [x] HTML parser ([html5ever](https://github.com/servo/html5ever))
|
||||
- [x] DOM tree
|
||||
|
||||
79
build.zig
79
build.zig
@@ -85,6 +85,15 @@ pub fn build(b: *Build) !void {
|
||||
break :blk mod;
|
||||
};
|
||||
|
||||
// Check compilation
|
||||
const check = b.step("check", "Check if lightpanda compiles");
|
||||
|
||||
const check_lib = b.addLibrary(.{
|
||||
.name = "lightpanda_check",
|
||||
.root_module = lightpanda_module,
|
||||
});
|
||||
check.dependOn(&check_lib.step);
|
||||
|
||||
{
|
||||
// browser
|
||||
const exe = b.addExecutable(.{
|
||||
@@ -103,6 +112,12 @@ pub fn build(b: *Build) !void {
|
||||
});
|
||||
b.installArtifact(exe);
|
||||
|
||||
const exe_check = b.addLibrary(.{
|
||||
.name = "lightpanda_exe_check",
|
||||
.root_module = exe.root_module,
|
||||
});
|
||||
check.dependOn(&exe_check.step);
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
@@ -132,6 +147,12 @@ pub fn build(b: *Build) !void {
|
||||
});
|
||||
b.installArtifact(exe);
|
||||
|
||||
const exe_check = b.addLibrary(.{
|
||||
.name = "snapshot_creator_check",
|
||||
.root_module = exe.root_module,
|
||||
});
|
||||
check.dependOn(&exe_check.step);
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
@@ -170,6 +191,12 @@ pub fn build(b: *Build) !void {
|
||||
});
|
||||
b.installArtifact(exe);
|
||||
|
||||
const exe_check = b.addLibrary(.{
|
||||
.name = "legacy_test_check",
|
||||
.root_module = exe.root_module,
|
||||
});
|
||||
check.dependOn(&exe_check.step);
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
@@ -719,45 +746,39 @@ fn buildCurl(
|
||||
return lib;
|
||||
}
|
||||
|
||||
/// Resolves the semantic version of the build.
|
||||
///
|
||||
/// The base version is read from `build.zig.zon`. This can be overridden
|
||||
/// using the `-Dversion` command-line flag:
|
||||
/// - If the flag contains a full semantic version (e.g., `1.2.3`), it replaces
|
||||
/// the base version entirely.
|
||||
/// - If the flag contains a simple string (e.g., `nightly`), it replaces only
|
||||
/// the pre-release tag of the base version (e.g., `1.0.0-dev` -> `1.0.0-nightly`).
|
||||
///
|
||||
/// For versions that have a pre-release tag and no explicit build metadata,
|
||||
/// this function automatically enriches the version with the git commit count
|
||||
/// and short hash (e.g., `1.0.0-dev.5243+dbe45229`).
|
||||
/// Returns `MAJOR.MINOR.PATCH-dev` when `git describe` fails.
|
||||
fn resolveVersion(b: *std.Build) std.SemanticVersion {
|
||||
const opt_version = b.option([]const u8, "version", "Override the version of this build");
|
||||
|
||||
const version = if (opt_version) |v|
|
||||
std.SemanticVersion.parse(v) catch blk: {
|
||||
var fallback = lightpanda_version;
|
||||
fallback.pre = v;
|
||||
break :blk fallback;
|
||||
const version_string = b.option([]const u8, "version_string", "Override the version of this build");
|
||||
if (version_string) |semver_string| {
|
||||
return std.SemanticVersion.parse(semver_string) catch |err| {
|
||||
std.debug.panic("Expected -Dversion-string={s} to be a semantic version: {}", .{ semver_string, err });
|
||||
};
|
||||
}
|
||||
else
|
||||
lightpanda_version;
|
||||
|
||||
// Only enrich versions that have a pre-release field and no explicit build metadata.
|
||||
if (version.pre == null or version.build != null) return version;
|
||||
const pre_version = b.option([]const u8, "pre_version", "Override the pre version of this build");
|
||||
const pre = blk: {
|
||||
if (pre_version) |pre| {
|
||||
break :blk pre;
|
||||
}
|
||||
|
||||
break :blk lightpanda_version.pre;
|
||||
};
|
||||
|
||||
// If it's a stable release (no pre or build metadata in build.zig.zon), use it as is
|
||||
if (pre == null and lightpanda_version.build == null) return lightpanda_version;
|
||||
|
||||
// For dev/nightly versions, calculate the commit count and hash
|
||||
const git_hash_raw = runGit(b, &.{ "rev-parse", "--short", "HEAD" }) catch return version;
|
||||
const git_hash_raw = runGit(b, &.{ "rev-parse", "--short", "HEAD" }) catch return lightpanda_version;
|
||||
const commit_hash = std.mem.trim(u8, git_hash_raw, " \n\r");
|
||||
|
||||
const git_count_raw = runGit(b, &.{ "rev-list", "--count", "HEAD" }) catch return version;
|
||||
const git_count_raw = runGit(b, &.{ "rev-list", "--count", "HEAD" }) catch return lightpanda_version;
|
||||
const commit_count = std.mem.trim(u8, git_count_raw, " \n\r");
|
||||
|
||||
return .{
|
||||
.major = version.major,
|
||||
.minor = version.minor,
|
||||
.patch = version.patch,
|
||||
.pre = b.fmt("{s}.{s}", .{ version.pre.?, commit_count }),
|
||||
.major = lightpanda_version.major,
|
||||
.minor = lightpanda_version.minor,
|
||||
.patch = lightpanda_version.patch,
|
||||
.pre = b.fmt("{s}.{s}", .{ pre.?, commit_count }),
|
||||
.build = commit_hash,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const log = @import("log.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArenaAllocator = std.heap.ArenaAllocator;
|
||||
@@ -63,7 +62,7 @@ pub fn deinit(self: *ArenaPool) void {
|
||||
var it = self._leak_track.iterator();
|
||||
while (it.next()) |kv| {
|
||||
if (kv.value_ptr.* != 0) {
|
||||
log.err(.bug, "ArenaPool leak", .{ .name = kv.key_ptr.*, .count = kv.value_ptr.* });
|
||||
std.debug.print("ArenaPool leak detected: '{s}' count={d}\n", .{ kv.key_ptr.*, kv.value_ptr.* });
|
||||
has_leaks = true;
|
||||
}
|
||||
}
|
||||
@@ -130,11 +129,11 @@ pub fn release(self: *ArenaPool, allocator: Allocator) void {
|
||||
if (self._leak_track.getPtr(entry.debug)) |count| {
|
||||
count.* -= 1;
|
||||
if (count.* < 0) {
|
||||
log.err(.bug, "ArenaPool double-free", .{ .name = entry.debug });
|
||||
std.debug.print("ArenaPool double-free detected: '{s}'\n", .{entry.debug});
|
||||
@panic("ArenaPool: double-free detected");
|
||||
}
|
||||
} else {
|
||||
log.err(.bug, "ArenaPool release unknown", .{ .name = entry.debug });
|
||||
std.debug.print("ArenaPool release of untracked arena: '{s}'\n", .{entry.debug});
|
||||
@panic("ArenaPool: release of untracked arena");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ const net = std.net;
|
||||
const posix = std.posix;
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArenaAllocator = std.heap.ArenaAllocator;
|
||||
|
||||
const log = @import("log.zig");
|
||||
const App = @import("App.zig");
|
||||
|
||||
@@ -19,13 +19,17 @@
|
||||
const std = @import("std");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArenaAllocator = std.heap.ArenaAllocator;
|
||||
|
||||
const js = @import("js/js.zig");
|
||||
const log = @import("../log.zig");
|
||||
const App = @import("../App.zig");
|
||||
const HttpClient = @import("HttpClient.zig");
|
||||
|
||||
const ArenaPool = App.ArenaPool;
|
||||
|
||||
const IS_DEBUG = @import("builtin").mode == .Debug;
|
||||
|
||||
const Session = @import("Session.zig");
|
||||
const Notification = @import("../Notification.zig");
|
||||
|
||||
|
||||
@@ -425,7 +425,7 @@ fn dispatchNode(self: *EventManager, target: *Node, event: *Event, comptime opts
|
||||
ls.deinit();
|
||||
}
|
||||
|
||||
const activation_state = try ActivationState.create(event, target, page);
|
||||
const activation_state = ActivationState.create(event, target, page);
|
||||
|
||||
// Defer runs even on early return - ensures event phase is reset
|
||||
// and default actions execute (unless prevented)
|
||||
@@ -820,7 +820,7 @@ const ActivationState = struct {
|
||||
|
||||
const Input = Element.Html.Input;
|
||||
|
||||
fn create(event: *const Event, target: *Node, page: *Page) !?ActivationState {
|
||||
fn create(event: *const Event, target: *Node, page: *Page) ?ActivationState {
|
||||
if (event._type_string.eql(comptime .wrap("click")) == false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -27,6 +27,7 @@ const IS_DEBUG = builtin.mode == .Debug;
|
||||
|
||||
const log = @import("../log.zig");
|
||||
|
||||
const App = @import("../App.zig");
|
||||
const String = @import("../string.zig").String;
|
||||
|
||||
const Mime = @import("Mime.zig");
|
||||
@@ -42,6 +43,7 @@ const URL = @import("URL.zig");
|
||||
const Blob = @import("webapi/Blob.zig");
|
||||
const Node = @import("webapi/Node.zig");
|
||||
const Event = @import("webapi/Event.zig");
|
||||
const EventTarget = @import("webapi/EventTarget.zig");
|
||||
const CData = @import("webapi/CData.zig");
|
||||
const Element = @import("webapi/Element.zig");
|
||||
const HtmlElement = @import("webapi/element/Html.zig");
|
||||
@@ -57,6 +59,7 @@ const AbstractRange = @import("webapi/AbstractRange.zig");
|
||||
const MutationObserver = @import("webapi/MutationObserver.zig");
|
||||
const IntersectionObserver = @import("webapi/IntersectionObserver.zig");
|
||||
const CustomElementDefinition = @import("webapi/CustomElementDefinition.zig");
|
||||
const storage = @import("webapi/storage/storage.zig");
|
||||
const PageTransitionEvent = @import("webapi/event/PageTransitionEvent.zig");
|
||||
const SubmitEvent = @import("webapi/event/SubmitEvent.zig");
|
||||
const NavigationKind = @import("webapi/navigation/root.zig").NavigationKind;
|
||||
@@ -64,6 +67,7 @@ const KeyboardEvent = @import("webapi/event/KeyboardEvent.zig");
|
||||
const MouseEvent = @import("webapi/event/MouseEvent.zig");
|
||||
|
||||
const HttpClient = @import("HttpClient.zig");
|
||||
const ArenaPool = App.ArenaPool;
|
||||
|
||||
const timestamp = @import("../datetime.zig").timestamp;
|
||||
const milliTimestamp = @import("../datetime.zig").milliTimestamp;
|
||||
@@ -381,9 +385,12 @@ pub fn getTitle(self: *Page) !?[]const u8 {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Add common headers for a request:
|
||||
// Add comon headers for a request:
|
||||
// * cookies
|
||||
// * referer
|
||||
pub fn headersForRequest(self: *Page, headers: *HttpClient.Headers) !void {
|
||||
pub fn headersForRequest(self: *Page, temp: Allocator, url: [:0]const u8, headers: *HttpClient.Headers) !void {
|
||||
try self.requestCookie(.{}).headersForRequest(temp, url, headers);
|
||||
|
||||
// Build the referer
|
||||
const referer = blk: {
|
||||
if (self.referer_header == null) {
|
||||
@@ -538,6 +545,8 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
|
||||
if (opts.header) |hdr| {
|
||||
try headers.add(hdr);
|
||||
}
|
||||
try self.requestCookie(.{ .is_navigation = true }).headersForRequest(self.arena, self.url, &headers);
|
||||
|
||||
// We dispatch page_navigate event before sending the request.
|
||||
// It ensures the event page_navigated is not dispatched before this one.
|
||||
session.notification.dispatch(.page_navigate, &.{
|
||||
@@ -564,7 +573,6 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
|
||||
.headers = headers,
|
||||
.body = opts.body,
|
||||
.cookie_jar = &session.cookie_jar,
|
||||
.cookie_origin = self.url,
|
||||
.resource_type = .document,
|
||||
.notification = self._session.notification,
|
||||
.header_callback = pageHeaderDoneCallback,
|
||||
@@ -1028,7 +1036,6 @@ fn pageDoneCallback(ctx: *anyopaque) !void {
|
||||
});
|
||||
|
||||
parser.parse(html);
|
||||
self._parse_state = .complete;
|
||||
self.documentIsComplete();
|
||||
},
|
||||
else => unreachable,
|
||||
@@ -3547,6 +3554,19 @@ pub fn insertText(self: *Page, v: []const u8) !void {
|
||||
}
|
||||
}
|
||||
|
||||
const RequestCookieOpts = struct {
|
||||
is_http: bool = true,
|
||||
is_navigation: bool = false,
|
||||
};
|
||||
pub fn requestCookie(self: *const Page, opts: RequestCookieOpts) HttpClient.RequestCookie {
|
||||
return .{
|
||||
.jar = &self._session.cookie_jar,
|
||||
.origin = self.url,
|
||||
.is_http = opts.is_http,
|
||||
.is_navigation = opts.is_navigation,
|
||||
};
|
||||
}
|
||||
|
||||
fn asUint(comptime string: anytype) std.meta.Int(
|
||||
.unsigned,
|
||||
@bitSizeOf(@TypeOf(string.*)) - 8, // (- 8) to exclude sentinel 0
|
||||
|
||||
@@ -21,9 +21,12 @@ const lp = @import("lightpanda");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const log = @import("../log.zig");
|
||||
const App = @import("../App.zig");
|
||||
|
||||
const Page = @import("Page.zig");
|
||||
const Session = @import("Session.zig");
|
||||
const Browser = @import("Browser.zig");
|
||||
const Factory = @import("Factory.zig");
|
||||
const HttpClient = @import("HttpClient.zig");
|
||||
|
||||
const IS_DEBUG = builtin.mode == .Debug;
|
||||
|
||||
@@ -28,10 +28,12 @@ const String = @import("../string.zig").String;
|
||||
const js = @import("js/js.zig");
|
||||
const URL = @import("URL.zig");
|
||||
const Page = @import("Page.zig");
|
||||
const Browser = @import("Browser.zig");
|
||||
|
||||
const Element = @import("webapi/Element.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArrayList = std.ArrayList;
|
||||
|
||||
const IS_DEBUG = builtin.mode == .Debug;
|
||||
|
||||
@@ -136,9 +138,9 @@ fn clearList(list: *std.DoublyLinkedList) void {
|
||||
}
|
||||
}
|
||||
|
||||
fn getHeaders(self: *ScriptManager) !net_http.Headers {
|
||||
fn getHeaders(self: *ScriptManager, arena: Allocator, url: [:0]const u8) !net_http.Headers {
|
||||
var headers = try self.client.newHeaders();
|
||||
try self.page.headersForRequest(&headers);
|
||||
try self.page.headersForRequest(arena, url, &headers);
|
||||
return headers;
|
||||
}
|
||||
|
||||
@@ -278,10 +280,9 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
|
||||
.ctx = script,
|
||||
.method = .GET,
|
||||
.frame_id = page._frame_id,
|
||||
.headers = try self.getHeaders(),
|
||||
.headers = try self.getHeaders(arena, url),
|
||||
.blocking = is_blocking,
|
||||
.cookie_jar = &page._session.cookie_jar,
|
||||
.cookie_origin = page.url,
|
||||
.resource_type = .script,
|
||||
.notification = page._session.notification,
|
||||
.start_callback = if (log.enabled(.http, .debug)) Script.startCallback else null,
|
||||
@@ -404,9 +405,8 @@ pub fn preloadImport(self: *ScriptManager, url: [:0]const u8, referrer: []const
|
||||
.ctx = script,
|
||||
.method = .GET,
|
||||
.frame_id = page._frame_id,
|
||||
.headers = try self.getHeaders(),
|
||||
.headers = try self.getHeaders(arena, url),
|
||||
.cookie_jar = &page._session.cookie_jar,
|
||||
.cookie_origin = page.url,
|
||||
.resource_type = .script,
|
||||
.notification = page._session.notification,
|
||||
.start_callback = if (log.enabled(.http, .debug)) Script.startCallback else null,
|
||||
@@ -508,11 +508,10 @@ pub fn getAsyncImport(self: *ScriptManager, url: [:0]const u8, cb: ImportAsync.C
|
||||
.url = url,
|
||||
.method = .GET,
|
||||
.frame_id = page._frame_id,
|
||||
.headers = try self.getHeaders(),
|
||||
.headers = try self.getHeaders(arena, url),
|
||||
.ctx = script,
|
||||
.resource_type = .script,
|
||||
.cookie_jar = &page._session.cookie_jar,
|
||||
.cookie_origin = page.url,
|
||||
.notification = page._session.notification,
|
||||
.start_callback = if (log.enabled(.http, .debug)) Script.startCallback else null,
|
||||
.header_callback = Script.headerCallback,
|
||||
@@ -655,6 +654,7 @@ pub const Script = struct {
|
||||
debug_transfer_aborted: bool = false,
|
||||
debug_transfer_bytes_received: usize = 0,
|
||||
debug_transfer_notified_fail: bool = false,
|
||||
debug_transfer_redirecting: bool = false,
|
||||
debug_transfer_intercept_state: u8 = 0,
|
||||
debug_transfer_auth_challenge: bool = false,
|
||||
debug_transfer_easy_id: usize = 0,
|
||||
@@ -730,6 +730,7 @@ pub const Script = struct {
|
||||
.a3 = self.debug_transfer_aborted,
|
||||
.a4 = self.debug_transfer_bytes_received,
|
||||
.a5 = self.debug_transfer_notified_fail,
|
||||
.a6 = self.debug_transfer_redirecting,
|
||||
.a7 = self.debug_transfer_intercept_state,
|
||||
.a8 = self.debug_transfer_auth_challenge,
|
||||
.a9 = self.debug_transfer_easy_id,
|
||||
@@ -738,9 +739,10 @@ pub const Script = struct {
|
||||
.b3 = transfer.aborted,
|
||||
.b4 = transfer.bytes_received,
|
||||
.b5 = transfer._notified_fail,
|
||||
.b6 = transfer._redirecting,
|
||||
.b7 = @intFromEnum(transfer._intercept_state),
|
||||
.b8 = transfer._auth_challenge != null,
|
||||
.b9 = if (transfer._conn) |c| @intFromPtr(c._easy) else 0,
|
||||
.b9 = if (transfer._conn) |c| @intFromPtr(c.easy) else 0,
|
||||
});
|
||||
self.header_callback_called = true;
|
||||
self.debug_transfer_id = transfer.id;
|
||||
@@ -748,9 +750,10 @@ pub const Script = struct {
|
||||
self.debug_transfer_aborted = transfer.aborted;
|
||||
self.debug_transfer_bytes_received = transfer.bytes_received;
|
||||
self.debug_transfer_notified_fail = transfer._notified_fail;
|
||||
self.debug_transfer_redirecting = transfer._redirecting;
|
||||
self.debug_transfer_intercept_state = @intFromEnum(transfer._intercept_state);
|
||||
self.debug_transfer_auth_challenge = transfer._auth_challenge != null;
|
||||
self.debug_transfer_easy_id = if (transfer._conn) |c| @intFromPtr(c._easy) else 0;
|
||||
self.debug_transfer_easy_id = if (transfer._conn) |c| @intFromPtr(c.easy) else 0;
|
||||
}
|
||||
|
||||
lp.assert(self.source.remote.capacity == 0, "ScriptManager.Header buffer", .{ .capacity = self.source.remote.capacity });
|
||||
|
||||
@@ -128,7 +128,7 @@ fn _constructor(self: *Caller, func: anytype, info: FunctionCallbackInfo) !void
|
||||
const new_this_handle = info.getThis();
|
||||
var this = js.Object{ .local = local, .handle = new_this_handle };
|
||||
if (@typeInfo(ReturnType) == .error_union) {
|
||||
const non_error_res = try res;
|
||||
const non_error_res = res catch |err| return err;
|
||||
this = try local.mapZigInstanceToJs(new_this_handle, non_error_res);
|
||||
} else {
|
||||
this = try local.mapZigInstanceToJs(new_this_handle, res);
|
||||
|
||||
@@ -22,6 +22,7 @@ const log = @import("../../log.zig");
|
||||
|
||||
const js = @import("js.zig");
|
||||
const Env = @import("Env.zig");
|
||||
const bridge = @import("bridge.zig");
|
||||
const Origin = @import("Origin.zig");
|
||||
const Scheduler = @import("Scheduler.zig");
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ const App = @import("../../App.zig");
|
||||
const log = @import("../../log.zig");
|
||||
|
||||
const bridge = @import("bridge.zig");
|
||||
const Origin = @import("Origin.zig");
|
||||
const Context = @import("Context.zig");
|
||||
const Isolate = @import("Isolate.zig");
|
||||
const Platform = @import("Platform.zig");
|
||||
@@ -33,6 +34,7 @@ const Snapshot = @import("Snapshot.zig");
|
||||
const Inspector = @import("Inspector.zig");
|
||||
|
||||
const Page = @import("../Page.zig");
|
||||
const Session = @import("../Session.zig");
|
||||
const Window = @import("../webapi/Window.zig");
|
||||
|
||||
const JsApis = bridge.JsApis;
|
||||
|
||||
@@ -21,6 +21,7 @@ const js = @import("js.zig");
|
||||
const v8 = js.v8;
|
||||
|
||||
const log = @import("../../log.zig");
|
||||
const Session = @import("../Session.zig");
|
||||
|
||||
const Function = @This();
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ const js = @import("js.zig");
|
||||
const Session = @import("../Session.zig");
|
||||
|
||||
const v8 = js.v8;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const Identity = @This();
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const Page = @import("../Page.zig");
|
||||
const Session = @import("../Session.zig");
|
||||
const log = @import("../../log.zig");
|
||||
const string = @import("../../string.zig");
|
||||
@@ -32,6 +33,7 @@ const IS_DEBUG = @import("builtin").mode == .Debug;
|
||||
|
||||
const v8 = js.v8;
|
||||
const CallOpts = Caller.CallOpts;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
// Where js.Context has a lifetime tied to the page, and holds the
|
||||
// v8::Global<v8::Context>, this has a much shorter lifetime and holds a
|
||||
|
||||
@@ -20,6 +20,8 @@ const std = @import("std");
|
||||
const js = @import("js.zig");
|
||||
const v8 = js.v8;
|
||||
|
||||
const Session = @import("../Session.zig");
|
||||
|
||||
const Promise = @This();
|
||||
|
||||
local: *const js.Local,
|
||||
|
||||
@@ -25,6 +25,7 @@ const IS_DEBUG = @import("builtin").mode == .Debug;
|
||||
|
||||
const v8 = js.v8;
|
||||
const JsApis = bridge.JsApis;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const Snapshot = @This();
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ const v8 = js.v8;
|
||||
const IS_DEBUG = @import("builtin").mode == .Debug;
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Session = @import("../Session.zig");
|
||||
|
||||
const Value = @This();
|
||||
|
||||
|
||||
@@ -18,12 +18,15 @@
|
||||
|
||||
const std = @import("std");
|
||||
const js = @import("js.zig");
|
||||
const lp = @import("lightpanda");
|
||||
const log = @import("../../log.zig");
|
||||
const Page = @import("../Page.zig");
|
||||
const Session = @import("../Session.zig");
|
||||
|
||||
const v8 = js.v8;
|
||||
|
||||
const Caller = @import("Caller.zig");
|
||||
const Context = @import("Context.zig");
|
||||
|
||||
const IS_DEBUG = @import("builtin").mode == .Debug;
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ const std = @import("std");
|
||||
const Page = @import("Page.zig");
|
||||
const URL = @import("URL.zig");
|
||||
const TreeWalker = @import("webapi/TreeWalker.zig");
|
||||
const CData = @import("webapi/CData.zig");
|
||||
const Element = @import("webapi/Element.zig");
|
||||
const Node = @import("webapi/Node.zig");
|
||||
const isAllWhitespace = @import("../string.zig").isAllWhitespace;
|
||||
|
||||
@@ -15,9 +15,8 @@
|
||||
testing.expectEqual(true, validPlatforms.includes(navigator.platform));
|
||||
testing.expectEqual('en-US', navigator.language);
|
||||
testing.expectEqual(true, Array.isArray(navigator.languages));
|
||||
testing.expectEqual(2, navigator.languages.length);
|
||||
testing.expectEqual(1, navigator.languages.length);
|
||||
testing.expectEqual('en-US', navigator.languages[0]);
|
||||
testing.expectEqual('en', navigator.languages[1]);
|
||||
testing.expectEqual(true, navigator.onLine);
|
||||
testing.expectEqual(true, navigator.cookieEnabled);
|
||||
testing.expectEqual(true, navigator.hardwareConcurrency > 0);
|
||||
|
||||
@@ -24,6 +24,7 @@ const Page = @import("../Page.zig");
|
||||
|
||||
const Node = @import("Node.zig");
|
||||
const Element = @import("Element.zig");
|
||||
const DOMException = @import("DOMException.zig");
|
||||
const Custom = @import("element/html/Custom.zig");
|
||||
const CustomElementDefinition = @import("CustomElementDefinition.zig");
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
const std = @import("std");
|
||||
const js = @import("../js/js.zig");
|
||||
const String = @import("../../string.zig").String;
|
||||
|
||||
const Page = @import("../Page.zig");
|
||||
const Node = @import("Node.zig");
|
||||
|
||||
@@ -19,8 +19,10 @@
|
||||
const std = @import("std");
|
||||
|
||||
const String = @import("../../string.zig").String;
|
||||
const log = @import("../../log.zig");
|
||||
|
||||
const js = @import("../js/js.zig");
|
||||
const color = @import("../color.zig");
|
||||
const Page = @import("../Page.zig");
|
||||
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/API/ImageData/ImageData
|
||||
|
||||
@@ -40,8 +40,8 @@ pub fn getUserAgent(_: *const Navigator, page: *Page) []const u8 {
|
||||
return page._session.browser.app.config.http_headers.user_agent;
|
||||
}
|
||||
|
||||
pub fn getLanguages(_: *const Navigator) [2][]const u8 {
|
||||
return .{ "en-US", "en" };
|
||||
pub fn getLanguages(_: *const Navigator) [1][]const u8 {
|
||||
return .{"en-US"};
|
||||
}
|
||||
|
||||
pub fn getPlatform(_: *const Navigator) []const u8 {
|
||||
|
||||
@@ -28,6 +28,8 @@ const DocumentFragment = @import("DocumentFragment.zig");
|
||||
const AbstractRange = @import("AbstractRange.zig");
|
||||
const DOMRect = @import("DOMRect.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const Range = @This();
|
||||
|
||||
_proto: *AbstractRange,
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const log = @import("../../log.zig");
|
||||
|
||||
const js = @import("../js/js.zig");
|
||||
const Page = @import("../Page.zig");
|
||||
@@ -26,6 +27,7 @@ const Range = @import("Range.zig");
|
||||
const AbstractRange = @import("AbstractRange.zig");
|
||||
const Node = @import("Node.zig");
|
||||
const Event = @import("Event.zig");
|
||||
const Document = @import("Document.zig");
|
||||
|
||||
/// https://w3c.github.io/selection-api/
|
||||
const Selection = @This();
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
const js = @import("../js/js.zig");
|
||||
const Page = @import("../Page.zig");
|
||||
const EventTarget = @import("EventTarget.zig");
|
||||
const Window = @import("Window.zig");
|
||||
|
||||
const VisualViewport = @This();
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// 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 String = @import("../../../string.zig").String;
|
||||
const js = @import("../../js/js.zig");
|
||||
const Page = @import("../../Page.zig");
|
||||
const CData = @import("../CData.zig");
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
const log = @import("../../../log.zig");
|
||||
const crypto = @import("../../../sys/libcrypto.zig");
|
||||
|
||||
const Page = @import("../../Page.zig");
|
||||
|
||||
@@ -20,10 +20,12 @@
|
||||
|
||||
const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
const log = @import("../../../log.zig");
|
||||
const crypto = @import("../../../sys/libcrypto.zig");
|
||||
|
||||
const Page = @import("../../Page.zig");
|
||||
const js = @import("../../js/js.zig");
|
||||
const Algorithm = @import("algorithm.zig").Algorithm;
|
||||
|
||||
const CryptoKey = @import("../CryptoKey.zig");
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ const reflect = @import("../../reflect.zig");
|
||||
const log = @import("../../../log.zig");
|
||||
|
||||
const global_event_handlers = @import("../global_event_handlers.zig");
|
||||
const GlobalEventHandlersLookup = global_event_handlers.Lookup;
|
||||
const GlobalEventHandler = global_event_handlers.Handler;
|
||||
|
||||
const Page = @import("../../Page.zig");
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// 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 log = @import("../../../../log.zig");
|
||||
const js = @import("../../../js/js.zig");
|
||||
const Page = @import("../../../Page.zig");
|
||||
const Window = @import("../../Window.zig");
|
||||
|
||||
@@ -5,6 +5,10 @@ const URL = @import("../../../URL.zig");
|
||||
const Node = @import("../../Node.zig");
|
||||
const Element = @import("../../Element.zig");
|
||||
const HtmlElement = @import("../Html.zig");
|
||||
const Event = @import("../../Event.zig");
|
||||
const log = @import("../../../../log.zig");
|
||||
|
||||
const IS_DEBUG = @import("builtin").mode == .Debug;
|
||||
|
||||
const Image = @This();
|
||||
_proto: *HtmlElement,
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
const std = @import("std");
|
||||
|
||||
const log = @import("../../../../log.zig");
|
||||
const js = @import("../../../js/js.zig");
|
||||
const Page = @import("../../../Page.zig");
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ const js = @import("../../js/js.zig");
|
||||
const Page = @import("../../Page.zig");
|
||||
const Session = @import("../../Session.zig");
|
||||
const Event = @import("../Event.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const CompositionEvent = @This();
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ const Session = @import("../../Session.zig");
|
||||
const js = @import("../../js/js.zig");
|
||||
|
||||
const Event = @import("../Event.zig");
|
||||
const UIEvent = @import("UIEvent.zig");
|
||||
|
||||
const FormData = @import("../net/FormData.zig");
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ const js = @import("../../js/js.zig");
|
||||
const Page = @import("../../Page.zig");
|
||||
const Session = @import("../../Session.zig");
|
||||
const Event = @import("../Event.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const PromiseRejectionEvent = @This();
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@ const Page = @import("../../Page.zig");
|
||||
const Event = @import("../Event.zig");
|
||||
const EventTarget = @import("../EventTarget.zig");
|
||||
|
||||
const IS_DEBUG = @import("builtin").mode == .Debug;
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Navigation
|
||||
const Navigation = @This();
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
const std = @import("std");
|
||||
const URL = @import("../URL.zig");
|
||||
const EventTarget = @import("../EventTarget.zig");
|
||||
const NavigationState = @import("root.zig").NavigationState;
|
||||
const Page = @import("../../Page.zig");
|
||||
const js = @import("../../js/js.zig");
|
||||
|
||||
@@ -80,7 +80,7 @@ pub fn init(input: Input, options: ?InitOpts, page: *Page) !js.Promise {
|
||||
if (request._headers) |h| {
|
||||
try h.populateHttpHeader(page.call_arena, &headers);
|
||||
}
|
||||
try page.headersForRequest(&headers);
|
||||
try page.headersForRequest(page.arena, request._url, &headers);
|
||||
|
||||
if (comptime IS_DEBUG) {
|
||||
log.debug(.http, "fetch", .{ .url = request._url });
|
||||
@@ -95,7 +95,6 @@ pub fn init(input: Input, options: ?InitOpts, page: *Page) !js.Promise {
|
||||
.headers = headers,
|
||||
.resource_type = .fetch,
|
||||
.cookie_jar = &page._session.cookie_jar,
|
||||
.cookie_origin = page.url,
|
||||
.notification = page._session.notification,
|
||||
.start_callback = httpStartCallback,
|
||||
.header_callback = httpHeaderDoneCallback,
|
||||
|
||||
@@ -22,6 +22,7 @@ const log = @import("../../../log.zig");
|
||||
|
||||
const js = @import("../../js/js.zig");
|
||||
const Page = @import("../../Page.zig");
|
||||
const Node = @import("../Node.zig");
|
||||
const Form = @import("../element/html/Form.zig");
|
||||
const Element = @import("../Element.zig");
|
||||
const KeyValueList = @import("../KeyValueList.zig");
|
||||
|
||||
@@ -29,6 +29,7 @@ const Page = @import("../../Page.zig");
|
||||
const Session = @import("../../Session.zig");
|
||||
|
||||
const Node = @import("../Node.zig");
|
||||
const Blob = @import("../Blob.zig");
|
||||
const Event = @import("../Event.zig");
|
||||
const Headers = @import("Headers.zig");
|
||||
const EventTarget = @import("../EventTarget.zig");
|
||||
@@ -224,7 +225,7 @@ pub fn send(self: *XMLHttpRequest, body_: ?[]const u8) !void {
|
||||
|
||||
try self._request_headers.populateHttpHeader(page.call_arena, &headers);
|
||||
if (cookie_support) {
|
||||
try page.headersForRequest(&headers);
|
||||
try page.headersForRequest(self._arena, self._url, &headers);
|
||||
}
|
||||
|
||||
try http_client.request(.{
|
||||
@@ -235,7 +236,6 @@ pub fn send(self: *XMLHttpRequest, body_: ?[]const u8) !void {
|
||||
.frame_id = page._frame_id,
|
||||
.body = self._request_body,
|
||||
.cookie_jar = if (cookie_support) &page._session.cookie_jar else null,
|
||||
.cookie_origin = page.url,
|
||||
.resource_type = .xhr,
|
||||
.notification = page._session.notification,
|
||||
.start_callback = httpStartCallback,
|
||||
|
||||
139
src/cdp/CDP.zig
139
src/cdp/CDP.zig
@@ -25,6 +25,7 @@ const json = std.json;
|
||||
const Incrementing = @import("id.zig").Incrementing;
|
||||
|
||||
const log = @import("../log.zig");
|
||||
const App = @import("../App.zig");
|
||||
const Notification = @import("../Notification.zig");
|
||||
|
||||
const Client = @import("../Server.zig").Client;
|
||||
@@ -34,6 +35,7 @@ const Browser = @import("../browser/Browser.zig");
|
||||
const Session = @import("../browser/Session.zig");
|
||||
const Page = @import("../browser/Page.zig");
|
||||
const Mime = @import("../browser/Mime.zig");
|
||||
const HttpClient = @import("../browser/HttpClient.zig");
|
||||
|
||||
const InterceptState = @import("domains/fetch.zig").InterceptState;
|
||||
|
||||
@@ -63,7 +65,7 @@ target_id_gen: TargetIdGen = .{},
|
||||
session_id_gen: SessionIdGen = .{},
|
||||
browser_context_id_gen: BrowserContextIdGen = .{},
|
||||
|
||||
browser_context: ?BrowserContext,
|
||||
browser_context: ?BrowserContext(CDP),
|
||||
|
||||
// Re-used arena for processing a message. We're assuming that we're getting
|
||||
// 1 message at a time.
|
||||
@@ -120,7 +122,7 @@ pub fn handleMessage(self: *CDP, msg: []const u8) bool {
|
||||
pub fn processMessage(self: *CDP, msg: []const u8) !void {
|
||||
const arena = &self.message_arena;
|
||||
defer _ = arena.reset(.{ .retain_with_limit = 1024 * 16 });
|
||||
return self.dispatch(arena.allocator(), .{ .cdp = self }, msg);
|
||||
return self.dispatch(arena.allocator(), self, msg);
|
||||
}
|
||||
|
||||
// @newhttp
|
||||
@@ -136,12 +138,12 @@ pub fn pageWait(self: *CDP, ms: u32) !Session.Runner.CDPWaitResult {
|
||||
// Called from above, in processMessage which handles client messages
|
||||
// but can also be called internally. For example, Target.sendMessageToTarget
|
||||
// calls back into dispatch to capture the response.
|
||||
pub fn dispatch(self: *CDP, arena: Allocator, sender: Command.Sender, str: []const u8) !void {
|
||||
pub fn dispatch(self: *CDP, arena: Allocator, sender: anytype, str: []const u8) !void {
|
||||
const input = json.parseFromSliceLeaky(InputMessage, arena, str, .{
|
||||
.ignore_unknown_fields = true,
|
||||
}) catch return error.InvalidJSON;
|
||||
|
||||
var command = Command{
|
||||
var command = Command(CDP, @TypeOf(sender)){
|
||||
.input = .{
|
||||
.json = str,
|
||||
.id = input.id,
|
||||
@@ -186,7 +188,7 @@ pub fn dispatch(self: *CDP, arena: Allocator, sender: Command.Sender, str: []con
|
||||
// "special" handling - the bare minimum we need to do until the driver
|
||||
// switches to a real BrowserContext.
|
||||
// (I can imagine this logic will become driver-specific)
|
||||
fn dispatchStartupCommand(command: *Command, method: []const u8) !void {
|
||||
fn dispatchStartupCommand(command: anytype, method: []const u8) !void {
|
||||
// Stagehand parses the response and error if we don't return a
|
||||
// correct one for Page.getFrameTree on startup call.
|
||||
if (std.mem.eql(u8, method, "Page.getFrameTree")) {
|
||||
@@ -197,7 +199,7 @@ fn dispatchStartupCommand(command: *Command, method: []const u8) !void {
|
||||
return command.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn dispatchCommand(command: *Command, method: []const u8) !void {
|
||||
fn dispatchCommand(command: anytype, method: []const u8) !void {
|
||||
const domain = blk: {
|
||||
const i = std.mem.indexOfScalarPos(u8, method, 0, '.') orelse {
|
||||
return error.InvalidMethod;
|
||||
@@ -273,10 +275,10 @@ pub fn createBrowserContext(self: *CDP) ![]const u8 {
|
||||
}
|
||||
const id = self.browser_context_id_gen.next();
|
||||
|
||||
self.browser_context = @as(BrowserContext, undefined);
|
||||
self.browser_context = @as(BrowserContext(CDP), undefined);
|
||||
const browser_context = &self.browser_context.?;
|
||||
|
||||
try BrowserContext.init(browser_context, id, self);
|
||||
try BrowserContext(CDP).init(browser_context, id, self);
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -308,7 +310,7 @@ pub fn sendJSON(self: *CDP, message: anytype) !void {
|
||||
});
|
||||
}
|
||||
|
||||
pub const BrowserContext = struct {
|
||||
pub fn BrowserContext(comptime CDP_T: type) type {
|
||||
const Node = @import("Node.zig");
|
||||
const AXNode = @import("AXNode.zig");
|
||||
|
||||
@@ -317,8 +319,9 @@ pub const BrowserContext = struct {
|
||||
data: std.ArrayList(u8),
|
||||
};
|
||||
|
||||
return struct {
|
||||
id: []const u8,
|
||||
cdp: *CDP,
|
||||
cdp: *CDP_T,
|
||||
|
||||
// Represents the browser session. There is no equivalent in CDP. For
|
||||
// all intents and purpose, from CDP's point of view our Browser and
|
||||
@@ -362,11 +365,6 @@ pub const BrowserContext = struct {
|
||||
inspector_session: *js.Inspector.Session,
|
||||
isolated_worlds: std.ArrayList(*IsolatedWorld),
|
||||
|
||||
// Scripts registered via Page.addScriptToEvaluateOnNewDocument.
|
||||
// Evaluated in each new document after navigation completes.
|
||||
scripts_on_new_document: std.ArrayList(ScriptOnNewDocument) = .empty,
|
||||
next_script_id: u32 = 1,
|
||||
|
||||
http_proxy_changed: bool = false,
|
||||
|
||||
// Extra headers to add to all requests.
|
||||
@@ -385,7 +383,9 @@ pub const BrowserContext = struct {
|
||||
|
||||
notification: *Notification,
|
||||
|
||||
fn init(self: *BrowserContext, id: []const u8, cdp: *CDP) !void {
|
||||
const Self = @This();
|
||||
|
||||
fn init(self: *Self, id: []const u8, cdp: *CDP_T) !void {
|
||||
const allocator = cdp.allocator;
|
||||
|
||||
// Create notification for this BrowserContext
|
||||
@@ -431,7 +431,7 @@ pub const BrowserContext = struct {
|
||||
try notification.register(.page_frame_created, self, onPageFrameCreated);
|
||||
}
|
||||
|
||||
pub fn deinit(self: *BrowserContext) void {
|
||||
pub fn deinit(self: *Self) void {
|
||||
const browser = &self.cdp.browser;
|
||||
const env = &browser.env;
|
||||
|
||||
@@ -470,19 +470,19 @@ pub const BrowserContext = struct {
|
||||
if (self.http_proxy_changed) {
|
||||
// has to be called after browser.closeSession, since it won't
|
||||
// work if there are active connections.
|
||||
browser.http_client.changeProxy(null) catch |err| {
|
||||
log.warn(.http, "changeProxy", .{ .err = err });
|
||||
browser.http_client.restoreOriginalProxy() catch |err| {
|
||||
log.warn(.http, "restoreOriginalProxy", .{ .err = err });
|
||||
};
|
||||
}
|
||||
self.intercept_state.deinit();
|
||||
}
|
||||
|
||||
pub fn reset(self: *BrowserContext) void {
|
||||
pub fn reset(self: *Self) void {
|
||||
self.node_registry.reset();
|
||||
self.node_search_list.reset();
|
||||
}
|
||||
|
||||
pub fn createIsolatedWorld(self: *BrowserContext, world_name: []const u8, grant_universal_access: bool) !*IsolatedWorld {
|
||||
pub fn createIsolatedWorld(self: *Self, world_name: []const u8, grant_universal_access: bool) !*IsolatedWorld {
|
||||
const browser = &self.cdp.browser;
|
||||
const arena = try browser.arena_pool.acquire(.{ .debug = "IsolatedWorld" });
|
||||
errdefer browser.arena_pool.release(arena);
|
||||
@@ -505,7 +505,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: *Self, root: *const Node, opts: Node.Writer.Opts) Node.Writer {
|
||||
return .{
|
||||
.root = root,
|
||||
.depth = opts.depth,
|
||||
@@ -514,7 +514,7 @@ pub const BrowserContext = struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn axnodeWriter(self: *BrowserContext, root: *const Node, opts: AXNode.Writer.Opts) !AXNode.Writer {
|
||||
pub fn axnodeWriter(self: *Self, root: *const Node, opts: AXNode.Writer.Opts) !AXNode.Writer {
|
||||
const page = self.session.currentPage() orelse return error.PageNotLoaded;
|
||||
_ = opts;
|
||||
return .{
|
||||
@@ -524,13 +524,13 @@ pub const BrowserContext = struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getURL(self: *const BrowserContext) ?[:0]const u8 {
|
||||
pub fn getURL(self: *const Self) ?[:0]const u8 {
|
||||
const page = self.session.currentPage() orelse return null;
|
||||
const url = page.url;
|
||||
return if (url.len == 0) null else url;
|
||||
}
|
||||
|
||||
pub fn getTitle(self: *const BrowserContext) ?[]const u8 {
|
||||
pub fn getTitle(self: *const Self) ?[]const u8 {
|
||||
const page = self.session.currentPage() orelse return null;
|
||||
return page.getTitle() catch |err| {
|
||||
log.err(.cdp, "page title", .{ .err = err });
|
||||
@@ -538,7 +538,7 @@ pub const BrowserContext = struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn networkEnable(self: *BrowserContext) !void {
|
||||
pub fn networkEnable(self: *Self) !void {
|
||||
try self.notification.register(.http_request_fail, self, onHttpRequestFail);
|
||||
try self.notification.register(.http_request_start, self, onHttpRequestStart);
|
||||
try self.notification.register(.http_request_done, self, onHttpRequestDone);
|
||||
@@ -546,7 +546,7 @@ pub const BrowserContext = struct {
|
||||
try self.notification.register(.http_response_header_done, self, onHttpResponseHeadersDone);
|
||||
}
|
||||
|
||||
pub fn networkDisable(self: *BrowserContext) void {
|
||||
pub fn networkDisable(self: *Self) void {
|
||||
self.notification.unregister(.http_request_fail, self);
|
||||
self.notification.unregister(.http_request_start, self);
|
||||
self.notification.unregister(.http_request_done, self);
|
||||
@@ -554,83 +554,83 @@ pub const BrowserContext = struct {
|
||||
self.notification.unregister(.http_response_header_done, self);
|
||||
}
|
||||
|
||||
pub fn fetchEnable(self: *BrowserContext, authRequests: bool) !void {
|
||||
pub fn fetchEnable(self: *Self, authRequests: bool) !void {
|
||||
try self.notification.register(.http_request_intercept, self, onHttpRequestIntercept);
|
||||
if (authRequests) {
|
||||
try self.notification.register(.http_request_auth_required, self, onHttpRequestAuthRequired);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fetchDisable(self: *BrowserContext) void {
|
||||
pub fn fetchDisable(self: *Self) void {
|
||||
self.notification.unregister(.http_request_intercept, self);
|
||||
self.notification.unregister(.http_request_auth_required, self);
|
||||
}
|
||||
|
||||
pub fn lifecycleEventsEnable(self: *BrowserContext) !void {
|
||||
pub fn lifecycleEventsEnable(self: *Self) !void {
|
||||
self.page_life_cycle_events = true;
|
||||
try self.notification.register(.page_network_idle, self, onPageNetworkIdle);
|
||||
try self.notification.register(.page_network_almost_idle, self, onPageNetworkAlmostIdle);
|
||||
}
|
||||
|
||||
pub fn lifecycleEventsDisable(self: *BrowserContext) void {
|
||||
pub fn lifecycleEventsDisable(self: *Self) void {
|
||||
self.page_life_cycle_events = false;
|
||||
self.notification.unregister(.page_network_idle, self);
|
||||
self.notification.unregister(.page_network_almost_idle, self);
|
||||
}
|
||||
|
||||
pub fn onPageRemove(ctx: *anyopaque, _: Notification.PageRemove) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
try @import("domains/page.zig").pageRemove(self);
|
||||
}
|
||||
|
||||
pub fn onPageCreated(ctx: *anyopaque, page: *Page) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
return @import("domains/page.zig").pageCreated(self, page);
|
||||
}
|
||||
|
||||
pub fn onPageNavigate(ctx: *anyopaque, msg: *const Notification.PageNavigate) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
return @import("domains/page.zig").pageNavigate(self, msg);
|
||||
}
|
||||
|
||||
pub fn onPageNavigated(ctx: *anyopaque, msg: *const Notification.PageNavigated) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
defer self.resetNotificationArena();
|
||||
return @import("domains/page.zig").pageNavigated(self.notification_arena, self, msg);
|
||||
}
|
||||
|
||||
pub fn onPageFrameCreated(ctx: *anyopaque, msg: *const Notification.PageFrameCreated) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
return @import("domains/page.zig").pageFrameCreated(self, msg);
|
||||
}
|
||||
|
||||
pub fn onPageNetworkIdle(ctx: *anyopaque, msg: *const Notification.PageNetworkIdle) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
return @import("domains/page.zig").pageNetworkIdle(self, msg);
|
||||
}
|
||||
|
||||
pub fn onPageNetworkAlmostIdle(ctx: *anyopaque, msg: *const Notification.PageNetworkAlmostIdle) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
return @import("domains/page.zig").pageNetworkAlmostIdle(self, msg);
|
||||
}
|
||||
|
||||
pub fn onHttpRequestStart(ctx: *anyopaque, msg: *const Notification.RequestStart) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
try @import("domains/network.zig").httpRequestStart(self, msg);
|
||||
}
|
||||
|
||||
pub fn onHttpRequestIntercept(ctx: *anyopaque, msg: *const Notification.RequestIntercept) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
try @import("domains/fetch.zig").requestIntercept(self, msg);
|
||||
}
|
||||
|
||||
pub fn onHttpRequestFail(ctx: *anyopaque, msg: *const Notification.RequestFail) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
return @import("domains/network.zig").httpRequestFail(self, msg);
|
||||
}
|
||||
|
||||
pub fn onHttpResponseHeadersDone(ctx: *anyopaque, msg: *const Notification.ResponseHeaderDone) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
defer self.resetNotificationArena();
|
||||
|
||||
const arena = self.page_arena;
|
||||
@@ -665,12 +665,12 @@ pub const BrowserContext = struct {
|
||||
}
|
||||
|
||||
pub fn onHttpRequestDone(ctx: *anyopaque, msg: *const Notification.RequestDone) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
return @import("domains/network.zig").httpRequestDone(self, msg);
|
||||
}
|
||||
|
||||
pub fn onHttpResponseData(ctx: *anyopaque, msg: *const Notification.ResponseData) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
const arena = self.page_arena;
|
||||
|
||||
const id = msg.transfer.id;
|
||||
@@ -680,16 +680,16 @@ pub const BrowserContext = struct {
|
||||
}
|
||||
|
||||
pub fn onHttpRequestAuthRequired(ctx: *anyopaque, data: *const Notification.RequestAuthRequired) !void {
|
||||
const self: *BrowserContext = @ptrCast(@alignCast(ctx));
|
||||
const self: *Self = @ptrCast(@alignCast(ctx));
|
||||
defer self.resetNotificationArena();
|
||||
try @import("domains/fetch.zig").requestAuthRequired(self, data);
|
||||
}
|
||||
|
||||
fn resetNotificationArena(self: *BrowserContext) void {
|
||||
fn resetNotificationArena(self: *Self) void {
|
||||
defer _ = self.cdp.notification_arena.reset(.{ .retain_with_limit = 1024 * 64 });
|
||||
}
|
||||
|
||||
pub fn callInspector(self: *const BrowserContext, msg: []const u8) void {
|
||||
pub fn callInspector(self: *const Self, msg: []const u8) void {
|
||||
self.inspector_session.send(msg);
|
||||
self.session.browser.env.runMicrotasks();
|
||||
}
|
||||
@@ -720,7 +720,7 @@ pub const BrowserContext = struct {
|
||||
// This is hacky x 2. First, we create the JSON payload by gluing our
|
||||
// session_id onto it. Second, we're much more client/websocket aware than
|
||||
// we should be.
|
||||
fn sendInspectorMessage(self: *BrowserContext, msg: []const u8) !void {
|
||||
fn sendInspectorMessage(self: *Self, msg: []const u8) !void {
|
||||
const session_id = self.session_id orelse {
|
||||
// We no longer have an active session. What should we do
|
||||
// in this case?
|
||||
@@ -757,17 +757,13 @@ pub const BrowserContext = struct {
|
||||
try cdp.client.sendJSONRaw(buf);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// see: https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/renderer/bindings/core/v8/V8BindingDesign.md#world
|
||||
/// The current understanding. An isolated world lives in the same isolate, but a separated context.
|
||||
/// Clients create this to be able to create variables and run code without interfering with the
|
||||
/// normal namespace and values of the webpage. Similar to the main context we need to pretend to recreate it after
|
||||
/// a executionContextsCleared event which happens when navigating to a new page. A client can have a command be executed
|
||||
const ScriptOnNewDocument = struct {
|
||||
identifier: u32,
|
||||
source: []const u8,
|
||||
};
|
||||
|
||||
/// in the isolated world by using its Context ID or the worldName.
|
||||
/// grantUniveralAccess Indecated whether the isolated world can reference objects like the DOM or other JS Objects.
|
||||
/// An isolated world has it's own instance of globals like Window.
|
||||
@@ -828,16 +824,17 @@ const IsolatedWorld = struct {
|
||||
// behaviors. Normally, we're sending the result to the client. But in some cases
|
||||
// we want to capture the result. So we want the command.sendResult to be
|
||||
// generic.
|
||||
pub const Command = struct {
|
||||
pub fn Command(comptime CDP_T: type, comptime Sender: type) type {
|
||||
return struct {
|
||||
// A misc arena that can be used for any allocation for processing
|
||||
// the message
|
||||
arena: Allocator,
|
||||
|
||||
// reference to our CDP instance
|
||||
cdp: *CDP,
|
||||
cdp: *CDP_T,
|
||||
|
||||
// The browser context this command targets
|
||||
browser_context: ?*BrowserContext,
|
||||
browser_context: ?*BrowserContext(CDP_T),
|
||||
|
||||
// The command input (the id, optional session_id, params, ...)
|
||||
input: Input,
|
||||
@@ -848,23 +845,9 @@ pub const Command = struct {
|
||||
// be code to capture the data that we were "sending".
|
||||
sender: Sender,
|
||||
|
||||
const Sender = union(enum) {
|
||||
cdp: *CDP,
|
||||
capture: *std.Io.Writer,
|
||||
const Self = @This();
|
||||
|
||||
pub fn sendJSON(self: Sender, message: anytype) !void {
|
||||
switch (self) {
|
||||
.cdp => |cdp| return cdp.sendJSON(message),
|
||||
.capture => |writer| {
|
||||
return std.json.Stringify.value(message, .{
|
||||
.emit_null_optional_fields = false,
|
||||
}, writer);
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub fn params(self: *const Command, comptime T: type) !?T {
|
||||
pub fn params(self: *const Self, comptime T: type) !?T {
|
||||
if (self.input.params) |p| {
|
||||
return try json.parseFromSliceLeaky(
|
||||
T,
|
||||
@@ -876,7 +859,7 @@ pub const Command = struct {
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn createBrowserContext(self: *Command) !*BrowserContext {
|
||||
pub fn createBrowserContext(self: *Self) !*BrowserContext(CDP_T) {
|
||||
_ = try self.cdp.createBrowserContext();
|
||||
self.browser_context = &(self.cdp.browser_context.?);
|
||||
return self.browser_context.?;
|
||||
@@ -885,7 +868,7 @@ pub const Command = struct {
|
||||
const SendResultOpts = struct {
|
||||
include_session_id: bool = true,
|
||||
};
|
||||
pub fn sendResult(self: *Command, result: anytype, opts: SendResultOpts) !void {
|
||||
pub fn sendResult(self: *Self, result: anytype, opts: SendResultOpts) !void {
|
||||
return self.sender.sendJSON(.{
|
||||
.id = self.input.id,
|
||||
.result = if (comptime @typeInfo(@TypeOf(result)) == .null) struct {}{} else result,
|
||||
@@ -893,7 +876,10 @@ pub const Command = struct {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn sendEvent(self: *Command, method: []const u8, p: anytype, opts: SendEventOpts) !void {
|
||||
const SendEventOpts = struct {
|
||||
session_id: ?[]const u8 = null,
|
||||
};
|
||||
pub fn sendEvent(self: *Self, method: []const u8, p: anytype, opts: CDP_T.SendEventOpts) !void {
|
||||
// Events ALWAYS go to the client. self.sender should not be used
|
||||
return self.cdp.sendEvent(method, p, opts);
|
||||
}
|
||||
@@ -901,7 +887,7 @@ pub const Command = struct {
|
||||
const SendErrorOpts = struct {
|
||||
include_session_id: bool = true,
|
||||
};
|
||||
pub fn sendError(self: *Command, code: i32, message: []const u8, opts: SendErrorOpts) !void {
|
||||
pub fn sendError(self: *Self, code: i32, message: []const u8, opts: SendErrorOpts) !void {
|
||||
return self.sender.sendJSON(.{
|
||||
.id = self.input.id,
|
||||
.@"error" = .{ .code = code, .message = message },
|
||||
@@ -927,6 +913,7 @@ pub const Command = struct {
|
||||
json: []const u8,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
// When we parse a JSON message from the client, this is the structure
|
||||
// we always expect
|
||||
|
||||
@@ -18,9 +18,8 @@
|
||||
|
||||
const std = @import("std");
|
||||
const id = @import("../id.zig");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
disable,
|
||||
@@ -33,15 +32,15 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
.getFullAXTree => return getFullAXTree(cmd),
|
||||
}
|
||||
}
|
||||
fn enable(cmd: *CDP.Command) !void {
|
||||
fn enable(cmd: anytype) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn disable(cmd: *CDP.Command) !void {
|
||||
fn disable(cmd: anytype) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn getFullAXTree(cmd: *CDP.Command) !void {
|
||||
fn getFullAXTree(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
depth: ?i32 = null,
|
||||
frameId: ?[]const u8 = null,
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
// TODO: hard coded data
|
||||
const PROTOCOL_VERSION = "1.3";
|
||||
@@ -36,7 +35,7 @@ const PRODUCT = "Chrome/124.0.6367.29";
|
||||
const JS_VERSION = "12.4.254.8";
|
||||
const DEV_TOOLS_WINDOW_ID = 1923710101;
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
getVersion,
|
||||
setPermission,
|
||||
@@ -58,7 +57,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
}
|
||||
}
|
||||
|
||||
fn getVersion(cmd: *CDP.Command) !void {
|
||||
fn getVersion(cmd: anytype) !void {
|
||||
// TODO: pre-serialize?
|
||||
return cmd.sendResult(.{
|
||||
.protocolVersion = PROTOCOL_VERSION,
|
||||
@@ -70,7 +69,7 @@ fn getVersion(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn setDownloadBehavior(cmd: *CDP.Command) !void {
|
||||
fn setDownloadBehavior(cmd: anytype) !void {
|
||||
// const params = (try cmd.params(struct {
|
||||
// behavior: []const u8,
|
||||
// browserContextId: ?[]const u8 = null,
|
||||
@@ -81,7 +80,7 @@ fn setDownloadBehavior(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{ .include_session_id = false });
|
||||
}
|
||||
|
||||
fn getWindowForTarget(cmd: *CDP.Command) !void {
|
||||
fn getWindowForTarget(cmd: anytype) !void {
|
||||
// const params = (try cmd.params(struct {
|
||||
// targetId: ?[]const u8 = null,
|
||||
// })) orelse return error.InvalidParams;
|
||||
@@ -92,22 +91,22 @@ fn getWindowForTarget(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn setWindowBounds(cmd: *CDP.Command) !void {
|
||||
fn setWindowBounds(cmd: anytype) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn grantPermissions(cmd: *CDP.Command) !void {
|
||||
fn grantPermissions(cmd: anytype) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn setPermission(cmd: *CDP.Command) !void {
|
||||
fn setPermission(cmd: anytype) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn resetPermissions(cmd: *CDP.Command) !void {
|
||||
fn resetPermissions(cmd: anytype) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
}, cmd.input.action) orelse return error.UnknownMethod;
|
||||
|
||||
@@ -18,18 +18,17 @@
|
||||
|
||||
const std = @import("std");
|
||||
const id = @import("../id.zig");
|
||||
const CDP = @import("../CDP.zig");
|
||||
const Node = @import("../Node.zig");
|
||||
|
||||
const log = @import("../../log.zig");
|
||||
const dump = @import("../../browser/dump.zig");
|
||||
const js = @import("../../browser/js/js.zig");
|
||||
const Node = @import("../Node.zig");
|
||||
const DOMNode = @import("../../browser/webapi/Node.zig");
|
||||
const Selector = @import("../../browser/webapi/selector/Selector.zig");
|
||||
|
||||
const dump = @import("../../browser/dump.zig");
|
||||
const js = @import("../../browser/js/js.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
getDocument,
|
||||
@@ -70,7 +69,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getDocument
|
||||
fn getDocument(cmd: *CDP.Command) !void {
|
||||
fn getDocument(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
// CDP documentation implies that 0 isn't valid, but it _does_ work in Chrome
|
||||
depth: i32 = 3,
|
||||
@@ -90,7 +89,7 @@ fn getDocument(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-performSearch
|
||||
fn performSearch(cmd: *CDP.Command) !void {
|
||||
fn performSearch(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
query: []const u8,
|
||||
includeUserAgentShadowDOM: ?bool = null,
|
||||
@@ -117,7 +116,7 @@ fn performSearch(cmd: *CDP.Command) !void {
|
||||
// hierarchy of each nodes.
|
||||
// We dispatch event in the reverse order: from the top level to the direct parents.
|
||||
// We should dispatch a node only if it has never been sent.
|
||||
fn dispatchSetChildNodes(cmd: *CDP.Command, dom_nodes: []const *DOMNode) !void {
|
||||
fn dispatchSetChildNodes(cmd: anytype, dom_nodes: []const *DOMNode) !void {
|
||||
const arena = cmd.arena;
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const session_id = bc.session_id orelse return error.SessionIdNotLoaded;
|
||||
@@ -173,7 +172,7 @@ fn dispatchSetChildNodes(cmd: *CDP.Command, dom_nodes: []const *DOMNode) !void {
|
||||
}
|
||||
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-discardSearchResults
|
||||
fn discardSearchResults(cmd: *CDP.Command) !void {
|
||||
fn discardSearchResults(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
searchId: []const u8,
|
||||
})) orelse return error.InvalidParams;
|
||||
@@ -185,7 +184,7 @@ fn discardSearchResults(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getSearchResults
|
||||
fn getSearchResults(cmd: *CDP.Command) !void {
|
||||
fn getSearchResults(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
searchId: []const u8,
|
||||
fromIndex: u32,
|
||||
@@ -210,7 +209,7 @@ fn getSearchResults(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{ .nodeIds = node_ids[params.fromIndex..params.toIndex] }, .{});
|
||||
}
|
||||
|
||||
fn querySelector(cmd: *CDP.Command) !void {
|
||||
fn querySelector(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
nodeId: Node.Id,
|
||||
selector: []const u8,
|
||||
@@ -236,7 +235,7 @@ fn querySelector(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn querySelectorAll(cmd: *CDP.Command) !void {
|
||||
fn querySelectorAll(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
nodeId: Node.Id,
|
||||
selector: []const u8,
|
||||
@@ -267,7 +266,7 @@ fn querySelectorAll(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn resolveNode(cmd: *CDP.Command) !void {
|
||||
fn resolveNode(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
backendNodeId: ?u32 = null,
|
||||
@@ -328,7 +327,7 @@ fn resolveNode(cmd: *CDP.Command) !void {
|
||||
} }, .{});
|
||||
}
|
||||
|
||||
fn describeNode(cmd: *CDP.Command) !void {
|
||||
fn describeNode(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
backendNodeId: ?Node.Id = null,
|
||||
@@ -375,7 +374,7 @@ fn rectToQuad(rect: DOMNode.Element.DOMRect) Quad {
|
||||
};
|
||||
}
|
||||
|
||||
fn scrollIntoViewIfNeeded(cmd: *CDP.Command) !void {
|
||||
fn scrollIntoViewIfNeeded(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
backendNodeId: ?u32 = null,
|
||||
@@ -398,7 +397,7 @@ fn scrollIntoViewIfNeeded(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn getNode(arena: Allocator, bc: *CDP.BrowserContext, node_id: ?Node.Id, backend_node_id: ?Node.Id, object_id: ?[]const u8) !*Node {
|
||||
fn getNode(arena: Allocator, bc: anytype, node_id: ?Node.Id, backend_node_id: ?Node.Id, object_id: ?[]const u8) !*Node {
|
||||
const input_node_id = node_id orelse backend_node_id;
|
||||
if (input_node_id) |input_node_id_| {
|
||||
return bc.node_registry.lookup_by_id.get(input_node_id_) orelse return error.NodeNotFound;
|
||||
@@ -418,7 +417,7 @@ fn getNode(arena: Allocator, bc: *CDP.BrowserContext, node_id: ?Node.Id, backend
|
||||
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getContentQuads
|
||||
// Related to: https://drafts.csswg.org/cssom-view/#the-geometryutils-interface
|
||||
fn getContentQuads(cmd: *CDP.Command) !void {
|
||||
fn getContentQuads(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
backendNodeId: ?Node.Id = null,
|
||||
@@ -444,7 +443,7 @@ fn getContentQuads(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{ .quads = &.{quad} }, .{});
|
||||
}
|
||||
|
||||
fn getBoxModel(cmd: *CDP.Command) !void {
|
||||
fn getBoxModel(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
backendNodeId: ?u32 = null,
|
||||
@@ -473,7 +472,7 @@ fn getBoxModel(cmd: *CDP.Command) !void {
|
||||
} }, .{});
|
||||
}
|
||||
|
||||
fn requestChildNodes(cmd: *CDP.Command) !void {
|
||||
fn requestChildNodes(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
nodeId: Node.Id,
|
||||
depth: i32 = 1,
|
||||
@@ -497,7 +496,7 @@ fn requestChildNodes(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn getFrameOwner(cmd: *CDP.Command) !void {
|
||||
fn getFrameOwner(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
frameId: []const u8,
|
||||
})) orelse return error.InvalidParams;
|
||||
@@ -513,7 +512,7 @@ fn getFrameOwner(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{ .nodeId = node.id, .backendNodeId = node.id }, .{});
|
||||
}
|
||||
|
||||
fn getOuterHTML(cmd: *CDP.Command) !void {
|
||||
fn getOuterHTML(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
backendNodeId: ?Node.Id = null,
|
||||
@@ -535,7 +534,7 @@ fn getOuterHTML(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{ .outerHTML = aw.written() }, .{});
|
||||
}
|
||||
|
||||
fn requestNode(cmd: *CDP.Command) !void {
|
||||
fn requestNode(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
objectId: []const u8,
|
||||
})) orelse return error.InvalidParams;
|
||||
|
||||
@@ -17,10 +17,9 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const CDP = @import("../CDP.zig");
|
||||
const log = @import("../../log.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
setEmulatedMedia,
|
||||
setFocusEmulationEnabled,
|
||||
@@ -39,7 +38,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn setEmulatedMedia(cmd: *CDP.Command) !void {
|
||||
fn setEmulatedMedia(cmd: anytype) !void {
|
||||
// const input = (try const incoming.params(struct {
|
||||
// media: ?[]const u8 = null,
|
||||
// features: ?[]struct{
|
||||
@@ -52,7 +51,7 @@ fn setEmulatedMedia(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn setFocusEmulationEnabled(cmd: *CDP.Command) !void {
|
||||
fn setFocusEmulationEnabled(cmd: anytype) !void {
|
||||
// const input = (try const incoming.params(struct {
|
||||
// enabled: bool,
|
||||
// })) orelse return error.InvalidParams;
|
||||
@@ -60,16 +59,16 @@ fn setFocusEmulationEnabled(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn setDeviceMetricsOverride(cmd: *CDP.Command) !void {
|
||||
fn setDeviceMetricsOverride(cmd: anytype) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn setTouchEmulationEnabled(cmd: *CDP.Command) !void {
|
||||
fn setTouchEmulationEnabled(cmd: anytype) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn setUserAgentOverride(cmd: *CDP.Command) !void {
|
||||
fn setUserAgentOverride(cmd: anytype) !void {
|
||||
log.info(.app, "setUserAgentOverride ignored", .{});
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
@@ -17,19 +17,17 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const id = @import("../id.zig");
|
||||
const CDP = @import("../CDP.zig");
|
||||
const log = @import("../../log.zig");
|
||||
const network = @import("network.zig");
|
||||
|
||||
const HttpClient = @import("../../browser/HttpClient.zig");
|
||||
const net_http = @import("../../network/http.zig");
|
||||
const Notification = @import("../../Notification.zig");
|
||||
|
||||
const network = @import("network.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
disable,
|
||||
enable,
|
||||
@@ -137,13 +135,13 @@ const ErrorReason = enum {
|
||||
BlockedByResponse,
|
||||
};
|
||||
|
||||
fn disable(cmd: *CDP.Command) !void {
|
||||
fn disable(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
bc.fetchDisable();
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn enable(cmd: *CDP.Command) !void {
|
||||
fn enable(cmd: anytype) !void {
|
||||
const params = (try cmd.params(EnableParam)) orelse EnableParam{};
|
||||
if (!arePatternsSupported(params.patterns)) {
|
||||
log.warn(.not_implemented, "Fetch.enable", .{ .params = "pattern" });
|
||||
@@ -182,7 +180,7 @@ fn arePatternsSupported(patterns: []RequestPattern) bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn requestIntercept(bc: *CDP.BrowserContext, intercept: *const Notification.RequestIntercept) !void {
|
||||
pub fn requestIntercept(bc: anytype, intercept: *const Notification.RequestIntercept) !void {
|
||||
// detachTarget could be called, in which case, we still have a page doing
|
||||
// things, but no session.
|
||||
const session_id = bc.session_id orelse return;
|
||||
@@ -217,7 +215,7 @@ pub fn requestIntercept(bc: *CDP.BrowserContext, intercept: *const Notification.
|
||||
intercept.wait_for_interception.* = true;
|
||||
}
|
||||
|
||||
fn continueRequest(cmd: *CDP.Command) !void {
|
||||
fn continueRequest(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const params = (try cmd.params(struct {
|
||||
requestId: []const u8, // INT-{d}"
|
||||
@@ -277,7 +275,7 @@ const AuthChallengeResponse = enum {
|
||||
ProvideCredentials,
|
||||
};
|
||||
|
||||
fn continueWithAuth(cmd: *CDP.Command) !void {
|
||||
fn continueWithAuth(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const params = (try cmd.params(struct {
|
||||
requestId: []const u8, // "INT-{d}"
|
||||
@@ -320,7 +318,7 @@ fn continueWithAuth(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn fulfillRequest(cmd: *CDP.Command) !void {
|
||||
fn fulfillRequest(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
|
||||
const params = (try cmd.params(struct {
|
||||
@@ -362,7 +360,7 @@ fn fulfillRequest(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn failRequest(cmd: *CDP.Command) !void {
|
||||
fn failRequest(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const params = (try cmd.params(struct {
|
||||
requestId: []const u8, // "INT-{d}"
|
||||
@@ -384,7 +382,7 @@ fn failRequest(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
pub fn requestAuthRequired(bc: *CDP.BrowserContext, intercept: *const Notification.RequestAuthRequired) !void {
|
||||
pub fn requestAuthRequired(bc: anytype, intercept: *const Notification.RequestAuthRequired) !void {
|
||||
// detachTarget could be called, in which case, we still have a page doing
|
||||
// things, but no session.
|
||||
const session_id = bc.session_id orelse return;
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
dispatchKeyEvent,
|
||||
dispatchMouseEvent,
|
||||
@@ -34,7 +33,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchKeyEvent
|
||||
fn dispatchKeyEvent(cmd: *CDP.Command) !void {
|
||||
fn dispatchKeyEvent(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
type: Type,
|
||||
key: []const u8 = "",
|
||||
@@ -75,7 +74,7 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchMouseEvent
|
||||
fn dispatchMouseEvent(cmd: *CDP.Command) !void {
|
||||
fn dispatchMouseEvent(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
x: f64,
|
||||
y: f64,
|
||||
@@ -105,7 +104,7 @@ fn dispatchMouseEvent(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-insertText
|
||||
fn insertText(cmd: *CDP.Command) !void {
|
||||
fn insertText(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
text: []const u8, // The text to insert
|
||||
})) orelse return error.InvalidParams;
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
disable,
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
disable,
|
||||
|
||||
@@ -18,18 +18,15 @@
|
||||
|
||||
const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
|
||||
const CDP = @import("../CDP.zig");
|
||||
const Node = @import("../Node.zig");
|
||||
|
||||
const DOMNode = @import("../../browser/webapi/Node.zig");
|
||||
|
||||
const log = @import("../../log.zig");
|
||||
const markdown = lp.markdown;
|
||||
const SemanticTree = lp.SemanticTree;
|
||||
const interactive = lp.interactive;
|
||||
const structured_data = lp.structured_data;
|
||||
const Node = @import("../Node.zig");
|
||||
const DOMNode = @import("../../browser/webapi/Node.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
getMarkdown,
|
||||
getSemanticTree,
|
||||
@@ -55,7 +52,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
}
|
||||
}
|
||||
|
||||
fn getSemanticTree(cmd: *CDP.Command) !void {
|
||||
fn getSemanticTree(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
format: ?enum { text } = null,
|
||||
prune: ?bool = null,
|
||||
@@ -100,7 +97,7 @@ fn getSemanticTree(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn getMarkdown(cmd: *CDP.Command) !void {
|
||||
fn getMarkdown(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
};
|
||||
@@ -123,7 +120,7 @@ fn getMarkdown(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn getInteractiveElements(cmd: *CDP.Command) !void {
|
||||
fn getInteractiveElements(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
};
|
||||
@@ -145,7 +142,7 @@ fn getInteractiveElements(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn getStructuredData(cmd: *CDP.Command) !void {
|
||||
fn getStructuredData(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.NoBrowserContext;
|
||||
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
|
||||
|
||||
@@ -160,7 +157,7 @@ fn getStructuredData(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn detectForms(cmd: *CDP.Command) !void {
|
||||
fn detectForms(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.NoBrowserContext;
|
||||
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
|
||||
|
||||
@@ -177,7 +174,7 @@ fn detectForms(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn clickNode(cmd: *CDP.Command) !void {
|
||||
fn clickNode(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
backendNodeId: ?Node.Id = null,
|
||||
@@ -198,7 +195,7 @@ fn clickNode(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{}, .{});
|
||||
}
|
||||
|
||||
fn fillNode(cmd: *CDP.Command) !void {
|
||||
fn fillNode(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
backendNodeId: ?Node.Id = null,
|
||||
@@ -220,7 +217,7 @@ fn fillNode(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{}, .{});
|
||||
}
|
||||
|
||||
fn scrollNode(cmd: *CDP.Command) !void {
|
||||
fn scrollNode(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
backendNodeId: ?Node.Id = null,
|
||||
@@ -248,7 +245,7 @@ fn scrollNode(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{}, .{});
|
||||
}
|
||||
|
||||
fn waitForSelector(cmd: *CDP.Command) !void {
|
||||
fn waitForSelector(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
selector: []const u8,
|
||||
timeout: ?u32 = null,
|
||||
|
||||
@@ -18,21 +18,18 @@
|
||||
|
||||
const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const log = @import("../../log.zig");
|
||||
|
||||
const id = @import("../id.zig");
|
||||
const CDP = @import("../CDP.zig");
|
||||
const CdpStorage = @import("storage.zig");
|
||||
|
||||
const id = @import("../id.zig");
|
||||
const URL = @import("../../browser/URL.zig");
|
||||
const Transfer = @import("../../browser/HttpClient.zig").Transfer;
|
||||
const Notification = @import("../../Notification.zig");
|
||||
const Mime = @import("../../browser/Mime.zig");
|
||||
|
||||
const CdpStorage = @import("storage.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
disable,
|
||||
@@ -62,19 +59,19 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
}
|
||||
}
|
||||
|
||||
fn enable(cmd: *CDP.Command) !void {
|
||||
fn enable(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
try bc.networkEnable();
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn disable(cmd: *CDP.Command) !void {
|
||||
fn disable(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
bc.networkDisable();
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn setExtraHTTPHeaders(cmd: *CDP.Command) !void {
|
||||
fn setExtraHTTPHeaders(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
headers: std.json.ArrayHashMap([]const u8),
|
||||
})) orelse return error.InvalidParams;
|
||||
@@ -113,7 +110,7 @@ fn cookieMatches(cookie: *const Cookie, name: []const u8, domain: ?[]const u8, p
|
||||
return true;
|
||||
}
|
||||
|
||||
fn deleteCookies(cmd: *CDP.Command) !void {
|
||||
fn deleteCookies(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
name: []const u8,
|
||||
url: ?[:0]const u8 = null,
|
||||
@@ -147,14 +144,14 @@ fn deleteCookies(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn clearBrowserCookies(cmd: *CDP.Command) !void {
|
||||
fn clearBrowserCookies(cmd: anytype) !void {
|
||||
if (try cmd.params(struct {}) != null) return error.InvalidParams;
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
bc.session.cookie_jar.clearRetainingCapacity();
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn setCookie(cmd: *CDP.Command) !void {
|
||||
fn setCookie(cmd: anytype) !void {
|
||||
const params = (try cmd.params(
|
||||
CdpStorage.CdpCookie,
|
||||
)) orelse return error.InvalidParams;
|
||||
@@ -165,7 +162,7 @@ fn setCookie(cmd: *CDP.Command) !void {
|
||||
try cmd.sendResult(.{ .success = true }, .{});
|
||||
}
|
||||
|
||||
fn setCookies(cmd: *CDP.Command) !void {
|
||||
fn setCookies(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
cookies: []const CdpStorage.CdpCookie,
|
||||
})) orelse return error.InvalidParams;
|
||||
@@ -181,7 +178,7 @@ fn setCookies(cmd: *CDP.Command) !void {
|
||||
const GetCookiesParam = struct {
|
||||
urls: ?[]const [:0]const u8 = null,
|
||||
};
|
||||
fn getCookies(cmd: *CDP.Command) !void {
|
||||
fn getCookies(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const params = (try cmd.params(GetCookiesParam)) orelse GetCookiesParam{};
|
||||
|
||||
@@ -204,7 +201,7 @@ fn getCookies(cmd: *CDP.Command) !void {
|
||||
try cmd.sendResult(.{ .cookies = writer }, .{});
|
||||
}
|
||||
|
||||
fn getResponseBody(cmd: *CDP.Command) !void {
|
||||
fn getResponseBody(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
requestId: []const u8, // "REQ-{d}"
|
||||
})) orelse return error.InvalidParams;
|
||||
@@ -230,7 +227,7 @@ fn getResponseBody(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
pub fn httpRequestFail(bc: *CDP.BrowserContext, msg: *const Notification.RequestFail) !void {
|
||||
pub fn httpRequestFail(bc: anytype, msg: *const Notification.RequestFail) !void {
|
||||
// It's possible that the request failed because we aborted when the client
|
||||
// sent Target.closeTarget. In that case, bc.session_id will be cleared
|
||||
// already, and we can skip sending these messages to the client.
|
||||
@@ -250,7 +247,7 @@ pub fn httpRequestFail(bc: *CDP.BrowserContext, msg: *const Notification.Request
|
||||
}, .{ .session_id = session_id });
|
||||
}
|
||||
|
||||
pub fn httpRequestStart(bc: *CDP.BrowserContext, msg: *const Notification.RequestStart) !void {
|
||||
pub fn httpRequestStart(bc: anytype, msg: *const Notification.RequestStart) !void {
|
||||
// detachTarget could be called, in which case, we still have a page doing
|
||||
// things, but no session.
|
||||
const session_id = bc.session_id orelse return;
|
||||
@@ -279,7 +276,7 @@ pub fn httpRequestStart(bc: *CDP.BrowserContext, msg: *const Notification.Reques
|
||||
}, .{ .session_id = session_id });
|
||||
}
|
||||
|
||||
pub fn httpResponseHeaderDone(arena: Allocator, bc: *CDP.BrowserContext, msg: *const Notification.ResponseHeaderDone) !void {
|
||||
pub fn httpResponseHeaderDone(arena: Allocator, bc: anytype, msg: *const Notification.ResponseHeaderDone) !void {
|
||||
// detachTarget could be called, in which case, we still have a page doing
|
||||
// things, but no session.
|
||||
const session_id = bc.session_id orelse return;
|
||||
@@ -296,7 +293,7 @@ pub fn httpResponseHeaderDone(arena: Allocator, bc: *CDP.BrowserContext, msg: *c
|
||||
}, .{ .session_id = session_id });
|
||||
}
|
||||
|
||||
pub fn httpRequestDone(bc: *CDP.BrowserContext, msg: *const Notification.RequestDone) !void {
|
||||
pub fn httpRequestDone(bc: anytype, msg: *const Notification.RequestDone) !void {
|
||||
// detachTarget could be called, in which case, we still have a page doing
|
||||
// things, but no session.
|
||||
const session_id = bc.session_id orelse return;
|
||||
@@ -354,10 +351,6 @@ pub const TransferAsRequestWriter = struct {
|
||||
try jws.objectField(hdr.name);
|
||||
try jws.write(hdr.value);
|
||||
}
|
||||
if (try transfer.getCookieString()) |cookies| {
|
||||
try jws.objectField("Cookie");
|
||||
try jws.write(cookies[0 .. cookies.len - 1]);
|
||||
}
|
||||
try jws.endObject();
|
||||
}
|
||||
try jws.endObject();
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
|
||||
|
||||
//
|
||||
// Francis Bouvier <francis@lightpanda.io>
|
||||
// Pierre Tachoire <pierre@lightpanda.io>
|
||||
@@ -23,8 +22,6 @@ const lp = @import("lightpanda");
|
||||
const screenshot_png = @embedFile("screenshot.png");
|
||||
|
||||
const id = @import("../id.zig");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
const log = @import("../../log.zig");
|
||||
const js = @import("../../browser/js/js.zig");
|
||||
const URL = @import("../../browser/URL.zig");
|
||||
@@ -34,13 +31,12 @@ const Notification = @import("../../Notification.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
getFrameTree,
|
||||
setLifecycleEventsEnabled,
|
||||
addScriptToEvaluateOnNewDocument,
|
||||
removeScriptToEvaluateOnNewDocument,
|
||||
createIsolatedWorld,
|
||||
navigate,
|
||||
reload,
|
||||
@@ -55,7 +51,6 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
.getFrameTree => return getFrameTree(cmd),
|
||||
.setLifecycleEventsEnabled => return setLifecycleEventsEnabled(cmd),
|
||||
.addScriptToEvaluateOnNewDocument => return addScriptToEvaluateOnNewDocument(cmd),
|
||||
.removeScriptToEvaluateOnNewDocument => return removeScriptToEvaluateOnNewDocument(cmd),
|
||||
.createIsolatedWorld => return createIsolatedWorld(cmd),
|
||||
.navigate => return navigate(cmd),
|
||||
.reload => return doReload(cmd),
|
||||
@@ -81,7 +76,7 @@ const Frame = struct {
|
||||
gatedAPIFeatures: [][]const u8 = &[0][]const u8{},
|
||||
};
|
||||
|
||||
fn getFrameTree(cmd: *CDP.Command) !void {
|
||||
fn getFrameTree(cmd: anytype) !void {
|
||||
// Stagehand parses the response and error if we don't return a
|
||||
// correct one for this call when browser context or target id are missing.
|
||||
const startup = .{
|
||||
@@ -111,7 +106,7 @@ fn getFrameTree(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn setLifecycleEventsEnabled(cmd: *CDP.Command) !void {
|
||||
fn setLifecycleEventsEnabled(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
enabled: bool,
|
||||
})) orelse return error.InvalidParams;
|
||||
@@ -152,56 +147,23 @@ fn setLifecycleEventsEnabled(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn addScriptToEvaluateOnNewDocument(cmd: *CDP.Command) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
source: []const u8,
|
||||
worldName: ?[]const u8 = null,
|
||||
includeCommandLineAPI: bool = false,
|
||||
runImmediately: bool = false,
|
||||
})) orelse return error.InvalidParams;
|
||||
// TODO: hard coded method
|
||||
// With the command we receive a script we need to store and run for each new document.
|
||||
// Note that the worldName refers to the name given to the isolated world.
|
||||
fn addScriptToEvaluateOnNewDocument(cmd: anytype) !void {
|
||||
// const params = (try cmd.params(struct {
|
||||
// source: []const u8,
|
||||
// worldName: ?[]const u8 = null,
|
||||
// includeCommandLineAPI: bool = false,
|
||||
// runImmediately: bool = false,
|
||||
// })) orelse return error.InvalidParams;
|
||||
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
|
||||
if (params.runImmediately) {
|
||||
log.warn(.not_implemented, "addScriptOnNewDocument", .{ .param = "runImmediately" });
|
||||
}
|
||||
|
||||
const script_id = bc.next_script_id;
|
||||
bc.next_script_id += 1;
|
||||
|
||||
const source_dupe = try bc.arena.dupe(u8, params.source);
|
||||
try bc.scripts_on_new_document.append(bc.arena, .{
|
||||
.identifier = script_id,
|
||||
.source = source_dupe,
|
||||
});
|
||||
|
||||
var id_buf: [16]u8 = undefined;
|
||||
const id_str = std.fmt.bufPrint(&id_buf, "{d}", .{script_id}) catch "1";
|
||||
return cmd.sendResult(.{
|
||||
.identifier = id_str,
|
||||
.identifier = "1",
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn removeScriptToEvaluateOnNewDocument(cmd: *CDP.Command) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
identifier: []const u8,
|
||||
})) orelse return error.InvalidParams;
|
||||
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
|
||||
const target_id = std.fmt.parseInt(u32, params.identifier, 10) catch
|
||||
return cmd.sendResult(null, .{});
|
||||
|
||||
for (bc.scripts_on_new_document.items, 0..) |script, i| {
|
||||
if (script.identifier == target_id) {
|
||||
_ = bc.scripts_on_new_document.orderedRemove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn close(cmd: *CDP.Command) !void {
|
||||
fn close(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
|
||||
const target_id = bc.target_id orelse return error.TargetNotLoaded;
|
||||
@@ -238,7 +200,7 @@ fn close(cmd: *CDP.Command) !void {
|
||||
bc.target_id = null;
|
||||
}
|
||||
|
||||
fn createIsolatedWorld(cmd: *CDP.Command) !void {
|
||||
fn createIsolatedWorld(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
frameId: []const u8,
|
||||
worldName: []const u8,
|
||||
@@ -258,7 +220,7 @@ fn createIsolatedWorld(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{ .executionContextId = js_context.id }, .{});
|
||||
}
|
||||
|
||||
fn navigate(cmd: *CDP.Command) !void {
|
||||
fn navigate(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
url: [:0]const u8,
|
||||
// referrer: ?[]const u8 = null,
|
||||
@@ -292,7 +254,7 @@ fn navigate(cmd: *CDP.Command) !void {
|
||||
});
|
||||
}
|
||||
|
||||
fn doReload(cmd: *CDP.Command) !void {
|
||||
fn doReload(cmd: anytype) !void {
|
||||
const params = try cmd.params(struct {
|
||||
ignoreCache: ?bool = null,
|
||||
scriptToEvaluateOnLoad: ?[]const u8 = null,
|
||||
@@ -322,7 +284,7 @@ fn doReload(cmd: *CDP.Command) !void {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn pageNavigate(bc: *CDP.BrowserContext, event: *const Notification.PageNavigate) !void {
|
||||
pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void {
|
||||
// detachTarget could be called, in which case, we still have a page doing
|
||||
// things, but no session.
|
||||
const session_id = bc.session_id orelse return;
|
||||
@@ -374,7 +336,7 @@ pub fn pageNavigate(bc: *CDP.BrowserContext, event: *const Notification.PageNavi
|
||||
}, .{ .session_id = session_id });
|
||||
}
|
||||
|
||||
pub fn pageRemove(bc: *CDP.BrowserContext) !void {
|
||||
pub fn pageRemove(bc: anytype) !void {
|
||||
// Clear all remote object mappings to prevent stale objectIds from being used
|
||||
// after the context is destroy
|
||||
bc.inspector_session.inspector.resetContextGroup();
|
||||
@@ -385,7 +347,7 @@ pub fn pageRemove(bc: *CDP.BrowserContext) !void {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pageCreated(bc: *CDP.BrowserContext, page: *Page) !void {
|
||||
pub fn pageCreated(bc: anytype, page: *Page) !void {
|
||||
_ = bc.cdp.page_arena.reset(.{ .retain_with_limit = 1024 * 512 });
|
||||
|
||||
for (bc.isolated_worlds.items) |isolated_world| {
|
||||
@@ -397,7 +359,7 @@ pub fn pageCreated(bc: *CDP.BrowserContext, page: *Page) !void {
|
||||
bc.captured_responses = .empty;
|
||||
}
|
||||
|
||||
pub fn pageFrameCreated(bc: *CDP.BrowserContext, event: *const Notification.PageFrameCreated) !void {
|
||||
pub fn pageFrameCreated(bc: anytype, event: *const Notification.PageFrameCreated) !void {
|
||||
const session_id = bc.session_id orelse return;
|
||||
|
||||
const cdp = bc.cdp;
|
||||
@@ -418,7 +380,7 @@ pub fn pageFrameCreated(bc: *CDP.BrowserContext, event: *const Notification.Page
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pageNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const Notification.PageNavigated) !void {
|
||||
pub fn pageNavigated(arena: Allocator, bc: anytype, event: *const Notification.PageNavigated) !void {
|
||||
// detachTarget could be called, in which case, we still have a page doing
|
||||
// things, but no session.
|
||||
const session_id = bc.session_id orelse return;
|
||||
@@ -520,27 +482,6 @@ pub fn pageNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const No
|
||||
);
|
||||
}
|
||||
|
||||
// Evaluate scripts registered via Page.addScriptToEvaluateOnNewDocument.
|
||||
// Must run after the execution context is created but before the client
|
||||
// receives frameNavigated/loadEventFired so polyfills are available for
|
||||
// subsequent CDP commands.
|
||||
if (bc.scripts_on_new_document.items.len > 0) {
|
||||
var ls: js.Local.Scope = undefined;
|
||||
page.js.localScope(&ls);
|
||||
defer ls.deinit();
|
||||
|
||||
for (bc.scripts_on_new_document.items) |script| {
|
||||
var try_catch: lp.js.TryCatch = undefined;
|
||||
try_catch.init(&ls.local);
|
||||
defer try_catch.deinit();
|
||||
|
||||
ls.local.eval(script.source, null) catch |err| {
|
||||
const caught = try_catch.caughtOrError(arena, err);
|
||||
log.warn(.cdp, "script on new doc", .{ .caught = caught });
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// frameNavigated event
|
||||
try cdp.sendEvent("Page.frameNavigated", .{
|
||||
.type = "Navigation",
|
||||
@@ -600,15 +541,15 @@ pub fn pageNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const No
|
||||
}, .{ .session_id = session_id });
|
||||
}
|
||||
|
||||
pub fn pageNetworkIdle(bc: *CDP.BrowserContext, event: *const Notification.PageNetworkIdle) !void {
|
||||
pub fn pageNetworkIdle(bc: anytype, event: *const Notification.PageNetworkIdle) !void {
|
||||
return sendPageLifecycle(bc, "networkIdle", event.timestamp, &id.toFrameId(event.frame_id), &id.toLoaderId(event.req_id));
|
||||
}
|
||||
|
||||
pub fn pageNetworkAlmostIdle(bc: *CDP.BrowserContext, event: *const Notification.PageNetworkAlmostIdle) !void {
|
||||
pub fn pageNetworkAlmostIdle(bc: anytype, event: *const Notification.PageNetworkAlmostIdle) !void {
|
||||
return sendPageLifecycle(bc, "networkAlmostIdle", event.timestamp, &id.toFrameId(event.frame_id), &id.toLoaderId(event.req_id));
|
||||
}
|
||||
|
||||
fn sendPageLifecycle(bc: *CDP.BrowserContext, name: []const u8, timestamp: u64, frame_id: []const u8, loader_id: []const u8) !void {
|
||||
fn sendPageLifecycle(bc: anytype, name: []const u8, timestamp: u64, frame_id: []const u8, loader_id: []const u8) !void {
|
||||
// detachTarget could be called, in which case, we still have a page doing
|
||||
// things, but no session.
|
||||
const session_id = bc.session_id orelse return;
|
||||
@@ -643,7 +584,7 @@ fn base64Encode(comptime input: []const u8) [std.base64.standard.Encoder.calcSiz
|
||||
return buf;
|
||||
}
|
||||
|
||||
fn captureScreenshot(cmd: *CDP.Command) !void {
|
||||
fn captureScreenshot(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
format: ?[]const u8 = "png",
|
||||
quality: ?u8 = null,
|
||||
@@ -679,7 +620,7 @@ fn captureScreenshot(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn getLayoutMetrics(cmd: *CDP.Command) !void {
|
||||
fn getLayoutMetrics(cmd: anytype) !void {
|
||||
const width = 1920;
|
||||
const height = 1080;
|
||||
|
||||
@@ -899,55 +840,3 @@ test "cdp.page: reload" {
|
||||
try ctx.processMessage(.{ .id = 32, .method = "Page.reload", .params = .{ .ignoreCache = true } });
|
||||
}
|
||||
}
|
||||
|
||||
test "cdp.page: addScriptToEvaluateOnNewDocument" {
|
||||
var ctx = try testing.context();
|
||||
defer ctx.deinit();
|
||||
|
||||
var bc = try ctx.loadBrowserContext(.{ .id = "BID-9", .url = "hi.html", .target_id = "FID-000000000X".* });
|
||||
|
||||
{
|
||||
// Register a script — should return unique identifier "1"
|
||||
try ctx.processMessage(.{ .id = 20, .method = "Page.addScriptToEvaluateOnNewDocument", .params = .{ .source = "window.__test = 1" } });
|
||||
try ctx.expectSentResult(.{
|
||||
.identifier = "1",
|
||||
}, .{ .id = 20 });
|
||||
}
|
||||
|
||||
{
|
||||
// Register another script — should return identifier "2"
|
||||
try ctx.processMessage(.{ .id = 21, .method = "Page.addScriptToEvaluateOnNewDocument", .params = .{ .source = "window.__test2 = 2" } });
|
||||
try ctx.expectSentResult(.{
|
||||
.identifier = "2",
|
||||
}, .{ .id = 21 });
|
||||
}
|
||||
|
||||
{
|
||||
// Remove the first script — should succeed
|
||||
try ctx.processMessage(.{ .id = 22, .method = "Page.removeScriptToEvaluateOnNewDocument", .params = .{ .identifier = "1" } });
|
||||
try ctx.expectSentResult(null, .{ .id = 22 });
|
||||
}
|
||||
|
||||
{
|
||||
// Remove a non-existent identifier — should succeed silently
|
||||
try ctx.processMessage(.{ .id = 23, .method = "Page.removeScriptToEvaluateOnNewDocument", .params = .{ .identifier = "999" } });
|
||||
try ctx.expectSentResult(null, .{ .id = 23 });
|
||||
}
|
||||
|
||||
{
|
||||
try ctx.processMessage(.{ .id = 34, .method = "Page.reload" });
|
||||
// wait for this event, which is sent after we've run the registered scripts
|
||||
try ctx.expectSentEvent("Page.frameNavigated", .{
|
||||
.frame = .{ .loaderId = "LID-0000000002" },
|
||||
}, .{});
|
||||
|
||||
const page = bc.session.currentPage() orelse unreachable;
|
||||
|
||||
var ls: js.Local.Scope = undefined;
|
||||
page.js.localScope(&ls);
|
||||
defer ls.deinit();
|
||||
|
||||
const test_val = try ls.local.exec("window.__test2", null);
|
||||
try testing.expectEqual(2, try test_val.toI32());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
disable,
|
||||
|
||||
@@ -19,9 +19,7 @@
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
runIfWaitingForDebugger,
|
||||
@@ -38,7 +36,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
}
|
||||
}
|
||||
|
||||
fn sendInspector(cmd: *CDP.Command, action: anytype) !void {
|
||||
fn sendInspector(cmd: anytype, action: anytype) !void {
|
||||
// save script in file at debug mode
|
||||
if (builtin.mode == .Debug) {
|
||||
try logInspector(cmd, action);
|
||||
@@ -50,7 +48,7 @@ fn sendInspector(cmd: *CDP.Command, action: anytype) !void {
|
||||
bc.callInspector(cmd.input.json);
|
||||
}
|
||||
|
||||
fn logInspector(cmd: *CDP.Command, action: anytype) !void {
|
||||
fn logInspector(cmd: anytype, action: anytype) !void {
|
||||
const script = switch (action) {
|
||||
.evaluate => blk: {
|
||||
const params = (try cmd.params(struct {
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
enable,
|
||||
disable,
|
||||
@@ -33,7 +32,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
}
|
||||
}
|
||||
|
||||
fn setIgnoreCertificateErrors(cmd: *CDP.Command) !void {
|
||||
fn setIgnoreCertificateErrors(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
ignore: bool,
|
||||
})) orelse return error.InvalidParams;
|
||||
|
||||
@@ -18,16 +18,13 @@
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
const log = @import("../../log.zig");
|
||||
const URL = @import("../../browser/URL.zig");
|
||||
const Cookie = @import("../../browser/webapi/storage/storage.zig").Cookie;
|
||||
|
||||
const CookieJar = Cookie.Jar;
|
||||
pub const PreparedUri = Cookie.PreparedUri;
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
clearCookies,
|
||||
setCookies,
|
||||
@@ -43,7 +40,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
|
||||
const BrowserContextParam = struct { browserContextId: ?[]const u8 = null };
|
||||
|
||||
fn clearCookies(cmd: *CDP.Command) !void {
|
||||
fn clearCookies(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const params = (try cmd.params(BrowserContextParam)) orelse BrowserContextParam{};
|
||||
|
||||
@@ -58,7 +55,7 @@ fn clearCookies(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn getCookies(cmd: *CDP.Command) !void {
|
||||
fn getCookies(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const params = (try cmd.params(BrowserContextParam)) orelse BrowserContextParam{};
|
||||
|
||||
@@ -72,7 +69,7 @@ fn getCookies(cmd: *CDP.Command) !void {
|
||||
try cmd.sendResult(.{ .cookies = writer }, .{});
|
||||
}
|
||||
|
||||
fn setCookies(cmd: *CDP.Command) !void {
|
||||
fn setCookies(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const params = (try cmd.params(struct {
|
||||
cookies: []const CdpCookie,
|
||||
|
||||
@@ -20,13 +20,14 @@ const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
|
||||
const id = @import("../id.zig");
|
||||
const CDP = @import("../CDP.zig");
|
||||
|
||||
const log = @import("../../log.zig");
|
||||
const URL = @import("../../browser/URL.zig");
|
||||
const js = @import("../../browser/js/js.zig");
|
||||
|
||||
pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
// TODO: hard coded IDs
|
||||
const LOADER_ID = "LOADERID42AA389647D702B4D805F49A";
|
||||
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
getTargets,
|
||||
attachToTarget,
|
||||
@@ -62,7 +63,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
}
|
||||
}
|
||||
|
||||
fn getTargets(cmd: *CDP.Command) !void {
|
||||
fn getTargets(cmd: anytype) !void {
|
||||
// If no context available, return an empty array.
|
||||
const bc = cmd.browser_context orelse {
|
||||
return cmd.sendResult(.{
|
||||
@@ -88,7 +89,7 @@ fn getTargets(cmd: *CDP.Command) !void {
|
||||
}, .{ .include_session_id = false });
|
||||
}
|
||||
|
||||
fn getBrowserContexts(cmd: *CDP.Command) !void {
|
||||
fn getBrowserContexts(cmd: anytype) !void {
|
||||
var browser_context_ids: []const []const u8 = undefined;
|
||||
if (cmd.browser_context) |bc| {
|
||||
browser_context_ids = &.{bc.id};
|
||||
@@ -101,7 +102,7 @@ fn getBrowserContexts(cmd: *CDP.Command) !void {
|
||||
}, .{ .include_session_id = false });
|
||||
}
|
||||
|
||||
fn createBrowserContext(cmd: *CDP.Command) !void {
|
||||
fn createBrowserContext(cmd: anytype) !void {
|
||||
const params = try cmd.params(struct {
|
||||
disposeOnDetach: bool = false,
|
||||
proxyServer: ?[:0]const u8 = null,
|
||||
@@ -132,7 +133,7 @@ fn createBrowserContext(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn disposeBrowserContext(cmd: *CDP.Command) !void {
|
||||
fn disposeBrowserContext(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
browserContextId: []const u8,
|
||||
})) orelse return error.InvalidParams;
|
||||
@@ -143,7 +144,7 @@ fn disposeBrowserContext(cmd: *CDP.Command) !void {
|
||||
try cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn createTarget(cmd: *CDP.Command) !void {
|
||||
fn createTarget(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
url: [:0]const u8 = "about:blank",
|
||||
// width: ?u64 = null,
|
||||
@@ -232,7 +233,7 @@ fn createTarget(cmd: *CDP.Command) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn attachToTarget(cmd: *CDP.Command) !void {
|
||||
fn attachToTarget(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
targetId: []const u8,
|
||||
flatten: bool = true,
|
||||
@@ -249,7 +250,7 @@ fn attachToTarget(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{ .sessionId = bc.session_id }, .{});
|
||||
}
|
||||
|
||||
fn attachToBrowserTarget(cmd: *CDP.Command) !void {
|
||||
fn attachToBrowserTarget(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
|
||||
const session_id = bc.session_id orelse cmd.cdp.session_id_gen.next();
|
||||
@@ -271,7 +272,7 @@ fn attachToBrowserTarget(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(.{ .sessionId = bc.session_id }, .{});
|
||||
}
|
||||
|
||||
fn closeTarget(cmd: *CDP.Command) !void {
|
||||
fn closeTarget(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
targetId: []const u8,
|
||||
})) orelse return error.InvalidParams;
|
||||
@@ -312,7 +313,7 @@ fn closeTarget(cmd: *CDP.Command) !void {
|
||||
bc.target_id = null;
|
||||
}
|
||||
|
||||
fn getTargetInfo(cmd: *CDP.Command) !void {
|
||||
fn getTargetInfo(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
targetId: ?[]const u8 = null,
|
||||
};
|
||||
@@ -349,7 +350,7 @@ fn getTargetInfo(cmd: *CDP.Command) !void {
|
||||
}, .{ .include_session_id = false });
|
||||
}
|
||||
|
||||
fn sendMessageToTarget(cmd: *CDP.Command) !void {
|
||||
fn sendMessageToTarget(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
message: []const u8,
|
||||
sessionId: []const u8,
|
||||
@@ -367,19 +368,32 @@ fn sendMessageToTarget(cmd: *CDP.Command) !void {
|
||||
return error.UnknownSessionId;
|
||||
}
|
||||
|
||||
var aw = std.Io.Writer.Allocating.init(cmd.arena);
|
||||
cmd.cdp.dispatch(cmd.arena, .{ .capture = &aw.writer }, params.message) catch |err| {
|
||||
const Capture = struct {
|
||||
aw: std.Io.Writer.Allocating,
|
||||
|
||||
pub fn sendJSON(self: *@This(), message: anytype) !void {
|
||||
return std.json.Stringify.value(message, .{
|
||||
.emit_null_optional_fields = false,
|
||||
}, &self.aw.writer);
|
||||
}
|
||||
};
|
||||
|
||||
var capture = Capture{
|
||||
.aw = .init(cmd.arena),
|
||||
};
|
||||
|
||||
cmd.cdp.dispatch(cmd.arena, &capture, params.message) catch |err| {
|
||||
log.err(.cdp, "internal dispatch error", .{ .err = err, .id = cmd.input.id, .message = params.message });
|
||||
return err;
|
||||
};
|
||||
|
||||
try cmd.sendEvent("Target.receivedMessageFromTarget", .{
|
||||
.message = aw.written(),
|
||||
.message = capture.aw.written(),
|
||||
.sessionId = params.sessionId,
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn detachFromTarget(cmd: *CDP.Command) !void {
|
||||
fn detachFromTarget(cmd: anytype) !void {
|
||||
if (cmd.browser_context) |bc| {
|
||||
if (bc.session_id) |session_id| {
|
||||
try cmd.sendEvent("Target.detachedFromTarget", .{
|
||||
@@ -393,11 +407,11 @@ fn detachFromTarget(cmd: *CDP.Command) !void {
|
||||
}
|
||||
|
||||
// TODO: noop method
|
||||
fn setDiscoverTargets(cmd: *CDP.Command) !void {
|
||||
fn setDiscoverTargets(cmd: anytype) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn setAutoAttach(cmd: *CDP.Command) !void {
|
||||
fn setAutoAttach(cmd: anytype) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
autoAttach: bool,
|
||||
waitForDebuggerOnStart: bool,
|
||||
@@ -457,7 +471,7 @@ fn setAutoAttach(cmd: *CDP.Command) !void {
|
||||
try cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn doAttachtoTarget(cmd: *CDP.Command, target_id: []const u8) !void {
|
||||
fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void {
|
||||
const bc = cmd.browser_context.?;
|
||||
const session_id = bc.session_id orelse cmd.cdp.session_id_gen.next();
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const IS_DEBUG = @import("builtin").mode == .Debug;
|
||||
|
||||
pub fn toPageId(comptime id_type: enum { frame_id, loader_id }, input: []const u8) !u32 {
|
||||
const err = switch (comptime id_type) {
|
||||
|
||||
@@ -19,6 +19,10 @@
|
||||
const std = @import("std");
|
||||
const json = std.json;
|
||||
const posix = std.posix;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArenaAllocator = std.heap.ArenaAllocator;
|
||||
|
||||
const Testing = @This();
|
||||
|
||||
const CDP = @import("CDP.zig");
|
||||
const Server = @import("../Server.zig");
|
||||
@@ -64,7 +68,7 @@ const TestContext = struct {
|
||||
session_id: ?[]const u8 = null,
|
||||
url: ?[:0]const u8 = null,
|
||||
};
|
||||
pub fn loadBrowserContext(self: *TestContext, opts: BrowserContextOpts) !*CDP.BrowserContext {
|
||||
pub fn loadBrowserContext(self: *TestContext, opts: BrowserContextOpts) !*CDP.BrowserContext(CDP) {
|
||||
var c = self.cdp();
|
||||
if (c.browser_context) |bc| {
|
||||
_ = c.disposeBrowserContext(bc.id);
|
||||
@@ -168,26 +172,13 @@ const TestContext = struct {
|
||||
index: ?usize = null,
|
||||
};
|
||||
pub fn expectSent(self: *TestContext, expected: anytype, opts: SentOpts) !void {
|
||||
const expected_json = blk: {
|
||||
// Zig makes this hard. When sendJSON is called, we're sending an anytype.
|
||||
// We can't record that in an ArrayList(???), so we serialize it to JSON.
|
||||
// Now, ideally, we could just take our expected structure, serialize it to
|
||||
// json and check if the two are equal.
|
||||
// Except serializing to JSON isn't deterministic.
|
||||
// So we serialize the JSON then we deserialize to json.Value. And then we can
|
||||
// compare our anytype expectation with the json.Value that we captured
|
||||
|
||||
const serialized = try json.Stringify.valueAlloc(base.arena_allocator, expected, .{
|
||||
.whitespace = .indent_2,
|
||||
.emit_null_optional_fields = false,
|
||||
});
|
||||
|
||||
break :blk try std.json.parseFromSliceLeaky(json.Value, base.arena_allocator, serialized, .{});
|
||||
};
|
||||
|
||||
for (0..5) |_| {
|
||||
for (self.received.items, 0..) |received, i| {
|
||||
if (try base.isEqualJson(expected_json, received) == false) {
|
||||
if (try compareExpectedToSent(serialized, received) == false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -200,15 +191,6 @@ const TestContext = struct {
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (self.cdp_) |*cdp__| {
|
||||
if (cdp__.browser_context) |*bc| {
|
||||
if (bc.session.page != null) {
|
||||
var runner = try bc.session.runner(.{});
|
||||
_ = try runner.tick(.{ .ms = 1000 });
|
||||
}
|
||||
}
|
||||
}
|
||||
std.Thread.sleep(5 * std.time.ns_per_ms);
|
||||
try self.read();
|
||||
}
|
||||
@@ -321,3 +303,17 @@ pub fn context() !TestContext {
|
||||
.socket = pair[0],
|
||||
};
|
||||
}
|
||||
|
||||
// Zig makes this hard. When sendJSON is called, we're sending an anytype.
|
||||
// We can't record that in an ArrayList(???), so we serialize it to JSON.
|
||||
// Now, ideally, we could just take our expected structure, serialize it to
|
||||
// json and check if the two are equal.
|
||||
// Except serializing to JSON isn't deterministic.
|
||||
// So we serialize the JSON then we deserialize to json.Value. And then we can
|
||||
// compare our anytype expectation with the json.Value that we captured
|
||||
|
||||
fn compareExpectedToSent(expected: []const u8, actual: json.Value) !bool {
|
||||
const expected_value = try std.json.parseFromSlice(json.Value, std.testing.allocator, expected, .{});
|
||||
defer expected_value.deinit();
|
||||
return base.isEqualJson(expected_value.value, actual);
|
||||
}
|
||||
|
||||
@@ -49,6 +49,9 @@ const Opts = struct {
|
||||
|
||||
pub var opts = Opts{};
|
||||
|
||||
// synchronizes writes to the output
|
||||
var out_lock: Thread.Mutex = .{};
|
||||
|
||||
// synchronizes access to last_log
|
||||
var last_log_lock: Thread.Mutex = .{};
|
||||
|
||||
|
||||
@@ -110,3 +110,5 @@ pub fn handleRead(server: *Server, arena: std.mem.Allocator, req: protocol.Reque
|
||||
return server.sendError(req_id, .InternalError, "Failed to serialize resource content");
|
||||
};
|
||||
}
|
||||
|
||||
const testing = @import("../testing.zig");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
const protocol = @import("protocol.zig");
|
||||
const resources = @import("resources.zig");
|
||||
const Server = @import("Server.zig");
|
||||
|
||||
@@ -4,7 +4,9 @@ const lp = @import("lightpanda");
|
||||
const log = lp.log;
|
||||
const js = lp.js;
|
||||
|
||||
const Element = @import("../browser/webapi/Element.zig");
|
||||
const DOMNode = @import("../browser/webapi/Node.zig");
|
||||
const Selector = @import("../browser/webapi/selector/Selector.zig");
|
||||
const protocol = @import("protocol.zig");
|
||||
const Server = @import("Server.zig");
|
||||
const CDPNode = @import("../cdp/Node.zig");
|
||||
|
||||
@@ -461,7 +461,7 @@ fn drainQueue(self: *Runtime) void {
|
||||
self.releaseConnection(conn);
|
||||
continue;
|
||||
};
|
||||
libcurl.curl_multi_add_handle(multi, conn._easy) catch |err| {
|
||||
libcurl.curl_multi_add_handle(multi, conn.easy) catch |err| {
|
||||
lp.log.err(.app, "curl multi add", .{ .err = err });
|
||||
self.releaseConnection(conn);
|
||||
};
|
||||
@@ -565,7 +565,7 @@ pub fn getConnection(self: *Runtime) ?*net_http.Connection {
|
||||
}
|
||||
|
||||
pub fn releaseConnection(self: *Runtime, conn: *net_http.Connection) void {
|
||||
conn.reset(self.config, self.ca_blob) catch |err| {
|
||||
conn.reset() catch |err| {
|
||||
lp.assert(false, "couldn't reset curl easy", .{ .err = err });
|
||||
};
|
||||
|
||||
|
||||
@@ -17,6 +17,10 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const posix = std.posix;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArenaAllocator = std.heap.ArenaAllocator;
|
||||
|
||||
const Config = @import("../Config.zig");
|
||||
const libcurl = @import("../sys/libcurl.zig");
|
||||
@@ -25,12 +29,18 @@ const log = @import("lightpanda").log;
|
||||
const assert = @import("lightpanda").assert;
|
||||
|
||||
pub const ENABLE_DEBUG = false;
|
||||
const IS_DEBUG = builtin.mode == .Debug;
|
||||
|
||||
pub const Blob = libcurl.CurlBlob;
|
||||
pub const WaitFd = libcurl.CurlWaitFd;
|
||||
pub const writefunc_error = libcurl.curl_writefunc_error;
|
||||
|
||||
const Error = libcurl.Error;
|
||||
const ErrorMulti = libcurl.ErrorMulti;
|
||||
const errorFromCode = libcurl.errorFromCode;
|
||||
const errorMFromCode = libcurl.errorMFromCode;
|
||||
const errorCheck = libcurl.errorCheck;
|
||||
const errorMCheck = libcurl.errorMCheck;
|
||||
|
||||
pub fn curl_version() [*c]const u8 {
|
||||
return libcurl.curl_version();
|
||||
@@ -54,13 +64,14 @@ pub const Header = struct {
|
||||
|
||||
pub const Headers = struct {
|
||||
headers: ?*libcurl.CurlSList,
|
||||
cookies: ?[*c]const u8,
|
||||
|
||||
pub fn init(user_agent: [:0]const u8) !Headers {
|
||||
const header_list = libcurl.curl_slist_append(null, user_agent);
|
||||
if (header_list == null) {
|
||||
return error.OutOfMemory;
|
||||
}
|
||||
return .{ .headers = header_list };
|
||||
return .{ .headers = header_list, .cookies = null };
|
||||
}
|
||||
|
||||
pub fn deinit(self: *const Headers) void {
|
||||
@@ -91,14 +102,20 @@ pub const Headers = struct {
|
||||
pub fn iterator(self: *Headers) Iterator {
|
||||
return .{
|
||||
.header = self.headers,
|
||||
.cookies = self.cookies,
|
||||
};
|
||||
}
|
||||
|
||||
const Iterator = struct {
|
||||
header: [*c]libcurl.CurlSList,
|
||||
cookies: ?[*c]const u8,
|
||||
|
||||
pub fn next(self: *Iterator) ?Header {
|
||||
const h = self.header orelse return null;
|
||||
const h = self.header orelse {
|
||||
const cookies = self.cookies orelse return null;
|
||||
self.cookies = null;
|
||||
return .{ .name = "Cookie", .value = std.mem.span(@as([*:0]const u8, cookies)) };
|
||||
};
|
||||
|
||||
self.header = h.*.next;
|
||||
return parseHeader(std.mem.span(@as([*:0]const u8, @ptrCast(h.*.data))));
|
||||
@@ -125,7 +142,7 @@ pub const HeaderIterator = union(enum) {
|
||||
prev: ?*libcurl.CurlHeader = null,
|
||||
|
||||
pub fn next(self: *CurlHeaderIterator) ?Header {
|
||||
const h = libcurl.curl_easy_nextheader(self.conn._easy, .header, -1, self.prev) orelse return null;
|
||||
const h = libcurl.curl_easy_nextheader(self.conn.easy, .header, -1, self.prev) orelse return null;
|
||||
self.prev = h;
|
||||
|
||||
const header = h.*;
|
||||
@@ -157,24 +174,33 @@ const HeaderValue = struct {
|
||||
};
|
||||
|
||||
pub const AuthChallenge = struct {
|
||||
const Source = enum { server, proxy };
|
||||
const Scheme = enum { basic, digest };
|
||||
|
||||
status: u16,
|
||||
source: ?Source,
|
||||
scheme: ?Scheme,
|
||||
source: ?enum { server, proxy },
|
||||
scheme: ?enum { basic, digest },
|
||||
realm: ?[]const u8,
|
||||
|
||||
pub fn parse(status: u16, source: Source, value: []const u8) !AuthChallenge {
|
||||
pub fn parse(status: u16, header: []const u8) !AuthChallenge {
|
||||
var ac: AuthChallenge = .{
|
||||
.status = status,
|
||||
.source = source,
|
||||
.source = null,
|
||||
.realm = null,
|
||||
.scheme = null,
|
||||
};
|
||||
|
||||
const pos = std.mem.indexOfPos(u8, std.mem.trim(u8, value, std.ascii.whitespace[0..]), 0, " ") orelse value.len;
|
||||
const _scheme = value[0..pos];
|
||||
const sep = std.mem.indexOfPos(u8, header, 0, ": ") orelse return error.InvalidHeader;
|
||||
const hname = header[0..sep];
|
||||
const hvalue = header[sep + 2 ..];
|
||||
|
||||
if (std.ascii.eqlIgnoreCase("WWW-Authenticate", hname)) {
|
||||
ac.source = .server;
|
||||
} else if (std.ascii.eqlIgnoreCase("Proxy-Authenticate", hname)) {
|
||||
ac.source = .proxy;
|
||||
} else {
|
||||
return error.InvalidAuthChallenge;
|
||||
}
|
||||
|
||||
const pos = std.mem.indexOfPos(u8, std.mem.trim(u8, hvalue, std.ascii.whitespace[0..]), 0, " ") orelse hvalue.len;
|
||||
const _scheme = hvalue[0..pos];
|
||||
if (std.ascii.eqlIgnoreCase(_scheme, "basic")) {
|
||||
ac.scheme = .basic;
|
||||
} else if (std.ascii.eqlIgnoreCase(_scheme, "digest")) {
|
||||
@@ -210,28 +236,77 @@ pub const ResponseHead = struct {
|
||||
};
|
||||
|
||||
pub const Connection = struct {
|
||||
_easy: *libcurl.Curl,
|
||||
easy: *libcurl.Curl,
|
||||
node: std.DoublyLinkedList.Node = .{},
|
||||
|
||||
pub fn init(
|
||||
ca_blob: ?libcurl.CurlBlob,
|
||||
ca_blob_: ?libcurl.CurlBlob,
|
||||
config: *const Config,
|
||||
) !Connection {
|
||||
const easy = libcurl.curl_easy_init() orelse return error.FailedToInitializeEasy;
|
||||
errdefer libcurl.curl_easy_cleanup(easy);
|
||||
|
||||
const self = Connection{ ._easy = easy };
|
||||
errdefer self.deinit();
|
||||
// timeouts
|
||||
try libcurl.curl_easy_setopt(easy, .timeout_ms, config.httpTimeout());
|
||||
try libcurl.curl_easy_setopt(easy, .connect_timeout_ms, config.httpConnectTimeout());
|
||||
|
||||
try self.reset(config, ca_blob);
|
||||
return self;
|
||||
// redirect behavior
|
||||
try libcurl.curl_easy_setopt(easy, .max_redirs, config.httpMaxRedirects());
|
||||
try libcurl.curl_easy_setopt(easy, .follow_location, 2);
|
||||
try libcurl.curl_easy_setopt(easy, .redir_protocols_str, "HTTP,HTTPS"); // remove FTP and FTPS from the default
|
||||
|
||||
// proxy
|
||||
const http_proxy = config.httpProxy();
|
||||
if (http_proxy) |proxy| {
|
||||
try libcurl.curl_easy_setopt(easy, .proxy, proxy.ptr);
|
||||
}
|
||||
|
||||
// tls
|
||||
if (ca_blob_) |ca_blob| {
|
||||
try libcurl.curl_easy_setopt(easy, .ca_info_blob, ca_blob);
|
||||
if (http_proxy != null) {
|
||||
try libcurl.curl_easy_setopt(easy, .proxy_ca_info_blob, ca_blob);
|
||||
}
|
||||
} else {
|
||||
assert(config.tlsVerifyHost() == false, "Http.init tls_verify_host", .{});
|
||||
|
||||
try libcurl.curl_easy_setopt(easy, .ssl_verify_host, false);
|
||||
try libcurl.curl_easy_setopt(easy, .ssl_verify_peer, false);
|
||||
|
||||
if (http_proxy != null) {
|
||||
try libcurl.curl_easy_setopt(easy, .proxy_ssl_verify_host, false);
|
||||
try libcurl.curl_easy_setopt(easy, .proxy_ssl_verify_peer, false);
|
||||
}
|
||||
}
|
||||
|
||||
// compression, don't remove this. CloudFront will send gzip content
|
||||
// even if we don't support it, and then it won't be decompressed.
|
||||
// empty string means: use whatever's available
|
||||
try libcurl.curl_easy_setopt(easy, .accept_encoding, "");
|
||||
|
||||
// debug
|
||||
if (comptime ENABLE_DEBUG) {
|
||||
try libcurl.curl_easy_setopt(easy, .verbose, true);
|
||||
|
||||
// Sometimes the default debug output hides some useful data. You can
|
||||
// uncomment the following line (BUT KEEP THE LIVE ABOVE AS-IS), to
|
||||
// get more control over the data (specifically, the `CURLINFO_TEXT`
|
||||
// can include useful data).
|
||||
|
||||
// try libcurl.curl_easy_setopt(easy, .debug_function, debugCallback);
|
||||
}
|
||||
|
||||
return .{
|
||||
.easy = easy,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *const Connection) void {
|
||||
libcurl.curl_easy_cleanup(self._easy);
|
||||
libcurl.curl_easy_cleanup(self.easy);
|
||||
}
|
||||
|
||||
pub fn setURL(self: *const Connection, url: [:0]const u8) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .url, url.ptr);
|
||||
try libcurl.curl_easy_setopt(self.easy, .url, url.ptr);
|
||||
}
|
||||
|
||||
// a libcurl request has 2 methods. The first is the method that
|
||||
@@ -254,7 +329,7 @@ pub const Connection = struct {
|
||||
// can infer that based on the presence of the body, but we also reset it
|
||||
// to be safe);
|
||||
pub fn setMethod(self: *const Connection, method: Method) !void {
|
||||
const easy = self._easy;
|
||||
const easy = self.easy;
|
||||
const m: [:0]const u8 = switch (method) {
|
||||
.GET => "GET",
|
||||
.POST => "POST",
|
||||
@@ -269,97 +344,56 @@ pub const Connection = struct {
|
||||
}
|
||||
|
||||
pub fn setBody(self: *const Connection, body: []const u8) !void {
|
||||
const easy = self._easy;
|
||||
const easy = self.easy;
|
||||
try libcurl.curl_easy_setopt(easy, .post, true);
|
||||
try libcurl.curl_easy_setopt(easy, .post_field_size, body.len);
|
||||
try libcurl.curl_easy_setopt(easy, .copy_post_fields, body.ptr);
|
||||
}
|
||||
|
||||
pub fn setGetMode(self: *const Connection) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .http_get, true);
|
||||
try libcurl.curl_easy_setopt(self.easy, .http_get, true);
|
||||
}
|
||||
|
||||
pub fn setHeaders(self: *const Connection, headers: *Headers) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .http_header, headers.headers);
|
||||
try libcurl.curl_easy_setopt(self.easy, .http_header, headers.headers);
|
||||
}
|
||||
|
||||
pub fn setCookies(self: *const Connection, cookies: [*c]const u8) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .cookie, cookies);
|
||||
try libcurl.curl_easy_setopt(self.easy, .cookie, cookies);
|
||||
}
|
||||
|
||||
pub fn setPrivate(self: *const Connection, ptr: *anyopaque) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .private, ptr);
|
||||
try libcurl.curl_easy_setopt(self.easy, .private, ptr);
|
||||
}
|
||||
|
||||
pub fn setProxyCredentials(self: *const Connection, creds: [:0]const u8) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .proxy_user_pwd, creds.ptr);
|
||||
try libcurl.curl_easy_setopt(self.easy, .proxy_user_pwd, creds.ptr);
|
||||
}
|
||||
|
||||
pub fn setCredentials(self: *const Connection, creds: [:0]const u8) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .user_pwd, creds.ptr);
|
||||
try libcurl.curl_easy_setopt(self.easy, .user_pwd, creds.ptr);
|
||||
}
|
||||
|
||||
pub fn setCallbacks(
|
||||
self: *Connection,
|
||||
self: *const Connection,
|
||||
comptime header_cb: libcurl.CurlHeaderFunction,
|
||||
comptime data_cb: libcurl.CurlWriteFunction,
|
||||
) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .write_data, self);
|
||||
try libcurl.curl_easy_setopt(self._easy, .write_function, data_cb);
|
||||
try libcurl.curl_easy_setopt(self.easy, .header_data, self.easy);
|
||||
try libcurl.curl_easy_setopt(self.easy, .header_function, header_cb);
|
||||
try libcurl.curl_easy_setopt(self.easy, .write_data, self.easy);
|
||||
try libcurl.curl_easy_setopt(self.easy, .write_function, data_cb);
|
||||
}
|
||||
|
||||
pub fn reset(
|
||||
self: *const Connection,
|
||||
config: *const Config,
|
||||
ca_blob: ?libcurl.CurlBlob,
|
||||
) !void {
|
||||
libcurl.curl_easy_reset(self._easy);
|
||||
pub fn reset(self: *const Connection) !void {
|
||||
try libcurl.curl_easy_setopt(self.easy, .proxy, null);
|
||||
try libcurl.curl_easy_setopt(self.easy, .http_header, null);
|
||||
|
||||
// timeouts
|
||||
try libcurl.curl_easy_setopt(self._easy, .timeout_ms, config.httpTimeout());
|
||||
try libcurl.curl_easy_setopt(self._easy, .connect_timeout_ms, config.httpConnectTimeout());
|
||||
try libcurl.curl_easy_setopt(self.easy, .header_data, null);
|
||||
try libcurl.curl_easy_setopt(self.easy, .header_function, null);
|
||||
|
||||
// compression, don't remove this. CloudFront will send gzip content
|
||||
// even if we don't support it, and then it won't be decompressed.
|
||||
// empty string means: use whatever's available
|
||||
try libcurl.curl_easy_setopt(self._easy, .accept_encoding, "");
|
||||
|
||||
// proxy
|
||||
const http_proxy = config.httpProxy();
|
||||
if (http_proxy) |proxy| {
|
||||
try libcurl.curl_easy_setopt(self._easy, .proxy, proxy.ptr);
|
||||
} else {
|
||||
try libcurl.curl_easy_setopt(self._easy, .proxy, null);
|
||||
}
|
||||
|
||||
// tls
|
||||
if (ca_blob) |ca| {
|
||||
try libcurl.curl_easy_setopt(self._easy, .ca_info_blob, ca);
|
||||
if (http_proxy != null) {
|
||||
try libcurl.curl_easy_setopt(self._easy, .proxy_ca_info_blob, ca);
|
||||
}
|
||||
} else {
|
||||
assert(config.tlsVerifyHost() == false, "Http.init tls_verify_host", .{});
|
||||
|
||||
try libcurl.curl_easy_setopt(self._easy, .ssl_verify_host, false);
|
||||
try libcurl.curl_easy_setopt(self._easy, .ssl_verify_peer, false);
|
||||
|
||||
if (http_proxy != null) {
|
||||
try libcurl.curl_easy_setopt(self._easy, .proxy_ssl_verify_host, false);
|
||||
try libcurl.curl_easy_setopt(self._easy, .proxy_ssl_verify_peer, false);
|
||||
}
|
||||
}
|
||||
|
||||
// debug
|
||||
if (comptime ENABLE_DEBUG) {
|
||||
try libcurl.curl_easy_setopt(self._easy, .verbose, true);
|
||||
|
||||
// Sometimes the default debug output hides some useful data. You can
|
||||
// uncomment the following line (BUT KEEP THE LIVE ABOVE AS-IS), to
|
||||
// get more control over the data (specifically, the `CURLINFO_TEXT`
|
||||
// can include useful data).
|
||||
|
||||
// try libcurl.curl_easy_setopt(easy, .debug_function, debugCallback);
|
||||
}
|
||||
try libcurl.curl_easy_setopt(self.easy, .write_data, null);
|
||||
try libcurl.curl_easy_setopt(self.easy, .write_function, discardBody);
|
||||
}
|
||||
|
||||
fn discardBody(_: [*]const u8, count: usize, len: usize, _: ?*anyopaque) usize {
|
||||
@@ -367,31 +401,27 @@ pub const Connection = struct {
|
||||
}
|
||||
|
||||
pub fn setProxy(self: *const Connection, proxy: ?[:0]const u8) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .proxy, if (proxy) |p| p.ptr else null);
|
||||
}
|
||||
|
||||
pub fn setFollowLocation(self: *const Connection, follow: bool) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .follow_location, @as(c_long, if (follow) 2 else 0));
|
||||
try libcurl.curl_easy_setopt(self.easy, .proxy, if (proxy) |p| p.ptr else null);
|
||||
}
|
||||
|
||||
pub fn setTlsVerify(self: *const Connection, verify: bool, use_proxy: bool) !void {
|
||||
try libcurl.curl_easy_setopt(self._easy, .ssl_verify_host, verify);
|
||||
try libcurl.curl_easy_setopt(self._easy, .ssl_verify_peer, verify);
|
||||
try libcurl.curl_easy_setopt(self.easy, .ssl_verify_host, verify);
|
||||
try libcurl.curl_easy_setopt(self.easy, .ssl_verify_peer, verify);
|
||||
if (use_proxy) {
|
||||
try libcurl.curl_easy_setopt(self._easy, .proxy_ssl_verify_host, verify);
|
||||
try libcurl.curl_easy_setopt(self._easy, .proxy_ssl_verify_peer, verify);
|
||||
try libcurl.curl_easy_setopt(self.easy, .proxy_ssl_verify_host, verify);
|
||||
try libcurl.curl_easy_setopt(self.easy, .proxy_ssl_verify_peer, verify);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getEffectiveUrl(self: *const Connection) ![*c]const u8 {
|
||||
var url: [*c]u8 = undefined;
|
||||
try libcurl.curl_easy_getinfo(self._easy, .effective_url, &url);
|
||||
try libcurl.curl_easy_getinfo(self.easy, .effective_url, &url);
|
||||
return url;
|
||||
}
|
||||
|
||||
pub fn getResponseCode(self: *const Connection) !u16 {
|
||||
var status: c_long = undefined;
|
||||
try libcurl.curl_easy_getinfo(self._easy, .response_code, &status);
|
||||
try libcurl.curl_easy_getinfo(self.easy, .response_code, &status);
|
||||
if (status < 0 or status > std.math.maxInt(u16)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -400,13 +430,13 @@ pub const Connection = struct {
|
||||
|
||||
pub fn getRedirectCount(self: *const Connection) !u32 {
|
||||
var count: c_long = undefined;
|
||||
try libcurl.curl_easy_getinfo(self._easy, .redirect_count, &count);
|
||||
try libcurl.curl_easy_getinfo(self.easy, .redirect_count, &count);
|
||||
return @intCast(count);
|
||||
}
|
||||
|
||||
pub fn getResponseHeader(self: *const Connection, name: [:0]const u8, index: usize) ?HeaderValue {
|
||||
var hdr: ?*libcurl.CurlHeader = null;
|
||||
libcurl.curl_easy_header(self._easy, name, index, .header, -1, &hdr) catch |err| {
|
||||
libcurl.curl_easy_header(self.easy, name, index, .header, -1, &hdr) catch |err| {
|
||||
// ErrorHeader includes OutOfMemory — rare but real errors from curl internals.
|
||||
// Logged and returned as null since callers don't expect errors.
|
||||
log.err(.http, "get response header", .{
|
||||
@@ -424,7 +454,7 @@ pub const Connection = struct {
|
||||
|
||||
pub fn getPrivate(self: *const Connection) !*anyopaque {
|
||||
var private: *anyopaque = undefined;
|
||||
try libcurl.curl_easy_getinfo(self._easy, .private, &private);
|
||||
try libcurl.curl_easy_getinfo(self.easy, .private, &private);
|
||||
return private;
|
||||
}
|
||||
|
||||
@@ -441,7 +471,12 @@ pub const Connection = struct {
|
||||
try self.secretHeaders(&header_list, http_headers);
|
||||
try self.setHeaders(&header_list);
|
||||
|
||||
try libcurl.curl_easy_perform(self._easy);
|
||||
// Add cookies.
|
||||
if (header_list.cookies) |cookies| {
|
||||
try self.setCookies(cookies);
|
||||
}
|
||||
|
||||
try libcurl.curl_easy_perform(self.easy);
|
||||
return self.getResponseCode();
|
||||
}
|
||||
};
|
||||
@@ -463,11 +498,11 @@ pub const Handles = struct {
|
||||
}
|
||||
|
||||
pub fn add(self: *Handles, conn: *const Connection) !void {
|
||||
try libcurl.curl_multi_add_handle(self.multi, conn._easy);
|
||||
try libcurl.curl_multi_add_handle(self.multi, conn.easy);
|
||||
}
|
||||
|
||||
pub fn remove(self: *Handles, conn: *const Connection) !void {
|
||||
try libcurl.curl_multi_remove_handle(self.multi, conn._easy);
|
||||
try libcurl.curl_multi_remove_handle(self.multi, conn.easy);
|
||||
}
|
||||
|
||||
pub fn perform(self: *Handles) !c_int {
|
||||
@@ -490,7 +525,7 @@ pub const Handles = struct {
|
||||
const msg = libcurl.curl_multi_info_read(self.multi, &messages_count) orelse return null;
|
||||
return switch (msg.data) {
|
||||
.done => |err| .{
|
||||
.conn = .{ ._easy = msg.easy_handle },
|
||||
.conn = .{ .easy = msg.easy_handle },
|
||||
.err = err,
|
||||
},
|
||||
else => unreachable,
|
||||
|
||||
@@ -516,10 +516,6 @@ pub fn curl_easy_cleanup(easy: *Curl) void {
|
||||
c.curl_easy_cleanup(easy);
|
||||
}
|
||||
|
||||
pub fn curl_easy_reset(easy: *Curl) void {
|
||||
c.curl_easy_reset(easy);
|
||||
}
|
||||
|
||||
pub fn curl_easy_perform(easy: *Curl) Error!void {
|
||||
try errorCheck(c.curl_easy_perform(easy));
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ const App = @import("../App.zig");
|
||||
const Config = @import("../Config.zig");
|
||||
const telemetry = @import("telemetry.zig");
|
||||
const Runtime = @import("../network/Runtime.zig");
|
||||
const Connection = @import("../network/http.zig").Connection;
|
||||
|
||||
const URL = "https://telemetry.lightpanda.io";
|
||||
const BUFFER_SIZE = 1024;
|
||||
|
||||
Reference in New Issue
Block a user