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/catalog.zig | 549 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 549 insertions(+) create mode 100644 web/catalog.zig (limited to 'web/catalog.zig') diff --git a/web/catalog.zig b/web/catalog.zig new file mode 100644 index 0000000..5e75555 --- /dev/null +++ b/web/catalog.zig @@ -0,0 +1,549 @@ +//! Local sites, build supervision and passive listener discovery. All state is +//! owned by 9web; HTTP and 9P use the same controls. Linux catalogue mode only. +const std = @import("std"); +const c9 = @import("cloud9"); +const Io = std.Io; +const A = std.heap.page_allocator; +const fs = c9.fs; +const Backend = struct { + pub const Req = fs.Req; + pub const Reply = fs.Reply; +}; +pub const Runner = c9.serve.Runner(Backend, .{ .fid_capacity = 512, .name_capacity = 255 }, .{ .msize = 65536, .connections = 16 }); + +pub const Site = struct { + name: []const u8, + title: []const u8, + cwd: []const u8, + root: []const u8, + port: u16, + build: []const []const u8 = &.{}, + watch: []const []const u8 = &.{}, + // Poll only these source paths for a managed rebuild watcher. + inputs: []const []const u8 = &.{}, +}; +const Settings = struct { sites: []const Site }; +const State = struct { + phase: []const u8 = "idle", + watching: bool = false, + queued: bool = false, + cancel: bool = false, + pid: ?i32 = null, + exit_code: ?u8 = null, + started: i64 = 0, + finished: i64 = 0, + generation: u64 = 0, + fingerprint: u64 = 0, + revision: u64 = 1, +}; +const EventCursor = struct { + used: bool = false, + revision: u64 = 0, + bytes: [2048]u8 = undefined, + len: usize = 0, + pos: usize = 0, +}; +pub const Catalog = struct { + io: Io, + sites: []const Site, + states: []State, + state_dir: []const u8, + mutex: Io.Mutex = .init, + nodes: std.ArrayList([]const u8) = .empty, + listeners: []const u8 = "[]", + discovery_error: []const u8 = "", + group: Io.Group = .init, + runner: *Runner, + http_port: u16 = 0, + cursors: [16][64]EventCursor = @splat(@splat(.{})), + + pub fn init(io: Io, config: []const u8, state_dir: []const u8) !*Catalog { + const bytes = try Io.Dir.cwd().readFileAlloc(io, config, A, .limited(1024 * 1024)); + defer A.free(bytes); + const parsed = try std.json.parseFromSlice(Settings, A, bytes, .{ .allocate = .alloc_always }); + // Config is immutable for the lifetime of the process. + var names: std.StringHashMap(void) = .init(A); + defer names.deinit(); + for (parsed.value.sites, 0..) |s, i| { + if (s.name.len == 0 or s.port == 0 or !std.fs.path.isAbsolute(s.root) or !std.fs.path.isAbsolute(s.cwd)) return error.InvalidSite; + for (s.name) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '-' and ch != '_') return error.InvalidSiteName; + if (names.contains(s.name)) return error.DuplicateSite; + try names.put(s.name, {}); + for (parsed.value.sites[0..i]) |other| if (other.port == s.port) return error.DuplicatePort; + for (s.inputs) |input| if (!std.fs.path.isAbsolute(input) and !safeRelative(input)) return error.InvalidWatchInput; + } + try Io.Dir.cwd().createDirPath(io, state_dir); + const cat = try A.create(Catalog); + cat.* = .{ .io = io, .sites = parsed.value.sites, .states = try A.alloc(State, parsed.value.sites.len), .state_dir = state_dir, .runner = try A.create(Runner) }; + @memset(cat.states, .{}); + try cat.nodes.append(A, ""); + cat.runner.init(.{ .io = io, .root = 1, .handler = .{ .ctx = cat, .serve = serve } }); + return cat; + } + + pub fn start(cat: *Catalog, socket: [:0]const u8) !void { + _ = try cat.runner.listen(.{ .unix = socket }, 16); + try cat.group.concurrent(cat.io, discoverLoop, .{cat}); + for (cat.sites, 0..) |_, i| try cat.group.concurrent(cat.io, siteLoop, .{ cat, i }); + } + + pub fn stop(cat: *Catalog) void { + cat.group.cancel(cat.io); + cat.runner.stop(); + } + + fn now(cat: *Catalog) i64 { + return Io.Timestamp.now(cat.io, .real).toSeconds(); + } + fn logPath(cat: *Catalog, a: std.mem.Allocator, i: usize) ![]const u8 { + return std.fmt.allocPrint(a, "{s}/{s}.log", .{ cat.state_dir, cat.sites[i].name }); + } + pub fn control(cat: *Catalog, i: usize, command: []const u8) !void { + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + const cmd = std.mem.trim(u8, command, " \r\n\t"); + const s = &cat.states[i]; + if (std.mem.eql(u8, cmd, "build")) { + if (cat.sites[i].build.len == 0) return error.NoBuildCommand; + if (s.watching and cat.sites[i].watch.len != 0) return error.NativeWatcherRunning; + s.queued = true; + } else if (std.mem.eql(u8, cmd, "watch on")) { + if (cat.sites[i].watch.len == 0 and (cat.sites[i].inputs.len == 0 or cat.sites[i].build.len == 0)) return error.NoWatchInputs; + s.watching = true; + s.queued = true; + } else if (std.mem.eql(u8, cmd, "watch off")) { + s.watching = false; + if (cat.sites[i].watch.len != 0) s.cancel = true; + } else if (std.mem.eql(u8, cmd, "cancel")) { + s.watching = false; + s.queued = false; + s.cancel = true; + } else return error.UnknownControl; + s.revision += 1; + cat.runner.wakeAll(); + } + + pub fn snapshot(cat: *Catalog, a: std.mem.Allocator) ![]const u8 { + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + const Row = struct { name: []const u8, title: []const u8, root: []const u8, port: u16, build: bool, watch: bool, status: State }; + const rows = try a.alloc(Row, cat.sites.len); + for (cat.sites, 0..) |s, i| rows[i] = .{ .name = s.name, .title = s.title, .root = s.root, .port = s.port, .build = s.build.len != 0, .watch = s.watch.len != 0 or s.inputs.len != 0, .status = cat.states[i] }; + const listeners = try std.json.parseFromSlice(std.json.Value, a, cat.listeners, .{}); + return std.json.Stringify.valueAlloc(a, .{ .sites = rows, .listeners = listeners.value, .discovery_error = cat.discovery_error }, .{}); + } + + fn discoverLoop(cat: *Catalog) void { + while (true) { + var arena = std.heap.ArenaAllocator.init(A); + defer arena.deinit(); + const a = arena.allocator(); + const pid = std.fmt.allocPrint(a, "{d}", .{std.os.linux.getpid()}) catch return; + var ports: std.Io.Writer.Allocating = .init(a); + ports.writer.print("{d}", .{cat.http_port}) catch return; + for (cat.sites) |site| ports.writer.print(",{d}", .{site.port}) catch return; + const result = std.process.run(A, cat.io, .{ .argv = &.{ "python3", "-c", @embedFile("discover.py"), pid, ports.written() }, .stdout_limit = .limited(2 * 1024 * 1024), .stderr_limit = .limited(8192), .timeout = .{ .duration = .{ .raw = .fromSeconds(15), .clock = .awake } } }) catch |err| { + if (err == error.Canceled) return; + cat.mutex.lockUncancelable(cat.io); + cat.discovery_error = @errorName(err); + cat.mutex.unlock(cat.io); + cat.io.sleep(.fromSeconds(5), .awake) catch return; + continue; + }; + defer A.free(result.stderr); + cat.mutex.lockUncancelable(cat.io); + if (result.term == .exited and result.term.exited == 0) { + if (cat.listeners.len > 2) A.free(cat.listeners); + cat.listeners = result.stdout; + cat.discovery_error = ""; + } else { + A.free(result.stdout); + cat.discovery_error = "listener inventory failed"; + } + cat.mutex.unlock(cat.io); + cat.io.sleep(.fromSeconds(5), .awake) catch return; + } + } + + fn fingerprint(cat: *Catalog, i: usize) u64 { + var hash = std.hash.Wyhash.init(0); + var arena = std.heap.ArenaAllocator.init(A); + defer arena.deinit(); + const a = arena.allocator(); + for (cat.sites[i].inputs) |input| { + const path = if (std.fs.path.isAbsolute(input)) input else std.fs.path.join(a, &.{ cat.sites[i].cwd, input }) catch continue; + const st = Io.Dir.cwd().statFile(cat.io, path, .{ .follow_symlinks = false }) catch continue; + hash.update(input); + if (st.kind != .directory) { + hash.update(std.mem.asBytes(&st.mtime)); + hash.update(std.mem.asBytes(&st.size)); + continue; + } + var dir = Io.Dir.cwd().openDir(cat.io, path, .{ .iterate = true, .follow_symlinks = false }) catch continue; + defer dir.close(cat.io); + var walker = dir.walkSelectively(a) catch continue; + defer walker.deinit(); + // XOR entry hashes so directory iteration ordering does not trigger builds. + var tree: u64 = 0; + while (walker.next(cat.io) catch null) |entry| { + if (entry.basename[0] == '.' or std.mem.eql(u8, entry.basename, "node_modules") or std.mem.eql(u8, entry.basename, "out") or std.mem.eql(u8, entry.basename, "build") or std.mem.eql(u8, entry.basename, "dist") or std.mem.eql(u8, entry.basename, "__pycache__")) continue; + if (entry.kind == .directory) { + walker.enter(cat.io, entry) catch {}; + continue; + } + if (entry.kind != .file) continue; + const es = entry.dir.statFile(cat.io, entry.basename, .{ .follow_symlinks = false }) catch continue; + var eh = std.hash.Wyhash.init(0); + eh.update(entry.path); + eh.update(std.mem.asBytes(&es.mtime)); + eh.update(std.mem.asBytes(&es.size)); + tree ^= eh.final(); + } + hash.update(std.mem.asBytes(&tree)); + } + return hash.final(); + } + + fn siteLoop(cat: *Catalog, i: usize) void { + while (true) { + cat.mutex.lockUncancelable(cat.io); + const scanning = cat.states[i].watching and cat.sites[i].watch.len == 0; + const previous = cat.states[i].fingerprint; + cat.mutex.unlock(cat.io); + const fp = if (scanning) cat.fingerprint(i) else previous; + cat.mutex.lockUncancelable(cat.io); + const s = &cat.states[i]; + if (s.watching and cat.sites[i].watch.len == 0 and fp != s.fingerprint) s.queued = true; + s.fingerprint = fp; + const run = s.queued; + const native = s.watching and cat.sites[i].watch.len != 0; + if (run) { + s.queued = false; + s.cancel = false; + s.phase = if (native) "watching" else "building"; + s.started = cat.now(); + s.exit_code = null; + s.revision += 1; + cat.runner.wakeAll(); + } + cat.mutex.unlock(cat.io); + if (run) cat.runBuild(i, native) catch |err| { + if (err == error.Canceled) return; + cat.mutex.lockUncancelable(cat.io); + cat.states[i].phase = @errorName(err); + cat.states[i].pid = null; + cat.states[i].finished = cat.now(); + cat.states[i].revision += 1; + cat.runner.wakeAll(); + cat.mutex.unlock(cat.io); + }; + cat.io.sleep(.fromSeconds(1), .awake) catch return; + } + } + + const Job = struct { + child: std.process.Child, + done: std.atomic.Value(bool) = .init(false), + term: ?std.process.Child.Term = null, + fn wait(job: *Job, io: Io) void { + job.term = job.child.wait(io) catch null; + job.done.store(true, .release); + } + }; + fn runBuild(cat: *Catalog, i: usize, native: bool) !void { + const path = try cat.logPath(A, i); + defer A.free(path); + const log = try Io.Dir.cwd().createFile(cat.io, path, .{}); + defer log.close(cat.io); + const cmd = if (native) cat.sites[i].watch else cat.sites[i].build; + const argv = try A.alloc([]const u8, cmd.len + 1); + defer A.free(argv); + argv[0] = "setsid"; + @memcpy(argv[1..], cmd); + var job: Job = .{ .child = try std.process.spawn(cat.io, .{ .argv = argv, .cwd = .{ .path = cat.sites[i].cwd }, .stdin = .ignore, .stdout = .{ .file = log }, .stderr = .{ .file = log } }) }; + const pid = job.child.id.?; + var group: Io.Group = .init; + // Cancel whole session, not just a shell, including on server shutdown. + defer { + _ = std.os.linux.kill(-pid, std.os.linux.SIG.KILL); + group.cancel(cat.io); + job.child.kill(cat.io); + } + cat.mutex.lockUncancelable(cat.io); + cat.states[i].pid = pid; + cat.mutex.unlock(cat.io); + try group.concurrent(cat.io, Job.wait, .{ &job, cat.io }); + var canceled = false; + while (!job.done.load(.acquire)) { + cat.mutex.lockUncancelable(cat.io); + const cancel = cat.states[i].cancel; + cat.mutex.unlock(cat.io); + if (cancel) { + canceled = true; + _ = std.os.linux.kill(-pid, std.os.linux.SIG.KILL); + } + try cat.io.sleep(.fromMilliseconds(100), .awake); + } + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + const s = &cat.states[i]; + const code: u8 = if (job.term) |term| (if (term == .exited) term.exited else 128) else 255; + s.pid = null; + s.exit_code = code; + s.finished = cat.now(); + s.phase = if (canceled) "canceled" else if (code == 0) "idle" else "failed"; + if (code == 0 and !canceled) s.generation += 1; + if (native) s.watching = false; + s.revision += 1; + cat.runner.wakeAll(); + } + + fn intern(cat: *Catalog, path: []const u8) !u64 { + for (cat.nodes.items, 0..) |n, i| if (std.mem.eql(u8, n, path)) return i + 1; + if (cat.nodes.items.len >= 65536) return error.TooManyPaths; + try cat.nodes.append(A, try A.dupe(u8, path)); + return cat.nodes.items.len; + } + fn sitePath(cat: *Catalog, path: []const u8) ?struct { i: usize, rest: []const u8 } { + if (!std.mem.startsWith(u8, path, "sites/")) return null; + const rest = path[6..]; + const n = std.mem.indexOfScalar(u8, rest, '/') orelse rest.len; + for (cat.sites, 0..) |s, i| if (std.mem.eql(u8, rest[0..n], s.name)) return .{ .i = i, .rest = if (n == rest.len) "" else rest[n + 1 ..] }; + return null; + } + fn attr(cat: *Catalog, a: std.mem.Allocator, path: []const u8) !fs.Attr { + var result = try cat.attrData(a, path); + result.node = try cat.intern(path); + return result; + } + fn attrData(cat: *Catalog, a: std.mem.Allocator, path: []const u8) !fs.Attr { + var result: fs.Attr = .{ .name = std.fs.path.basename(path), .mode = 0o444 }; + if (path.len == 0 or std.mem.eql(u8, path, "sites") or std.mem.eql(u8, path, "services") or std.mem.eql(u8, path, "listeners")) { + result.dir = true; + result.mode = 0o555; + return result; + } + if (cat.sitePath(path)) |p| { + if (p.rest.len == 0 or std.mem.eql(u8, p.rest, "public")) { + result.dir = true; + result.mode = 0o555; + return result; + } + if (std.mem.startsWith(u8, p.rest, "public/")) { + const file = try openBeneath(cat.io, cat.sites[p.i].root, p.rest[7..]); + defer file.close(cat.io); + const st = try file.stat(cat.io); + result.dir = st.kind == .directory; + result.mode = if (result.dir) 0o555 else 0o444; + result.size = st.size; + result.mtime = @intCast(@max(0, st.mtime.toSeconds())); + return result; + } + if (std.mem.eql(u8, p.rest, "ctl")) { + result.mode = 0o666; + return result; + } + } + const data = try cat.content(a, path); + result.size = data.len; + return result; + } + fn content(cat: *Catalog, a: std.mem.Allocator, path: []const u8) ![]const u8 { + if (std.mem.eql(u8, path, "index.html")) return @embedFile("static/catalog.html"); + if (std.mem.eql(u8, path, "listeners/index.json")) return cat.listeners; + if (std.mem.eql(u8, path, "services/index.json")) { + const parsed = try std.json.parseFromSlice([]std.json.Value, a, cat.listeners, .{}); + var known: std.ArrayList(std.json.Value) = .empty; + for (parsed.value) |v| if (!std.mem.eql(u8, v.object.get("protocol").?.string, "unknown")) { + try known.append(a, v); + }; + return std.json.Stringify.valueAlloc(a, known.items, .{}); + } + if (cat.sitePath(path)) |p| { + if (std.mem.eql(u8, p.rest, "status") or std.mem.eql(u8, p.rest, "events")) return std.json.Stringify.valueAlloc(a, cat.states[p.i], .{}); + if (std.mem.eql(u8, p.rest, "ctl")) return "build\nwatch on\nwatch off\ncancel\n"; + if (std.mem.eql(u8, p.rest, "log")) { + const f = Io.Dir.cwd().openFile(cat.io, try cat.logPath(a, p.i), .{}) catch return ""; + defer f.close(cat.io); + const st = try f.stat(cat.io); + const n = @min(st.size, 256 * 1024); + const b = try a.alloc(u8, @intCast(n)); + return b[0..try f.readPositionalAll(cat.io, b, st.size - n)]; + } + } + return error.FileNotFound; + } + fn children(cat: *Catalog, a: std.mem.Allocator, path: []const u8) ![][]const u8 { + var names: std.ArrayList([]const u8) = .empty; + if (path.len == 0) try names.appendSlice(a, &.{ "index.html", "sites", "services", "listeners" }) else if (std.mem.eql(u8, path, "sites")) { + for (cat.sites) |s| try names.append(a, s.name); + } else if (std.mem.eql(u8, path, "services") or std.mem.eql(u8, path, "listeners")) try names.append(a, "index.json") else if (cat.sitePath(path)) |p| { + if (p.rest.len == 0) try names.appendSlice(a, &.{ "public", "ctl", "status", "log", "events" }) else if (std.mem.eql(u8, p.rest, "public") or std.mem.startsWith(u8, p.rest, "public/")) { + const rel = if (p.rest.len == 6) "" else p.rest[7..]; + const f = try openBeneath(cat.io, cat.sites[p.i].root, rel); + defer f.close(cat.io); + var dir: Io.Dir = .{ .handle = f.handle }; + var it = dir.iterate(); + while (try it.next(cat.io)) |e| if (e.kind == .file or e.kind == .directory) { + try names.append(a, try a.dupe(u8, e.name)); + }; + } else return error.NotDir; + } else return error.NotDir; + std.mem.sort([]const u8, names.items, {}, struct { + fn less(_: void, x: []const u8, y: []const u8) bool { + return std.mem.lessThan(u8, x, y); + } + }.less); + return names.items; + } + fn serve(ctx: ?*anyopaque, conn: *Runner.Conn, req: fs.Req) void { + const cat: *Catalog = @ptrCast(@alignCast(ctx.?)); + var arena = std.heap.ArenaAllocator.init(A); + defer arena.deinit(); + // Controls take their own lock; all other node operations are serialized. + if (req.op == .write) { + cat.mutex.lockUncancelable(cat.io); + const path = if (req.node > 0 and req.node <= cat.nodes.items.len) cat.nodes.items[@intCast(req.node - 1)] else ""; + const p = cat.sitePath(path); + cat.mutex.unlock(cat.io); + if (p) |s| { + if (std.mem.eql(u8, s.rest, "ctl") and req.off == 0 and req.data.len <= 128) { + cat.control(s.i, req.data) catch |err| { + conn.reply(&.{ .tag = req.tag, .status = .err, .ename = @errorName(err) }, ""); + return; + }; + conn.reply(&.{ .tag = req.tag, .written = @intCast(req.data.len) }, ""); + return; + } + } + conn.reply(&fs.Reply.fail(req.tag, fs.E.PERM), ""); + return; + } + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + cat.answer(arena.allocator(), conn, req) catch |err| conn.reply(&.{ .tag = req.tag, .status = .err, .ename = if (err == error.FileNotFound) "not found" else @errorName(err) }, ""); + } + fn answer(cat: *Catalog, a: std.mem.Allocator, conn: *Runner.Conn, req: fs.Req) !void { + if (req.node == 0 or req.node > cat.nodes.items.len) return error.FileNotFound; + const path = cat.nodes.items[@intCast(req.node - 1)]; + var reply: fs.Reply = .{ .tag = req.tag }; + var bytes: []const u8 = ""; + switch (req.op) { + .lookup => { + const next = if (std.mem.eql(u8, req.data, ".")) path else if (std.mem.eql(u8, req.data, "..")) (std.fs.path.dirname(path) orelse "") else try std.fs.path.join(a, &.{ path, req.data }); + reply.attr = try cat.attr(a, next); + }, + .getattr => reply.attr = try cat.attr(a, path), + .setattr => { + if (!std.mem.endsWith(u8, path, "/ctl")) return error.PermissionDenied; + reply.attr = try cat.attr(a, path); + }, + .open => { + if ((req.omode & 3) != 0 and !std.mem.endsWith(u8, path, "/ctl")) return error.PermissionDenied; + if (std.mem.endsWith(u8, path, "/events")) { + var found = false; + for (&cat.cursors[conn.index], 0..) |*cursor, ci| if (!cursor.used) { + cursor.* = .{ .used = true }; + reply.handle = @intCast(ci + 1); + found = true; + break; + }; + if (!found) return error.TooManyEventReaders; + } + }, + .release => { + if (req.opened and req.handle != 0) cat.cursors[conn.index][req.handle - 1].used = false; + }, + .write => unreachable, + .read => { + if (cat.sitePath(path)) |p| { + if (std.mem.eql(u8, p.rest, "events") and req.handle != 0) { + const cursor = &cat.cursors[conn.index][req.handle - 1]; + if (cursor.pos == cursor.len) { + if (cursor.revision == cat.states[p.i].revision) { + conn.reply(&.{ .tag = req.tag, .status = .again }, ""); + return; + } + const json = try std.json.Stringify.valueAlloc(a, cat.states[p.i], .{}); + if (json.len + 1 > cursor.bytes.len) return error.EventTooLarge; + @memcpy(cursor.bytes[0..json.len], json); + cursor.bytes[json.len] = '\n'; + cursor.len = json.len + 1; + cursor.pos = 0; + cursor.revision = cat.states[p.i].revision; + } + const end = @min(cursor.len, cursor.pos + req.size); + bytes = cursor.bytes[cursor.pos..end]; + cursor.pos = end; + conn.reply(&reply, bytes); + return; + } + if (std.mem.startsWith(u8, p.rest, "public/")) { + const f = try openBeneath(cat.io, cat.sites[p.i].root, p.rest[7..]); + defer f.close(cat.io); + const b = try a.alloc(u8, req.size); + bytes = b[0..try f.readPositionalAll(cat.io, b, req.off)]; + conn.reply(&reply, bytes); + return; + } + } + const all = try cat.content(a, path); + const from = @min(req.off, all.len); + bytes = all[@intCast(from)..@intCast(@min(all.len, from + req.size))]; + }, + .readdir => { + var all: std.ArrayList(u8) = .empty; + const names = try cat.children(a, path); + // fs.Req.off is an ENTRY index, not the wire byte offset. + // The engine consumes compact node/dir/name records and owns + // Stat encoding plus the client's byte-offset cursor. + for (names[@intCast(@min(req.off, names.len))..]) |name| { + if (name.len > 255) continue; + const child_path = try std.fs.path.join(a, &.{ path, name }); + const at = cat.attr(a, child_path) catch continue; + var header: [10]u8 = undefined; + std.mem.writeInt(u64, header[0..8], at.node, .little); + header[8] = @intFromBool(at.dir); + header[9] = @intCast(name.len); + try all.appendSlice(a, &header); + try all.appendSlice(a, name); + if (all.items.len >= req.size) break; + } + bytes = all.items; + }, + } + conn.reply(&reply, bytes); + } +}; + +pub fn safeRelative(path: []const u8) bool { + if (std.fs.path.isAbsolute(path) or std.mem.indexOfScalar(u8, path, 0) != null or std.mem.indexOfScalar(u8, path, '\\') != null) return false; + var parts = std.mem.splitScalar(u8, path, '/'); + while (parts.next()) |p| if (std.mem.eql(u8, p, "..") or std.mem.eql(u8, p, ".")) return false; + return true; +} + +/// Descriptor-relative traversal refuses symlinks in every component, including +/// the leaf. This also prevents TOCTOU escapes when a build replaces a directory. +pub fn openBeneath(io: Io, root: []const u8, relative: []const u8) !Io.File { + if (!safeRelative(relative)) return error.AccessDenied; + var dir = try Io.Dir.cwd().openDir(io, root, .{}); + defer dir.close(io); + var parts = std.mem.tokenizeScalar(u8, relative, '/'); + var part = parts.next(); + while (part) |p| { + part = parts.next(); + if (part == null) return dir.openFile(io, p, .{ .follow_symlinks = false }); + const next = try dir.openDir(io, p, .{ .follow_symlinks = false }); + dir.close(io); + dir = next; + } + return dir.openFile(io, ".", .{ .follow_symlinks = false }); +} + +pub fn mime(path: []const u8) []const u8 { + const ext = std.fs.path.extension(path); + const types = .{ .{ ".html", "text/html; charset=utf-8" }, .{ ".css", "text/css" }, .{ ".js", "text/javascript" }, .{ ".mjs", "text/javascript" }, .{ ".json", "application/json" }, .{ ".svg", "image/svg+xml" }, .{ ".png", "image/png" }, .{ ".jpg", "image/jpeg" }, .{ ".jpeg", "image/jpeg" }, .{ ".gif", "image/gif" }, .{ ".webp", "image/webp" }, .{ ".ico", "image/x-icon" }, .{ ".woff2", "font/woff2" }, .{ ".woff", "font/woff" }, .{ ".pdf", "application/pdf" }, .{ ".wasm", "application/wasm" }, .{ ".txt", "text/plain; charset=utf-8" } }; + inline for (types) |pair| if (std.ascii.eqlIgnoreCase(ext, pair[0])) return pair[1]; + return "application/octet-stream"; +} -- cgit v1.3