Compare commits

..

20 Commits

Author SHA1 Message Date
Pierre Tachoire
6bb8bc8391 ci: use proxy for integration tests 2026-04-03 09:24:17 +02:00
Karl Seguin
fec02850d4 Merge pull request #2068 from lightpanda-io/refactor/markdown-anchor-rendering
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
markdown: simplify and optimize anchor rendering
2026-04-02 17:06:26 +08:00
Adrià Arrufat
71ac2e8c7f markdown: simplify and optimize anchor rendering 2026-04-02 09:11:26 +02:00
Karl Seguin
38fa9602fa Merge pull request #2067 from lightpanda-io/percent-encode-version
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
percent encode version query string for crash report
2026-04-02 07:50:31 +08:00
Pierre Tachoire
9661204c8d percent encode version query string for crash report 2026-04-01 22:15:56 +02:00
Karl Seguin
6800e53b0e Merge pull request #2014 from lightpanda-io/build-check
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
build: add check step to verify compilation
2026-04-01 21:05:04 +08:00
Nikolay Govorov
e79da3a4ad Merge pull request #2064 from lightpanda-io/network_naming
Improve network naming consistency
2026-04-01 13:10:12 +01:00
Karl Seguin
145792c4f5 Merge pull request #2061 from lightpanda-io/ariaAtomic
Add Element.ariaAtomic and Element.ariaLive properties
2026-04-01 20:06:15 +08:00
Karl Seguin
0bb3e3827d Merge pull request #2060 from lightpanda-io/HTMLAnchorElement.rel
Add HTMLAnchorElement.rel property
2026-04-01 20:04:43 +08:00
Karl Seguin
6e6e6e6fad Merge pull request #2057 from lightpanda-io/element-title
Add HTMLElement.title property
2026-04-01 19:36:12 +08:00
Karl Seguin
9d13a7ccdb Merge pull request #2065 from lightpanda-io/browser/resolve-scheme-in-path
Browser/resolve scheme in path
2026-04-01 19:30:40 +08:00
Karl Seguin
7fcaa500d8 Fix typo in variable name
protect against overflow if path stats with ':'

