mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-04-02 10:19:17 +00:00
Compare commits
4 Commits
http-clien
...
mcp-new-ac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c9a5ddab8 | ||
|
|
46a63e0b4b | ||
|
|
58143ee3d1 | ||
|
|
5e79af42f4 |
15
.github/workflows/e2e-integration-test.yml
vendored
15
.github/workflows/e2e-integration-test.yml
vendored
@@ -60,20 +60,7 @@ jobs:
|
|||||||
- run: chmod a+x ./lightpanda
|
- run: chmod a+x ./lightpanda
|
||||||
|
|
||||||
- name: run end to end integration tests
|
- name: run end to end integration tests
|
||||||
continue-on-error: true
|
|
||||||
run: |
|
run: |
|
||||||
./lightpanda serve --log-level error & echo $! > LPD.pid
|
./lightpanda serve --log-level error & echo $! > LPD.pid
|
||||||
go run integration/main.go |tee result.log
|
go run integration/main.go
|
||||||
kill `cat LPD.pid`
|
kill `cat LPD.pid`
|
||||||
|
|
||||||
- name: Send result to slack
|
|
||||||
uses: slackapi/slack-github-action@v3.0.1
|
|
||||||
with:
|
|
||||||
errors: true
|
|
||||||
method: files.uploadV2
|
|
||||||
token: ${{ secrets.CI_SLACK_BOT_TOKEN }}
|
|
||||||
payload: |
|
|
||||||
channel_id: ${{ vars.E2E_SLACK_CHANNEL_ID }}
|
|
||||||
initial_comment: "Last e2e integration tests"
|
|
||||||
file: "./result.log"
|
|
||||||
filename: "e2e-integration-${{ github.sha }}.txt"
|
|
||||||
|
|||||||
31
.github/workflows/wpt.yml
vendored
31
.github/workflows/wpt.yml
vendored
@@ -153,34 +153,3 @@ jobs:
|
|||||||
|
|
||||||
- name: format and send json result
|
- name: format and send json result
|
||||||
run: /perf-fmt wpt ${{ github.sha }} wpt.json
|
run: /perf-fmt wpt ${{ github.sha }} wpt.json
|
||||||
|
|
||||||
wptdiff:
|
|
||||||
name: perf-fmt
|
|
||||||
needs: perf-fmt
|
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v6
|
|
||||||
with:
|
|
||||||
repository: 'lightpanda-io/demo'
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- run: |
|
|
||||||
cd ./wptdiff
|
|
||||||
CGO_ENABLED=0 go build
|
|
||||||
|
|
||||||
- run: |
|
|
||||||
./wptdiff/wptdiff |tee diff.log
|
|
||||||
|
|
||||||
- name: Send regression to slack
|
|
||||||
uses: slackapi/slack-github-action@v3.0.1
|
|
||||||
with:
|
|
||||||
errors: true
|
|
||||||
method: files.uploadV2
|
|
||||||
token: ${{ secrets.CI_SLACK_BOT_TOKEN }}
|
|
||||||
payload: |
|
|
||||||
channel_id: ${{ vars.WPT_SLACK_CHANNEL_ID }}
|
|
||||||
initial_comment: "Last WPT regressions"
|
|
||||||
file: "./diff.log"
|
|
||||||
filename: "wpt-regression-${{ github.sha }}.txt"
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ const Snapshot = @import("browser/js/Snapshot.zig");
|
|||||||
const Platform = @import("browser/js/Platform.zig");
|
const Platform = @import("browser/js/Platform.zig");
|
||||||
const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
|
const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
|
||||||
|
|
||||||
const Network = @import("network/Network.zig");
|
const Network = @import("network/Runtime.zig");
|
||||||
pub const ArenaPool = @import("ArenaPool.zig");
|
pub const ArenaPool = @import("ArenaPool.zig");
|
||||||
|
|
||||||
const App = @This();
|
const App = @This();
|
||||||
@@ -55,7 +55,7 @@ pub fn init(allocator: Allocator, config: *const Config) !*App {
|
|||||||
.arena_pool = undefined,
|
.arena_pool = undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
app.network = try Network.init(allocator, app, config);
|
app.network = try Network.init(allocator, config);
|
||||||
errdefer app.network.deinit();
|
errdefer app.network.deinit();
|
||||||
|
|
||||||
app.platform = try Platform.init();
|
app.platform = try Platform.init();
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ pub const RunMode = enum {
|
|||||||
mcp,
|
mcp,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const MAX_LISTENERS = 16;
|
||||||
pub const CDP_MAX_HTTP_REQUEST_SIZE = 4096;
|
pub const CDP_MAX_HTTP_REQUEST_SIZE = 4096;
|
||||||
|
|
||||||
// max message size
|
// max message size
|
||||||
@@ -156,13 +157,6 @@ pub fn userAgentSuffix(self: *const Config) ?[]const u8 {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cacheDir(self: *const Config) ?[]const u8 {
|
|
||||||
return switch (self.mode) {
|
|
||||||
inline .serve, .fetch, .mcp => |opts| opts.common.cache_dir,
|
|
||||||
else => null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn cdpTimeout(self: *const Config) usize {
|
pub fn cdpTimeout(self: *const Config) usize {
|
||||||
return switch (self.mode) {
|
return switch (self.mode) {
|
||||||
.serve => |opts| if (opts.timeout > 604_800) 604_800_000 else @as(usize, opts.timeout) * 1000,
|
.serve => |opts| if (opts.timeout > 604_800) 604_800_000 else @as(usize, opts.timeout) * 1000,
|
||||||
@@ -274,7 +268,6 @@ pub const Common = struct {
|
|||||||
log_format: ?log.Format = null,
|
log_format: ?log.Format = null,
|
||||||
log_filter_scopes: ?[]log.Scope = null,
|
log_filter_scopes: ?[]log.Scope = null,
|
||||||
user_agent_suffix: ?[]const u8 = null,
|
user_agent_suffix: ?[]const u8 = null,
|
||||||
cache_dir: ?[]const u8 = null,
|
|
||||||
|
|
||||||
web_bot_auth_key_file: ?[]const u8 = null,
|
web_bot_auth_key_file: ?[]const u8 = null,
|
||||||
web_bot_auth_keyid: ?[]const u8 = null,
|
web_bot_auth_keyid: ?[]const u8 = null,
|
||||||
@@ -1055,14 +1048,5 @@ fn parseCommonArg(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std.mem.eql(u8, "--cache_dir", opt)) {
|
|
||||||
const str = args.next() orelse {
|
|
||||||
log.fatal(.app, "missing argument value", .{ .arg = "--cache_dir" });
|
|
||||||
return error.InvalidArgument;
|
|
||||||
};
|
|
||||||
common.cache_dir = try allocator.dupe(u8, str);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -260,14 +260,14 @@ pub const Client = struct {
|
|||||||
|
|
||||||
fn start(self: *Client) void {
|
fn start(self: *Client) void {
|
||||||
const http = self.http;
|
const http = self.http;
|
||||||
http.setCdpClient(.{
|
http.cdp_client = .{
|
||||||
.socket = self.ws.socket,
|
.socket = self.ws.socket,
|
||||||
.ctx = self,
|
.ctx = self,
|
||||||
.blocking_read_start = Client.blockingReadStart,
|
.blocking_read_start = Client.blockingReadStart,
|
||||||
.blocking_read = Client.blockingRead,
|
.blocking_read = Client.blockingRead,
|
||||||
.blocking_read_end = Client.blockingReadStop,
|
.blocking_read_end = Client.blockingReadStop,
|
||||||
});
|
};
|
||||||
defer http.setCdpClient(null);
|
defer http.cdp_client = null;
|
||||||
|
|
||||||
self.httpLoop(http) catch |err| {
|
self.httpLoop(http) catch |err| {
|
||||||
log.err(.app, "CDP client loop", .{ .err = err });
|
log.err(.app, "CDP client loop", .{ .err = err });
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -27,9 +27,6 @@ charset: [41]u8 = default_charset,
|
|||||||
charset_len: usize = default_charset_len,
|
charset_len: usize = default_charset_len,
|
||||||
is_default_charset: bool = true,
|
is_default_charset: bool = true,
|
||||||
|
|
||||||
type_buf: [127]u8 = @splat(0),
|
|
||||||
sub_type_buf: [127]u8 = @splat(0),
|
|
||||||
|
|
||||||
/// String "UTF-8" continued by null characters.
|
/// String "UTF-8" continued by null characters.
|
||||||
const default_charset = .{ 'U', 'T', 'F', '-', '8' } ++ .{0} ** 36;
|
const default_charset = .{ 'U', 'T', 'F', '-', '8' } ++ .{0} ** 36;
|
||||||
const default_charset_len = 5;
|
const default_charset_len = 5;
|
||||||
@@ -64,10 +61,7 @@ pub const ContentType = union(ContentTypeEnum) {
|
|||||||
image_webp: void,
|
image_webp: void,
|
||||||
application_json: void,
|
application_json: void,
|
||||||
unknown: void,
|
unknown: void,
|
||||||
other: struct {
|
other: struct { type: []const u8, sub_type: []const u8 },
|
||||||
type: []const u8,
|
|
||||||
sub_type: []const u8,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn contentTypeString(mime: *const Mime) []const u8 {
|
pub fn contentTypeString(mime: *const Mime) []const u8 {
|
||||||
@@ -118,18 +112,17 @@ fn parseCharset(value: []const u8) error{ CharsetTooBig, Invalid }![]const u8 {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(input: []const u8) !Mime {
|
pub fn parse(input: []u8) !Mime {
|
||||||
if (input.len > 255) {
|
if (input.len > 255) {
|
||||||
return error.TooBig;
|
return error.TooBig;
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf: [255]u8 = undefined;
|
// Zig's trim API is broken. The return type is always `[]const u8`,
|
||||||
const normalized = std.ascii.lowerString(&buf, std.mem.trim(u8, input, &std.ascii.whitespace));
|
// even if the input type is `[]u8`. @constCast is safe here.
|
||||||
|
var normalized = @constCast(std.mem.trim(u8, input, &std.ascii.whitespace));
|
||||||
_ = std.ascii.lowerString(normalized, normalized);
|
_ = std.ascii.lowerString(normalized, normalized);
|
||||||
|
|
||||||
var mime = Mime{ .content_type = undefined };
|
const content_type, const type_len = try parseContentType(normalized);
|
||||||
|
|
||||||
const content_type, const type_len = try parseContentType(normalized, &mime.type_buf, &mime.sub_type_buf);
|
|
||||||
if (type_len >= normalized.len) {
|
if (type_len >= normalized.len) {
|
||||||
return .{ .content_type = content_type };
|
return .{ .content_type = content_type };
|
||||||
}
|
}
|
||||||
@@ -170,12 +163,13 @@ pub fn parse(input: []const u8) !Mime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mime.params = params;
|
return .{
|
||||||
mime.charset = charset;
|
.params = params,
|
||||||
mime.charset_len = charset_len;
|
.charset = charset,
|
||||||
mime.content_type = content_type;
|
.charset_len = charset_len,
|
||||||
mime.is_default_charset = !has_explicit_charset;
|
.content_type = content_type,
|
||||||
return mime;
|
.is_default_charset = !has_explicit_charset,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prescan the first 1024 bytes of an HTML document for a charset declaration.
|
/// Prescan the first 1024 bytes of an HTML document for a charset declaration.
|
||||||
@@ -401,7 +395,7 @@ pub fn isText(mime: *const Mime) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we expect value to be lowercase
|
// we expect value to be lowercase
|
||||||
fn parseContentType(value: []const u8, type_buf: []u8, sub_type_buf: []u8) !struct { ContentType, usize } {
|
fn parseContentType(value: []const u8) !struct { ContentType, usize } {
|
||||||
const end = std.mem.indexOfScalarPos(u8, value, 0, ';') orelse value.len;
|
const end = std.mem.indexOfScalarPos(u8, value, 0, ';') orelse value.len;
|
||||||
const type_name = trimRight(value[0..end]);
|
const type_name = trimRight(value[0..end]);
|
||||||
const attribute_start = end + 1;
|
const attribute_start = end + 1;
|
||||||
@@ -450,18 +444,10 @@ fn parseContentType(value: []const u8, type_buf: []u8, sub_type_buf: []u8) !stru
|
|||||||
return error.Invalid;
|
return error.Invalid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@memcpy(type_buf[0..main_type.len], main_type);
|
return .{ .{ .other = .{
|
||||||
@memcpy(sub_type_buf[0..sub_type.len], sub_type);
|
.type = main_type,
|
||||||
|
.sub_type = sub_type,
|
||||||
return .{
|
} }, attribute_start };
|
||||||
.{
|
|
||||||
.other = .{
|
|
||||||
.type = type_buf[0..main_type.len],
|
|
||||||
.sub_type = sub_type_buf[0..sub_type.len],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
attribute_start,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const VALID_CODEPOINTS = blk: {
|
const VALID_CODEPOINTS = blk: {
|
||||||
@@ -475,13 +461,6 @@ const VALID_CODEPOINTS = blk: {
|
|||||||
break :blk v;
|
break :blk v;
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn typeString(self: *const Mime) []const u8 {
|
|
||||||
return switch (self.content_type) {
|
|
||||||
.other => |o| o.type[0..o.type_len],
|
|
||||||
else => "",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
fn validType(value: []const u8) bool {
|
fn validType(value: []const u8) bool {
|
||||||
for (value) |b| {
|
for (value) |b| {
|
||||||
if (VALID_CODEPOINTS[b] == false) {
|
if (VALID_CODEPOINTS[b] == false) {
|
||||||
|
|||||||
@@ -854,10 +854,12 @@ fn notifyParentLoadComplete(self: *Page) void {
|
|||||||
parent.iframeCompletedLoading(self.iframe.?);
|
parent.iframeCompletedLoading(self.iframe.?);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pageHeaderDoneCallback(response: HttpClient.Response) !bool {
|
fn pageHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
|
||||||
var self: *Page = @ptrCast(@alignCast(response.ctx));
|
var self: *Page = @ptrCast(@alignCast(transfer.ctx));
|
||||||
|
|
||||||
const response_url = response.url();
|
const header = &transfer.response_header.?;
|
||||||
|
|
||||||
|
const response_url = std.mem.span(header.url);
|
||||||
if (std.mem.eql(u8, response_url, self.url) == false) {
|
if (std.mem.eql(u8, response_url, self.url) == false) {
|
||||||
// would be different than self.url in the case of a redirect
|
// would be different than self.url in the case of a redirect
|
||||||
self.url = try self.arena.dupeZ(u8, response_url);
|
self.url = try self.arena.dupeZ(u8, response_url);
|
||||||
@@ -871,8 +873,8 @@ fn pageHeaderDoneCallback(response: HttpClient.Response) !bool {
|
|||||||
if (comptime IS_DEBUG) {
|
if (comptime IS_DEBUG) {
|
||||||
log.debug(.page, "navigate header", .{
|
log.debug(.page, "navigate header", .{
|
||||||
.url = self.url,
|
.url = self.url,
|
||||||
.status = response.status(),
|
.status = header.status,
|
||||||
.content_type = response.contentType(),
|
.content_type = header.contentType(),
|
||||||
.type = self._type,
|
.type = self._type,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -893,14 +895,14 @@ fn pageHeaderDoneCallback(response: HttpClient.Response) !bool {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pageDataCallback(response: HttpClient.Response, data: []const u8) !void {
|
fn pageDataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
|
||||||
var self: *Page = @ptrCast(@alignCast(response.ctx));
|
var self: *Page = @ptrCast(@alignCast(transfer.ctx));
|
||||||
|
|
||||||
if (self._parse_state == .pre) {
|
if (self._parse_state == .pre) {
|
||||||
// we lazily do this, because we might need the first chunk of data
|
// we lazily do this, because we might need the first chunk of data
|
||||||
// to sniff the content type
|
// to sniff the content type
|
||||||
var mime: Mime = blk: {
|
var mime: Mime = blk: {
|
||||||
if (response.contentType()) |ct| {
|
if (transfer.response_header.?.contentType()) |ct| {
|
||||||
break :blk try Mime.parse(ct);
|
break :blk try Mime.parse(ct);
|
||||||
}
|
}
|
||||||
break :blk Mime.sniff(data);
|
break :blk Mime.sniff(data);
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ fn _tick(self: *Runner, comptime is_cdp: bool, opts: TickOpts) !CDPTickResult {
|
|||||||
.pre, .raw, .text, .image => {
|
.pre, .raw, .text, .image => {
|
||||||
// The main page hasn't started/finished navigating.
|
// The main page hasn't started/finished navigating.
|
||||||
// There's no JS to run, and no reason to run the scheduler.
|
// There's no JS to run, and no reason to run the scheduler.
|
||||||
if (http_client.active() == 0 and (comptime is_cdp) == false) {
|
if (http_client.active == 0 and (comptime is_cdp) == false) {
|
||||||
// haven't started navigating, I guess.
|
// haven't started navigating, I guess.
|
||||||
return .done;
|
return .done;
|
||||||
}
|
}
|
||||||
@@ -171,8 +171,8 @@ fn _tick(self: *Runner, comptime is_cdp: bool, opts: TickOpts) !CDPTickResult {
|
|||||||
// Each call to this runs scheduled load events.
|
// Each call to this runs scheduled load events.
|
||||||
try page.dispatchLoad();
|
try page.dispatchLoad();
|
||||||
|
|
||||||
const http_active = http_client.active();
|
const http_active = http_client.active;
|
||||||
const total_network_activity = http_active + http_client.intercepted();
|
const total_network_activity = http_active + http_client.intercepted;
|
||||||
if (page._notified_network_almost_idle.check(total_network_activity <= 2)) {
|
if (page._notified_network_almost_idle.check(total_network_activity <= 2)) {
|
||||||
page.notifyNetworkAlmostIdle();
|
page.notifyNetworkAlmostIdle();
|
||||||
}
|
}
|
||||||
@@ -185,7 +185,7 @@ fn _tick(self: *Runner, comptime is_cdp: bool, opts: TickOpts) !CDPTickResult {
|
|||||||
// because is_cdp is true, and that can only be
|
// because is_cdp is true, and that can only be
|
||||||
// the case when interception isn't possible.
|
// the case when interception isn't possible.
|
||||||
if (comptime IS_DEBUG) {
|
if (comptime IS_DEBUG) {
|
||||||
std.debug.assert(http_client.intercepted() == 0);
|
std.debug.assert(http_client.intercepted == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (browser.hasBackgroundTasks()) {
|
if (browser.hasBackgroundTasks()) {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ const builtin = @import("builtin");
|
|||||||
|
|
||||||
const log = @import("../log.zig");
|
const log = @import("../log.zig");
|
||||||
const HttpClient = @import("HttpClient.zig");
|
const HttpClient = @import("HttpClient.zig");
|
||||||
const http = @import("../network/http.zig");
|
const net_http = @import("../network/http.zig");
|
||||||
const String = @import("../string.zig").String;
|
const String = @import("../string.zig").String;
|
||||||
|
|
||||||
const js = @import("js/js.zig");
|
const js = @import("js/js.zig");
|
||||||
@@ -136,7 +136,7 @@ fn clearList(list: *std.DoublyLinkedList) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getHeaders(self: *ScriptManager) !http.Headers {
|
fn getHeaders(self: *ScriptManager) !net_http.Headers {
|
||||||
var headers = try self.client.newHeaders();
|
var headers = try self.client.newHeaders();
|
||||||
try self.page.headersForRequest(&headers);
|
try self.page.headersForRequest(&headers);
|
||||||
return headers;
|
return headers;
|
||||||
@@ -694,86 +694,82 @@ pub const Script = struct {
|
|||||||
self.manager.page.releaseArena(self.arena);
|
self.manager.page.releaseArena(self.arena);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn startCallback(response: HttpClient.Response) !void {
|
fn startCallback(transfer: *HttpClient.Transfer) !void {
|
||||||
log.debug(.http, "script fetch start", .{ .req = response });
|
log.debug(.http, "script fetch start", .{ .req = transfer });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn headerCallback(response: HttpClient.Response) !bool {
|
fn headerCallback(transfer: *HttpClient.Transfer) !bool {
|
||||||
const self: *Script = @ptrCast(@alignCast(response.ctx));
|
const self: *Script = @ptrCast(@alignCast(transfer.ctx));
|
||||||
|
const header = &transfer.response_header.?;
|
||||||
self.status = response.status().?;
|
self.status = header.status;
|
||||||
if (response.status() != 200) {
|
if (header.status != 200) {
|
||||||
log.info(.http, "script header", .{
|
log.info(.http, "script header", .{
|
||||||
.req = response,
|
.req = transfer,
|
||||||
.status = response.status(),
|
.status = header.status,
|
||||||
.content_type = response.contentType(),
|
.content_type = header.contentType(),
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (comptime IS_DEBUG) {
|
if (comptime IS_DEBUG) {
|
||||||
log.debug(.http, "script header", .{
|
log.debug(.http, "script header", .{
|
||||||
.req = response,
|
.req = transfer,
|
||||||
.status = response.status(),
|
.status = header.status,
|
||||||
.content_type = response.contentType(),
|
.content_type = header.contentType(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (response.inner) {
|
{
|
||||||
.transfer => |transfer| {
|
// temp debug, trying to figure out why the next assert sometimes
|
||||||
// temp debug, trying to figure out why the next assert sometimes
|
// fails. Is the buffer just corrupt or is headerCallback really
|
||||||
// fails. Is the buffer just corrupt or is headerCallback really
|
// being called twice?
|
||||||
// being called twice?
|
lp.assert(self.header_callback_called == false, "ScriptManager.Header recall", .{
|
||||||
lp.assert(self.header_callback_called == false, "ScriptManager.Header recall", .{
|
.m = @tagName(std.meta.activeTag(self.mode)),
|
||||||
.m = @tagName(std.meta.activeTag(self.mode)),
|
.a1 = self.debug_transfer_id,
|
||||||
.a1 = self.debug_transfer_id,
|
.a2 = self.debug_transfer_tries,
|
||||||
.a2 = self.debug_transfer_tries,
|
.a3 = self.debug_transfer_aborted,
|
||||||
.a3 = self.debug_transfer_aborted,
|
.a4 = self.debug_transfer_bytes_received,
|
||||||
.a4 = self.debug_transfer_bytes_received,
|
.a5 = self.debug_transfer_notified_fail,
|
||||||
.a5 = self.debug_transfer_notified_fail,
|
.a7 = self.debug_transfer_intercept_state,
|
||||||
.a7 = self.debug_transfer_intercept_state,
|
.a8 = self.debug_transfer_auth_challenge,
|
||||||
.a8 = self.debug_transfer_auth_challenge,
|
.a9 = self.debug_transfer_easy_id,
|
||||||
.a9 = self.debug_transfer_easy_id,
|
.b1 = transfer.id,
|
||||||
.b1 = transfer.id,
|
.b2 = transfer._tries,
|
||||||
.b2 = transfer._tries,
|
.b3 = transfer.aborted,
|
||||||
.b3 = transfer.aborted,
|
.b4 = transfer.bytes_received,
|
||||||
.b4 = transfer.bytes_received,
|
.b5 = transfer._notified_fail,
|
||||||
.b5 = transfer._notified_fail,
|
.b7 = @intFromEnum(transfer._intercept_state),
|
||||||
.b7 = @intFromEnum(transfer._intercept_state),
|
.b8 = transfer._auth_challenge != null,
|
||||||
.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.header_callback_called = true;
|
self.debug_transfer_id = transfer.id;
|
||||||
self.debug_transfer_id = transfer.id;
|
self.debug_transfer_tries = transfer._tries;
|
||||||
self.debug_transfer_tries = transfer._tries;
|
self.debug_transfer_aborted = transfer.aborted;
|
||||||
self.debug_transfer_aborted = transfer.aborted;
|
self.debug_transfer_bytes_received = transfer.bytes_received;
|
||||||
self.debug_transfer_bytes_received = transfer.bytes_received;
|
self.debug_transfer_notified_fail = transfer._notified_fail;
|
||||||
self.debug_transfer_notified_fail = transfer._notified_fail;
|
self.debug_transfer_intercept_state = @intFromEnum(transfer._intercept_state);
|
||||||
self.debug_transfer_intercept_state = @intFromEnum(transfer._intercept_state);
|
self.debug_transfer_auth_challenge = transfer._auth_challenge != null;
|
||||||
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;
|
|
||||||
},
|
|
||||||
else => {},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lp.assert(self.source.remote.capacity == 0, "ScriptManager.Header buffer", .{ .capacity = self.source.remote.capacity });
|
lp.assert(self.source.remote.capacity == 0, "ScriptManager.Header buffer", .{ .capacity = self.source.remote.capacity });
|
||||||
var buffer: std.ArrayList(u8) = .empty;
|
var buffer: std.ArrayList(u8) = .empty;
|
||||||
if (response.contentLength()) |cl| {
|
if (transfer.getContentLength()) |cl| {
|
||||||
try buffer.ensureTotalCapacity(self.arena, cl);
|
try buffer.ensureTotalCapacity(self.arena, cl);
|
||||||
}
|
}
|
||||||
self.source = .{ .remote = buffer };
|
self.source = .{ .remote = buffer };
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dataCallback(response: HttpClient.Response, data: []const u8) !void {
|
fn dataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
|
||||||
const self: *Script = @ptrCast(@alignCast(response.ctx));
|
const self: *Script = @ptrCast(@alignCast(transfer.ctx));
|
||||||
self._dataCallback(response, data) catch |err| {
|
self._dataCallback(transfer, data) catch |err| {
|
||||||
log.err(.http, "SM.dataCallback", .{ .err = err, .transfer = response, .len = data.len });
|
log.err(.http, "SM.dataCallback", .{ .err = err, .transfer = transfer, .len = data.len });
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
fn _dataCallback(self: *Script, _: *HttpClient.Transfer, data: []const u8) !void {
|
||||||
fn _dataCallback(self: *Script, _: HttpClient.Response, data: []const u8) !void {
|
|
||||||
try self.source.remote.appendSlice(self.arena, data);
|
try self.source.remote.appendSlice(self.arena, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,72 +25,28 @@ const ResolveOpts = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// path is anytype, so that it can be used with both []const u8 and [:0]const u8
|
// path is anytype, so that it can be used with both []const u8 and [:0]const u8
|
||||||
pub fn resolve(allocator: Allocator, base: [:0]const u8, source_path: anytype, comptime opts: ResolveOpts) ![:0]const u8 {
|
pub fn resolve(allocator: Allocator, base: [:0]const u8, path: anytype, comptime opts: ResolveOpts) ![:0]const u8 {
|
||||||
const PT = @TypeOf(source_path);
|
const PT = @TypeOf(path);
|
||||||
|
if (base.len == 0 or isCompleteHTTPUrl(path)) {
|
||||||
var path: [:0]const u8 = if (comptime !isNullTerminated(PT) or opts.always_dupe) try allocator.dupeZ(u8, source_path) else source_path;
|
if (comptime opts.always_dupe or !isNullTerminated(PT)) {
|
||||||
|
const duped = try allocator.dupeZ(u8, path);
|
||||||
if (base.len == 0) {
|
return processResolved(allocator, duped, opts);
|
||||||
return processResolved(allocator, path, opts);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Minimum is "x:" and skip relative path (very common case)
|
|
||||||
if (path.len >= 2 and path[0] != '/') {
|
|
||||||
if (std.mem.indexOfScalar(u8, path[0..], ':')) |scheme_path_end| {
|
|
||||||
scheme_check: {
|
|
||||||
const scheme_path = path[0..scheme_path_end];
|
|
||||||
//from "ws" to "https"
|
|
||||||
if (scheme_path_end >= 2 and scheme_path_end <= 5) {
|
|
||||||
const has_double_slashes: bool = scheme_path_end + 3 <= path.len and path[scheme_path_end + 1] == '/' and path[scheme_path_end + 2] == '/';
|
|
||||||
const special_schemes = [_][]const u8{ "https", "http", "ws", "wss", "file", "ftp" };
|
|
||||||
|
|
||||||
for (special_schemes) |special_scheme| {
|
|
||||||
if (std.ascii.eqlIgnoreCase(scheme_path, special_scheme)) {
|
|
||||||
const base_scheme_end = std.mem.indexOf(u8, base, "://") orelse 0;
|
|
||||||
|
|
||||||
if (base_scheme_end > 0 and std.mem.eql(u8, base[0..base_scheme_end], scheme_path) and !has_double_slashes) {
|
|
||||||
//Skip ":" and exit as relative state
|
|
||||||
path = path[scheme_path_end + 1 ..];
|
|
||||||
break :scheme_check;
|
|
||||||
} else {
|
|
||||||
var rest_start: usize = scheme_path_end + 1;
|
|
||||||
//Skip any slashas after "scheme:"
|
|
||||||
while (rest_start < path.len and (path[rest_start] == '/' or path[rest_start] == '\\')) {
|
|
||||||
rest_start += 1;
|
|
||||||
}
|
|
||||||
// A special scheme (exclude "file") must contain at least any chars after "://"
|
|
||||||
if (rest_start == path.len and !std.ascii.eqlIgnoreCase(scheme_path, "file")) {
|
|
||||||
return error.TypeError;
|
|
||||||
}
|
|
||||||
//File scheme allow empty host
|
|
||||||
const separator: []const u8 = if (!has_double_slashes and std.ascii.eqlIgnoreCase(scheme_path, "file")) ":///" else "://";
|
|
||||||
|
|
||||||
path = try std.mem.joinZ(allocator, "", &.{ scheme_path, separator, path[rest_start..] });
|
|
||||||
return processResolved(allocator, path, opts);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (scheme_path.len > 0) {
|
|
||||||
for (scheme_path[1..]) |c| {
|
|
||||||
if (!std.ascii.isAlphanumeric(c) and c != '+' and c != '-' and c != '.') {
|
|
||||||
//Exit as relative state
|
|
||||||
break :scheme_check;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//path is complete http url
|
|
||||||
return processResolved(allocator, path, opts);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (comptime opts.encode) {
|
||||||
|
return processResolved(allocator, path, opts);
|
||||||
|
}
|
||||||
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.len == 0) {
|
if (path.len == 0) {
|
||||||
if (opts.always_dupe) {
|
if (comptime opts.always_dupe) {
|
||||||
const dupe = try allocator.dupeZ(u8, base);
|
const duped = try allocator.dupeZ(u8, base);
|
||||||
return processResolved(allocator, dupe, opts);
|
return processResolved(allocator, duped, opts);
|
||||||
}
|
}
|
||||||
return processResolved(allocator, base, opts);
|
if (comptime opts.encode) {
|
||||||
|
return processResolved(allocator, base, opts);
|
||||||
|
}
|
||||||
|
return base;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path[0] == '?') {
|
if (path[0] == '?') {
|
||||||
@@ -107,7 +63,14 @@ pub fn resolve(allocator: Allocator, base: [:0]const u8, source_path: anytype, c
|
|||||||
if (std.mem.startsWith(u8, path, "//")) {
|
if (std.mem.startsWith(u8, path, "//")) {
|
||||||
// network-path reference
|
// network-path reference
|
||||||
const index = std.mem.indexOfScalar(u8, base, ':') orelse {
|
const index = std.mem.indexOfScalar(u8, base, ':') orelse {
|
||||||
return processResolved(allocator, path, opts);
|
if (comptime isNullTerminated(PT)) {
|
||||||
|
if (comptime opts.encode) {
|
||||||
|
return processResolved(allocator, path, opts);
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
const duped = try allocator.dupeZ(u8, path);
|
||||||
|
return processResolved(allocator, duped, opts);
|
||||||
};
|
};
|
||||||
const protocol = base[0 .. index + 1];
|
const protocol = base[0 .. index + 1];
|
||||||
const result = try std.mem.joinZ(allocator, "", &.{ protocol, path });
|
const result = try std.mem.joinZ(allocator, "", &.{ protocol, path });
|
||||||
@@ -133,7 +96,6 @@ pub fn resolve(allocator: Allocator, base: [:0]const u8, source_path: anytype, c
|
|||||||
// trailing space so that we always have space to append the null terminator
|
// trailing space so that we always have space to append the null terminator
|
||||||
// and so that we can compare the next two characters without needing to length check
|
// and so that we can compare the next two characters without needing to length check
|
||||||
var out = try std.mem.join(allocator, "", &.{ normalized_base, "/", path, " " });
|
var out = try std.mem.join(allocator, "", &.{ normalized_base, "/", path, " " });
|
||||||
|
|
||||||
const end = out.len - 2;
|
const end = out.len - 2;
|
||||||
|
|
||||||
const path_marker = path_start + 1;
|
const path_marker = path_start + 1;
|
||||||
@@ -1608,182 +1570,3 @@ test "URL: getOrigin" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test "URL: resolve path scheme" {
|
|
||||||
const Case = struct {
|
|
||||||
base: [:0]const u8,
|
|
||||||
path: [:0]const u8,
|
|
||||||
expected: [:0]const u8,
|
|
||||||
expected_error: bool = false,
|
|
||||||
};
|
|
||||||
|
|
||||||
const cases = [_]Case{
|
|
||||||
//same schemes and path as relative path (one slash)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "https:/about",
|
|
||||||
.expected = "https://www.example.com/about",
|
|
||||||
},
|
|
||||||
//same schemes and path as relative path (without slash)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "https:about",
|
|
||||||
.expected = "https://www.example.com/about",
|
|
||||||
},
|
|
||||||
//same schemes and path as absolute path (two slashes)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "https://about",
|
|
||||||
.expected = "https://about",
|
|
||||||
},
|
|
||||||
//different schemes and path as absolute (without slash)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "http:about",
|
|
||||||
.expected = "http://about",
|
|
||||||
},
|
|
||||||
//different schemes and path as absolute (with one slash)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "http:/about",
|
|
||||||
.expected = "http://about",
|
|
||||||
},
|
|
||||||
//different schemes and path as absolute (with two slashes)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "http://about",
|
|
||||||
.expected = "http://about",
|
|
||||||
},
|
|
||||||
//same schemes and path as absolute (with more slashes)
|
|
||||||
.{
|
|
||||||
.base = "https://site/",
|
|
||||||
.path = "https://path",
|
|
||||||
.expected = "https://path",
|
|
||||||
},
|
|
||||||
//path scheme is not special and path as absolute (without additional slashes)
|
|
||||||
.{
|
|
||||||
.base = "http://localhost/",
|
|
||||||
.path = "data:test",
|
|
||||||
.expected = "data:test",
|
|
||||||
},
|
|
||||||
//different schemes and path as absolute (pathscheme=ws)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "ws://about",
|
|
||||||
.expected = "ws://about",
|
|
||||||
},
|
|
||||||
//different schemes and path as absolute (path scheme=wss)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "wss://about",
|
|
||||||
.expected = "wss://about",
|
|
||||||
},
|
|
||||||
//different schemes and path as absolute (path scheme=ftp)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "ftp://about",
|
|
||||||
.expected = "ftp://about",
|
|
||||||
},
|
|
||||||
//different schemes and path as absolute (path scheme=file)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "file://path/to/file",
|
|
||||||
.expected = "file://path/to/file",
|
|
||||||
},
|
|
||||||
//different schemes and path as absolute (path scheme=file, host is empty)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "file:/path/to/file",
|
|
||||||
.expected = "file:///path/to/file",
|
|
||||||
},
|
|
||||||
//different schemes and path as absolute (path scheme=file, host is empty)
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "file:/",
|
|
||||||
.expected = "file:///",
|
|
||||||
},
|
|
||||||
//different schemes without :// and normalize "file" scheme, absolute path
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "file:path/to/file",
|
|
||||||
.expected = "file:///path/to/file",
|
|
||||||
},
|
|
||||||
//same schemes without :// in path and rest starts with scheme:/, relative path
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "https:/file:/relative/path/",
|
|
||||||
.expected = "https://www.example.com/file:/relative/path/",
|
|
||||||
},
|
|
||||||
//same schemes without :// in path and rest starts with scheme://, relative path
|
|
||||||
.{
|
|
||||||
.base = "https://www.example.com/example",
|
|
||||||
.path = "https:/http://relative/path/",
|
|
||||||
.expected = "https://www.example.com/http://relative/path/",
|
|
||||||
},
|
|
||||||
//same schemes without :// in path , relative state
|
|
||||||
.{
|
|
||||||
.base = "http://www.example.com/example",
|
|
||||||
.path = "http:relative:path",
|
|
||||||
.expected = "http://www.example.com/relative:path",
|
|
||||||
},
|
|
||||||
//repeat different schemes in path
|
|
||||||
.{
|
|
||||||
.base = "http://www.example.com/example",
|
|
||||||
.path = "http:http:/relative/path/",
|
|
||||||
.expected = "http://www.example.com/http:/relative/path/",
|
|
||||||
},
|
|
||||||
//repeat different schemes in path
|
|
||||||
.{
|
|
||||||
.base = "http://www.example.com/example",
|
|
||||||
.path = "http:https://relative:path",
|
|
||||||
.expected = "http://www.example.com/https://relative:path",
|
|
||||||
},
|
|
||||||
//NOT required :// for blob scheme
|
|
||||||
.{
|
|
||||||
.base = "http://www.example.com/example",
|
|
||||||
.path = "blob:other",
|
|
||||||
.expected = "blob:other",
|
|
||||||
},
|
|
||||||
//NOT required :// for NON-special schemes and can contains "+" or "-" or "." in scheme
|
|
||||||
.{
|
|
||||||
.base = "http://www.example.com/example",
|
|
||||||
.path = "custom+foo:other",
|
|
||||||
.expected = "custom+foo:other",
|
|
||||||
},
|
|
||||||
//NOT required :// for NON-special schemes
|
|
||||||
.{
|
|
||||||
.base = "http://www.example.com/example",
|
|
||||||
.path = "blob:",
|
|
||||||
.expected = "blob:",
|
|
||||||
},
|
|
||||||
//NOT required :// for special scheme equal base scheme
|
|
||||||
.{
|
|
||||||
.base = "http://www.example.com/example",
|
|
||||||
.path = "http:",
|
|
||||||
.expected = "http://www.example.com/example",
|
|
||||||
},
|
|
||||||
//required :// for special scheme, so throw error.InvalidURL
|
|
||||||
.{
|
|
||||||
.base = "http://www.example.com/example",
|
|
||||||
.path = "https:",
|
|
||||||
.expected = "",
|
|
||||||
.expected_error = true,
|
|
||||||
},
|
|
||||||
//incorrect symbols in path scheme
|
|
||||||
.{
|
|
||||||
.base = "https://site",
|
|
||||||
.path = "http?://host/some",
|
|
||||||
.expected = "https://site/http?://host/some",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
for (cases) |case| {
|
|
||||||
if (case.expected_error) {
|
|
||||||
const result = resolve(testing.arena_allocator, case.base, case.path, .{});
|
|
||||||
try testing.expectError(error.TypeError, result);
|
|
||||||
} else {
|
|
||||||
const result = try resolve(testing.arena_allocator, case.base, case.path, .{});
|
|
||||||
try testing.expectString(case.expected, result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -22,10 +22,23 @@ const DOMNode = @import("webapi/Node.zig");
|
|||||||
const Element = @import("webapi/Element.zig");
|
const Element = @import("webapi/Element.zig");
|
||||||
const Event = @import("webapi/Event.zig");
|
const Event = @import("webapi/Event.zig");
|
||||||
const MouseEvent = @import("webapi/event/MouseEvent.zig");
|
const MouseEvent = @import("webapi/event/MouseEvent.zig");
|
||||||
|
const KeyboardEvent = @import("webapi/event/KeyboardEvent.zig");
|
||||||
const Page = @import("Page.zig");
|
const Page = @import("Page.zig");
|
||||||
const Session = @import("Session.zig");
|
const Session = @import("Session.zig");
|
||||||
const Selector = @import("webapi/selector/Selector.zig");
|
const Selector = @import("webapi/selector/Selector.zig");
|
||||||
|
|
||||||
|
fn dispatchInputAndChangeEvents(el: *Element, page: *Page) !void {
|
||||||
|
const input_evt: *Event = try .initTrusted(comptime .wrap("input"), .{ .bubbles = true }, page);
|
||||||
|
page._event_manager.dispatch(el.asEventTarget(), input_evt) catch |err| {
|
||||||
|
lp.log.err(.app, "dispatch input event failed", .{ .err = err });
|
||||||
|
};
|
||||||
|
|
||||||
|
const change_evt: *Event = try .initTrusted(comptime .wrap("change"), .{ .bubbles = true }, page);
|
||||||
|
page._event_manager.dispatch(el.asEventTarget(), change_evt) catch |err| {
|
||||||
|
lp.log.err(.app, "dispatch change event failed", .{ .err = err });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn click(node: *DOMNode, page: *Page) !void {
|
pub fn click(node: *DOMNode, page: *Page) !void {
|
||||||
const el = node.is(Element) orelse return error.InvalidNodeType;
|
const el = node.is(Element) orelse return error.InvalidNodeType;
|
||||||
|
|
||||||
@@ -43,9 +56,107 @@ pub fn click(node: *DOMNode, page: *Page) !void {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn hover(node: *DOMNode, page: *Page) !void {
|
||||||
|
const el = node.is(Element) orelse return error.InvalidNodeType;
|
||||||
|
|
||||||
|
const mouseover_event: *MouseEvent = try .initTrusted(comptime .wrap("mouseover"), .{
|
||||||
|
.bubbles = true,
|
||||||
|
.cancelable = true,
|
||||||
|
.composed = true,
|
||||||
|
}, page);
|
||||||
|
|
||||||
|
page._event_manager.dispatch(el.asEventTarget(), mouseover_event.asEvent()) catch |err| {
|
||||||
|
lp.log.err(.app, "hover mouseover failed", .{ .err = err });
|
||||||
|
return error.ActionFailed;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mouseenter_event: *MouseEvent = try .initTrusted(comptime .wrap("mouseenter"), .{
|
||||||
|
.composed = true,
|
||||||
|
}, page);
|
||||||
|
|
||||||
|
page._event_manager.dispatch(el.asEventTarget(), mouseenter_event.asEvent()) catch |err| {
|
||||||
|
lp.log.err(.app, "hover mouseenter failed", .{ .err = err });
|
||||||
|
return error.ActionFailed;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn press(node: ?*DOMNode, key: []const u8, page: *Page) !void {
|
||||||
|
const target = if (node) |n|
|
||||||
|
(n.is(Element) orelse return error.InvalidNodeType).asEventTarget()
|
||||||
|
else
|
||||||
|
page.document.asNode().asEventTarget();
|
||||||
|
|
||||||
|
const keydown_event: *KeyboardEvent = try .initTrusted(comptime .wrap("keydown"), .{
|
||||||
|
.bubbles = true,
|
||||||
|
.cancelable = true,
|
||||||
|
.composed = true,
|
||||||
|
.key = key,
|
||||||
|
}, page);
|
||||||
|
|
||||||
|
page._event_manager.dispatch(target, keydown_event.asEvent()) catch |err| {
|
||||||
|
lp.log.err(.app, "press keydown failed", .{ .err = err });
|
||||||
|
return error.ActionFailed;
|
||||||
|
};
|
||||||
|
|
||||||
|
const keyup_event: *KeyboardEvent = try .initTrusted(comptime .wrap("keyup"), .{
|
||||||
|
.bubbles = true,
|
||||||
|
.cancelable = true,
|
||||||
|
.composed = true,
|
||||||
|
.key = key,
|
||||||
|
}, page);
|
||||||
|
|
||||||
|
page._event_manager.dispatch(target, keyup_event.asEvent()) catch |err| {
|
||||||
|
lp.log.err(.app, "press keyup failed", .{ .err = err });
|
||||||
|
return error.ActionFailed;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn selectOption(node: *DOMNode, value: []const u8, page: *Page) !void {
|
||||||
|
const el = node.is(Element) orelse return error.InvalidNodeType;
|
||||||
|
const select = el.is(Element.Html.Select) orelse return error.InvalidNodeType;
|
||||||
|
|
||||||
|
select.setValue(value, page) catch |err| {
|
||||||
|
lp.log.err(.app, "select setValue failed", .{ .err = err });
|
||||||
|
return error.ActionFailed;
|
||||||
|
};
|
||||||
|
|
||||||
|
try dispatchInputAndChangeEvents(el, page);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setChecked(node: *DOMNode, checked: bool, page: *Page) !void {
|
||||||
|
const el = node.is(Element) orelse return error.InvalidNodeType;
|
||||||
|
const input = el.is(Element.Html.Input) orelse return error.InvalidNodeType;
|
||||||
|
|
||||||
|
if (input._input_type != .checkbox and input._input_type != .radio) {
|
||||||
|
return error.InvalidNodeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
input.setChecked(checked, page) catch |err| {
|
||||||
|
lp.log.err(.app, "setChecked failed", .{ .err = err });
|
||||||
|
return error.ActionFailed;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Match browser event order: click fires first, then input and change.
|
||||||
|
const click_event: *MouseEvent = try .initTrusted(comptime .wrap("click"), .{
|
||||||
|
.bubbles = true,
|
||||||
|
.cancelable = true,
|
||||||
|
.composed = true,
|
||||||
|
}, page);
|
||||||
|
|
||||||
|
page._event_manager.dispatch(el.asEventTarget(), click_event.asEvent()) catch |err| {
|
||||||
|
lp.log.err(.app, "dispatch click event failed", .{ .err = err });
|
||||||
|
};
|
||||||
|
|
||||||
|
try dispatchInputAndChangeEvents(el, page);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn fill(node: *DOMNode, text: []const u8, page: *Page) !void {
|
pub fn fill(node: *DOMNode, text: []const u8, page: *Page) !void {
|
||||||
const el = node.is(Element) orelse return error.InvalidNodeType;
|
const el = node.is(Element) orelse return error.InvalidNodeType;
|
||||||
|
|
||||||
|
el.focus(page) catch |err| {
|
||||||
|
lp.log.err(.app, "fill focus failed", .{ .err = err });
|
||||||
|
};
|
||||||
|
|
||||||
if (el.is(Element.Html.Input)) |input| {
|
if (el.is(Element.Html.Input)) |input| {
|
||||||
input.setValue(text, page) catch |err| {
|
input.setValue(text, page) catch |err| {
|
||||||
lp.log.err(.app, "fill input failed", .{ .err = err });
|
lp.log.err(.app, "fill input failed", .{ .err = err });
|
||||||
@@ -65,15 +176,7 @@ pub fn fill(node: *DOMNode, text: []const u8, page: *Page) !void {
|
|||||||
return error.InvalidNodeType;
|
return error.InvalidNodeType;
|
||||||
}
|
}
|
||||||
|
|
||||||
const input_evt: *Event = try .initTrusted(comptime .wrap("input"), .{ .bubbles = true }, page);
|
try dispatchInputAndChangeEvents(el, page);
|
||||||
page._event_manager.dispatch(el.asEventTarget(), input_evt) catch |err| {
|
|
||||||
lp.log.err(.app, "dispatch input event failed", .{ .err = err });
|
|
||||||
};
|
|
||||||
|
|
||||||
const change_evt: *Event = try .initTrusted(comptime .wrap("change"), .{ .bubbles = true }, page);
|
|
||||||
page._event_manager.dispatch(el.asEventTarget(), change_evt) catch |err| {
|
|
||||||
lp.log.err(.app, "dispatch change event failed", .{ .err = err });
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scroll(node: ?*DOMNode, x: ?i32, y: ?i32, page: *Page) !void {
|
pub fn scroll(node: ?*DOMNode, x: ?i32, y: ?i32, page: *Page) !void {
|
||||||
|
|||||||
@@ -10,5 +10,20 @@
|
|||||||
<div id="scrollbox" style="width: 100px; height: 100px; overflow: scroll;" onscroll="window.scrolled = true;">
|
<div id="scrollbox" style="width: 100px; height: 100px; overflow: scroll;" onscroll="window.scrolled = true;">
|
||||||
<div style="height: 500px;">Long content</div>
|
<div style="height: 500px;">Long content</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div id="hoverTarget" onmouseover="window.hovered = true;">Hover Me</div>
|
||||||
|
<input id="keyTarget" onkeydown="window.keyPressed = event.key;" onkeyup="window.keyReleased = event.key;">
|
||||||
|
<select id="sel2" onchange="window.sel2Changed = this.value">
|
||||||
|
<option value="a">Alpha</option>
|
||||||
|
<option value="b">Beta</option>
|
||||||
|
<option value="c">Gamma</option>
|
||||||
|
</select>
|
||||||
|
<input id="chk" type="checkbox">
|
||||||
|
<input id="rad" type="radio" name="group1">
|
||||||
|
<script>
|
||||||
|
document.getElementById('chk').addEventListener('click', function() { window.chkClicked = true; });
|
||||||
|
document.getElementById('chk').addEventListener('change', function() { window.chkChanged = true; });
|
||||||
|
document.getElementById('rad').addEventListener('click', function() { window.radClicked = true; });
|
||||||
|
document.getElementById('rad').addEventListener('change', function() { window.radChanged = true; });
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -523,31 +523,6 @@ pub fn setDir(self: *Element, value: []const u8, page: *Page) !void {
|
|||||||
return self.setAttributeSafe(comptime .wrap("dir"), .wrap(value), page);
|
return self.setAttributeSafe(comptime .wrap("dir"), .wrap(value), page);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ARIAMixin - ARIA attribute reflection
|
|
||||||
pub fn getAriaAtomic(self: *const Element) ?[]const u8 {
|
|
||||||
return self.getAttributeSafe(comptime .wrap("aria-atomic"));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn setAriaAtomic(self: *Element, value: ?[]const u8, page: *Page) !void {
|
|
||||||
if (value) |v| {
|
|
||||||
try self.setAttributeSafe(comptime .wrap("aria-atomic"), .wrap(v), page);
|
|
||||||
} else {
|
|
||||||
try self.removeAttribute(comptime .wrap("aria-atomic"), page);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getAriaLive(self: *const Element) ?[]const u8 {
|
|
||||||
return self.getAttributeSafe(comptime .wrap("aria-live"));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn setAriaLive(self: *Element, value: ?[]const u8, page: *Page) !void {
|
|
||||||
if (value) |v| {
|
|
||||||
try self.setAttributeSafe(comptime .wrap("aria-live"), .wrap(v), page);
|
|
||||||
} else {
|
|
||||||
try self.removeAttribute(comptime .wrap("aria-live"), page);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getClassName(self: *const Element) []const u8 {
|
pub fn getClassName(self: *const Element) []const u8 {
|
||||||
return self.getAttributeSafe(comptime .wrap("class")) orelse "";
|
return self.getAttributeSafe(comptime .wrap("class")) orelse "";
|
||||||
}
|
}
|
||||||
@@ -1711,8 +1686,6 @@ pub const JsApi = struct {
|
|||||||
pub const localName = bridge.accessor(Element.getLocalName, null, .{});
|
pub const localName = bridge.accessor(Element.getLocalName, null, .{});
|
||||||
pub const id = bridge.accessor(Element.getId, Element.setId, .{});
|
pub const id = bridge.accessor(Element.getId, Element.setId, .{});
|
||||||
pub const slot = bridge.accessor(Element.getSlot, Element.setSlot, .{});
|
pub const slot = bridge.accessor(Element.getSlot, Element.setSlot, .{});
|
||||||
pub const ariaAtomic = bridge.accessor(Element.getAriaAtomic, Element.setAriaAtomic, .{});
|
|
||||||
pub const ariaLive = bridge.accessor(Element.getAriaLive, Element.setAriaLive, .{});
|
|
||||||
pub const dir = bridge.accessor(Element.getDir, Element.setDir, .{});
|
pub const dir = bridge.accessor(Element.getDir, Element.setDir, .{});
|
||||||
pub const className = bridge.accessor(Element.getClassName, Element.setClassName, .{});
|
pub const className = bridge.accessor(Element.getClassName, Element.setClassName, .{});
|
||||||
pub const classList = bridge.accessor(Element.getClassList, Element.setClassList, .{});
|
pub const classList = bridge.accessor(Element.getClassList, Element.setClassList, .{});
|
||||||
|
|||||||
@@ -391,14 +391,6 @@ pub fn setLang(self: *HtmlElement, value: []const u8, page: *Page) !void {
|
|||||||
try self.asElement().setAttributeSafe(comptime .wrap("lang"), .wrap(value), page);
|
try self.asElement().setAttributeSafe(comptime .wrap("lang"), .wrap(value), page);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getTitle(self: *HtmlElement) []const u8 {
|
|
||||||
return self.asElement().getAttributeSafe(comptime .wrap("title")) orelse "";
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn setTitle(self: *HtmlElement, value: []const u8, page: *Page) !void {
|
|
||||||
try self.asElement().setAttributeSafe(comptime .wrap("title"), .wrap(value), page);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getAttributeFunction(
|
pub fn getAttributeFunction(
|
||||||
self: *HtmlElement,
|
self: *HtmlElement,
|
||||||
listener_type: GlobalEventHandler,
|
listener_type: GlobalEventHandler,
|
||||||
@@ -1239,7 +1231,6 @@ pub const JsApi = struct {
|
|||||||
pub const hidden = bridge.accessor(HtmlElement.getHidden, HtmlElement.setHidden, .{});
|
pub const hidden = bridge.accessor(HtmlElement.getHidden, HtmlElement.setHidden, .{});
|
||||||
pub const lang = bridge.accessor(HtmlElement.getLang, HtmlElement.setLang, .{});
|
pub const lang = bridge.accessor(HtmlElement.getLang, HtmlElement.setLang, .{});
|
||||||
pub const tabIndex = bridge.accessor(HtmlElement.getTabIndex, HtmlElement.setTabIndex, .{});
|
pub const tabIndex = bridge.accessor(HtmlElement.getTabIndex, HtmlElement.setTabIndex, .{});
|
||||||
pub const title = bridge.accessor(HtmlElement.getTitle, HtmlElement.setTitle, .{});
|
|
||||||
|
|
||||||
pub const onabort = bridge.accessor(HtmlElement.getOnAbort, HtmlElement.setOnAbort, .{});
|
pub const onabort = bridge.accessor(HtmlElement.getOnAbort, HtmlElement.setOnAbort, .{});
|
||||||
pub const onanimationcancel = bridge.accessor(HtmlElement.getOnAnimationCancel, HtmlElement.setOnAnimationCancel, .{});
|
pub const onanimationcancel = bridge.accessor(HtmlElement.getOnAnimationCancel, HtmlElement.setOnAnimationCancel, .{});
|
||||||
|
|||||||
@@ -174,14 +174,6 @@ pub fn setType(self: *Anchor, value: []const u8, page: *Page) !void {
|
|||||||
try self.asElement().setAttributeSafe(comptime .wrap("type"), .wrap(value), page);
|
try self.asElement().setAttributeSafe(comptime .wrap("type"), .wrap(value), page);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getRel(self: *Anchor) []const u8 {
|
|
||||||
return self.asConstElement().getAttributeSafe(comptime .wrap("rel")) orelse "";
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn setRel(self: *Anchor, value: []const u8, page: *Page) !void {
|
|
||||||
try self.asElement().setAttributeSafe(comptime .wrap("rel"), .wrap(value), page);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getName(self: *const Anchor) []const u8 {
|
pub fn getName(self: *const Anchor) []const u8 {
|
||||||
return self.asConstElement().getAttributeSafe(comptime .wrap("name")) orelse "";
|
return self.asConstElement().getAttributeSafe(comptime .wrap("name")) orelse "";
|
||||||
}
|
}
|
||||||
@@ -226,7 +218,6 @@ pub const JsApi = struct {
|
|||||||
pub const pathname = bridge.accessor(Anchor.getPathname, Anchor.setPathname, .{});
|
pub const pathname = bridge.accessor(Anchor.getPathname, Anchor.setPathname, .{});
|
||||||
pub const search = bridge.accessor(Anchor.getSearch, Anchor.setSearch, .{});
|
pub const search = bridge.accessor(Anchor.getSearch, Anchor.setSearch, .{});
|
||||||
pub const hash = bridge.accessor(Anchor.getHash, Anchor.setHash, .{});
|
pub const hash = bridge.accessor(Anchor.getHash, Anchor.setHash, .{});
|
||||||
pub const rel = bridge.accessor(Anchor.getRel, Anchor.setRel, .{});
|
|
||||||
pub const @"type" = bridge.accessor(Anchor.getType, Anchor.setType, .{});
|
pub const @"type" = bridge.accessor(Anchor.getType, Anchor.setType, .{});
|
||||||
pub const text = bridge.accessor(Anchor.getText, Anchor.setText, .{});
|
pub const text = bridge.accessor(Anchor.getText, Anchor.setText, .{});
|
||||||
pub const relList = bridge.accessor(_getRelList, null, .{ .null_as_undefined = true });
|
pub const relList = bridge.accessor(_getRelList, null, .{ .null_as_undefined = true });
|
||||||
|
|||||||
@@ -127,16 +127,16 @@ fn handleBlobUrl(url: []const u8, resolver: js.PromiseResolver, page: *Page) !js
|
|||||||
return resolver.promise();
|
return resolver.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpStartCallback(response: HttpClient.Response) !void {
|
fn httpStartCallback(transfer: *HttpClient.Transfer) !void {
|
||||||
const self: *Fetch = @ptrCast(@alignCast(response.ctx));
|
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
|
||||||
if (comptime IS_DEBUG) {
|
if (comptime IS_DEBUG) {
|
||||||
log.debug(.http, "request start", .{ .url = self._url, .source = "fetch" });
|
log.debug(.http, "request start", .{ .url = self._url, .source = "fetch" });
|
||||||
}
|
}
|
||||||
self._response._http_response = response;
|
self._response._transfer = transfer;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpHeaderDoneCallback(response: HttpClient.Response) !bool {
|
fn httpHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
|
||||||
const self: *Fetch = @ptrCast(@alignCast(response.ctx));
|
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
|
||||||
|
|
||||||
if (self._signal) |signal| {
|
if (self._signal) |signal| {
|
||||||
if (signal._aborted) {
|
if (signal._aborted) {
|
||||||
@@ -145,24 +145,25 @@ fn httpHeaderDoneCallback(response: HttpClient.Response) !bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const arena = self._response._arena;
|
const arena = self._response._arena;
|
||||||
if (response.contentLength()) |cl| {
|
if (transfer.getContentLength()) |cl| {
|
||||||
try self._buf.ensureTotalCapacity(arena, cl);
|
try self._buf.ensureTotalCapacity(arena, cl);
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = self._response;
|
const res = self._response;
|
||||||
|
const header = transfer.response_header.?;
|
||||||
|
|
||||||
if (comptime IS_DEBUG) {
|
if (comptime IS_DEBUG) {
|
||||||
log.debug(.http, "request header", .{
|
log.debug(.http, "request header", .{
|
||||||
.source = "fetch",
|
.source = "fetch",
|
||||||
.url = self._url,
|
.url = self._url,
|
||||||
.status = response.status(),
|
.status = header.status,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
res._status = response.status().?;
|
res._status = header.status;
|
||||||
res._status_text = std.http.Status.phrase(@enumFromInt(response.status().?)) orelse "";
|
res._status_text = std.http.Status.phrase(@enumFromInt(header.status)) orelse "";
|
||||||
res._url = try arena.dupeZ(u8, response.url());
|
res._url = try arena.dupeZ(u8, std.mem.span(header.url));
|
||||||
res._is_redirected = response.redirectCount().? > 0;
|
res._is_redirected = header.redirect_count > 0;
|
||||||
|
|
||||||
// Determine response type based on origin comparison
|
// Determine response type based on origin comparison
|
||||||
const page_origin = URL.getOrigin(arena, self._page.url) catch null;
|
const page_origin = URL.getOrigin(arena, self._page.url) catch null;
|
||||||
@@ -182,7 +183,7 @@ fn httpHeaderDoneCallback(response: HttpClient.Response) !bool {
|
|||||||
res._type = .basic;
|
res._type = .basic;
|
||||||
}
|
}
|
||||||
|
|
||||||
var it = response.headerIterator();
|
var it = transfer.responseHeaderIterator();
|
||||||
while (it.next()) |hdr| {
|
while (it.next()) |hdr| {
|
||||||
try res._headers.append(hdr.name, hdr.value, self._page);
|
try res._headers.append(hdr.name, hdr.value, self._page);
|
||||||
}
|
}
|
||||||
@@ -190,8 +191,8 @@ fn httpHeaderDoneCallback(response: HttpClient.Response) !bool {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpDataCallback(response: HttpClient.Response, data: []const u8) !void {
|
fn httpDataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
|
||||||
const self: *Fetch = @ptrCast(@alignCast(response.ctx));
|
const self: *Fetch = @ptrCast(@alignCast(transfer.ctx));
|
||||||
|
|
||||||
// Check if aborted
|
// Check if aborted
|
||||||
if (self._signal) |signal| {
|
if (self._signal) |signal| {
|
||||||
@@ -206,7 +207,7 @@ fn httpDataCallback(response: HttpClient.Response, data: []const u8) !void {
|
|||||||
fn httpDoneCallback(ctx: *anyopaque) !void {
|
fn httpDoneCallback(ctx: *anyopaque) !void {
|
||||||
const self: *Fetch = @ptrCast(@alignCast(ctx));
|
const self: *Fetch = @ptrCast(@alignCast(ctx));
|
||||||
var response = self._response;
|
var response = self._response;
|
||||||
response._http_response = null;
|
response._transfer = null;
|
||||||
response._body = self._buf.items;
|
response._body = self._buf.items;
|
||||||
|
|
||||||
log.info(.http, "request complete", .{
|
log.info(.http, "request complete", .{
|
||||||
@@ -229,7 +230,7 @@ fn httpErrorCallback(ctx: *anyopaque, _: anyerror) void {
|
|||||||
const self: *Fetch = @ptrCast(@alignCast(ctx));
|
const self: *Fetch = @ptrCast(@alignCast(ctx));
|
||||||
|
|
||||||
var response = self._response;
|
var response = self._response;
|
||||||
response._http_response = null;
|
response._transfer = null;
|
||||||
// the response is only passed on v8 on success, if we're here, it's safe to
|
// the response is only passed on v8 on success, if we're here, it's safe to
|
||||||
// clear this. (defer since `self is in the response's arena).
|
// clear this. (defer since `self is in the response's arena).
|
||||||
|
|
||||||
@@ -255,7 +256,7 @@ fn httpShutdownCallback(ctx: *anyopaque) void {
|
|||||||
|
|
||||||
if (self._owns_response) {
|
if (self._owns_response) {
|
||||||
var response = self._response;
|
var response = self._response;
|
||||||
response._http_response = null;
|
response._transfer = null;
|
||||||
response.deinit(self._page._session);
|
response.deinit(self._page._session);
|
||||||
// Do not access `self` after this point: the Fetch struct was
|
// Do not access `self` after this point: the Fetch struct was
|
||||||
// allocated from response._arena which has been released.
|
// allocated from response._arena which has been released.
|
||||||
|
|||||||
@@ -86,8 +86,8 @@ pub fn forEach(self: *Headers, cb_: js.Function, js_this_: ?js.Object) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: do we really need 2 different header structs??
|
// TODO: do we really need 2 different header structs??
|
||||||
const http = @import("../../../network/http.zig");
|
const net_http = @import("../../../network/http.zig");
|
||||||
pub fn populateHttpHeader(self: *Headers, allocator: Allocator, http_headers: *http.Headers) !void {
|
pub fn populateHttpHeader(self: *Headers, allocator: Allocator, http_headers: *net_http.Headers) !void {
|
||||||
for (self._list._entries.items) |entry| {
|
for (self._list._entries.items) |entry| {
|
||||||
const merged = try std.mem.concatWithSentinel(allocator, u8, &.{ entry.name.str(), ": ", entry.value.str() }, 0);
|
const merged = try std.mem.concatWithSentinel(allocator, u8, &.{ entry.name.str(), ": ", entry.value.str() }, 0);
|
||||||
try http_headers.add(merged);
|
try http_headers.add(merged);
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const js = @import("../../js/js.zig");
|
const js = @import("../../js/js.zig");
|
||||||
const http = @import("../../../network/http.zig");
|
const net_http = @import("../../../network/http.zig");
|
||||||
|
|
||||||
const URL = @import("../URL.zig");
|
const URL = @import("../URL.zig");
|
||||||
const Page = @import("../../Page.zig");
|
const Page = @import("../../Page.zig");
|
||||||
@@ -31,7 +31,7 @@ const Allocator = std.mem.Allocator;
|
|||||||
const Request = @This();
|
const Request = @This();
|
||||||
|
|
||||||
_url: [:0]const u8,
|
_url: [:0]const u8,
|
||||||
_method: http.Method,
|
_method: net_http.Method,
|
||||||
_headers: ?*Headers,
|
_headers: ?*Headers,
|
||||||
_body: ?[]const u8,
|
_body: ?[]const u8,
|
||||||
_arena: Allocator,
|
_arena: Allocator,
|
||||||
@@ -119,14 +119,14 @@ pub fn init(input: Input, opts_: ?InitOpts, page: *Page) !*Request {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseMethod(method: []const u8, page: *Page) !http.Method {
|
fn parseMethod(method: []const u8, page: *Page) !net_http.Method {
|
||||||
if (method.len > "propfind".len) {
|
if (method.len > "propfind".len) {
|
||||||
return error.InvalidMethod;
|
return error.InvalidMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
const lower = std.ascii.lowerString(&page.buf, method);
|
const lower = std.ascii.lowerString(&page.buf, method);
|
||||||
|
|
||||||
const method_lookup = std.StaticStringMap(http.Method).initComptime(.{
|
const method_lookup = std.StaticStringMap(net_http.Method).initComptime(.{
|
||||||
.{ "get", .GET },
|
.{ "get", .GET },
|
||||||
.{ "post", .POST },
|
.{ "post", .POST },
|
||||||
.{ "delete", .DELETE },
|
.{ "delete", .DELETE },
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ _type: Type,
|
|||||||
_status_text: []const u8,
|
_status_text: []const u8,
|
||||||
_url: [:0]const u8,
|
_url: [:0]const u8,
|
||||||
_is_redirected: bool,
|
_is_redirected: bool,
|
||||||
_http_response: ?HttpClient.Response = null,
|
_transfer: ?*HttpClient.Transfer = null,
|
||||||
|
|
||||||
const InitOpts = struct {
|
const InitOpts = struct {
|
||||||
status: u16 = 200,
|
status: u16 = 200,
|
||||||
@@ -81,9 +81,9 @@ pub fn init(body_: ?[]const u8, opts_: ?InitOpts, page: *Page) !*Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Response, session: *Session) void {
|
pub fn deinit(self: *Response, session: *Session) void {
|
||||||
if (self._http_response) |resp| {
|
if (self._transfer) |transfer| {
|
||||||
resp.abort(error.Abort);
|
transfer.abort(error.Abort);
|
||||||
self._http_response = null;
|
self._transfer = null;
|
||||||
}
|
}
|
||||||
session.releaseArena(self._arena);
|
session.releaseArena(self._arena);
|
||||||
}
|
}
|
||||||
@@ -191,7 +191,7 @@ pub fn clone(self: *const Response, page: *Page) !*Response {
|
|||||||
._type = self._type,
|
._type = self._type,
|
||||||
._is_redirected = self._is_redirected,
|
._is_redirected = self._is_redirected,
|
||||||
._headers = try Headers.init(.{ .obj = self._headers }, page),
|
._headers = try Headers.init(.{ .obj = self._headers }, page),
|
||||||
._http_response = null,
|
._transfer = null,
|
||||||
};
|
};
|
||||||
return cloned;
|
return cloned;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ const js = @import("../../js/js.zig");
|
|||||||
|
|
||||||
const log = @import("../../../log.zig");
|
const log = @import("../../../log.zig");
|
||||||
const HttpClient = @import("../../HttpClient.zig");
|
const HttpClient = @import("../../HttpClient.zig");
|
||||||
const http = @import("../../../network/http.zig");
|
const net_http = @import("../../../network/http.zig");
|
||||||
|
|
||||||
const URL = @import("../../URL.zig");
|
const URL = @import("../../URL.zig");
|
||||||
const Mime = @import("../../Mime.zig");
|
const Mime = @import("../../Mime.zig");
|
||||||
@@ -43,11 +43,11 @@ _rc: lp.RC(u8) = .{},
|
|||||||
_page: *Page,
|
_page: *Page,
|
||||||
_proto: *XMLHttpRequestEventTarget,
|
_proto: *XMLHttpRequestEventTarget,
|
||||||
_arena: Allocator,
|
_arena: Allocator,
|
||||||
_http_response: ?HttpClient.Response = null,
|
_transfer: ?*HttpClient.Transfer = null,
|
||||||
_active_request: bool = false,
|
_active_request: bool = false,
|
||||||
|
|
||||||
_url: [:0]const u8 = "",
|
_url: [:0]const u8 = "",
|
||||||
_method: http.Method = .GET,
|
_method: net_http.Method = .GET,
|
||||||
_request_headers: *Headers,
|
_request_headers: *Headers,
|
||||||
_request_body: ?[]const u8 = null,
|
_request_body: ?[]const u8 = null,
|
||||||
|
|
||||||
@@ -100,9 +100,9 @@ pub fn init(page: *Page) !*XMLHttpRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *XMLHttpRequest, session: *Session) void {
|
pub fn deinit(self: *XMLHttpRequest, session: *Session) void {
|
||||||
if (self._http_response) |resp| {
|
if (self._transfer) |transfer| {
|
||||||
resp.abort(error.Abort);
|
transfer.abort(error.Abort);
|
||||||
self._http_response = null;
|
self._transfer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self._on_ready_state_change) |func| {
|
if (self._on_ready_state_change) |func| {
|
||||||
@@ -184,9 +184,9 @@ pub fn setWithCredentials(self: *XMLHttpRequest, value: bool) !void {
|
|||||||
// TODO: url should be a union, as it can be multiple things
|
// TODO: url should be a union, as it can be multiple things
|
||||||
pub fn open(self: *XMLHttpRequest, method_: []const u8, url: [:0]const u8) !void {
|
pub fn open(self: *XMLHttpRequest, method_: []const u8, url: [:0]const u8) !void {
|
||||||
// Abort any in-progress request
|
// Abort any in-progress request
|
||||||
if (self._http_response) |transfer| {
|
if (self._transfer) |transfer| {
|
||||||
transfer.abort(error.Abort);
|
transfer.abort(error.Abort);
|
||||||
self._http_response = null;
|
self._transfer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset internal state
|
// Reset internal state
|
||||||
@@ -398,32 +398,34 @@ pub fn getResponseXML(self: *XMLHttpRequest, page: *Page) !?*Node.Document {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpStartCallback(response: HttpClient.Response) !void {
|
fn httpStartCallback(transfer: *HttpClient.Transfer) !void {
|
||||||
const self: *XMLHttpRequest = @ptrCast(@alignCast(response.ctx));
|
const self: *XMLHttpRequest = @ptrCast(@alignCast(transfer.ctx));
|
||||||
if (comptime IS_DEBUG) {
|
if (comptime IS_DEBUG) {
|
||||||
log.debug(.http, "request start", .{ .method = self._method, .url = self._url, .source = "xhr" });
|
log.debug(.http, "request start", .{ .method = self._method, .url = self._url, .source = "xhr" });
|
||||||
}
|
}
|
||||||
self._http_response = response;
|
self._transfer = transfer;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpHeaderCallback(response: HttpClient.Response, header: http.Header) !void {
|
fn httpHeaderCallback(transfer: *HttpClient.Transfer, header: net_http.Header) !void {
|
||||||
const self: *XMLHttpRequest = @ptrCast(@alignCast(response.ctx));
|
const self: *XMLHttpRequest = @ptrCast(@alignCast(transfer.ctx));
|
||||||
const joined = try std.fmt.allocPrint(self._arena, "{s}: {s}", .{ header.name, header.value });
|
const joined = try std.fmt.allocPrint(self._arena, "{s}: {s}", .{ header.name, header.value });
|
||||||
try self._response_headers.append(self._arena, joined);
|
try self._response_headers.append(self._arena, joined);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpHeaderDoneCallback(response: HttpClient.Response) !bool {
|
fn httpHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
|
||||||
const self: *XMLHttpRequest = @ptrCast(@alignCast(response.ctx));
|
const self: *XMLHttpRequest = @ptrCast(@alignCast(transfer.ctx));
|
||||||
|
|
||||||
|
const header = &transfer.response_header.?;
|
||||||
|
|
||||||
if (comptime IS_DEBUG) {
|
if (comptime IS_DEBUG) {
|
||||||
log.debug(.http, "request header", .{
|
log.debug(.http, "request header", .{
|
||||||
.source = "xhr",
|
.source = "xhr",
|
||||||
.url = self._url,
|
.url = self._url,
|
||||||
.status = response.status(),
|
.status = header.status,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.contentType()) |ct| {
|
if (header.contentType()) |ct| {
|
||||||
self._response_mime = Mime.parse(ct) catch |e| {
|
self._response_mime = Mime.parse(ct) catch |e| {
|
||||||
log.info(.http, "invalid content type", .{
|
log.info(.http, "invalid content type", .{
|
||||||
.content_Type = ct,
|
.content_Type = ct,
|
||||||
@@ -434,18 +436,18 @@ fn httpHeaderDoneCallback(response: HttpClient.Response) !bool {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var it = response.headerIterator();
|
var it = transfer.responseHeaderIterator();
|
||||||
while (it.next()) |hdr| {
|
while (it.next()) |hdr| {
|
||||||
const joined = try std.fmt.allocPrint(self._arena, "{s}: {s}", .{ hdr.name, hdr.value });
|
const joined = try std.fmt.allocPrint(self._arena, "{s}: {s}", .{ hdr.name, hdr.value });
|
||||||
try self._response_headers.append(self._arena, joined);
|
try self._response_headers.append(self._arena, joined);
|
||||||
}
|
}
|
||||||
|
|
||||||
self._response_status = response.status().?;
|
self._response_status = header.status;
|
||||||
if (response.contentLength()) |cl| {
|
if (transfer.getContentLength()) |cl| {
|
||||||
self._response_len = cl;
|
self._response_len = cl;
|
||||||
try self._response_data.ensureTotalCapacity(self._arena, cl);
|
try self._response_data.ensureTotalCapacity(self._arena, cl);
|
||||||
}
|
}
|
||||||
self._response_url = try self._arena.dupeZ(u8, response.url());
|
self._response_url = try self._arena.dupeZ(u8, std.mem.span(header.url));
|
||||||
|
|
||||||
const page = self._page;
|
const page = self._page;
|
||||||
|
|
||||||
@@ -460,8 +462,8 @@ fn httpHeaderDoneCallback(response: HttpClient.Response) !bool {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpDataCallback(response: HttpClient.Response, data: []const u8) !void {
|
fn httpDataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
|
||||||
const self: *XMLHttpRequest = @ptrCast(@alignCast(response.ctx));
|
const self: *XMLHttpRequest = @ptrCast(@alignCast(transfer.ctx));
|
||||||
try self._response_data.appendSlice(self._arena, data);
|
try self._response_data.appendSlice(self._arena, data);
|
||||||
|
|
||||||
const page = self._page;
|
const page = self._page;
|
||||||
@@ -484,7 +486,7 @@ fn httpDoneCallback(ctx: *anyopaque) !void {
|
|||||||
|
|
||||||
// Not that the request is done, the http/client will free the transfer
|
// Not that the request is done, the http/client will free the transfer
|
||||||
// object. It isn't safe to keep it around.
|
// object. It isn't safe to keep it around.
|
||||||
self._http_response = null;
|
self._transfer = null;
|
||||||
|
|
||||||
const page = self._page;
|
const page = self._page;
|
||||||
|
|
||||||
@@ -507,22 +509,22 @@ fn httpErrorCallback(ctx: *anyopaque, err: anyerror) void {
|
|||||||
const self: *XMLHttpRequest = @ptrCast(@alignCast(ctx));
|
const self: *XMLHttpRequest = @ptrCast(@alignCast(ctx));
|
||||||
// http client will close it after an error, it isn't safe to keep around
|
// http client will close it after an error, it isn't safe to keep around
|
||||||
self.handleError(err);
|
self.handleError(err);
|
||||||
if (self._http_response != null) {
|
if (self._transfer != null) {
|
||||||
self._http_response = null;
|
self._transfer = null;
|
||||||
}
|
}
|
||||||
self.releaseSelfRef();
|
self.releaseSelfRef();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn httpShutdownCallback(ctx: *anyopaque) void {
|
fn httpShutdownCallback(ctx: *anyopaque) void {
|
||||||
const self: *XMLHttpRequest = @ptrCast(@alignCast(ctx));
|
const self: *XMLHttpRequest = @ptrCast(@alignCast(ctx));
|
||||||
self._http_response = null;
|
self._transfer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn abort(self: *XMLHttpRequest) void {
|
pub fn abort(self: *XMLHttpRequest) void {
|
||||||
self.handleError(error.Abort);
|
self.handleError(error.Abort);
|
||||||
if (self._http_response) |resp| {
|
if (self._transfer) |transfer| {
|
||||||
self._http_response = null;
|
self._transfer = null;
|
||||||
resp.abort(error.Abort);
|
transfer.abort(error.Abort);
|
||||||
}
|
}
|
||||||
self.releaseSelfRef();
|
self.releaseSelfRef();
|
||||||
}
|
}
|
||||||
@@ -572,7 +574,7 @@ fn stateChanged(self: *XMLHttpRequest, state: ReadyState, page: *Page) !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseMethod(method: []const u8) !http.Method {
|
fn parseMethod(method: []const u8) !net_http.Method {
|
||||||
if (std.ascii.eqlIgnoreCase(method, "get")) {
|
if (std.ascii.eqlIgnoreCase(method, "get")) {
|
||||||
return .GET;
|
return .GET;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ const CDP = @import("../CDP.zig");
|
|||||||
const log = @import("../../log.zig");
|
const log = @import("../../log.zig");
|
||||||
|
|
||||||
const HttpClient = @import("../../browser/HttpClient.zig");
|
const HttpClient = @import("../../browser/HttpClient.zig");
|
||||||
const http = @import("../../network/http.zig");
|
const net_http = @import("../../network/http.zig");
|
||||||
const Notification = @import("../../Notification.zig");
|
const Notification = @import("../../Notification.zig");
|
||||||
|
|
||||||
const network = @import("network.zig");
|
const network = @import("network.zig");
|
||||||
@@ -224,7 +224,7 @@ fn continueRequest(cmd: *CDP.Command) !void {
|
|||||||
url: ?[]const u8 = null,
|
url: ?[]const u8 = null,
|
||||||
method: ?[]const u8 = null,
|
method: ?[]const u8 = null,
|
||||||
postData: ?[]const u8 = null,
|
postData: ?[]const u8 = null,
|
||||||
headers: ?[]const http.Header = null,
|
headers: ?[]const net_http.Header = null,
|
||||||
interceptResponse: bool = false,
|
interceptResponse: bool = false,
|
||||||
})) orelse return error.InvalidParams;
|
})) orelse return error.InvalidParams;
|
||||||
|
|
||||||
@@ -249,7 +249,7 @@ fn continueRequest(cmd: *CDP.Command) !void {
|
|||||||
try transfer.updateURL(try arena.dupeZ(u8, url));
|
try transfer.updateURL(try arena.dupeZ(u8, url));
|
||||||
}
|
}
|
||||||
if (params.method) |method| {
|
if (params.method) |method| {
|
||||||
transfer.req.method = std.meta.stringToEnum(http.Method, method) orelse return error.InvalidParams;
|
transfer.req.method = std.meta.stringToEnum(net_http.Method, method) orelse return error.InvalidParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.headers) |headers| {
|
if (params.headers) |headers| {
|
||||||
@@ -326,7 +326,7 @@ fn fulfillRequest(cmd: *CDP.Command) !void {
|
|||||||
const params = (try cmd.params(struct {
|
const params = (try cmd.params(struct {
|
||||||
requestId: []const u8, // "INT-{d}"
|
requestId: []const u8, // "INT-{d}"
|
||||||
responseCode: u16,
|
responseCode: u16,
|
||||||
responseHeaders: ?[]const http.Header = null,
|
responseHeaders: ?[]const net_http.Header = null,
|
||||||
binaryResponseHeaders: ?[]const u8 = null,
|
binaryResponseHeaders: ?[]const u8 = null,
|
||||||
body: ?[]const u8 = null,
|
body: ?[]const u8 = null,
|
||||||
responsePhrase: ?[]const u8 = null,
|
responsePhrase: ?[]const u8 = null,
|
||||||
|
|||||||
@@ -139,8 +139,8 @@ fn setLifecycleEventsEnabled(cmd: *CDP.Command) !void {
|
|||||||
try sendPageLifecycle(bc, "load", now, frame_id, loader_id);
|
try sendPageLifecycle(bc, "load", now, frame_id, loader_id);
|
||||||
|
|
||||||
const http_client = page._session.browser.http_client;
|
const http_client = page._session.browser.http_client;
|
||||||
const http_active = http_client.active();
|
const http_active = http_client.active;
|
||||||
const total_network_activity = http_active + http_client.intercepted();
|
const total_network_activity = http_active + http_client.intercepted;
|
||||||
if (page._notified_network_almost_idle.check(total_network_activity <= 2)) {
|
if (page._notified_network_almost_idle.check(total_network_activity <= 2)) {
|
||||||
try sendPageLifecycle(bc, "networkAlmostIdle", now, frame_id, loader_id);
|
try sendPageLifecycle(bc, "networkAlmostIdle", now, frame_id, loader_id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
pub const App = @import("App.zig");
|
pub const App = @import("App.zig");
|
||||||
pub const Network = @import("network/Network.zig");
|
pub const Network = @import("network/Runtime.zig");
|
||||||
pub const Server = @import("Server.zig");
|
pub const Server = @import("Server.zig");
|
||||||
pub const Config = @import("Config.zig");
|
pub const Config = @import("Config.zig");
|
||||||
pub const URL = @import("browser/URL.zig");
|
pub const URL = @import("browser/URL.zig");
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ pub const Scope = enum {
|
|||||||
telemetry,
|
telemetry,
|
||||||
unknown_prop,
|
unknown_prop,
|
||||||
mcp,
|
mcp,
|
||||||
cache,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Opts = struct {
|
const Opts = struct {
|
||||||
|
|||||||
@@ -179,6 +179,74 @@ pub const tool_list = [_]protocol.Tool{
|
|||||||
\\}
|
\\}
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
.{
|
||||||
|
.name = "hover",
|
||||||
|
.description = "Hover over an element, triggering mouseover and mouseenter events. Useful for menus, tooltips, and hover states.",
|
||||||
|
.inputSchema = protocol.minify(
|
||||||
|
\\{
|
||||||
|
\\ "type": "object",
|
||||||
|
\\ "properties": {
|
||||||
|
\\ "backendNodeId": { "type": "integer", "description": "The backend node ID of the element to hover over." }
|
||||||
|
\\ },
|
||||||
|
\\ "required": ["backendNodeId"]
|
||||||
|
\\}
|
||||||
|
),
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.name = "press",
|
||||||
|
.description = "Press a keyboard key, dispatching keydown and keyup events. Use key names like 'Enter', 'Tab', 'Escape', 'ArrowDown', 'Backspace', or single characters like 'a', '1'.",
|
||||||
|
.inputSchema = protocol.minify(
|
||||||
|
\\{
|
||||||
|
\\ "type": "object",
|
||||||
|
\\ "properties": {
|
||||||
|
\\ "key": { "type": "string", "description": "The key to press (e.g. 'Enter', 'Tab', 'a')." },
|
||||||
|
\\ "backendNodeId": { "type": "integer", "description": "Optional backend node ID of the element to target. Defaults to the document." }
|
||||||
|
\\ },
|
||||||
|
\\ "required": ["key"]
|
||||||
|
\\}
|
||||||
|
),
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.name = "selectOption",
|
||||||
|
.description = "Select an option in a <select> dropdown element by its value. Dispatches input and change events.",
|
||||||
|
.inputSchema = protocol.minify(
|
||||||
|
\\{
|
||||||
|
\\ "type": "object",
|
||||||
|
\\ "properties": {
|
||||||
|
\\ "backendNodeId": { "type": "integer", "description": "The backend node ID of the <select> element." },
|
||||||
|
\\ "value": { "type": "string", "description": "The value of the option to select." }
|
||||||
|
\\ },
|
||||||
|
\\ "required": ["backendNodeId", "value"]
|
||||||
|
\\}
|
||||||
|
),
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.name = "setChecked",
|
||||||
|
.description = "Check or uncheck a checkbox or radio button. Dispatches input, change, and click events.",
|
||||||
|
.inputSchema = protocol.minify(
|
||||||
|
\\{
|
||||||
|
\\ "type": "object",
|
||||||
|
\\ "properties": {
|
||||||
|
\\ "backendNodeId": { "type": "integer", "description": "The backend node ID of the checkbox or radio input element." },
|
||||||
|
\\ "checked": { "type": "boolean", "description": "Whether to check (true) or uncheck (false) the element." }
|
||||||
|
\\ },
|
||||||
|
\\ "required": ["backendNodeId", "checked"]
|
||||||
|
\\}
|
||||||
|
),
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.name = "findElement",
|
||||||
|
.description = "Find interactive elements by role and/or accessible name. Returns matching elements with their backend node IDs. Useful for locating specific elements without parsing the full semantic tree.",
|
||||||
|
.inputSchema = protocol.minify(
|
||||||
|
\\{
|
||||||
|
\\ "type": "object",
|
||||||
|
\\ "properties": {
|
||||||
|
\\ "role": { "type": "string", "description": "Optional ARIA role to match (e.g. 'button', 'link', 'textbox', 'checkbox')." },
|
||||||
|
\\ "name": { "type": "string", "description": "Optional accessible name substring to match (case-insensitive)." }
|
||||||
|
\\ }
|
||||||
|
\\}
|
||||||
|
),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn handleList(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {
|
pub fn handleList(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {
|
||||||
@@ -279,6 +347,11 @@ const ToolAction = enum {
|
|||||||
fill,
|
fill,
|
||||||
scroll,
|
scroll,
|
||||||
waitForSelector,
|
waitForSelector,
|
||||||
|
hover,
|
||||||
|
press,
|
||||||
|
selectOption,
|
||||||
|
setChecked,
|
||||||
|
findElement,
|
||||||
};
|
};
|
||||||
|
|
||||||
const tool_map = std.StaticStringMap(ToolAction).initComptime(.{
|
const tool_map = std.StaticStringMap(ToolAction).initComptime(.{
|
||||||
@@ -296,6 +369,11 @@ const tool_map = std.StaticStringMap(ToolAction).initComptime(.{
|
|||||||
.{ "fill", .fill },
|
.{ "fill", .fill },
|
||||||
.{ "scroll", .scroll },
|
.{ "scroll", .scroll },
|
||||||
.{ "waitForSelector", .waitForSelector },
|
.{ "waitForSelector", .waitForSelector },
|
||||||
|
.{ "hover", .hover },
|
||||||
|
.{ "press", .press },
|
||||||
|
.{ "selectOption", .selectOption },
|
||||||
|
.{ "setChecked", .setChecked },
|
||||||
|
.{ "findElement", .findElement },
|
||||||
});
|
});
|
||||||
|
|
||||||
pub fn handleCall(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {
|
pub fn handleCall(server: *Server, arena: std.mem.Allocator, req: protocol.Request) !void {
|
||||||
@@ -330,6 +408,11 @@ pub fn handleCall(server: *Server, arena: std.mem.Allocator, req: protocol.Reque
|
|||||||
.fill => try handleFill(server, arena, req.id.?, call_params.arguments),
|
.fill => try handleFill(server, arena, req.id.?, call_params.arguments),
|
||||||
.scroll => try handleScroll(server, arena, req.id.?, call_params.arguments),
|
.scroll => try handleScroll(server, arena, req.id.?, call_params.arguments),
|
||||||
.waitForSelector => try handleWaitForSelector(server, arena, req.id.?, call_params.arguments),
|
.waitForSelector => try handleWaitForSelector(server, arena, req.id.?, call_params.arguments),
|
||||||
|
.hover => try handleHover(server, arena, req.id.?, call_params.arguments),
|
||||||
|
.press => try handlePress(server, arena, req.id.?, call_params.arguments),
|
||||||
|
.selectOption => try handleSelectOption(server, arena, req.id.?, call_params.arguments),
|
||||||
|
.setChecked => try handleSetChecked(server, arena, req.id.?, call_params.arguments),
|
||||||
|
.findElement => try handleFindElement(server, arena, req.id.?, call_params.arguments),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -394,17 +477,9 @@ fn handleNodeDetails(server: *Server, arena: std.mem.Allocator, id: std.json.Val
|
|||||||
backendNodeId: CDPNode.Id,
|
backendNodeId: CDPNode.Id,
|
||||||
};
|
};
|
||||||
const args = try parseArgs(Params, arena, arguments, server, id, "nodeDetails");
|
const args = try parseArgs(Params, arena, arguments, server, id, "nodeDetails");
|
||||||
|
const resolved = try resolveNodeAndPage(server, id, args.backendNodeId);
|
||||||
|
|
||||||
_ = server.session.currentPage() orelse {
|
const details = lp.SemanticTree.getNodeDetails(arena, resolved.node, &server.node_registry, resolved.page) catch {
|
||||||
return server.sendError(id, .PageNotLoaded, "Page not loaded");
|
|
||||||
};
|
|
||||||
|
|
||||||
const node = server.node_registry.lookup_by_id.get(args.backendNodeId) orelse {
|
|
||||||
return server.sendError(id, .InvalidParams, "Node not found");
|
|
||||||
};
|
|
||||||
|
|
||||||
const page = server.session.currentPage().?;
|
|
||||||
const details = lp.SemanticTree.getNodeDetails(arena, node.dom, &server.node_registry, page) catch {
|
|
||||||
return server.sendError(id, .InternalError, "Failed to get node details");
|
return server.sendError(id, .InternalError, "Failed to get node details");
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -504,26 +579,19 @@ fn handleClick(server: *Server, arena: std.mem.Allocator, id: std.json.Value, ar
|
|||||||
backendNodeId: CDPNode.Id,
|
backendNodeId: CDPNode.Id,
|
||||||
};
|
};
|
||||||
const args = try parseArgs(ClickParams, arena, arguments, server, id, "click");
|
const args = try parseArgs(ClickParams, arena, arguments, server, id, "click");
|
||||||
|
const resolved = try resolveNodeAndPage(server, id, args.backendNodeId);
|
||||||
|
|
||||||
const page = server.session.currentPage() orelse {
|
lp.actions.click(resolved.node, resolved.page) catch |err| {
|
||||||
return server.sendError(id, .PageNotLoaded, "Page not loaded");
|
|
||||||
};
|
|
||||||
|
|
||||||
const node = server.node_registry.lookup_by_id.get(args.backendNodeId) orelse {
|
|
||||||
return server.sendError(id, .InvalidParams, "Node not found");
|
|
||||||
};
|
|
||||||
|
|
||||||
lp.actions.click(node.dom, page) catch |err| {
|
|
||||||
if (err == error.InvalidNodeType) {
|
if (err == error.InvalidNodeType) {
|
||||||
return server.sendError(id, .InvalidParams, "Node is not an HTML element");
|
return server.sendError(id, .InvalidParams, "Node is not an HTML element");
|
||||||
}
|
}
|
||||||
return server.sendError(id, .InternalError, "Failed to click element");
|
return server.sendError(id, .InternalError, "Failed to click element");
|
||||||
};
|
};
|
||||||
|
|
||||||
const page_title = page.getTitle() catch null;
|
const page_title = resolved.page.getTitle() catch null;
|
||||||
const result_text = try std.fmt.allocPrint(arena, "Clicked element (backendNodeId: {d}). Page url: {s}, title: {s}", .{
|
const result_text = try std.fmt.allocPrint(arena, "Clicked element (backendNodeId: {d}). Page url: {s}, title: {s}", .{
|
||||||
args.backendNodeId,
|
args.backendNodeId,
|
||||||
page.url,
|
resolved.page.url,
|
||||||
page_title orelse "(none)",
|
page_title orelse "(none)",
|
||||||
});
|
});
|
||||||
const content = [_]protocol.TextContent([]const u8){.{ .text = result_text }};
|
const content = [_]protocol.TextContent([]const u8){.{ .text = result_text }};
|
||||||
@@ -536,27 +604,20 @@ fn handleFill(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arg
|
|||||||
text: []const u8,
|
text: []const u8,
|
||||||
};
|
};
|
||||||
const args = try parseArgs(FillParams, arena, arguments, server, id, "fill");
|
const args = try parseArgs(FillParams, arena, arguments, server, id, "fill");
|
||||||
|
const resolved = try resolveNodeAndPage(server, id, args.backendNodeId);
|
||||||
|
|
||||||
const page = server.session.currentPage() orelse {
|
lp.actions.fill(resolved.node, args.text, resolved.page) catch |err| {
|
||||||
return server.sendError(id, .PageNotLoaded, "Page not loaded");
|
|
||||||
};
|
|
||||||
|
|
||||||
const node = server.node_registry.lookup_by_id.get(args.backendNodeId) orelse {
|
|
||||||
return server.sendError(id, .InvalidParams, "Node not found");
|
|
||||||
};
|
|
||||||
|
|
||||||
lp.actions.fill(node.dom, args.text, page) catch |err| {
|
|
||||||
if (err == error.InvalidNodeType) {
|
if (err == error.InvalidNodeType) {
|
||||||
return server.sendError(id, .InvalidParams, "Node is not an input, textarea or select");
|
return server.sendError(id, .InvalidParams, "Node is not an input, textarea or select");
|
||||||
}
|
}
|
||||||
return server.sendError(id, .InternalError, "Failed to fill element");
|
return server.sendError(id, .InternalError, "Failed to fill element");
|
||||||
};
|
};
|
||||||
|
|
||||||
const page_title = page.getTitle() catch null;
|
const page_title = resolved.page.getTitle() catch null;
|
||||||
const result_text = try std.fmt.allocPrint(arena, "Filled element (backendNodeId: {d}) with \"{s}\". Page url: {s}, title: {s}", .{
|
const result_text = try std.fmt.allocPrint(arena, "Filled element (backendNodeId: {d}) with \"{s}\". Page url: {s}, title: {s}", .{
|
||||||
args.backendNodeId,
|
args.backendNodeId,
|
||||||
args.text,
|
args.text,
|
||||||
page.url,
|
resolved.page.url,
|
||||||
page_title orelse "(none)",
|
page_title orelse "(none)",
|
||||||
});
|
});
|
||||||
const content = [_]protocol.TextContent([]const u8){.{ .text = result_text }};
|
const content = [_]protocol.TextContent([]const u8){.{ .text = result_text }};
|
||||||
@@ -630,6 +691,189 @@ fn handleWaitForSelector(server: *Server, arena: std.mem.Allocator, id: std.json
|
|||||||
return server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content });
|
return server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handleHover(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
||||||
|
const Params = struct {
|
||||||
|
backendNodeId: CDPNode.Id,
|
||||||
|
};
|
||||||
|
const args = try parseArgs(Params, arena, arguments, server, id, "hover");
|
||||||
|
const resolved = try resolveNodeAndPage(server, id, args.backendNodeId);
|
||||||
|
|
||||||
|
lp.actions.hover(resolved.node, resolved.page) catch |err| {
|
||||||
|
if (err == error.InvalidNodeType) {
|
||||||
|
return server.sendError(id, .InvalidParams, "Node is not an HTML element");
|
||||||
|
}
|
||||||
|
return server.sendError(id, .InternalError, "Failed to hover element");
|
||||||
|
};
|
||||||
|
|
||||||
|
const page_title = resolved.page.getTitle() catch null;
|
||||||
|
const result_text = try std.fmt.allocPrint(arena, "Hovered element (backendNodeId: {d}). Page url: {s}, title: {s}", .{
|
||||||
|
args.backendNodeId,
|
||||||
|
resolved.page.url,
|
||||||
|
page_title orelse "(none)",
|
||||||
|
});
|
||||||
|
const content = [_]protocol.TextContent([]const u8){.{ .text = result_text }};
|
||||||
|
try server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handlePress(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
||||||
|
const Params = struct {
|
||||||
|
key: []const u8,
|
||||||
|
backendNodeId: ?CDPNode.Id = null,
|
||||||
|
};
|
||||||
|
const args = try parseArgs(Params, arena, arguments, server, id, "press");
|
||||||
|
|
||||||
|
const page = server.session.currentPage() orelse {
|
||||||
|
return server.sendError(id, .PageNotLoaded, "Page not loaded");
|
||||||
|
};
|
||||||
|
|
||||||
|
var target_node: ?*DOMNode = null;
|
||||||
|
if (args.backendNodeId) |node_id| {
|
||||||
|
const node = server.node_registry.lookup_by_id.get(node_id) orelse {
|
||||||
|
return server.sendError(id, .InvalidParams, "Node not found");
|
||||||
|
};
|
||||||
|
target_node = node.dom;
|
||||||
|
}
|
||||||
|
|
||||||
|
lp.actions.press(target_node, args.key, page) catch |err| {
|
||||||
|
if (err == error.InvalidNodeType) {
|
||||||
|
return server.sendError(id, .InvalidParams, "Node is not an HTML element");
|
||||||
|
}
|
||||||
|
return server.sendError(id, .InternalError, "Failed to press key");
|
||||||
|
};
|
||||||
|
|
||||||
|
const page_title = page.getTitle() catch null;
|
||||||
|
const result_text = try std.fmt.allocPrint(arena, "Pressed key '{s}'. Page url: {s}, title: {s}", .{
|
||||||
|
args.key,
|
||||||
|
page.url,
|
||||||
|
page_title orelse "(none)",
|
||||||
|
});
|
||||||
|
const content = [_]protocol.TextContent([]const u8){.{ .text = result_text }};
|
||||||
|
try server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handleSelectOption(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
||||||
|
const Params = struct {
|
||||||
|
backendNodeId: CDPNode.Id,
|
||||||
|
value: []const u8,
|
||||||
|
};
|
||||||
|
const args = try parseArgs(Params, arena, arguments, server, id, "selectOption");
|
||||||
|
const resolved = try resolveNodeAndPage(server, id, args.backendNodeId);
|
||||||
|
|
||||||
|
lp.actions.selectOption(resolved.node, args.value, resolved.page) catch |err| {
|
||||||
|
if (err == error.InvalidNodeType) {
|
||||||
|
return server.sendError(id, .InvalidParams, "Node is not a <select> element");
|
||||||
|
}
|
||||||
|
return server.sendError(id, .InternalError, "Failed to select option");
|
||||||
|
};
|
||||||
|
|
||||||
|
const page_title = resolved.page.getTitle() catch null;
|
||||||
|
const result_text = try std.fmt.allocPrint(arena, "Selected option '{s}' (backendNodeId: {d}). Page url: {s}, title: {s}", .{
|
||||||
|
args.value,
|
||||||
|
args.backendNodeId,
|
||||||
|
resolved.page.url,
|
||||||
|
page_title orelse "(none)",
|
||||||
|
});
|
||||||
|
const content = [_]protocol.TextContent([]const u8){.{ .text = result_text }};
|
||||||
|
try server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handleSetChecked(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
||||||
|
const Params = struct {
|
||||||
|
backendNodeId: CDPNode.Id,
|
||||||
|
checked: bool,
|
||||||
|
};
|
||||||
|
const args = try parseArgs(Params, arena, arguments, server, id, "setChecked");
|
||||||
|
const resolved = try resolveNodeAndPage(server, id, args.backendNodeId);
|
||||||
|
|
||||||
|
lp.actions.setChecked(resolved.node, args.checked, resolved.page) catch |err| {
|
||||||
|
if (err == error.InvalidNodeType) {
|
||||||
|
return server.sendError(id, .InvalidParams, "Node is not a checkbox or radio input");
|
||||||
|
}
|
||||||
|
return server.sendError(id, .InternalError, "Failed to set checked state");
|
||||||
|
};
|
||||||
|
|
||||||
|
const state_str = if (args.checked) "checked" else "unchecked";
|
||||||
|
const page_title = resolved.page.getTitle() catch null;
|
||||||
|
const result_text = try std.fmt.allocPrint(arena, "Set element (backendNodeId: {d}) to {s}. Page url: {s}, title: {s}", .{
|
||||||
|
args.backendNodeId,
|
||||||
|
state_str,
|
||||||
|
resolved.page.url,
|
||||||
|
page_title orelse "(none)",
|
||||||
|
});
|
||||||
|
const content = [_]protocol.TextContent([]const u8){.{ .text = result_text }};
|
||||||
|
try server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handleFindElement(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
|
||||||
|
const Params = struct {
|
||||||
|
role: ?[]const u8 = null,
|
||||||
|
name: ?[]const u8 = null,
|
||||||
|
};
|
||||||
|
const args = try parseArgsOrDefault(Params, arena, arguments, server, id);
|
||||||
|
|
||||||
|
if (args.role == null and args.name == null) {
|
||||||
|
return server.sendError(id, .InvalidParams, "At least one of 'role' or 'name' must be provided");
|
||||||
|
}
|
||||||
|
|
||||||
|
const page = server.session.currentPage() orelse {
|
||||||
|
return server.sendError(id, .PageNotLoaded, "Page not loaded");
|
||||||
|
};
|
||||||
|
|
||||||
|
const elements = lp.interactive.collectInteractiveElements(page.document.asNode(), arena, page) catch |err| {
|
||||||
|
log.err(.mcp, "elements collection failed", .{ .err = err });
|
||||||
|
return server.sendError(id, .InternalError, "Failed to collect interactive elements");
|
||||||
|
};
|
||||||
|
|
||||||
|
var matches: std.ArrayList(lp.interactive.InteractiveElement) = .empty;
|
||||||
|
for (elements) |el| {
|
||||||
|
if (args.role) |role| {
|
||||||
|
const el_role = el.role orelse continue;
|
||||||
|
if (!std.ascii.eqlIgnoreCase(el_role, role)) continue;
|
||||||
|
}
|
||||||
|
if (args.name) |name| {
|
||||||
|
const el_name = el.name orelse continue;
|
||||||
|
if (!containsIgnoreCase(el_name, name)) continue;
|
||||||
|
}
|
||||||
|
try matches.append(arena, el);
|
||||||
|
}
|
||||||
|
|
||||||
|
const matched = try matches.toOwnedSlice(arena);
|
||||||
|
lp.interactive.registerNodes(matched, &server.node_registry) catch |err| {
|
||||||
|
log.err(.mcp, "node registration failed", .{ .err = err });
|
||||||
|
return server.sendError(id, .InternalError, "Failed to register element nodes");
|
||||||
|
};
|
||||||
|
|
||||||
|
var aw: std.Io.Writer.Allocating = .init(arena);
|
||||||
|
try std.json.Stringify.value(matched, .{}, &aw.writer);
|
||||||
|
|
||||||
|
const content = [_]protocol.TextContent([]const u8){.{ .text = aw.written() }};
|
||||||
|
try server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn containsIgnoreCase(haystack: []const u8, needle: []const u8) bool {
|
||||||
|
if (needle.len > haystack.len) return false;
|
||||||
|
if (needle.len == 0) return true;
|
||||||
|
const end = haystack.len - needle.len + 1;
|
||||||
|
for (0..end) |i| {
|
||||||
|
if (std.ascii.eqlIgnoreCase(haystack[i..][0..needle.len], needle)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const NodeAndPage = struct { node: *DOMNode, page: *lp.Page };
|
||||||
|
|
||||||
|
fn resolveNodeAndPage(server: *Server, id: std.json.Value, node_id: CDPNode.Id) !NodeAndPage {
|
||||||
|
const page = server.session.currentPage() orelse {
|
||||||
|
try server.sendError(id, .PageNotLoaded, "Page not loaded");
|
||||||
|
return error.PageNotLoaded;
|
||||||
|
};
|
||||||
|
const node = server.node_registry.lookup_by_id.get(node_id) orelse {
|
||||||
|
try server.sendError(id, .InvalidParams, "Node not found");
|
||||||
|
return error.InvalidParams;
|
||||||
|
};
|
||||||
|
return .{ .node = node.dom, .page = page };
|
||||||
|
}
|
||||||
|
|
||||||
fn ensurePage(server: *Server, id: std.json.Value, url: ?[:0]const u8) !*lp.Page {
|
fn ensurePage(server: *Server, id: std.json.Value, url: ?[:0]const u8) !*lp.Page {
|
||||||
if (url) |u| {
|
if (url) |u| {
|
||||||
try performGoto(server, u, id);
|
try performGoto(server, u, id);
|
||||||
@@ -727,7 +971,7 @@ test "MCP - evaluate error reporting" {
|
|||||||
} }, out.written());
|
} }, out.written());
|
||||||
}
|
}
|
||||||
|
|
||||||
test "MCP - Actions: click, fill, scroll" {
|
test "MCP - Actions: click, fill, scroll, hover, press, selectOption, setChecked" {
|
||||||
defer testing.reset();
|
defer testing.reset();
|
||||||
const aa = testing.arena_allocator;
|
const aa = testing.arena_allocator;
|
||||||
|
|
||||||
@@ -788,7 +1032,67 @@ test "MCP - Actions: click, fill, scroll" {
|
|||||||
out.clearRetainingCapacity();
|
out.clearRetainingCapacity();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate assertions
|
{
|
||||||
|
// Test Hover
|
||||||
|
const el = page.document.getElementById("hoverTarget", page).?.asNode();
|
||||||
|
const el_id = (try server.node_registry.register(el)).id;
|
||||||
|
var id_buf: [12]u8 = undefined;
|
||||||
|
const id_str = std.fmt.bufPrint(&id_buf, "{d}", .{el_id}) catch unreachable;
|
||||||
|
const msg = try std.mem.concat(aa, u8, &.{ "{\"jsonrpc\":\"2.0\",\"id\":5,\"method\":\"tools/call\",\"params\":{\"name\":\"hover\",\"arguments\":{\"backendNodeId\":", id_str, "}}}" });
|
||||||
|
try router.handleMessage(server, aa, msg);
|
||||||
|
try testing.expect(std.mem.indexOf(u8, out.written(), "Hovered element") != null);
|
||||||
|
out.clearRetainingCapacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Test Press
|
||||||
|
const el = page.document.getElementById("keyTarget", page).?.asNode();
|
||||||
|
const el_id = (try server.node_registry.register(el)).id;
|
||||||
|
var id_buf: [12]u8 = undefined;
|
||||||
|
const id_str = std.fmt.bufPrint(&id_buf, "{d}", .{el_id}) catch unreachable;
|
||||||
|
const msg = try std.mem.concat(aa, u8, &.{ "{\"jsonrpc\":\"2.0\",\"id\":6,\"method\":\"tools/call\",\"params\":{\"name\":\"press\",\"arguments\":{\"key\":\"Enter\",\"backendNodeId\":", id_str, "}}}" });
|
||||||
|
try router.handleMessage(server, aa, msg);
|
||||||
|
try testing.expect(std.mem.indexOf(u8, out.written(), "Pressed key") != null);
|
||||||
|
out.clearRetainingCapacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Test SelectOption
|
||||||
|
const el = page.document.getElementById("sel2", page).?.asNode();
|
||||||
|
const el_id = (try server.node_registry.register(el)).id;
|
||||||
|
var id_buf: [12]u8 = undefined;
|
||||||
|
const id_str = std.fmt.bufPrint(&id_buf, "{d}", .{el_id}) catch unreachable;
|
||||||
|
const msg = try std.mem.concat(aa, u8, &.{ "{\"jsonrpc\":\"2.0\",\"id\":7,\"method\":\"tools/call\",\"params\":{\"name\":\"selectOption\",\"arguments\":{\"backendNodeId\":", id_str, ",\"value\":\"b\"}}}" });
|
||||||
|
try router.handleMessage(server, aa, msg);
|
||||||
|
try testing.expect(std.mem.indexOf(u8, out.written(), "Selected option") != null);
|
||||||
|
out.clearRetainingCapacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Test SetChecked (checkbox)
|
||||||
|
const el = page.document.getElementById("chk", page).?.asNode();
|
||||||
|
const el_id = (try server.node_registry.register(el)).id;
|
||||||
|
var id_buf: [12]u8 = undefined;
|
||||||
|
const id_str = std.fmt.bufPrint(&id_buf, "{d}", .{el_id}) catch unreachable;
|
||||||
|
const msg = try std.mem.concat(aa, u8, &.{ "{\"jsonrpc\":\"2.0\",\"id\":8,\"method\":\"tools/call\",\"params\":{\"name\":\"setChecked\",\"arguments\":{\"backendNodeId\":", id_str, ",\"checked\":true}}}" });
|
||||||
|
try router.handleMessage(server, aa, msg);
|
||||||
|
try testing.expect(std.mem.indexOf(u8, out.written(), "checked") != null);
|
||||||
|
out.clearRetainingCapacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Test SetChecked (radio)
|
||||||
|
const el = page.document.getElementById("rad", page).?.asNode();
|
||||||
|
const el_id = (try server.node_registry.register(el)).id;
|
||||||
|
var id_buf: [12]u8 = undefined;
|
||||||
|
const id_str = std.fmt.bufPrint(&id_buf, "{d}", .{el_id}) catch unreachable;
|
||||||
|
const msg = try std.mem.concat(aa, u8, &.{ "{\"jsonrpc\":\"2.0\",\"id\":9,\"method\":\"tools/call\",\"params\":{\"name\":\"setChecked\",\"arguments\":{\"backendNodeId\":", id_str, ",\"checked\":true}}}" });
|
||||||
|
try router.handleMessage(server, aa, msg);
|
||||||
|
try testing.expect(std.mem.indexOf(u8, out.written(), "checked") != null);
|
||||||
|
out.clearRetainingCapacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate JS assertions for all actions
|
||||||
var ls: js.Local.Scope = undefined;
|
var ls: js.Local.Scope = undefined;
|
||||||
page.js.localScope(&ls);
|
page.js.localScope(&ls);
|
||||||
defer ls.deinit();
|
defer ls.deinit();
|
||||||
@@ -800,12 +1104,66 @@ test "MCP - Actions: click, fill, scroll" {
|
|||||||
const result = try ls.local.exec(
|
const result = try ls.local.exec(
|
||||||
\\ window.clicked === true && window.inputVal === 'hello' &&
|
\\ window.clicked === true && window.inputVal === 'hello' &&
|
||||||
\\ window.changed === true && window.selChanged === 'opt2' &&
|
\\ window.changed === true && window.selChanged === 'opt2' &&
|
||||||
\\ window.scrolled === true
|
\\ window.scrolled === true &&
|
||||||
|
\\ window.hovered === true &&
|
||||||
|
\\ window.keyPressed === 'Enter' && window.keyReleased === 'Enter' &&
|
||||||
|
\\ window.sel2Changed === 'b' &&
|
||||||
|
\\ window.chkClicked === true && window.chkChanged === true &&
|
||||||
|
\\ window.radClicked === true && window.radChanged === true
|
||||||
, null);
|
, null);
|
||||||
|
|
||||||
try testing.expect(result.isTrue());
|
try testing.expect(result.isTrue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "MCP - findElement" {
|
||||||
|
defer testing.reset();
|
||||||
|
const aa = testing.arena_allocator;
|
||||||
|
|
||||||
|
var out: std.io.Writer.Allocating = .init(aa);
|
||||||
|
const server = try testLoadPage("http://localhost:9582/src/browser/tests/mcp_actions.html", &out.writer);
|
||||||
|
defer server.deinit();
|
||||||
|
|
||||||
|
{
|
||||||
|
// Find by role
|
||||||
|
const msg =
|
||||||
|
\\{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"findElement","arguments":{"role":"button"}}}
|
||||||
|
;
|
||||||
|
try router.handleMessage(server, aa, msg);
|
||||||
|
try testing.expect(std.mem.indexOf(u8, out.written(), "Click Me") != null);
|
||||||
|
out.clearRetainingCapacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Find by name (case-insensitive substring)
|
||||||
|
const msg =
|
||||||
|
\\{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"findElement","arguments":{"name":"click"}}}
|
||||||
|
;
|
||||||
|
try router.handleMessage(server, aa, msg);
|
||||||
|
try testing.expect(std.mem.indexOf(u8, out.written(), "Click Me") != null);
|
||||||
|
out.clearRetainingCapacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Find with no matches
|
||||||
|
const msg =
|
||||||
|
\\{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"findElement","arguments":{"role":"slider"}}}
|
||||||
|
;
|
||||||
|
try router.handleMessage(server, aa, msg);
|
||||||
|
try testing.expect(std.mem.indexOf(u8, out.written(), "[]") != null);
|
||||||
|
out.clearRetainingCapacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Error: no params provided
|
||||||
|
const msg =
|
||||||
|
\\{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"findElement","arguments":{}}}
|
||||||
|
;
|
||||||
|
try router.handleMessage(server, aa, msg);
|
||||||
|
try testing.expect(std.mem.indexOf(u8, out.written(), "error") != null);
|
||||||
|
out.clearRetainingCapacity();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test "MCP - waitForSelector: existing element" {
|
test "MCP - waitForSelector: existing element" {
|
||||||
defer testing.reset();
|
defer testing.reset();
|
||||||
var out: std.io.Writer.Allocating = .init(testing.arena_allocator);
|
var out: std.io.Writer.Allocating = .init(testing.arena_allocator);
|
||||||
|
|||||||
@@ -26,13 +26,11 @@ const lp = @import("lightpanda");
|
|||||||
const Config = @import("../Config.zig");
|
const Config = @import("../Config.zig");
|
||||||
const libcurl = @import("../sys/libcurl.zig");
|
const libcurl = @import("../sys/libcurl.zig");
|
||||||
|
|
||||||
const http = @import("http.zig");
|
const net_http = @import("http.zig");
|
||||||
const RobotStore = @import("Robots.zig").RobotStore;
|
const RobotStore = @import("Robots.zig").RobotStore;
|
||||||
const WebBotAuth = @import("WebBotAuth.zig");
|
const WebBotAuth = @import("WebBotAuth.zig");
|
||||||
const Cache = @import("cache/Cache.zig");
|
|
||||||
|
|
||||||
const App = @import("../App.zig");
|
const Runtime = @This();
|
||||||
const Network = @This();
|
|
||||||
|
|
||||||
const Listener = struct {
|
const Listener = struct {
|
||||||
socket: posix.socket_t,
|
socket: posix.socket_t,
|
||||||
@@ -47,14 +45,12 @@ const MAX_TICK_CALLBACKS = 16;
|
|||||||
|
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
|
|
||||||
app: *App,
|
|
||||||
config: *const Config,
|
config: *const Config,
|
||||||
ca_blob: ?http.Blob,
|
ca_blob: ?net_http.Blob,
|
||||||
robot_store: RobotStore,
|
robot_store: RobotStore,
|
||||||
web_bot_auth: ?WebBotAuth,
|
web_bot_auth: ?WebBotAuth,
|
||||||
cache: ?Cache,
|
|
||||||
|
|
||||||
connections: []http.Connection,
|
connections: []net_http.Connection,
|
||||||
available: std.DoublyLinkedList = .{},
|
available: std.DoublyLinkedList = .{},
|
||||||
conn_mutex: std.Thread.Mutex = .{},
|
conn_mutex: std.Thread.Mutex = .{},
|
||||||
|
|
||||||
@@ -67,8 +63,8 @@ wakeup_pipe: [2]posix.fd_t = .{ -1, -1 },
|
|||||||
shutdown: std.atomic.Value(bool) = .init(false),
|
shutdown: std.atomic.Value(bool) = .init(false),
|
||||||
|
|
||||||
// Multi is a heavy structure that can consume up to 2MB of RAM.
|
// Multi is a heavy structure that can consume up to 2MB of RAM.
|
||||||
// Currently, Network is used sparingly, and we only create it on demand.
|
// Currently, Runtime is used sparingly, and we only create it on demand.
|
||||||
// When Network becomes truly shared, it should become a regular field.
|
// When Runtime becomes truly shared, it should become a regular field.
|
||||||
multi: ?*libcurl.CurlM = null,
|
multi: ?*libcurl.CurlM = null,
|
||||||
submission_mutex: std.Thread.Mutex = .{},
|
submission_mutex: std.Thread.Mutex = .{},
|
||||||
submission_queue: std.DoublyLinkedList = .{},
|
submission_queue: std.DoublyLinkedList = .{},
|
||||||
@@ -204,7 +200,7 @@ fn globalDeinit() void {
|
|||||||
libcurl.curl_global_cleanup();
|
libcurl.curl_global_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(allocator: Allocator, app: *App, config: *const Config) !Network {
|
pub fn init(allocator: Allocator, config: *const Config) !Runtime {
|
||||||
globalInit(allocator);
|
globalInit(allocator);
|
||||||
errdefer globalDeinit();
|
errdefer globalDeinit();
|
||||||
|
|
||||||
@@ -217,18 +213,18 @@ pub fn init(allocator: Allocator, app: *App, config: *const Config) !Network {
|
|||||||
@memset(pollfds, .{ .fd = -1, .events = 0, .revents = 0 });
|
@memset(pollfds, .{ .fd = -1, .events = 0, .revents = 0 });
|
||||||
pollfds[0] = .{ .fd = pipe[0], .events = posix.POLL.IN, .revents = 0 };
|
pollfds[0] = .{ .fd = pipe[0], .events = posix.POLL.IN, .revents = 0 };
|
||||||
|
|
||||||
var ca_blob: ?http.Blob = null;
|
var ca_blob: ?net_http.Blob = null;
|
||||||
if (config.tlsVerifyHost()) {
|
if (config.tlsVerifyHost()) {
|
||||||
ca_blob = try loadCerts(allocator);
|
ca_blob = try loadCerts(allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
const count: usize = config.httpMaxConcurrent();
|
const count: usize = config.httpMaxConcurrent();
|
||||||
const connections = try allocator.alloc(http.Connection, count);
|
const connections = try allocator.alloc(net_http.Connection, count);
|
||||||
errdefer allocator.free(connections);
|
errdefer allocator.free(connections);
|
||||||
|
|
||||||
var available: std.DoublyLinkedList = .{};
|
var available: std.DoublyLinkedList = .{};
|
||||||
for (0..count) |i| {
|
for (0..count) |i| {
|
||||||
connections[i] = try http.Connection.init(ca_blob, config);
|
connections[i] = try net_http.Connection.init(ca_blob, config);
|
||||||
available.append(&connections[i].node);
|
available.append(&connections[i].node);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,11 +233,6 @@ pub fn init(allocator: Allocator, app: *App, config: *const Config) !Network {
|
|||||||
else
|
else
|
||||||
null;
|
null;
|
||||||
|
|
||||||
const cache = if (config.cacheDir()) |cache_dir_path|
|
|
||||||
Cache{ .kind = .{ .fs = try .init(cache_dir_path) } }
|
|
||||||
else
|
|
||||||
null;
|
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.config = config,
|
.config = config,
|
||||||
@@ -253,14 +244,12 @@ pub fn init(allocator: Allocator, app: *App, config: *const Config) !Network {
|
|||||||
.available = available,
|
.available = available,
|
||||||
.connections = connections,
|
.connections = connections,
|
||||||
|
|
||||||
.app = app,
|
|
||||||
.robot_store = RobotStore.init(allocator),
|
.robot_store = RobotStore.init(allocator),
|
||||||
.web_bot_auth = web_bot_auth,
|
.web_bot_auth = web_bot_auth,
|
||||||
.cache = cache,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Network) void {
|
pub fn deinit(self: *Runtime) void {
|
||||||
if (self.multi) |multi| {
|
if (self.multi) |multi| {
|
||||||
libcurl.curl_multi_cleanup(multi) catch {};
|
libcurl.curl_multi_cleanup(multi) catch {};
|
||||||
}
|
}
|
||||||
@@ -289,13 +278,11 @@ pub fn deinit(self: *Network) void {
|
|||||||
wba.deinit(self.allocator);
|
wba.deinit(self.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self.cache) |*cache| cache.deinit();
|
|
||||||
|
|
||||||
globalDeinit();
|
globalDeinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bind(
|
pub fn bind(
|
||||||
self: *Network,
|
self: *Runtime,
|
||||||
address: net.Address,
|
address: net.Address,
|
||||||
ctx: *anyopaque,
|
ctx: *anyopaque,
|
||||||
on_accept: *const fn (ctx: *anyopaque, socket: posix.socket_t) void,
|
on_accept: *const fn (ctx: *anyopaque, socket: posix.socket_t) void,
|
||||||
@@ -326,7 +313,7 @@ pub fn bind(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn onTick(self: *Network, ctx: *anyopaque, callback: *const fn (*anyopaque) void) void {
|
pub fn onTick(self: *Runtime, ctx: *anyopaque, callback: *const fn (*anyopaque) void) void {
|
||||||
self.callbacks_mutex.lock();
|
self.callbacks_mutex.lock();
|
||||||
defer self.callbacks_mutex.unlock();
|
defer self.callbacks_mutex.unlock();
|
||||||
|
|
||||||
@@ -341,7 +328,7 @@ pub fn onTick(self: *Network, ctx: *anyopaque, callback: *const fn (*anyopaque)
|
|||||||
self.wakeupPoll();
|
self.wakeupPoll();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fireTicks(self: *Network) void {
|
pub fn fireTicks(self: *Runtime) void {
|
||||||
self.callbacks_mutex.lock();
|
self.callbacks_mutex.lock();
|
||||||
defer self.callbacks_mutex.unlock();
|
defer self.callbacks_mutex.unlock();
|
||||||
|
|
||||||
@@ -350,7 +337,7 @@ pub fn fireTicks(self: *Network) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(self: *Network) void {
|
pub fn run(self: *Runtime) void {
|
||||||
var drain_buf: [64]u8 = undefined;
|
var drain_buf: [64]u8 = undefined;
|
||||||
var running_handles: c_int = 0;
|
var running_handles: c_int = 0;
|
||||||
|
|
||||||
@@ -441,18 +428,18 @@ pub fn run(self: *Network) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn submitRequest(self: *Network, conn: *http.Connection) void {
|
pub fn submitRequest(self: *Runtime, conn: *net_http.Connection) void {
|
||||||
self.submission_mutex.lock();
|
self.submission_mutex.lock();
|
||||||
self.submission_queue.append(&conn.node);
|
self.submission_queue.append(&conn.node);
|
||||||
self.submission_mutex.unlock();
|
self.submission_mutex.unlock();
|
||||||
self.wakeupPoll();
|
self.wakeupPoll();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wakeupPoll(self: *Network) void {
|
fn wakeupPoll(self: *Runtime) void {
|
||||||
_ = posix.write(self.wakeup_pipe[1], &.{1}) catch {};
|
_ = posix.write(self.wakeup_pipe[1], &.{1}) catch {};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn drainQueue(self: *Network) void {
|
fn drainQueue(self: *Runtime) void {
|
||||||
self.submission_mutex.lock();
|
self.submission_mutex.lock();
|
||||||
defer self.submission_mutex.unlock();
|
defer self.submission_mutex.unlock();
|
||||||
|
|
||||||
@@ -468,7 +455,7 @@ fn drainQueue(self: *Network) void {
|
|||||||
};
|
};
|
||||||
|
|
||||||
while (self.submission_queue.popFirst()) |node| {
|
while (self.submission_queue.popFirst()) |node| {
|
||||||
const conn: *http.Connection = @fieldParentPtr("node", node);
|
const conn: *net_http.Connection = @fieldParentPtr("node", node);
|
||||||
conn.setPrivate(conn) catch |err| {
|
conn.setPrivate(conn) catch |err| {
|
||||||
lp.log.err(.app, "curl set private", .{ .err = err });
|
lp.log.err(.app, "curl set private", .{ .err = err });
|
||||||
self.releaseConnection(conn);
|
self.releaseConnection(conn);
|
||||||
@@ -481,12 +468,12 @@ fn drainQueue(self: *Network) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stop(self: *Network) void {
|
pub fn stop(self: *Runtime) void {
|
||||||
self.shutdown.store(true, .release);
|
self.shutdown.store(true, .release);
|
||||||
self.wakeupPoll();
|
self.wakeupPoll();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn acceptConnections(self: *Network) void {
|
fn acceptConnections(self: *Runtime) void {
|
||||||
if (self.shutdown.load(.acquire)) {
|
if (self.shutdown.load(.acquire)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -516,7 +503,7 @@ fn acceptConnections(self: *Network) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn preparePollFds(self: *Network, multi: *libcurl.CurlM) void {
|
fn preparePollFds(self: *Runtime, multi: *libcurl.CurlM) void {
|
||||||
const curl_fds = self.pollfds[PSEUDO_POLLFDS..];
|
const curl_fds = self.pollfds[PSEUDO_POLLFDS..];
|
||||||
@memset(curl_fds, .{ .fd = -1, .events = 0, .revents = 0 });
|
@memset(curl_fds, .{ .fd = -1, .events = 0, .revents = 0 });
|
||||||
|
|
||||||
@@ -527,14 +514,14 @@ fn preparePollFds(self: *Network, multi: *libcurl.CurlM) void {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getCurlTimeout(self: *Network) i32 {
|
fn getCurlTimeout(self: *Runtime) i32 {
|
||||||
const multi = self.multi orelse return -1;
|
const multi = self.multi orelse return -1;
|
||||||
var timeout_ms: c_long = -1;
|
var timeout_ms: c_long = -1;
|
||||||
libcurl.curl_multi_timeout(multi, &timeout_ms) catch return -1;
|
libcurl.curl_multi_timeout(multi, &timeout_ms) catch return -1;
|
||||||
return @intCast(@min(timeout_ms, std.math.maxInt(i32)));
|
return @intCast(@min(timeout_ms, std.math.maxInt(i32)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn processCompletions(self: *Network, multi: *libcurl.CurlM) void {
|
fn processCompletions(self: *Runtime, multi: *libcurl.CurlM) void {
|
||||||
var msgs_in_queue: c_int = 0;
|
var msgs_in_queue: c_int = 0;
|
||||||
while (libcurl.curl_multi_info_read(multi, &msgs_in_queue)) |msg| {
|
while (libcurl.curl_multi_info_read(multi, &msgs_in_queue)) |msg| {
|
||||||
switch (msg.data) {
|
switch (msg.data) {
|
||||||
@@ -550,7 +537,7 @@ fn processCompletions(self: *Network, multi: *libcurl.CurlM) void {
|
|||||||
var ptr: *anyopaque = undefined;
|
var ptr: *anyopaque = undefined;
|
||||||
libcurl.curl_easy_getinfo(easy, .private, &ptr) catch
|
libcurl.curl_easy_getinfo(easy, .private, &ptr) catch
|
||||||
lp.assert(false, "curl getinfo private", .{});
|
lp.assert(false, "curl getinfo private", .{});
|
||||||
const conn: *http.Connection = @ptrCast(@alignCast(ptr));
|
const conn: *net_http.Connection = @ptrCast(@alignCast(ptr));
|
||||||
|
|
||||||
libcurl.curl_multi_remove_handle(multi, easy) catch {};
|
libcurl.curl_multi_remove_handle(multi, easy) catch {};
|
||||||
self.releaseConnection(conn);
|
self.releaseConnection(conn);
|
||||||
@@ -569,7 +556,7 @@ comptime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getConnection(self: *Network) ?*http.Connection {
|
pub fn getConnection(self: *Runtime) ?*net_http.Connection {
|
||||||
self.conn_mutex.lock();
|
self.conn_mutex.lock();
|
||||||
defer self.conn_mutex.unlock();
|
defer self.conn_mutex.unlock();
|
||||||
|
|
||||||
@@ -577,7 +564,7 @@ pub fn getConnection(self: *Network) ?*http.Connection {
|
|||||||
return @fieldParentPtr("node", node);
|
return @fieldParentPtr("node", node);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn releaseConnection(self: *Network, conn: *http.Connection) void {
|
pub fn releaseConnection(self: *Runtime, conn: *net_http.Connection) void {
|
||||||
conn.reset(self.config, self.ca_blob) catch |err| {
|
conn.reset(self.config, self.ca_blob) catch |err| {
|
||||||
lp.assert(false, "couldn't reset curl easy", .{ .err = err });
|
lp.assert(false, "couldn't reset curl easy", .{ .err = err });
|
||||||
};
|
};
|
||||||
@@ -588,8 +575,8 @@ pub fn releaseConnection(self: *Network, conn: *http.Connection) void {
|
|||||||
self.available.append(&conn.node);
|
self.available.append(&conn.node);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn newConnection(self: *Network) !http.Connection {
|
pub fn newConnection(self: *Runtime) !net_http.Connection {
|
||||||
return http.Connection.init(self.ca_blob, self.config);
|
return net_http.Connection.init(self.ca_blob, self.config);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wraps lines @ 64 columns. A PEM is basically a base64 encoded DER (which is
|
// Wraps lines @ 64 columns. A PEM is basically a base64 encoded DER (which is
|
||||||
156
src/network/cache/Cache.zig
vendored
156
src/network/cache/Cache.zig
vendored
@@ -1,156 +0,0 @@
|
|||||||
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
|
|
||||||
//
|
|
||||||
// Francis Bouvier <francis@lightpanda.io>
|
|
||||||
// Pierre Tachoire <pierre@lightpanda.io>
|
|
||||||
//
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Affero General Public License as
|
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
|
||||||
// License, or (at your option) any later version.
|
|
||||||
//
|
|
||||||
// This program is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Affero General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
const std = @import("std");
|
|
||||||
const Http = @import("../http.zig");
|
|
||||||
const FsCache = @import("FsCache.zig");
|
|
||||||
|
|
||||||
/// A browser-wide cache for resources across the network.
|
|
||||||
/// This mostly conforms to RFC9111 with regards to caching behavior.
|
|
||||||
pub const Cache = @This();
|
|
||||||
|
|
||||||
kind: union(enum) {
|
|
||||||
fs: FsCache,
|
|
||||||
},
|
|
||||||
|
|
||||||
pub fn deinit(self: *Cache) void {
|
|
||||||
return switch (self.kind) {
|
|
||||||
inline else => |*c| c.deinit(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get(self: *Cache, arena: std.mem.Allocator, req: CacheRequest) ?CachedResponse {
|
|
||||||
return switch (self.kind) {
|
|
||||||
inline else => |*c| c.get(arena, req),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn put(self: *Cache, metadata: CachedMetadata, body: []const u8) !void {
|
|
||||||
return switch (self.kind) {
|
|
||||||
inline else => |*c| c.put(metadata, body),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const CacheControl = struct {
|
|
||||||
max_age: u64,
|
|
||||||
|
|
||||||
pub fn parse(value: []const u8) ?CacheControl {
|
|
||||||
var cc: CacheControl = .{ .max_age = undefined };
|
|
||||||
|
|
||||||
var max_age_set = false;
|
|
||||||
var max_s_age_set = false;
|
|
||||||
var is_public = false;
|
|
||||||
|
|
||||||
var iter = std.mem.splitScalar(u8, value, ',');
|
|
||||||
while (iter.next()) |part| {
|
|
||||||
const directive = std.mem.trim(u8, part, &std.ascii.whitespace);
|
|
||||||
if (std.ascii.eqlIgnoreCase(directive, "no-store")) {
|
|
||||||
return null;
|
|
||||||
} else if (std.ascii.eqlIgnoreCase(directive, "no-cache")) {
|
|
||||||
return null;
|
|
||||||
} else if (std.ascii.eqlIgnoreCase(directive, "public")) {
|
|
||||||
is_public = true;
|
|
||||||
} else if (std.ascii.startsWithIgnoreCase(directive, "max-age=")) {
|
|
||||||
if (!max_s_age_set) {
|
|
||||||
if (std.fmt.parseInt(u64, directive[8..], 10) catch null) |max_age| {
|
|
||||||
cc.max_age = max_age;
|
|
||||||
max_age_set = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (std.ascii.startsWithIgnoreCase(directive, "s-maxage=")) {
|
|
||||||
if (std.fmt.parseInt(u64, directive[9..], 10) catch null) |max_age| {
|
|
||||||
cc.max_age = max_age;
|
|
||||||
max_age_set = true;
|
|
||||||
max_s_age_set = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!max_age_set) return null;
|
|
||||||
if (!is_public) return null;
|
|
||||||
if (cc.max_age == 0) return null;
|
|
||||||
|
|
||||||
return cc;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const CachedMetadata = struct {
|
|
||||||
url: [:0]const u8,
|
|
||||||
content_type: []const u8,
|
|
||||||
|
|
||||||
status: u16,
|
|
||||||
stored_at: i64,
|
|
||||||
age_at_store: u64,
|
|
||||||
|
|
||||||
cache_control: CacheControl,
|
|
||||||
/// Response Headers
|
|
||||||
headers: []const Http.Header,
|
|
||||||
|
|
||||||
/// These are Request Headers used by Vary.
|
|
||||||
vary_headers: []const Http.Header,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const CacheRequest = struct {
|
|
||||||
url: []const u8,
|
|
||||||
timestamp: i64,
|
|
||||||
request_headers: []const Http.Header,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const CachedData = union(enum) {
|
|
||||||
buffer: []const u8,
|
|
||||||
file: struct {
|
|
||||||
file: std.fs.File,
|
|
||||||
offset: usize,
|
|
||||||
len: usize,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const CachedResponse = struct {
|
|
||||||
metadata: CachedMetadata,
|
|
||||||
data: CachedData,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn tryCache(
|
|
||||||
arena: std.mem.Allocator,
|
|
||||||
timestamp: i64,
|
|
||||||
url: [:0]const u8,
|
|
||||||
status: u16,
|
|
||||||
content_type: ?[]const u8,
|
|
||||||
cache_control: ?[]const u8,
|
|
||||||
vary: ?[]const u8,
|
|
||||||
age: ?[]const u8,
|
|
||||||
has_set_cookie: bool,
|
|
||||||
has_authorization: bool,
|
|
||||||
) !?CachedMetadata {
|
|
||||||
if (status != 200) return null;
|
|
||||||
if (has_set_cookie) return null;
|
|
||||||
if (has_authorization) return null;
|
|
||||||
if (vary) |v| if (std.mem.eql(u8, v, "*")) return null;
|
|
||||||
const cc = CacheControl.parse(cache_control orelse return null) orelse return null;
|
|
||||||
|
|
||||||
return .{
|
|
||||||
.url = url,
|
|
||||||
.content_type = if (content_type) |ct| try arena.dupe(u8, ct) else "application/octet-stream",
|
|
||||||
.status = status,
|
|
||||||
.stored_at = timestamp,
|
|
||||||
.age_at_store = if (age) |a| std.fmt.parseInt(u64, a, 10) catch 0 else 0,
|
|
||||||
.cache_control = cc,
|
|
||||||
.headers = &.{},
|
|
||||||
.vary_headers = &.{},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
580
src/network/cache/FsCache.zig
vendored
580
src/network/cache/FsCache.zig
vendored
@@ -1,580 +0,0 @@
|
|||||||
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
|
|
||||||
//
|
|
||||||
// Francis Bouvier <francis@lightpanda.io>
|
|
||||||
// Pierre Tachoire <pierre@lightpanda.io>
|
|
||||||
//
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Affero General Public License as
|
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
|
||||||
// License, or (at your option) any later version.
|
|
||||||
//
|
|
||||||
// This program is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Affero General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
const std = @import("std");
|
|
||||||
const log = @import("../../log.zig");
|
|
||||||
const Cache = @import("Cache.zig");
|
|
||||||
const Http = @import("../http.zig");
|
|
||||||
const CacheRequest = Cache.CacheRequest;
|
|
||||||
const CachedMetadata = Cache.CachedMetadata;
|
|
||||||
const CachedResponse = Cache.CachedResponse;
|
|
||||||
|
|
||||||
const CACHE_VERSION: usize = 1;
|
|
||||||
const LOCK_STRIPES = 16;
|
|
||||||
comptime {
|
|
||||||
std.debug.assert(std.math.isPowerOfTwo(LOCK_STRIPES));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const FsCache = @This();
|
|
||||||
|
|
||||||
dir: std.fs.Dir,
|
|
||||||
locks: [LOCK_STRIPES]std.Thread.Mutex = .{std.Thread.Mutex{}} ** LOCK_STRIPES,
|
|
||||||
|
|
||||||
const CacheMetadataJson = struct {
|
|
||||||
version: usize,
|
|
||||||
metadata: CachedMetadata,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn getLockPtr(self: *FsCache, key: *const [HASHED_KEY_LEN]u8) *std.Thread.Mutex {
|
|
||||||
const lock_idx = std.hash.Wyhash.hash(0, key[0..]) & (LOCK_STRIPES - 1);
|
|
||||||
return &self.locks[lock_idx];
|
|
||||||
}
|
|
||||||
|
|
||||||
const BODY_LEN_HEADER_LEN = 8;
|
|
||||||
const HASHED_KEY_LEN = 64;
|
|
||||||
const HASHED_PATH_LEN = HASHED_KEY_LEN + 6;
|
|
||||||
const HASHED_TMP_PATH_LEN = HASHED_PATH_LEN + 4;
|
|
||||||
|
|
||||||
fn hashKey(key: []const u8) [HASHED_KEY_LEN]u8 {
|
|
||||||
var digest: [std.crypto.hash.sha2.Sha256.digest_length]u8 = undefined;
|
|
||||||
std.crypto.hash.sha2.Sha256.hash(key, &digest, .{});
|
|
||||||
var hex: [HASHED_KEY_LEN]u8 = undefined;
|
|
||||||
_ = std.fmt.bufPrint(&hex, "{s}", .{std.fmt.bytesToHex(&digest, .lower)}) catch unreachable;
|
|
||||||
return hex;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cachePath(hashed_key: *const [HASHED_KEY_LEN]u8) [HASHED_PATH_LEN]u8 {
|
|
||||||
var path: [HASHED_PATH_LEN]u8 = undefined;
|
|
||||||
_ = std.fmt.bufPrint(&path, "{s}.cache", .{hashed_key}) catch unreachable;
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cacheTmpPath(hashed_key: *const [HASHED_KEY_LEN]u8) [HASHED_TMP_PATH_LEN]u8 {
|
|
||||||
var path: [HASHED_TMP_PATH_LEN]u8 = undefined;
|
|
||||||
_ = std.fmt.bufPrint(&path, "{s}.cache.tmp", .{hashed_key}) catch unreachable;
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(path: []const u8) !FsCache {
|
|
||||||
const cwd = std.fs.cwd();
|
|
||||||
|
|
||||||
cwd.makeDir(path) catch |err| switch (err) {
|
|
||||||
error.PathAlreadyExists => {},
|
|
||||||
else => return err,
|
|
||||||
};
|
|
||||||
|
|
||||||
const dir = try cwd.openDir(path, .{ .iterate = true });
|
|
||||||
return .{ .dir = dir };
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *FsCache) void {
|
|
||||||
self.dir.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get(self: *FsCache, arena: std.mem.Allocator, req: CacheRequest) ?Cache.CachedResponse {
|
|
||||||
const hashed_key = hashKey(req.url);
|
|
||||||
const cache_p = cachePath(&hashed_key);
|
|
||||||
|
|
||||||
const lock = self.getLockPtr(&hashed_key);
|
|
||||||
lock.lock();
|
|
||||||
defer lock.unlock();
|
|
||||||
|
|
||||||
const file = self.dir.openFile(&cache_p, .{ .mode = .read_only }) catch |e| {
|
|
||||||
switch (e) {
|
|
||||||
std.fs.File.OpenError.FileNotFound => {
|
|
||||||
log.debug(.cache, "miss", .{ .url = req.url, .hash = &hashed_key });
|
|
||||||
},
|
|
||||||
else => |err| {
|
|
||||||
log.warn(.cache, "open file err", .{ .url = req.url, .err = err });
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
var cleanup = false;
|
|
||||||
defer if (cleanup) {
|
|
||||||
file.close();
|
|
||||||
self.dir.deleteFile(&cache_p) catch |e| {
|
|
||||||
log.err(.cache, "clean fail", .{ .url = req.url, .file = &cache_p, .err = e });
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
var file_buf: [1024]u8 = undefined;
|
|
||||||
var len_buf: [BODY_LEN_HEADER_LEN]u8 = undefined;
|
|
||||||
|
|
||||||
var file_reader = file.reader(&file_buf);
|
|
||||||
const file_reader_iface = &file_reader.interface;
|
|
||||||
|
|
||||||
file_reader_iface.readSliceAll(&len_buf) catch |e| {
|
|
||||||
log.warn(.cache, "read header", .{ .url = req.url, .err = e });
|
|
||||||
cleanup = true;
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
const body_len = std.mem.readInt(u64, &len_buf, .little);
|
|
||||||
|
|
||||||
// Now we read metadata.
|
|
||||||
file_reader.seekTo(body_len + BODY_LEN_HEADER_LEN) catch |e| {
|
|
||||||
log.warn(.cache, "seek metadata", .{ .url = req.url, .err = e });
|
|
||||||
cleanup = true;
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
var json_reader = std.json.Reader.init(arena, file_reader_iface);
|
|
||||||
const cache_file: CacheMetadataJson = std.json.parseFromTokenSourceLeaky(
|
|
||||||
CacheMetadataJson,
|
|
||||||
arena,
|
|
||||||
&json_reader,
|
|
||||||
.{ .allocate = .alloc_always },
|
|
||||||
) catch |e| {
|
|
||||||
log.warn(.cache, "metadata parse", .{ .url = req.url, .err = e });
|
|
||||||
cleanup = true;
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (cache_file.version != CACHE_VERSION) {
|
|
||||||
log.warn(.cache, "version", .{ .url = req.url, .expected = CACHE_VERSION, .got = cache_file.version });
|
|
||||||
cleanup = true;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const metadata = cache_file.metadata;
|
|
||||||
|
|
||||||
// Check entry expiration.
|
|
||||||
const now = req.timestamp;
|
|
||||||
const age = (now - metadata.stored_at) + @as(i64, @intCast(metadata.age_at_store));
|
|
||||||
if (age < 0 or @as(u64, @intCast(age)) >= metadata.cache_control.max_age) {
|
|
||||||
log.debug(.cache, "expired", .{ .url = req.url });
|
|
||||||
cleanup = true;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have Vary headers, ensure they are present & matching.
|
|
||||||
for (metadata.vary_headers) |vary_hdr| {
|
|
||||||
const name = vary_hdr.name;
|
|
||||||
const value = vary_hdr.value;
|
|
||||||
|
|
||||||
const incoming = for (req.request_headers) |h| {
|
|
||||||
if (std.ascii.eqlIgnoreCase(h.name, name)) break h.value;
|
|
||||||
} else "";
|
|
||||||
|
|
||||||
if (!std.ascii.eqlIgnoreCase(value, incoming)) {
|
|
||||||
log.debug(.cache, "vary mismatch", .{ .url = req.url, .header = name });
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// On the case of a hash collision.
|
|
||||||
if (!std.ascii.eqlIgnoreCase(metadata.url, req.url)) {
|
|
||||||
log.warn(.cache, "collision", .{ .url = req.url, .expected = metadata.url, .got = req.url });
|
|
||||||
cleanup = true;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return .{
|
|
||||||
.metadata = metadata,
|
|
||||||
.data = .{
|
|
||||||
.file = .{
|
|
||||||
.file = file,
|
|
||||||
.offset = BODY_LEN_HEADER_LEN,
|
|
||||||
.len = body_len,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn put(self: *FsCache, meta: CachedMetadata, body: []const u8) !void {
|
|
||||||
const hashed_key = hashKey(meta.url);
|
|
||||||
const cache_p = cachePath(&hashed_key);
|
|
||||||
const cache_tmp_p = cacheTmpPath(&hashed_key);
|
|
||||||
|
|
||||||
const lock = self.getLockPtr(&hashed_key);
|
|
||||||
lock.lock();
|
|
||||||
defer lock.unlock();
|
|
||||||
|
|
||||||
const file = try self.dir.createFile(&cache_tmp_p, .{ .truncate = true });
|
|
||||||
defer file.close();
|
|
||||||
|
|
||||||
var writer_buf: [1024]u8 = undefined;
|
|
||||||
|
|
||||||
var file_writer = file.writer(&writer_buf);
|
|
||||||
var file_writer_iface = &file_writer.interface;
|
|
||||||
|
|
||||||
var len_buf: [8]u8 = undefined;
|
|
||||||
std.mem.writeInt(u64, &len_buf, body.len, .little);
|
|
||||||
try file_writer_iface.writeAll(&len_buf);
|
|
||||||
try file_writer_iface.writeAll(body);
|
|
||||||
|
|
||||||
try std.json.Stringify.value(
|
|
||||||
CacheMetadataJson{ .version = CACHE_VERSION, .metadata = meta },
|
|
||||||
.{ .whitespace = .minified },
|
|
||||||
file_writer_iface,
|
|
||||||
);
|
|
||||||
|
|
||||||
try file_writer_iface.flush();
|
|
||||||
try self.dir.rename(&cache_tmp_p, &cache_p);
|
|
||||||
}
|
|
||||||
|
|
||||||
const testing = std.testing;
|
|
||||||
|
|
||||||
fn setupCache() !struct { tmp: testing.TmpDir, cache: Cache } {
|
|
||||||
var tmp = testing.tmpDir(.{});
|
|
||||||
errdefer tmp.cleanup();
|
|
||||||
|
|
||||||
const path = try tmp.dir.realpathAlloc(testing.allocator, ".");
|
|
||||||
defer testing.allocator.free(path);
|
|
||||||
|
|
||||||
return .{
|
|
||||||
.tmp = tmp,
|
|
||||||
.cache = Cache{ .kind = .{ .fs = try FsCache.init(path) } },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
test "FsCache: basic put and get" {
|
|
||||||
var setup = try setupCache();
|
|
||||||
defer {
|
|
||||||
setup.cache.deinit();
|
|
||||||
setup.tmp.cleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
const cache = &setup.cache;
|
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(testing.allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const now = std.time.timestamp();
|
|
||||||
const meta = CachedMetadata{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.content_type = "text/html",
|
|
||||||
.status = 200,
|
|
||||||
.stored_at = now,
|
|
||||||
.age_at_store = 0,
|
|
||||||
.cache_control = .{ .max_age = 600 },
|
|
||||||
.headers = &.{},
|
|
||||||
.vary_headers = &.{},
|
|
||||||
};
|
|
||||||
|
|
||||||
const body = "hello world";
|
|
||||||
try cache.put(meta, body);
|
|
||||||
|
|
||||||
const result = cache.get(
|
|
||||||
arena.allocator(),
|
|
||||||
.{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{},
|
|
||||||
},
|
|
||||||
) orelse return error.CacheMiss;
|
|
||||||
const f = result.data.file;
|
|
||||||
const file = f.file;
|
|
||||||
defer file.close();
|
|
||||||
|
|
||||||
var buf: [64]u8 = undefined;
|
|
||||||
var file_reader = file.reader(&buf);
|
|
||||||
try file_reader.seekTo(f.offset);
|
|
||||||
|
|
||||||
const read_buf = try file_reader.interface.readAlloc(testing.allocator, f.len);
|
|
||||||
defer testing.allocator.free(read_buf);
|
|
||||||
try testing.expectEqualStrings(body, read_buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
test "FsCache: get expiration" {
|
|
||||||
var setup = try setupCache();
|
|
||||||
defer {
|
|
||||||
setup.cache.deinit();
|
|
||||||
setup.tmp.cleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
const cache = &setup.cache;
|
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(testing.allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const now = 5000;
|
|
||||||
const max_age = 1000;
|
|
||||||
|
|
||||||
const meta = CachedMetadata{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.content_type = "text/html",
|
|
||||||
.status = 200,
|
|
||||||
.stored_at = now,
|
|
||||||
.age_at_store = 900,
|
|
||||||
.cache_control = .{ .max_age = max_age },
|
|
||||||
.headers = &.{},
|
|
||||||
.vary_headers = &.{},
|
|
||||||
};
|
|
||||||
|
|
||||||
const body = "hello world";
|
|
||||||
try cache.put(meta, body);
|
|
||||||
|
|
||||||
const result = cache.get(
|
|
||||||
arena.allocator(),
|
|
||||||
.{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now + 50,
|
|
||||||
.request_headers = &.{},
|
|
||||||
},
|
|
||||||
) orelse return error.CacheMiss;
|
|
||||||
result.data.file.file.close();
|
|
||||||
|
|
||||||
try testing.expectEqual(null, cache.get(
|
|
||||||
arena.allocator(),
|
|
||||||
.{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now + 200,
|
|
||||||
.request_headers = &.{},
|
|
||||||
},
|
|
||||||
));
|
|
||||||
|
|
||||||
try testing.expectEqual(null, cache.get(
|
|
||||||
arena.allocator(),
|
|
||||||
.{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{},
|
|
||||||
},
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
test "FsCache: put override" {
|
|
||||||
var setup = try setupCache();
|
|
||||||
defer {
|
|
||||||
setup.cache.deinit();
|
|
||||||
setup.tmp.cleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
const cache = &setup.cache;
|
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(testing.allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
{
|
|
||||||
const now = 5000;
|
|
||||||
const max_age = 1000;
|
|
||||||
|
|
||||||
const meta = CachedMetadata{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.content_type = "text/html",
|
|
||||||
.status = 200,
|
|
||||||
.stored_at = now,
|
|
||||||
.age_at_store = 900,
|
|
||||||
.cache_control = .{ .max_age = max_age },
|
|
||||||
.headers = &.{},
|
|
||||||
.vary_headers = &.{},
|
|
||||||
};
|
|
||||||
|
|
||||||
const body = "hello world";
|
|
||||||
try cache.put(meta, body);
|
|
||||||
|
|
||||||
const result = cache.get(
|
|
||||||
arena.allocator(),
|
|
||||||
.{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{},
|
|
||||||
},
|
|
||||||
) orelse return error.CacheMiss;
|
|
||||||
const f = result.data.file;
|
|
||||||
const file = f.file;
|
|
||||||
defer file.close();
|
|
||||||
|
|
||||||
var buf: [64]u8 = undefined;
|
|
||||||
var file_reader = file.reader(&buf);
|
|
||||||
try file_reader.seekTo(f.offset);
|
|
||||||
|
|
||||||
const read_buf = try file_reader.interface.readAlloc(testing.allocator, f.len);
|
|
||||||
defer testing.allocator.free(read_buf);
|
|
||||||
|
|
||||||
try testing.expectEqualStrings(body, read_buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
const now = 10000;
|
|
||||||
const max_age = 2000;
|
|
||||||
|
|
||||||
const meta = CachedMetadata{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.content_type = "text/html",
|
|
||||||
.status = 200,
|
|
||||||
.stored_at = now,
|
|
||||||
.age_at_store = 0,
|
|
||||||
.cache_control = .{ .max_age = max_age },
|
|
||||||
.headers = &.{},
|
|
||||||
.vary_headers = &.{},
|
|
||||||
};
|
|
||||||
|
|
||||||
const body = "goodbye world";
|
|
||||||
try cache.put(meta, body);
|
|
||||||
|
|
||||||
const result = cache.get(
|
|
||||||
arena.allocator(),
|
|
||||||
.{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{},
|
|
||||||
},
|
|
||||||
) orelse return error.CacheMiss;
|
|
||||||
const f = result.data.file;
|
|
||||||
const file = f.file;
|
|
||||||
defer file.close();
|
|
||||||
|
|
||||||
var buf: [64]u8 = undefined;
|
|
||||||
var file_reader = file.reader(&buf);
|
|
||||||
try file_reader.seekTo(f.offset);
|
|
||||||
|
|
||||||
const read_buf = try file_reader.interface.readAlloc(testing.allocator, f.len);
|
|
||||||
defer testing.allocator.free(read_buf);
|
|
||||||
|
|
||||||
try testing.expectEqualStrings(body, read_buf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
test "FsCache: garbage file" {
|
|
||||||
var setup = try setupCache();
|
|
||||||
defer {
|
|
||||||
setup.cache.deinit();
|
|
||||||
setup.tmp.cleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
const hashed_key = hashKey("https://example.com");
|
|
||||||
const cache_p = cachePath(&hashed_key);
|
|
||||||
const file = try setup.cache.kind.fs.dir.createFile(&cache_p, .{});
|
|
||||||
try file.writeAll("this is not a valid cache file !@#$%");
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(testing.allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
try testing.expectEqual(
|
|
||||||
null,
|
|
||||||
setup.cache.get(arena.allocator(), .{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = 5000,
|
|
||||||
.request_headers = &.{},
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
test "FsCache: vary hit and miss" {
|
|
||||||
var setup = try setupCache();
|
|
||||||
defer {
|
|
||||||
setup.cache.deinit();
|
|
||||||
setup.tmp.cleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
const cache = &setup.cache;
|
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(testing.allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const now = std.time.timestamp();
|
|
||||||
const meta = CachedMetadata{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.content_type = "text/html",
|
|
||||||
.status = 200,
|
|
||||||
.stored_at = now,
|
|
||||||
.age_at_store = 0,
|
|
||||||
.cache_control = .{ .max_age = 600 },
|
|
||||||
.headers = &.{},
|
|
||||||
.vary_headers = &.{
|
|
||||||
.{ .name = "Accept-Encoding", .value = "gzip" },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
try cache.put(meta, "hello world");
|
|
||||||
|
|
||||||
const result = cache.get(arena.allocator(), .{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{
|
|
||||||
.{ .name = "Accept-Encoding", .value = "gzip" },
|
|
||||||
},
|
|
||||||
}) orelse return error.CacheMiss;
|
|
||||||
result.data.file.file.close();
|
|
||||||
|
|
||||||
try testing.expectEqual(null, cache.get(arena.allocator(), .{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{
|
|
||||||
.{ .name = "Accept-Encoding", .value = "br" },
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
try testing.expectEqual(null, cache.get(arena.allocator(), .{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const result2 = cache.get(arena.allocator(), .{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{
|
|
||||||
.{ .name = "Accept-Encoding", .value = "gzip" },
|
|
||||||
},
|
|
||||||
}) orelse return error.CacheMiss;
|
|
||||||
result2.data.file.file.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
test "FsCache: vary multiple headers" {
|
|
||||||
var setup = try setupCache();
|
|
||||||
defer {
|
|
||||||
setup.cache.deinit();
|
|
||||||
setup.tmp.cleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
const cache = &setup.cache;
|
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(testing.allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const now = std.time.timestamp();
|
|
||||||
const meta = CachedMetadata{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.content_type = "text/html",
|
|
||||||
.status = 200,
|
|
||||||
.stored_at = now,
|
|
||||||
.age_at_store = 0,
|
|
||||||
.cache_control = .{ .max_age = 600 },
|
|
||||||
.headers = &.{},
|
|
||||||
.vary_headers = &.{
|
|
||||||
.{ .name = "Accept-Encoding", .value = "gzip" },
|
|
||||||
.{ .name = "Accept-Language", .value = "en" },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
try cache.put(meta, "hello world");
|
|
||||||
|
|
||||||
const result = cache.get(arena.allocator(), .{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{
|
|
||||||
.{ .name = "Accept-Encoding", .value = "gzip" },
|
|
||||||
.{ .name = "Accept-Language", .value = "en" },
|
|
||||||
},
|
|
||||||
}) orelse return error.CacheMiss;
|
|
||||||
result.data.file.file.close();
|
|
||||||
|
|
||||||
try testing.expectEqual(null, cache.get(arena.allocator(), .{
|
|
||||||
.url = "https://example.com",
|
|
||||||
.timestamp = now,
|
|
||||||
.request_headers = &.{
|
|
||||||
.{ .name = "Accept-Encoding", .value = "gzip" },
|
|
||||||
.{ .name = "Accept-Language", .value = "fr" },
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
@@ -79,7 +79,7 @@ pub const Headers = struct {
|
|||||||
self.headers = updated_headers;
|
self.headers = updated_headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parseHeader(header_str: []const u8) ?Header {
|
fn parseHeader(header_str: []const u8) ?Header {
|
||||||
const colon_pos = std.mem.indexOfScalar(u8, header_str, ':') orelse return null;
|
const colon_pos = std.mem.indexOfScalar(u8, header_str, ':') orelse return null;
|
||||||
|
|
||||||
const name = std.mem.trim(u8, header_str[0..colon_pos], " \t");
|
const name = std.mem.trim(u8, header_str[0..colon_pos], " \t");
|
||||||
@@ -88,9 +88,22 @@ pub const Headers = struct {
|
|||||||
return .{ .name = name, .value = value };
|
return .{ .name = name, .value = value };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iterator(self: Headers) HeaderIterator {
|
pub fn iterator(self: *Headers) Iterator {
|
||||||
return .{ .curl_slist = .{ .header = self.headers } };
|
return .{
|
||||||
|
.header = self.headers,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Iterator = struct {
|
||||||
|
header: [*c]libcurl.CurlSList,
|
||||||
|
|
||||||
|
pub fn next(self: *Iterator) ?Header {
|
||||||
|
const h = self.header orelse return null;
|
||||||
|
|
||||||
|
self.header = h.*.next;
|
||||||
|
return parseHeader(std.mem.span(@as([*:0]const u8, @ptrCast(h.*.data))));
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// In normal cases, the header iterator comes from the curl linked list.
|
// In normal cases, the header iterator comes from the curl linked list.
|
||||||
@@ -99,7 +112,6 @@ pub const Headers = struct {
|
|||||||
// This union, is an iterator that exposes the same API for either case.
|
// This union, is an iterator that exposes the same API for either case.
|
||||||
pub const HeaderIterator = union(enum) {
|
pub const HeaderIterator = union(enum) {
|
||||||
curl: CurlHeaderIterator,
|
curl: CurlHeaderIterator,
|
||||||
curl_slist: CurlSListIterator,
|
|
||||||
list: ListHeaderIterator,
|
list: ListHeaderIterator,
|
||||||
|
|
||||||
pub fn next(self: *HeaderIterator) ?Header {
|
pub fn next(self: *HeaderIterator) ?Header {
|
||||||
@@ -108,19 +120,6 @@ pub const HeaderIterator = union(enum) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn collect(self: *HeaderIterator, allocator: std.mem.Allocator) !std.ArrayList(Header) {
|
|
||||||
var list: std.ArrayList(Header) = .empty;
|
|
||||||
|
|
||||||
while (self.next()) |hdr| {
|
|
||||||
try list.append(allocator, .{
|
|
||||||
.name = try allocator.dupe(u8, hdr.name),
|
|
||||||
.value = try allocator.dupe(u8, hdr.value),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
const CurlHeaderIterator = struct {
|
const CurlHeaderIterator = struct {
|
||||||
conn: *const Connection,
|
conn: *const Connection,
|
||||||
prev: ?*libcurl.CurlHeader = null,
|
prev: ?*libcurl.CurlHeader = null,
|
||||||
@@ -137,16 +136,6 @@ pub const HeaderIterator = union(enum) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const CurlSListIterator = struct {
|
|
||||||
header: [*c]libcurl.CurlSList,
|
|
||||||
|
|
||||||
pub fn next(self: *CurlSListIterator) ?Header {
|
|
||||||
const h = self.header orelse return null;
|
|
||||||
self.header = h.*.next;
|
|
||||||
return Headers.parseHeader(std.mem.span(@as([*:0]const u8, @ptrCast(h.*.data))));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const ListHeaderIterator = struct {
|
const ListHeaderIterator = struct {
|
||||||
index: usize = 0,
|
index: usize = 0,
|
||||||
list: []const Header,
|
list: []const Header,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ const log = @import("../log.zig");
|
|||||||
const App = @import("../App.zig");
|
const App = @import("../App.zig");
|
||||||
const Config = @import("../Config.zig");
|
const Config = @import("../Config.zig");
|
||||||
const telemetry = @import("telemetry.zig");
|
const telemetry = @import("telemetry.zig");
|
||||||
const Network = @import("../network/Network.zig");
|
const Runtime = @import("../network/Runtime.zig");
|
||||||
|
|
||||||
const URL = "https://telemetry.lightpanda.io";
|
const URL = "https://telemetry.lightpanda.io";
|
||||||
const BUFFER_SIZE = 1024;
|
const BUFFER_SIZE = 1024;
|
||||||
@@ -17,7 +17,7 @@ const MAX_BODY_SIZE = 500 * 1024; // 500KB server limit
|
|||||||
const LightPanda = @This();
|
const LightPanda = @This();
|
||||||
|
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
network: *Network,
|
runtime: *Runtime,
|
||||||
writer: std.Io.Writer.Allocating,
|
writer: std.Io.Writer.Allocating,
|
||||||
|
|
||||||
/// Protects concurrent producers in send().
|
/// Protects concurrent producers in send().
|
||||||
@@ -36,11 +36,11 @@ pub fn init(self: *LightPanda, app: *App, iid: ?[36]u8, run_mode: Config.RunMode
|
|||||||
.iid = iid,
|
.iid = iid,
|
||||||
.run_mode = run_mode,
|
.run_mode = run_mode,
|
||||||
.allocator = app.allocator,
|
.allocator = app.allocator,
|
||||||
.network = &app.network,
|
.runtime = &app.network,
|
||||||
.writer = std.Io.Writer.Allocating.init(app.allocator),
|
.writer = std.Io.Writer.Allocating.init(app.allocator),
|
||||||
};
|
};
|
||||||
|
|
||||||
self.network.onTick(@ptrCast(self), flushCallback);
|
self.runtime.onTick(@ptrCast(self), flushCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *LightPanda) void {
|
pub fn deinit(self: *LightPanda) void {
|
||||||
@@ -70,17 +70,17 @@ fn flushCallback(ctx: *anyopaque) void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn postEvent(self: *LightPanda) !void {
|
fn postEvent(self: *LightPanda) !void {
|
||||||
const conn = self.network.getConnection() orelse {
|
const conn = self.runtime.getConnection() orelse {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
errdefer self.network.releaseConnection(conn);
|
errdefer self.runtime.releaseConnection(conn);
|
||||||
|
|
||||||
const h = self.head.load(.monotonic);
|
const h = self.head.load(.monotonic);
|
||||||
const t = self.tail.load(.acquire);
|
const t = self.tail.load(.acquire);
|
||||||
const dropped = self.dropped.swap(0, .monotonic);
|
const dropped = self.dropped.swap(0, .monotonic);
|
||||||
|
|
||||||
if (h == t and dropped == 0) {
|
if (h == t and dropped == 0) {
|
||||||
self.network.releaseConnection(conn);
|
self.runtime.releaseConnection(conn);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
errdefer _ = self.dropped.fetchAdd(dropped, .monotonic);
|
errdefer _ = self.dropped.fetchAdd(dropped, .monotonic);
|
||||||
@@ -104,7 +104,7 @@ fn postEvent(self: *LightPanda) !void {
|
|||||||
try conn.setBody(self.writer.written());
|
try conn.setBody(self.writer.written());
|
||||||
|
|
||||||
self.head.store(h + sent, .release);
|
self.head.store(h + sent, .release);
|
||||||
self.network.submitRequest(conn);
|
self.runtime.submitRequest(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn writeEvent(self: *LightPanda, event: telemetry.Event) !bool {
|
fn writeEvent(self: *LightPanda, event: telemetry.Event) !bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user