From fd7775b4e9c24b6bb8f664beb7d4fd2ccecd8d85 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 12 Oct 2026 15:45:52 -0300 Subject: 9web: serve local sites with 9P build controls and passive listener discovery Add optional catalogue mode with an explicit site registry, per-site HTTP roots, MIME/index/pretty-URL handling, HEAD and byte ranges. Existing generated sites are available immediately, without preview processes. Expose build, watch, cancel, status, logs and state events through the shared 9P backend and HTTP dashboard. Supervise process groups, coalesce queued rebuilds and watch configured source paths. Discover Linux TCP listeners by socket inode ownership and executable/argument recognizers; never probe endpoints. Document configuration, the user-systemd unit, workstation ports, network access assumptions and limitations including in-place publication and static-only Worker sites. Keep the original upstream gateway mode. Validation: catalogue integration suite (including paginated directory reads, cancellation and parked events), existing HTTP filesystem and mux suites, real 9ns mount, dual-stack service restart and all five workstation sites. Real entomology build: 129 pages, zero failures. --- web/main.zig | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 193 insertions(+), 3 deletions(-) (limited to 'web/main.zig') diff --git a/web/main.zig b/web/main.zig index 130ed4b..8c3d6ad 100644 --- a/web/main.zig +++ b/web/main.zig @@ -11,6 +11,7 @@ const c9 = @import("cloud9"); const mux = @import("mux.zig"); const httpfs = @import("httpfs.zig"); const probe = @import("probe.zig"); +const catalog = @import("catalog.zig"); const Io = std.Io; const max_frame = 65536; @@ -29,6 +30,7 @@ const Config = struct { public_host: []const u8, timeout_ms: u32, browser_config: []const u8, + wildcard: bool = false, }; /// One downstream slot: buffers, the HTTP/WebSocket streams and the mux @@ -48,6 +50,7 @@ const Session = struct { conn: MuxT.Conn = undefined, wmutex: Io.Mutex = .init, is_ws: bool = false, + site: ?usize = null, fn sink(s: *Session) mux.Sink { return .{ .ctx = s, .send = sendReply }; @@ -70,6 +73,7 @@ pub const App = struct { io: Io, m: *MuxT, config: Config, + catalog: ?*catalog.Catalog = null, sessions: [max_connections]Session = @splat(.{}), active: std.atomic.Value(u32) = .init(0), @@ -120,6 +124,10 @@ pub fn main(init: std.process.Init) !void { var user: []const u8 = "user"; var tree: []const u8 = ""; var public_origin: ?[]const u8 = null; + var catalog_file: ?[]const u8 = null; + var catalog_socket: ?[]const u8 = null; + var state_dir: ?[]const u8 = null; + var explicit_upstream = false; var i: usize = 1; while (i < args.len) : (i += 1) { if (std.mem.eql(u8, args[i], "--help")) { @@ -127,6 +135,8 @@ pub fn main(init: std.process.Init) !void { \\Usage: 9web [--listen IP:PORT] [--upstream tcp:IP:PORT|unix:PATH|file:PATH] \\ [--serve tcp:IP:PORT|unix:PATH] [--probe unix:PATH|tcp:IP:PORT] \\ [--origin https://HOST:PORT] [--timeout-ms 300000] [--user NAME] [--tree NAME] + \\ [--catalog FILE --catalog-socket PATH --state-dir PATH] + \\Catalogue mode serves configured sites, build controls and passive listener discovery. \\One shared upstream, many downstreams. Default: http://127.0.0.1:8080 -> tcp:127.0.0.1:564 \\ , .{}); @@ -136,11 +146,36 @@ pub fn main(init: std.process.Init) !void { i += 1; const v = args[i]; const k = args[i - 1]; + if (std.mem.eql(u8, k, "--catalog")) { + catalog_file = v; + continue; + } + if (std.mem.eql(u8, k, "--catalog-socket")) { + catalog_socket = v; + continue; + } + if (std.mem.eql(u8, k, "--state-dir")) { + state_dir = v; + continue; + } + if (std.mem.eql(u8, k, "--upstream")) explicit_upstream = true; if (std.mem.eql(u8, k, "--listen")) listen_text = v else if (std.mem.eql(u8, k, "--upstream")) upstream_text = v else if (std.mem.eql(u8, k, "--serve")) serve_text = v else if (std.mem.eql(u8, k, "--probe")) probe_text = v else if (std.mem.eql(u8, k, "--origin")) public_origin = v else if (std.mem.eql(u8, k, "--timeout-ms")) timeout_ms = try std.fmt.parseInt(u32, v, 10) else if (std.mem.eql(u8, k, "--user")) user = v else if (std.mem.eql(u8, k, "--tree")) tree = v else return error.UnknownArgument; } if (user.len > 256 or tree.len > 256) return error.AttachNameTooLong; const io = init.io; + var cat: ?*catalog.Catalog = null; + if (catalog_file) |file| { + if (@import("builtin").os.tag == .linux) { + if (explicit_upstream) return error.CatalogAndUpstreamAreExclusive; + const socket = catalog_socket orelse return error.MissingCatalogSocket; + cat = try catalog.Catalog.init(io, file, state_dir orelse return error.MissingStateDir); + cat.?.http_port = (try Io.net.IpAddress.parseLiteral(listen_text)).getPort(); + try cat.?.start(try allocator.dupeSentinel(u8, socket, 0)); + upstream_text = try std.fmt.allocPrint(allocator, "unix:{s}", .{socket}); + } else return error.CatalogRequiresLinux; + } + defer if (cat) |c| c.stop(); const address = try Io.net.IpAddress.parseLiteral(listen_text); const upstream = try parseUpstream(allocator, upstream_text); @@ -167,12 +202,13 @@ pub fn main(init: std.process.Init) !void { origin_uri.path.percent_encoded.len != 0 or origin_uri.query != null or origin_uri.fragment != null) return error.InvalidOrigin; const browser_config = try std.json.Stringify.valueAlloc(allocator, .{ .user = user, .tree = tree }, .{}); - var app: App = .{ .io = io, .m = &m, .config = .{ + var app: App = .{ .io = io, .m = &m, .catalog = cat, .config = .{ .host = host, .origin = origin_text, .public_host = origin_text[origin_uri.scheme.len + 3 ..], .timeout_ms = timeout_ms, .browser_config = browser_config, + .wildcard = std.mem.startsWith(u8, listen_text, "0.0.0.0:") or std.mem.startsWith(u8, listen_text, "[::]:"), } }; std.debug.print("9web {s} -> {s}\n", .{ app.config.origin, upstream_text }); @@ -181,6 +217,19 @@ pub fn main(init: std.process.Init) !void { defer group.cancel(io); try group.concurrent(io, MuxT.readerLoop, .{&m}); + // Each site gets its own HTTP root, preserving /assets and /blog links. + // These listeners belong to this same 9web process, not preview daemons. + if (cat) |c| { + for (c.sites, 0..) |site, si| { + const l = try allocator.create(Io.net.Server); + var bind_address = address; + bind_address.setPort(site.port); + l.* = try c9.transport.listen(io, .{ .tcp = bind_address }, max_connections); + try group.concurrent(io, siteAcceptLoop, .{ &app, l, &group, si }); + std.debug.print("9web: site {s} on {f} from {s}\n", .{ site.name, l.socket.address, site.root }); + } + } + // Optional 9P listener for plan9port / 9ns clients (a mux like 9pserve). var serve_listener: ?Io.net.Server = null; if (serve_text) |t| { @@ -216,6 +265,7 @@ pub fn main(init: std.process.Init) !void { continue; }; s.app = &app; + s.site = null; s.stream = stream; group.concurrent(io, handleHttp, .{s}) catch { app.giveBack(s); @@ -225,6 +275,24 @@ pub fn main(init: std.process.Init) !void { } } +fn siteAcceptLoop(app: *App, listener: *Io.net.Server, group: *Io.Group, si: usize) void { + defer listener.deinit(app.io); + while (true) { + const stream = listener.accept(app.io) catch return; + const s = app.take() orelse { + stream.close(app.io); + continue; + }; + s.app = app; + s.stream = stream; + s.site = si; + group.concurrent(app.io, handleHttp, .{s}) catch { + app.giveBack(s); + stream.close(app.io); + }; + } +} + /// Accepts plain 9P clients on the `--serve` listener; each runs a raw mux /// session for its lifetime. fn serveLoop(app: *App, listener: *Io.net.Server, group: *Io.Group) void { @@ -318,15 +386,48 @@ fn serveHttp(s: *Session) !void { if (std.ascii.eqlIgnoreCase(header.name, "origin")) origin = header.value; if (std.ascii.eqlIgnoreCase(header.name, "sec-websocket-version")) version = header.value; } - if (!std.mem.eql(u8, host orelse "", config.host) and !std.mem.eql(u8, host orelse "", config.public_host)) + if (s.site) |si| return serveSite(s, &request, app.catalog.?.sites[si]); + const host_text = host orelse ""; + const literal = Io.net.IpAddress.parseLiteral(host_text) catch null; + const bound = try Io.net.IpAddress.parseLiteral(config.host); + const wildcard_host = config.wildcard and ((if (literal) |ip| ip.getPort() == bound.getPort() else false) or std.mem.eql(u8, host_text, try std.fmt.bufPrint(&s.body, "localhost:{d}", .{bound.getPort()}))); + if (!wildcard_host and !std.mem.eql(u8, host_text, config.host) and !std.mem.eql(u8, host_text, config.public_host)) return respond(&request, "Unexpected Host\n", "text/plain", .forbidden); const path = std.mem.sliceTo(request.head.target, '?'); + // Browsers must originate control writes from this dashboard. Site ports + // are separate origins. CLI and 9P clients do not send Origin. + const same_origin = if (origin) |o| std.mem.eql(u8, o, config.origin) or + (std.mem.startsWith(u8, o, "http://") and std.mem.eql(u8, o[7..], host_text)) else false; + if (app.catalog) |c| { + if (request.head.method != .GET and request.head.method != .HEAD and origin != null and !same_origin) + return respond(&request, "Unexpected Origin\n", "text/plain", .forbidden); + if (std.mem.eql(u8, path, "/")) return respond(&request, @embedFile("static/catalog.html"), "text/html; charset=utf-8", .ok); + if (std.mem.eql(u8, path, "/_cloud9/catalog.js")) return respond(&request, @embedFile("static/catalog.js"), "text/javascript", .ok); + if (std.mem.eql(u8, path, "/_cloud9/catalog.json")) { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + return respond(&request, try c.snapshot(arena.allocator()), "application/json", .ok); + } + if (std.mem.startsWith(u8, path, "/_cloud9/sites/") and std.mem.endsWith(u8, path, "/ctl")) { + if (request.head.method != .POST) return respond(&request, "Use POST\n", "text/plain", .method_not_allowed); + const name = path[15 .. path.len - 4]; + for (c.sites, 0..) |site, si| if (std.mem.eql(u8, name, site.name)) { + var body_buf: [256]u8 = undefined; + const reader = try request.readerExpectContinue(&body_buf); + const command = reader.allocRemaining(std.heap.page_allocator, .limited(128)) catch return respond(&request, "Command too long\n", "text/plain", .bad_request); + defer std.heap.page_allocator.free(command); + c.control(si, command) catch |err| return respond(&request, @errorName(err), "text/plain", .bad_request); + return respond(&request, "queued\n", "text/plain", .accepted); + }; + return respond(&request, "Unknown site\n", "text/plain", .not_found); + } + } // The WebSocket endpoint. if (std.mem.eql(u8, path, "/_cloud9/9p")) { if (request.head.method != .GET) return respond(&request, "Use GET\n", "text/plain", .method_not_allowed); - if (!std.mem.eql(u8, origin orelse "", config.origin)) return respond(&request, "Unexpected Origin\n", "text/plain", .forbidden); + if (!same_origin) return respond(&request, "Unexpected Origin\n", "text/plain", .forbidden); if (!std.mem.eql(u8, version orelse "", "13")) return respond(&request, "WebSocket version 13 required\n", "text/plain", .bad_request); return wsSession(s, &request); } @@ -375,3 +476,92 @@ fn wsSession(s: *Session, request: *std.http.Server.Request) !void { } } } + +fn serveSite(s: *Session, request: *std.http.Server.Request, site: catalog.Site) !void { + if (request.head.method != .GET and request.head.method != .HEAD) return respond(request, "Use GET or HEAD\n", "text/plain", .method_not_allowed); + const raw = std.mem.sliceTo(request.head.target, '?'); + if (raw.len == 0 or raw[0] != '/') return respond(request, "Bad path\n", "text/plain", .bad_request); + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const a = arena.allocator(); + const decoded = try a.alloc(u8, raw.len); + var n: usize = 0; + var i: usize = 1; + while (i < raw.len) : (i += 1) { + var ch = raw[i]; + if (ch == '%') { + if (i + 2 >= raw.len) return respond(request, "Bad escape\n", "text/plain", .bad_request); + ch = std.fmt.parseInt(u8, raw[i + 1 .. i + 3], 16) catch return respond(request, "Bad escape\n", "text/plain", .bad_request); + if (ch == '/' or ch == '\\') return respond(request, "Bad escape\n", "text/plain", .bad_request); + i += 2; + } + decoded[n] = ch; + n += 1; + } + var rel: []const u8 = decoded[0..n]; + if (!catalog.safeRelative(rel)) return respond(request, "Forbidden path\n", "text/plain", .forbidden); + // Dotfiles and builder/provider configuration are not public documents. + var components = std.mem.tokenizeScalar(u8, rel, '/'); + while (components.next()) |component| if (component[0] == '.' or std.mem.eql(u8, component, "_headers") or std.mem.eql(u8, component, "_redirects")) return respond(request, "Not found\n", "text/plain", .not_found); + var file = catalog.openBeneath(s.app.io, site.root, rel) catch blk: { + // Pretty URLs such as /blog/post resolve to /blog/post.html. + if (std.fs.path.extension(rel).len != 0) return respond(request, "Not found\n", "text/plain", .not_found); + rel = try std.fmt.allocPrint(a, "{s}.html", .{rel}); + break :blk catalog.openBeneath(s.app.io, site.root, rel) catch return respond(request, "Not found\n", "text/plain", .not_found); + }; + var st = try file.stat(s.app.io); + if (st.kind == .directory) { + file.close(s.app.io); + if (!std.mem.endsWith(u8, raw, "/")) { + const location = try std.fmt.allocPrint(a, "{s}/", .{raw}); + return request.respond("", .{ .status = .moved_permanently, .keep_alive = false, .extra_headers = &.{.{ .name = "location", .value = location }} }); + } + rel = try std.fs.path.join(a, &.{ rel, "index.html" }); + file = catalog.openBeneath(s.app.io, site.root, rel) catch return respond(request, "Site has not been built, or index.html is missing.\n", "text/plain", .not_found); + st = try file.stat(s.app.io); + } + defer file.close(s.app.io); + if (st.kind != .file) return respond(request, "Not a regular file\n", "text/plain", .forbidden); + var start: u64 = 0; + var end = st.size; + var partial = false; + var headers = request.iterateHeaders(); + while (headers.next()) |h| if (std.ascii.eqlIgnoreCase(h.name, "range")) { + const text = h.value; + if (!std.mem.startsWith(u8, text, "bytes=") or std.mem.indexOfScalar(u8, text, ',') != null) return respond(request, "Unsupported range\n", "text/plain", .range_not_satisfiable); + const dash = std.mem.indexOfScalar(u8, text[6..], '-') orelse return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); + const left = text[6 .. 6 + dash]; + const right = text[7 + dash ..]; + if (left.len == 0) { + const suffix = std.fmt.parseInt(u64, right, 10) catch return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); + start = st.size - @min(st.size, suffix); + } else { + start = std.fmt.parseInt(u64, left, 10) catch return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); + if (right.len != 0) { + const last = std.fmt.parseInt(u64, right, 10) catch return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); + end = @min(st.size, last +| 1); + } + } + if (start >= end) return respond(request, "Unsatisfiable range\n", "text/plain", .range_not_satisfiable); + partial = true; + }; + var extra: std.ArrayList(std.http.Header) = .empty; + try extra.appendSlice(a, &.{ .{ .name = "content-type", .value = catalog.mime(rel) }, .{ .name = "cache-control", .value = "no-cache" }, .{ .name = "x-content-type-options", .value = "nosniff" }, .{ .name = "accept-ranges", .value = "bytes" } }); + if (partial) try extra.append(a, .{ .name = "content-range", .value = try std.fmt.allocPrint(a, "bytes {d}-{d}/{d}", .{ start, end - 1, st.size }) }); + if (request.head.method == .HEAD) { + try extra.append(a, .{ .name = "content-length", .value = try std.fmt.allocPrint(a, "{d}", .{end - start}) }); + return request.respond("", .{ .status = if (partial) .partial_content else .ok, .keep_alive = false, .transfer_encoding = .none, .extra_headers = extra.items }); + } + var bw = try request.respondStreaming(&s.body, .{ .content_length = end - start, .respond_options = .{ .status = if (partial) .partial_content else .ok, .keep_alive = false, .extra_headers = extra.items } }); + if (request.head.method != .HEAD) { + var offset = start; + while (offset < end) { + const count: usize = @intCast(@min(end - offset, s.frame.len)); + const got = try file.readPositionalAll(s.app.io, s.frame[0..count], offset); + if (got == 0) return error.EndOfStream; + try bw.writer.writeAll(s.frame[0..got]); + offset += got; + } + } + try bw.end(); +} -- cgit v1.3