Minor tweaks to https://github.com/lightpanda-io/browser/pull/2046
2026-04-01 19:20:55 +08:00
Karl Seguin
0604056f76 Improve network naming consistency
1.
Runtime.zig -> Network.zig (especially since most places imported it as
`const Network = @import("Runtime.zig")`

2.
const net_http = @import(...) -> const http = @import(...)
2026-04-01 18:46:03 +08:00
Pierre Tachoire
5965d37c79 Add HTMLAnchorElement.rel property
Reflects the `rel` HTML attribute. The `relList` DOMTokenList was
already implemented but the string `rel` accessor was missing.
2026-04-01 11:15:10 +02:00
Pierre Tachoire
e430051fff Add Element.ariaAtomic and Element.ariaLive properties
ARIAMixin attribute reflection on Element, per the ARIA spec.
2026-04-01 11:13:52 +02:00
Pierre Tachoire
e412dfed2f Add HTMLElement.title property
Reflects the `title` HTML attribute as a getter/setter on HTMLElement.
2026-04-01 09:15:34 +02:00
dinisimys2018
2d87f5bf47 fix(browser-url): handle specific file scheme and change error InvalidURL to TypeError 2026-03-31 18:42:03 +03:00
dinisimys2018
0a222ff397 fix(browser-url): add more combinations base+path handle 2026-03-31 16:54:06 +03:00
dinisimys2018
9a0cefad26 fix(browser-url): url resolve scheme in path 2026-03-30 18:58:19 +03:00
Adrià Arrufat
3aeba97fc9 build: add check step to verify compilation 2026-03-27 14:25:17 +09:00
21 changed files with 422 additions and 191 deletions

View File

@@ -62,7 +62,7 @@ jobs:
- name: run end to end integration tests - name: run end to end integration tests
continue-on-error: true continue-on-error: true
run: | run: |
./lightpanda serve --log-level error & echo $! > LPD.pid ./lightpanda serve --http-proxy ${{ secrets.MASSIVE_PROXY_RESIDENTIAL_US }} --log-level error & echo $! > LPD.pid
go run integration/main.go |tee result.log go run integration/main.go |tee result.log
kill `cat LPD.pid` kill `cat LPD.pid`

View File

@@ -46,8 +46,12 @@ pub fn build(b: *Build) !void {
var stdout = std.fs.File.stdout().writer(&.{}); var stdout = std.fs.File.stdout().writer(&.{});
try stdout.interface.print("Lightpanda {f}\n", .{version}); try stdout.interface.print("Lightpanda {f}\n", .{version});
const version_string = b.fmt("{f}", .{version});
const version_encoded = std.mem.replaceOwned(u8, b.allocator, version_string, "+", "%2B") catch @panic("OOM");
var opts = b.addOptions(); var opts = b.addOptions();
opts.addOption([]const u8, "version", b.fmt("{f}", .{version})); opts.addOption([]const u8, "version", version_string);
opts.addOption([]const u8, "version_encoded", version_encoded);
opts.addOption(?[]const u8, "snapshot_path", snapshot_path); opts.addOption(?[]const u8, "snapshot_path", snapshot_path);
const enable_tsan = b.option(bool, "tsan", "Enable Thread Sanitizer") orelse false; const enable_tsan = b.option(bool, "tsan", "Enable Thread Sanitizer") orelse false;
@@ -85,6 +89,15 @@ pub fn build(b: *Build) !void {
break :blk mod; break :blk mod;
}; };
// Check compilation
const check = b.step("check", "Check if lightpanda compiles");
const check_lib = b.addLibrary(.{
.name = "lightpanda_check",
.root_module = lightpanda_module,
});
check.dependOn(&check_lib.step);
{ {
// browser // browser
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
@@ -103,6 +116,12 @@ pub fn build(b: *Build) !void {
}); });
b.installArtifact(exe); b.installArtifact(exe);
const exe_check = b.addLibrary(.{
.name = "lightpanda_exe_check",
.root_module = exe.root_module,
});
check.dependOn(&exe_check.step);
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| { if (b.args) |args| {
run_cmd.addArgs(args); run_cmd.addArgs(args);
@@ -132,6 +151,12 @@ pub fn build(b: *Build) !void {
}); });
b.installArtifact(exe); b.installArtifact(exe);
const exe_check = b.addLibrary(.{
.name = "snapshot_creator_check",
.root_module = exe.root_module,
});
check.dependOn(&exe_check.step);
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| { if (b.args) |args| {
run_cmd.addArgs(args); run_cmd.addArgs(args);
@@ -170,6 +195,12 @@ pub fn build(b: *Build) !void {
}); });
b.installArtifact(exe); b.installArtifact(exe);
const exe_check = b.addLibrary(.{
.name = "legacy_test_check",
.root_module = exe.root_module,
});
check.dependOn(&exe_check.step);
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| { if (b.args) |args| {
run_cmd.addArgs(args); run_cmd.addArgs(args);

View File

@@ -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/Runtime.zig"); const Network = @import("network/Network.zig");
pub const ArenaPool = @import("ArenaPool.zig"); pub const ArenaPool = @import("ArenaPool.zig");
const App = @This(); const App = @This();

View File

@@ -34,7 +34,6 @@ 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

View File

@@ -30,7 +30,7 @@ const Notification = @import("../Notification.zig");
const CookieJar = @import("webapi/storage/Cookie.zig").Jar; const CookieJar = @import("webapi/storage/Cookie.zig").Jar;
const http = @import("../network/http.zig"); const http = @import("../network/http.zig");
const Runtime = @import("../network/Runtime.zig"); const Network = @import("../network/Network.zig");
const Robots = @import("../network/Robots.zig"); const Robots = @import("../network/Robots.zig");
const IS_DEBUG = builtin.mode == .Debug; const IS_DEBUG = builtin.mode == .Debug;
@@ -86,7 +86,7 @@ queue: std.DoublyLinkedList = .{},
// The main app allocator // The main app allocator
allocator: Allocator, allocator: Allocator,
network: *Runtime, network: *Network,
// Queue of requests that depend on a robots.txt. // Queue of requests that depend on a robots.txt.
// Allows us to fetch the robots.txt just once. // Allows us to fetch the robots.txt just once.
@@ -131,7 +131,7 @@ pub const CDPClient = struct {
blocking_read_end: *const fn (*anyopaque) bool, blocking_read_end: *const fn (*anyopaque) bool,
}; };
pub fn init(allocator: Allocator, network: *Runtime) !*Client { pub fn init(allocator: Allocator, network: *Network) !*Client {
var transfer_pool = std.heap.MemoryPool(Transfer).init(allocator); var transfer_pool = std.heap.MemoryPool(Transfer).init(allocator);
errdefer transfer_pool.deinit(); errdefer transfer_pool.deinit();
@@ -695,7 +695,7 @@ fn perform(self: *Client, timeout_ms: c_int) anyerror!PerformStatus {
break :blk try self.handles.perform(); break :blk try self.handles.perform();
}; };
// Process dirty connections — return them to Runtime pool. // Process dirty connections — return them to Network pool.
while (self.dirty.popFirst()) |node| { while (self.dirty.popFirst()) |node| {
const conn: *http.Connection = @fieldParentPtr("node", node); const conn: *http.Connection = @fieldParentPtr("node", node);
self.handles.remove(conn) catch |err| { self.handles.remove(conn) catch |err| {
@@ -928,7 +928,6 @@ pub const Request = struct {
credentials: ?[:0]const u8 = null, credentials: ?[:0]const u8 = null,
notification: *Notification, notification: *Notification,
max_response_size: ?usize = null, max_response_size: ?usize = null,
timeout_ms: u32 = 0,
// This is only relevant for intercepted requests. If a request is flagged // This is only relevant for intercepted requests. If a request is flagged
// as blocking AND is intercepted, then it'll be up to us to wait until // as blocking AND is intercepted, then it'll be up to us to wait until
@@ -1143,11 +1142,6 @@ pub const Transfer = struct {
try conn.setPrivate(self); try conn.setPrivate(self);
// Per-request timeout override (e.g. XHR timeout)
if (req.timeout_ms > 0) {
try conn.setTimeout(req.timeout_ms);
}
// add credentials // add credentials
if (req.credentials) |creds| { if (req.credentials) |creds| {
if (self._auth_challenge != null and self._auth_challenge.?.source == .proxy) { if (self._auth_challenge != null and self._auth_challenge.?.source == .proxy) {

View File

@@ -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 net_http = @import("../network/http.zig"); const 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) !net_http.Headers { fn getHeaders(self: *ScriptManager) !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;

View File

@@ -25,28 +25,72 @@ 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, path: anytype, comptime opts: ResolveOpts) ![:0]const u8 { pub fn resolve(allocator: Allocator, base: [:0]const u8, source_path: anytype, comptime opts: ResolveOpts) ![:0]const u8 {
const PT = @TypeOf(path); const PT = @TypeOf(source_path);
if (base.len == 0 or isCompleteHTTPUrl(path)) {
if (comptime opts.always_dupe or !isNullTerminated(PT)) { var path: [:0]const u8 = if (comptime !isNullTerminated(PT) or opts.always_dupe) try allocator.dupeZ(u8, source_path) else source_path;
const duped = try allocator.dupeZ(u8, path);
return processResolved(allocator, duped, opts); if (base.len == 0) {
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 (comptime opts.always_dupe) { if (opts.always_dupe) {
const duped = try allocator.dupeZ(u8, base); const dupe = try allocator.dupeZ(u8, base);
return processResolved(allocator, duped, opts); return processResolved(allocator, dupe, opts);
} }
if (comptime opts.encode) { return processResolved(allocator, base, opts);
return processResolved(allocator, base, opts);
}
return base;
} }
if (path[0] == '?') { if (path[0] == '?') {
@@ -63,14 +107,7 @@ pub fn resolve(allocator: Allocator, base: [:0]const u8, path: anytype, comptime
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 {
if (comptime isNullTerminated(PT)) { return processResolved(allocator, path, opts);
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 });
@@ -96,6 +133,7 @@ pub fn resolve(allocator: Allocator, base: [:0]const u8, path: anytype, comptime
// 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;
@@ -1570,3 +1608,182 @@ 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);
}
}
}

View File

@@ -25,9 +25,7 @@ const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig"); const Node = @import("webapi/Node.zig");
const isAllWhitespace = @import("../string.zig").isAllWhitespace; const isAllWhitespace = @import("../string.zig").isAllWhitespace;
pub const Opts = struct { pub const Opts = struct {};
// Options for future customization (e.g., dialect)
};
const State = struct { const State = struct {
const ListType = enum { ordered, unordered }; const ListType = enum { ordered, unordered };
@@ -39,7 +37,6 @@ const State = struct {
list_depth: usize = 0, list_depth: usize = 0,
list_stack: [32]ListState = undefined, list_stack: [32]ListState = undefined,
pre_node: ?*Node = null, pre_node: ?*Node = null,
in_code: bool = false,
in_table: bool = false, in_table: bool = false,
table_row_index: usize = 0, table_row_index: usize = 0,
table_col_count: usize = 0, table_col_count: usize = 0,
@@ -100,27 +97,35 @@ fn getAnchorLabel(el: *Element) ?[]const u8 {
return el.getAttributeSafe(comptime .wrap("aria-label")) orelse el.getAttributeSafe(comptime .wrap("title")); return el.getAttributeSafe(comptime .wrap("aria-label")) orelse el.getAttributeSafe(comptime .wrap("title"));
} }
fn hasBlockDescendant(root: *Node) bool { const ContentInfo = struct {
var tw = TreeWalker.FullExcludeSelf.Elements.init(root, .{}); has_visible: bool,
while (tw.next()) |el| { has_block: bool,
if (el.getTag().isBlock()) return true; };
}
return false;
}
fn hasVisibleContent(root: *Node) bool { fn analyzeContent(root: *Node) ContentInfo {
var result: ContentInfo = .{ .has_visible = false, .has_block = false };
var tw = TreeWalker.FullExcludeSelf.init(root, .{}); var tw = TreeWalker.FullExcludeSelf.init(root, .{});
while (tw.next()) |node| { while (tw.next()) |node| {
if (isSignificantText(node)) return true; if (isSignificantText(node)) {
if (node.is(Element)) |el| { result.has_visible = true;
if (result.has_block) return result;
} else if (node.is(Element)) |el| {
if (!isVisibleElement(el)) { if (!isVisibleElement(el)) {
tw.skipChildren(); tw.skipChildren();
} else if (el.getTag() == .img) { } else {
return true; const tag = el.getTag();
if (tag == .img) {
result.has_visible = true;
if (result.has_block) return result;
}
if (tag.isBlock()) {
result.has_block = true;
if (result.has_visible) return result;
}
} }
} }
} }
return false; return result;
} }
const Context = struct { const Context = struct {
@@ -170,9 +175,7 @@ const Context = struct {
if (!isVisibleElement(el)) return; if (!isVisibleElement(el)) return;
// --- Opening Tag Logic --- // Ensure block elements start on a new line
// Ensure block elements start on a new line (double newline for paragraphs etc)
if (tag.isBlock() and !self.state.in_table) { if (tag.isBlock() and !self.state.in_table) {
try self.ensureNewline(); try self.ensureNewline();
if (shouldAddSpacing(tag)) { if (shouldAddSpacing(tag)) {
@@ -182,7 +185,6 @@ const Context = struct {
try self.ensureNewline(); try self.ensureNewline();
} }
// Prefixes
switch (tag) { switch (tag) {
.h1 => try self.writer.writeAll("# "), .h1 => try self.writer.writeAll("# "),
.h2 => try self.writer.writeAll("## "), .h2 => try self.writer.writeAll("## "),
@@ -225,7 +227,6 @@ const Context = struct {
try self.writer.writeByte('|'); try self.writer.writeByte('|');
}, },
.td, .th => { .td, .th => {
// Note: leading pipe handled by previous cell closing or tr opening
self.state.last_char_was_newline = false; self.state.last_char_was_newline = false;
try self.writer.writeByte(' '); try self.writer.writeByte(' ');
}, },
@@ -241,7 +242,6 @@ const Context = struct {
.code => { .code => {
if (self.state.pre_node == null) { if (self.state.pre_node == null) {
try self.writer.writeByte('`'); try self.writer.writeByte('`');
self.state.in_code = true;
self.state.last_char_was_newline = false; self.state.last_char_was_newline = false;
} }
}, },
@@ -286,16 +286,15 @@ const Context = struct {
return; return;
}, },
.anchor => { .anchor => {
const has_content = hasVisibleContent(el.asNode()); const info = analyzeContent(el.asNode());
const label = getAnchorLabel(el); const label = getAnchorLabel(el);
const href_raw = el.getAttributeSafe(comptime .wrap("href")); const href_raw = el.getAttributeSafe(comptime .wrap("href"));
if (!has_content and label == null and href_raw == null) return; if (!info.has_visible and label == null and href_raw == null) return;
const has_block = hasBlockDescendant(el.asNode());
const href = if (href_raw) |h| URL.resolve(self.page.call_arena, self.page.base(), h, .{ .encode = true }) catch h else null; const href = if (href_raw) |h| URL.resolve(self.page.call_arena, self.page.base(), h, .{ .encode = true }) catch h else null;
if (has_block) { if (info.has_block) {
try self.renderChildren(el.asNode()); try self.renderChildren(el.asNode());
if (href) |h| { if (href) |h| {
if (!self.state.last_char_was_newline) try self.writer.writeByte('\n'); if (!self.state.last_char_was_newline) try self.writer.writeByte('\n');
@@ -307,25 +306,12 @@ const Context = struct {
return; return;
} }
if (isStandaloneAnchor(el)) { const standalone = isStandaloneAnchor(el);
if (standalone) {
if (!self.state.last_char_was_newline) try self.writer.writeByte('\n'); if (!self.state.last_char_was_newline) try self.writer.writeByte('\n');
try self.writer.writeByte('[');
if (has_content) {
try self.renderChildren(el.asNode());
} else {
try self.writer.writeAll(label orelse "");
}
try self.writer.writeAll("](");
if (href) |h| {
try self.writer.writeAll(h);
}
try self.writer.writeAll(")\n");
self.state.last_char_was_newline = true;
return;
} }
try self.writer.writeByte('['); try self.writer.writeByte('[');
if (has_content) { if (info.has_visible) {
try self.renderChildren(el.asNode()); try self.renderChildren(el.asNode());
} else { } else {
try self.writer.writeAll(label orelse ""); try self.writer.writeAll(label orelse "");
@@ -335,7 +321,12 @@ const Context = struct {
try self.writer.writeAll(h); try self.writer.writeAll(h);
} }
try self.writer.writeByte(')'); try self.writer.writeByte(')');
self.state.last_char_was_newline = false; if (standalone) {
try self.writer.writeByte('\n');
self.state.last_char_was_newline = true;
} else {
self.state.last_char_was_newline = false;
}
return; return;
}, },
.input => { .input => {
@@ -350,12 +341,8 @@ const Context = struct {
else => {}, else => {},
} }
// --- Render Children ---
try self.renderChildren(el.asNode()); try self.renderChildren(el.asNode());
// --- Closing Tag Logic ---
// Suffixes
switch (tag) { switch (tag) {
.pre => { .pre => {
if (!self.state.last_char_was_newline) { if (!self.state.last_char_was_newline) {
@@ -368,7 +355,6 @@ const Context = struct {
.code => { .code => {
if (self.state.pre_node == null) { if (self.state.pre_node == null) {
try self.writer.writeByte('`'); try self.writer.writeByte('`');
self.state.in_code = false;
self.state.last_char_was_newline = false; self.state.last_char_was_newline = false;
} }
}, },
@@ -411,7 +397,6 @@ const Context = struct {
else => {}, else => {},
} }
// Post-block newlines
if (tag.isBlock() and !self.state.in_table) { if (tag.isBlock() and !self.state.in_table) {
try self.ensureNewline(); try self.ensureNewline();
} }
@@ -454,15 +439,19 @@ const Context = struct {
} }
fn escape(self: *Context, text: []const u8) !void { fn escape(self: *Context, text: []const u8) !void {
for (text) |c| { var start: usize = 0;
for (text, 0..) |c, i| {
switch (c) { switch (c) {
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '#', '+', '-', '!', '|' => { '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '#', '+', '-', '!', '|' => {
if (i > start) try self.writer.writeAll(text[start..i]);
try self.writer.writeByte('\\'); try self.writer.writeByte('\\');
try self.writer.writeByte(c); try self.writer.writeByte(c);
start = i + 1;
}, },
else => try self.writer.writeByte(c), else => {},
} }
} }
if (start < text.len) try self.writer.writeAll(text[start..]);
} }
}; };

View File

@@ -306,27 +306,3 @@
URL.revokeObjectURL(blobUrl); URL.revokeObjectURL(blobUrl);
}); });
</script> </script>
<script id=xhr_timeout>
// timeout property: default is 0
const req = new XMLHttpRequest();
testing.expectEqual(0, req.timeout);
// timeout can be set and read back
req.timeout = 5000;
testing.expectEqual(5000, req.timeout);
// request with timeout set succeeds normally when server responds in time
testing.async(async (restore) => {
const event = await new Promise((resolve) => {
req.onload = resolve;
req.open('GET', 'http://127.0.0.1:9582/xhr');
req.send();
});
restore();
testing.expectEqual('load', event.type);
testing.expectEqual(200, req.status);
testing.expectEqual(5000, req.timeout);
});
</script>

View File

@@ -523,6 +523,31 @@ 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 "";
} }
@@ -1686,6 +1711,8 @@ 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, .{});

View File

@@ -391,6 +391,14 @@ 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,
@@ -1231,6 +1239,7 @@ 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, .{});

View File

@@ -174,6 +174,14 @@ 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 "";
} }
@@ -218,6 +226,7 @@ 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 });

View File

@@ -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 net_http = @import("../../../network/http.zig"); const http = @import("../../../network/http.zig");
pub fn populateHttpHeader(self: *Headers, allocator: Allocator, http_headers: *net_http.Headers) !void { pub fn populateHttpHeader(self: *Headers, allocator: Allocator, http_headers: *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);

View File

@@ -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 net_http = @import("../../../network/http.zig"); const 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: net_http.Method, _method: 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) !net_http.Method { fn parseMethod(method: []const u8, page: *Page) !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(net_http.Method).initComptime(.{ const method_lookup = std.StaticStringMap(http.Method).initComptime(.{
.{ "get", .GET }, .{ "get", .GET },
.{ "post", .POST }, .{ "post", .POST },
.{ "delete", .DELETE }, .{ "delete", .DELETE },

View File

@@ -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 net_http = @import("../../../network/http.zig"); const 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");
@@ -47,7 +47,7 @@ _transfer: ?*HttpClient.Transfer = null,
_active_request: bool = false, _active_request: bool = false,
_url: [:0]const u8 = "", _url: [:0]const u8 = "",
_method: net_http.Method = .GET, _method: http.Method = .GET,
_request_headers: *Headers, _request_headers: *Headers,
_request_body: ?[]const u8 = null, _request_body: ?[]const u8 = null,
@@ -63,7 +63,6 @@ _response_type: ResponseType = .text,
_ready_state: ReadyState = .unsent, _ready_state: ReadyState = .unsent,
_on_ready_state_change: ?js.Function.Temp = null, _on_ready_state_change: ?js.Function.Temp = null,
_with_credentials: bool = false, _with_credentials: bool = false,
_timeout: u32 = 0,
const ReadyState = enum(u8) { const ReadyState = enum(u8) {
unsent = 0, unsent = 0,
@@ -181,14 +180,6 @@ pub fn setWithCredentials(self: *XMLHttpRequest, value: bool) !void {
self._with_credentials = value; self._with_credentials = value;
} }
pub fn getTimeout(self: *const XMLHttpRequest) u32 {
return self._timeout;
}
pub fn setTimeout(self: *XMLHttpRequest, value: u32) void {
self._timeout = value;
}
// TODO: this takes an optional 3 more parameters // TODO: this takes an optional 3 more parameters
// 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 {
@@ -262,7 +253,6 @@ pub fn send(self: *XMLHttpRequest, body_: ?[]const u8) !void {
.cookie_jar = if (cookie_support) &page._session.cookie_jar else null, .cookie_jar = if (cookie_support) &page._session.cookie_jar else null,
.cookie_origin = page.url, .cookie_origin = page.url,
.resource_type = .xhr, .resource_type = .xhr,
.timeout_ms = self._timeout,
.notification = page._session.notification, .notification = page._session.notification,
.start_callback = httpStartCallback, .start_callback = httpStartCallback,
.header_callback = httpHeaderDoneCallback, .header_callback = httpHeaderDoneCallback,
@@ -416,7 +406,7 @@ fn httpStartCallback(transfer: *HttpClient.Transfer) !void {
self._transfer = transfer; self._transfer = transfer;
} }
fn httpHeaderCallback(transfer: *HttpClient.Transfer, header: net_http.Header) !void { fn httpHeaderCallback(transfer: *HttpClient.Transfer, header: http.Header) !void {
const self: *XMLHttpRequest = @ptrCast(@alignCast(transfer.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);
@@ -549,7 +539,6 @@ fn handleError(self: *XMLHttpRequest, err: anyerror) void {
} }
fn _handleError(self: *XMLHttpRequest, err: anyerror) !void { fn _handleError(self: *XMLHttpRequest, err: anyerror) !void {
const is_abort = err == error.Abort; const is_abort = err == error.Abort;
const is_timeout = err == error.OperationTimedout;
const new_state: ReadyState = if (is_abort) .unsent else .done; const new_state: ReadyState = if (is_abort) .unsent else .done;
if (new_state != self._ready_state) { if (new_state != self._ready_state) {
@@ -558,12 +547,8 @@ fn _handleError(self: *XMLHttpRequest, err: anyerror) !void {
try self.stateChanged(new_state, page); try self.stateChanged(new_state, page);
if (is_abort) { if (is_abort) {
try self._proto.dispatch(.abort, null, page); try self._proto.dispatch(.abort, null, page);
} else if (is_timeout) {
try self._proto.dispatch(.timeout, null, page);
}
if (!is_timeout) {
try self._proto.dispatch(.err, null, page);
} }
try self._proto.dispatch(.err, null, page);
try self._proto.dispatch(.load_end, null, page); try self._proto.dispatch(.load_end, null, page);
} }
@@ -589,7 +574,7 @@ fn stateChanged(self: *XMLHttpRequest, state: ReadyState, page: *Page) !void {
} }
} }
fn parseMethod(method: []const u8) !net_http.Method { fn parseMethod(method: []const u8) !http.Method {
if (std.ascii.eqlIgnoreCase(method, "get")) { if (std.ascii.eqlIgnoreCase(method, "get")) {
return .GET; return .GET;
} }
@@ -625,7 +610,6 @@ pub const JsApi = struct {
pub const DONE = bridge.property(@intFromEnum(XMLHttpRequest.ReadyState.done), .{ .template = true }); pub const DONE = bridge.property(@intFromEnum(XMLHttpRequest.ReadyState.done), .{ .template = true });
pub const onreadystatechange = bridge.accessor(XMLHttpRequest.getOnReadyStateChange, XMLHttpRequest.setOnReadyStateChange, .{}); pub const onreadystatechange = bridge.accessor(XMLHttpRequest.getOnReadyStateChange, XMLHttpRequest.setOnReadyStateChange, .{});
pub const timeout = bridge.accessor(XMLHttpRequest.getTimeout, XMLHttpRequest.setTimeout, .{});
pub const withCredentials = bridge.accessor(XMLHttpRequest.getWithCredentials, XMLHttpRequest.setWithCredentials, .{ .dom_exception = true }); pub const withCredentials = bridge.accessor(XMLHttpRequest.getWithCredentials, XMLHttpRequest.setWithCredentials, .{ .dom_exception = true });
pub const open = bridge.function(XMLHttpRequest.open, .{}); pub const open = bridge.function(XMLHttpRequest.open, .{});
pub const send = bridge.function(XMLHttpRequest.send, .{ .dom_exception = true }); pub const send = bridge.function(XMLHttpRequest.send, .{ .dom_exception = true });

View File

@@ -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 net_http = @import("../../network/http.zig"); const 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 net_http.Header = null, headers: ?[]const 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(net_http.Method, method) orelse return error.InvalidParams; transfer.req.method = std.meta.stringToEnum(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 net_http.Header = null, responseHeaders: ?[]const 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,

View File

@@ -86,7 +86,7 @@ fn report(reason: []const u8, begin_addr: usize, args: anytype) !void {
var url_buffer: [4096]u8 = undefined; var url_buffer: [4096]u8 = undefined;
const url = blk: { const url = blk: {
var writer: std.Io.Writer = .fixed(&url_buffer); var writer: std.Io.Writer = .fixed(&url_buffer);
try writer.print("https://crash.lightpanda.io/c?v={s}&r=", .{lp.build_config.version}); try writer.print("https://crash.lightpanda.io/c?v={s}&r=", .{lp.build_config.version_encoded});
for (reason) |b| { for (reason) |b| {
switch (b) { switch (b) {
'A'...'Z', 'a'...'z', '0'...'9', '-', '.', '_' => try writer.writeByte(b), 'A'...'Z', 'a'...'z', '0'...'9', '-', '.', '_' => try writer.writeByte(b),

View File

@@ -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/Runtime.zig"); pub const Network = @import("network/Network.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");

View File

@@ -26,11 +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 net_http = @import("http.zig"); const 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 Runtime = @This(); const Network = @This();
const Listener = struct { const Listener = struct {
socket: posix.socket_t, socket: posix.socket_t,
@@ -46,11 +46,11 @@ const MAX_TICK_CALLBACKS = 16;
allocator: Allocator, allocator: Allocator,
config: *const Config, config: *const Config,
ca_blob: ?net_http.Blob, ca_blob: ?http.Blob,
robot_store: RobotStore, robot_store: RobotStore,
web_bot_auth: ?WebBotAuth, web_bot_auth: ?WebBotAuth,
connections: []net_http.Connection, connections: []http.Connection,
available: std.DoublyLinkedList = .{}, available: std.DoublyLinkedList = .{},
conn_mutex: std.Thread.Mutex = .{}, conn_mutex: std.Thread.Mutex = .{},
@@ -63,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, Runtime is used sparingly, and we only create it on demand. // Currently, Network is used sparingly, and we only create it on demand.
// When Runtime becomes truly shared, it should become a regular field. // When Network 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 = .{},
@@ -200,7 +200,7 @@ fn globalDeinit() void {
libcurl.curl_global_cleanup(); libcurl.curl_global_cleanup();
} }
pub fn init(allocator: Allocator, config: *const Config) !Runtime { pub fn init(allocator: Allocator, config: *const Config) !Network {
globalInit(allocator); globalInit(allocator);
errdefer globalDeinit(); errdefer globalDeinit();
@@ -213,18 +213,18 @@ pub fn init(allocator: Allocator, config: *const Config) !Runtime {
@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: ?net_http.Blob = null; var ca_blob: ?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(net_http.Connection, count); const connections = try allocator.alloc(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 net_http.Connection.init(ca_blob, config); connections[i] = try http.Connection.init(ca_blob, config);
available.append(&connections[i].node); available.append(&connections[i].node);
} }
@@ -249,7 +249,7 @@ pub fn init(allocator: Allocator, config: *const Config) !Runtime {
}; };
} }
pub fn deinit(self: *Runtime) void { pub fn deinit(self: *Network) void {
if (self.multi) |multi| { if (self.multi) |multi| {
libcurl.curl_multi_cleanup(multi) catch {}; libcurl.curl_multi_cleanup(multi) catch {};
} }
@@ -282,7 +282,7 @@ pub fn deinit(self: *Runtime) void {
} }
pub fn bind( pub fn bind(
self: *Runtime, self: *Network,
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,
@@ -313,7 +313,7 @@ pub fn bind(
}; };
} }
pub fn onTick(self: *Runtime, ctx: *anyopaque, callback: *const fn (*anyopaque) void) void { pub fn onTick(self: *Network, 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();
@@ -328,7 +328,7 @@ pub fn onTick(self: *Runtime, ctx: *anyopaque, callback: *const fn (*anyopaque)
self.wakeupPoll(); self.wakeupPoll();
} }
pub fn fireTicks(self: *Runtime) void { pub fn fireTicks(self: *Network) void {
self.callbacks_mutex.lock(); self.callbacks_mutex.lock();
defer self.callbacks_mutex.unlock(); defer self.callbacks_mutex.unlock();
@@ -337,7 +337,7 @@ pub fn fireTicks(self: *Runtime) void {
} }
} }
pub fn run(self: *Runtime) void { pub fn run(self: *Network) 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;
@@ -428,18 +428,18 @@ pub fn run(self: *Runtime) void {
} }
} }
pub fn submitRequest(self: *Runtime, conn: *net_http.Connection) void { pub fn submitRequest(self: *Network, conn: *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: *Runtime) void { fn wakeupPoll(self: *Network) void {
_ = posix.write(self.wakeup_pipe[1], &.{1}) catch {}; _ = posix.write(self.wakeup_pipe[1], &.{1}) catch {};
} }
fn drainQueue(self: *Runtime) void { fn drainQueue(self: *Network) void {
self.submission_mutex.lock(); self.submission_mutex.lock();
defer self.submission_mutex.unlock(); defer self.submission_mutex.unlock();
@@ -455,7 +455,7 @@ fn drainQueue(self: *Runtime) void {
}; };
while (self.submission_queue.popFirst()) |node| { while (self.submission_queue.popFirst()) |node| {
const conn: *net_http.Connection = @fieldParentPtr("node", node); const conn: *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);
@@ -468,12 +468,12 @@ fn drainQueue(self: *Runtime) void {
} }
} }
pub fn stop(self: *Runtime) void { pub fn stop(self: *Network) void {
self.shutdown.store(true, .release); self.shutdown.store(true, .release);
self.wakeupPoll(); self.wakeupPoll();
} }
fn acceptConnections(self: *Runtime) void { fn acceptConnections(self: *Network) void {
if (self.shutdown.load(.acquire)) { if (self.shutdown.load(.acquire)) {
return; return;
} }
@@ -503,7 +503,7 @@ fn acceptConnections(self: *Runtime) void {
} }
} }
fn preparePollFds(self: *Runtime, multi: *libcurl.CurlM) void { fn preparePollFds(self: *Network, 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 });
@@ -514,14 +514,14 @@ fn preparePollFds(self: *Runtime, multi: *libcurl.CurlM) void {
}; };
} }
fn getCurlTimeout(self: *Runtime) i32 { fn getCurlTimeout(self: *Network) 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: *Runtime, multi: *libcurl.CurlM) void { fn processCompletions(self: *Network, 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) {
@@ -537,7 +537,7 @@ fn processCompletions(self: *Runtime, 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: *net_http.Connection = @ptrCast(@alignCast(ptr)); const conn: *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);
@@ -556,7 +556,7 @@ comptime {
} }
} }
pub fn getConnection(self: *Runtime) ?*net_http.Connection { pub fn getConnection(self: *Network) ?*http.Connection {
self.conn_mutex.lock(); self.conn_mutex.lock();
defer self.conn_mutex.unlock(); defer self.conn_mutex.unlock();
@@ -564,7 +564,7 @@ pub fn getConnection(self: *Runtime) ?*net_http.Connection {
return @fieldParentPtr("node", node); return @fieldParentPtr("node", node);
} }
pub fn releaseConnection(self: *Runtime, conn: *net_http.Connection) void { pub fn releaseConnection(self: *Network, conn: *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 });
}; };
@@ -575,8 +575,8 @@ pub fn releaseConnection(self: *Runtime, conn: *net_http.Connection) void {
self.available.append(&conn.node); self.available.append(&conn.node);
} }
pub fn newConnection(self: *Runtime) !net_http.Connection { pub fn newConnection(self: *Network) !http.Connection {
return net_http.Connection.init(self.ca_blob, self.config); return 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

View File

@@ -234,10 +234,6 @@ pub const Connection = struct {
try libcurl.curl_easy_setopt(self._easy, .url, url.ptr); try libcurl.curl_easy_setopt(self._easy, .url, url.ptr);
} }
pub fn setTimeout(self: *const Connection, timeout_ms: u32) !void {
try libcurl.curl_easy_setopt(self._easy, .timeout_ms, timeout_ms);
}
// a libcurl request has 2 methods. The first is the method that // a libcurl request has 2 methods. The first is the method that
// controls how libcurl behaves. This specifically influences how redirects // controls how libcurl behaves. This specifically influences how redirects
// are handled. For example, if you do a POST and get a 301, libcurl will // are handled. For example, if you do a POST and get a 301, libcurl will

View File

@@ -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 Runtime = @import("../network/Runtime.zig"); const Network = @import("../network/Network.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,
runtime: *Runtime, network: *Network,
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,
.runtime = &app.network, .network = &app.network,
.writer = std.Io.Writer.Allocating.init(app.allocator), .writer = std.Io.Writer.Allocating.init(app.allocator),
}; };
self.runtime.onTick(@ptrCast(self), flushCallback); self.network.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.runtime.getConnection() orelse { const conn = self.network.getConnection() orelse {
return; return;
}; };
errdefer self.runtime.releaseConnection(conn); errdefer self.network.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.runtime.releaseConnection(conn); self.network.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.runtime.submitRequest(conn); self.network.submitRequest(conn);
} }
fn writeEvent(self: *LightPanda, event: telemetry.Event) !bool { fn writeEvent(self: *LightPanda, event: telemetry.Event) !bool {