Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TCP server websocket-aware #408

Merged
merged 9 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@
[submodule "vendor/zig-async-io"]
path = vendor/zig-async-io
url = https://github.com/lightpanda-io/zig-async-io.git/
[submodule "vendor/websocket.zig"]
path = vendor/websocket.zig
url = https://github.com/lightpanda-io/websocket.zig.git/
branch = lightpanda
5 changes: 0 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ fn common(
.root_source_file = b.path("vendor/tls.zig/src/main.zig"),
});
step.root_module.addImport("tls", tlsmod);

const wsmod = b.addModule("websocket", .{
.root_source_file = b.path("vendor/websocket.zig/src/websocket.zig"),
});
step.root_module.addImport("websocket", wsmod);
}

fn moduleNetSurf(b: *std.Build, target: std.Build.ResolvedTarget) !*std.Build.Module {
Expand Down
4 changes: 2 additions & 2 deletions src/cdp/runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ fn sendInspector(
const buf = try alloc.alloc(u8, msg.json.len + 1);
defer alloc.free(buf);
_ = std.mem.replace(u8, msg.json, "\"awaitPromise\":true", "\"awaitPromise\":false", buf);
ctx.sendInspector(buf);
try ctx.sendInspector(buf);
return "";
}
}

ctx.sendInspector(msg.json);
try ctx.sendInspector(msg.json);

if (msg.id == null) return "";

Expand Down
95 changes: 0 additions & 95 deletions src/handler.zig

This file was deleted.

71 changes: 6 additions & 65 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ const std = @import("std");
const builtin = @import("builtin");

const jsruntime = @import("jsruntime");
const websocket = @import("websocket");

const Browser = @import("browser/browser.zig").Browser;
const server = @import("server.zig");
const handler = @import("handler.zig");
const MaxSize = @import("msg.zig").MaxSize;

const parser = @import("netsurf");
const apiweb = @import("apiweb.zig");
Expand Down Expand Up @@ -86,11 +83,9 @@ const CliMode = union(CliModeTag) {
const Server = struct {
execname: []const u8 = undefined,
args: *std.process.ArgIterator = undefined,
addr: std.net.Address = undefined,
host: []const u8 = Host,
port: u16 = Port,
timeout: u8 = Timeout,
tcp: bool = false, // undocumented TCP mode

// default options
const Host = "127.0.0.1";
Expand Down Expand Up @@ -160,10 +155,6 @@ const CliMode = union(CliModeTag) {
return printUsageExit(execname, 1);
}
}
if (std.mem.eql(u8, "--tcp", opt)) {
_server.tcp = true;
continue;
}

// unknown option
if (std.mem.startsWith(u8, opt, "--")) {
Expand All @@ -186,10 +177,6 @@ const CliMode = union(CliModeTag) {
if (default_mode == .server) {

// server mode
_server.addr = std.net.Address.parseIp4(_server.host, _server.port) catch |err| {
log.err("address (host:port) {any}\n", .{err});
return printUsageExit(execname, 1);
};
_server.execname = execname;
_server.args = args;
return CliMode{ .server = _server };
Expand Down Expand Up @@ -247,65 +234,19 @@ pub fn main() !void {

switch (cli_mode) {
.server => |opts| {

// Stream server
const addr = blk: {
if (opts.tcp) {
break :blk opts.addr;
} else {
const unix_path = "/tmp/lightpanda";
std.fs.deleteFileAbsolute(unix_path) catch {}; // file could not exists
break :blk try std.net.Address.initUnix(unix_path);
}
};
const socket = server.listen(addr) catch |err| {
log.err("Server listen error: {any}\n", .{err});
const address = std.net.Address.parseIp4(opts.host, opts.port) catch |err| {
log.err("address (host:port) {any}\n", .{err});
return printUsageExit(opts.execname, 1);
};
defer std.posix.close(socket);
log.debug("Server opts: listening internally on {any}...", .{addr});

const timeout = std.time.ns_per_s * @as(u64, opts.timeout);

// loop
var loop = try jsruntime.Loop.init(alloc);
defer loop.deinit();

// TCP server mode
if (opts.tcp) {
return server.handle(alloc, &loop, socket, null, timeout);
}

// start stream server in separate thread
var stream = handler.Stream{
.ws_host = opts.host,
.ws_port = opts.port,
.addr = addr,
const timeout = std.time.ns_per_s * @as(u64, opts.timeout);
server.run(alloc, address, timeout, &loop) catch |err| {
log.err("Server error", .{});
return err;
};
const cdp_thread = try std.Thread.spawn(
.{ .allocator = alloc },
server.handle,
.{ alloc, &loop, socket, &stream, timeout },
);

// Websocket server
var ws = try websocket.Server(handler.Handler).init(alloc, .{
.port = opts.port,
.address = opts.host,
.max_message_size = MaxSize + 14, // overhead websocket
.max_conn = 1,
.handshake = .{
.timeout = 3,
.max_size = 1024,
// since we aren't using hanshake.headers
// we can set this to 0 to save a few bytes.
.max_headers = 0,
},
});
defer ws.deinit();

try ws.listen(&stream);
cdp_thread.join();
},

.fetch => |opts| {
Expand Down
9 changes: 6 additions & 3 deletions src/main_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,6 @@ const kb = 1024;
const ms = std.time.ns_per_ms;

test {
const msgTest = @import("msg.zig");
std.testing.refAllDecls(msgTest);

const dumpTest = @import("browser/dump.zig");
std.testing.refAllDecls(dumpTest);

Expand All @@ -340,6 +337,12 @@ test {

std.testing.refAllDecls(@import("generate.zig"));
std.testing.refAllDecls(@import("cdp/msg.zig"));

// Don't use refAllDecls, as this will pull in the entire project
// and break the test build.
// We should fix this. See this branch & the commit message for details:
// https://github.com/karlseguin/browser/commit/193ab5ceab3d3758ea06db04f7690460d79eb79e
_ = @import("server.zig");
}

fn testJSRuntime(alloc: std.mem.Allocator) !void {
Expand Down
Loading