diff options
Diffstat (limited to 'web')
| -rw-r--r-- | web/catalog.zig | 862 | ||||
| -rw-r--r-- | web/client.zig | 4 | ||||
| -rw-r--r-- | web/discover.py | 93 | ||||
| -rw-r--r-- | web/httpfs.zig | 173 | ||||
| -rw-r--r-- | web/main.zig | 168 | ||||
| -rwxr-xr-x | web/prefix.py | 223 | ||||
| -rw-r--r-- | web/static/app.mjs | 164 | ||||
| -rw-r--r-- | web/static/catalog.mjs | 105 | ||||
| -rw-r--r-- | web/static/client.mjs | 3 | ||||
| -rw-r--r-- | web/static/index.html | 54 | ||||
| -rw-r--r-- | web/static/style.css | 33 |
11 files changed, 1749 insertions, 133 deletions
diff --git a/web/catalog.zig b/web/catalog.zig new file mode 100644 index 0000000..977e0ae --- /dev/null +++ b/web/catalog.zig @@ -0,0 +1,862 @@ +//! 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 features: fs.Features = .{ .create = true, .remove = true, .wstat = true }; +}; +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, + // Optional compatibility listener. New routes can share the gateway port. + port: ?u16 = null, + 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, filesystem: ?[]const u8 = null }; +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, + host_filesystem: bool = false, + export_host: bool = false, + namespace: []const u8 = "", + host_export: ?*Catalog = null, + files: [16][512]?Io.File = @splat(@splat(null)), + reads: [16][32]u64 = @splat(@splat(0)), + read_handles: [16][32]u32 = @splat(@splat(0)), + read_groups: [16][32]Io.Group = @splat(@splat(.init)), + 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 }); + if (parsed.value.filesystem) |root| { + // This is intentionally a whole-host export, not a sandbox/chroot. + if (!std.mem.eql(u8, root, "/")) return error.FilesystemMustBeRoot; + if (Io.Dir.cwd().statFile(io, "/_9web", .{ .follow_symlinks = false })) |_| { + return error.CatalogMountCollision; + } else |err| if (err != error.FileNotFound) return err; + } + // 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 (s.port != null and 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) }; + cat.export_host = parsed.value.filesystem != null; + @memset(cat.states, .{}); + try cat.nodes.append(A, ""); + cat.runner.init(.{ .io = io, .root = 1, .handler = .{ .ctx = cat, .serve = serve, .closed = closed } }); + return cat; + } + + pub fn start(cat: *Catalog, socket: [:0]const u8, env: c9.post.Env, post_name: []const u8) !void { + if (cat.export_host) { + // Only the pure service is posted. The host adapter has its own + // runner, lock and fid/read tables, and NO supervision tasks. + cat.namespace = try std.fmt.allocPrint(A, "/mnt/9p/{s}", .{post_name}); + try cat.runner.listenPosted(env, post_name, 16); + const host = try A.create(Catalog); + host.* = .{ .io = cat.io, .sites = &.{}, .states = &.{}, .state_dir = "", .runner = try A.create(Runner), .host_filesystem = true, .namespace = cat.namespace }; + try host.nodes.append(A, ""); + host.runner.init(.{ .io = cat.io, .root = 1, .handler = .{ .ctx = host, .serve = serve, .closed = closed } }); + cat.host_export = host; + _ = try host.runner.listen(.{ .unix = socket }, 16); + } else { + _ = 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 { + if (cat.host_export) |host| host.stop(); + 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); + return cat.snapshotLocked(a); + } + fn snapshotLocked(cat: *Catalog, a: std.mem.Allocator) ![]const u8 { + 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, .namespace = cat.namespace }, .{}); + } + + 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| if (site.port) |port| { + ports.writer.print(",{d}", .{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 virtualPath(cat: *Catalog, path: []const u8) ?[]const u8 { + if (!cat.host_filesystem) return path; + return null; + } + fn sitePath(cat: *Catalog, raw: []const u8) ?struct { i: usize, rest: []const u8 } { + const path = cat.virtualPath(raw) orelse return null; + 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, raw: []const u8) !fs.Attr { + const path = cat.virtualPath(raw) orelse return cat.hostAttr(a, raw); + var result: fs.Attr = .{ .name = std.fs.path.basename(raw), .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(raw)) |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, raw); + result.size = data.len; + return result; + } + fn content(cat: *Catalog, a: std.mem.Allocator, raw: []const u8) ![]const u8 { + const path = cat.virtualPath(raw) orelse return error.FileNotFound; + if (std.mem.eql(u8, path, "index.html")) return @embedFile("static/index.html"); + if (std.mem.eql(u8, path, "index.json")) return cat.snapshotLocked(a); + 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(raw)) |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, raw: []const u8) ![][]const u8 { + var names: std.ArrayList([]const u8) = .empty; + const path = cat.virtualPath(raw) orelse { + var dir = try Io.Dir.cwd().openDir(cat.io, try cat.hostPath(a, raw), .{ .iterate = true }); + defer dir.close(cat.io); + var it = dir.iterate(); + while (try it.next(cat.io)) |entry| try names.append(a, try a.dupe(u8, entry.name)); + if (raw.len == 0) try names.append(a, "_9web"); + sortNames(names.items); + return names.items; + }; + if (path.len == 0) try names.appendSlice(a, &.{ "index.html", "index.json", "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(raw)) |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; + sortNames(names.items); + return names.items; + } + fn sortNames(names: [][]const u8) void { + std.mem.sort([]const u8, names, {}, struct { + fn less(_: void, x: []const u8, y: []const u8) bool { + return std.mem.lessThan(u8, x, y); + } + }.less); + } + 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(); + if (req.op == .release and req.opened and req.handle != 0) { + cat.mutex.lockUncancelable(cat.io); + const host = req.node > 0 and req.node <= cat.nodes.items.len and cat.virtualPath(cat.nodes.items[@intCast(req.node - 1)]) == null; + var cancel: [32]bool = @splat(false); + if (host) for (cat.reads[conn.index], 0..) |tag, ri| { + cancel[ri] = tag != 0 and cat.read_handles[conn.index][ri] == req.handle; + }; + cat.mutex.unlock(cat.io); + // Flush followed by clunk must interrupt kernel/FUSE reads too, + // not just discard the eventual 9P reply and keep consuming events. + for (cancel, 0..) |yes, ri| if (yes) cat.read_groups[conn.index][ri].cancel(cat.io); + } + // Files may be blocking 9P event streams through FUSE. Never hold the + // catalogue lock while reading/writing them; other connections must run. + if (req.op == .read or req.op == .write) { + cat.mutex.lockUncancelable(cat.io); + const host = req.node > 0 and req.node <= cat.nodes.items.len and cat.virtualPath(cat.nodes.items[@intCast(req.node - 1)]) == null; + const file = if (host and req.handle > 0 and req.handle <= 512) cat.files[conn.index][req.handle - 1] else null; + cat.mutex.unlock(cat.io); + if (host) { + if (req.op == .read) { + cat.startRead(conn, req, file) catch |err| replyError(conn, req.tag, err); + } else cat.hostIo(arena.allocator(), conn, req, file) catch |err| replyError(conn, req.tag, err); + return; + } + } + // 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| replyError(conn, req.tag, err); + } + fn closed(ctx: ?*anyopaque, conn: *Runner.Conn) void { + const cat: *Catalog = @ptrCast(@alignCast(ctx.?)); + for (&cat.read_groups[conn.index]) |*group| group.cancel(cat.io); + cat.read_groups[conn.index] = @splat(.init); + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + cat.reads[conn.index] = @splat(0); + } + fn startRead(cat: *Catalog, conn: *Runner.Conn, req: fs.Req, file: ?Io.File) !void { + const f = file orelse return error.FileNotOpen; + cat.mutex.lockUncancelable(cat.io); + defer cat.mutex.unlock(cat.io); + // Park before dispatch, including retries caused by catalogue changes. + // A completed/flushed request has no ticket and must not be reissued. + _ = conn.hold(&.{ .tag = req.tag, .status = .again }) orelse return; + for (cat.reads[conn.index]) |tag| if (tag == req.tag) return; + var slot: usize = 0; + while (slot < 32 and cat.reads[conn.index][slot] != 0) : (slot += 1) {} + if (slot == 32) return error.TooManyPendingReads; + try cat.read_groups[conn.index][slot].await(cat.io); + cat.read_groups[conn.index][slot] = .init; + // Clunk/flush can release the original handle while this read waits. + const linux = std.os.linux; + const rc = linux.fcntl(f.handle, linux.F.DUPFD_CLOEXEC, 0); + if (linux.errno(rc) != .SUCCESS) return error.TooManyOpenFiles; + const duplicate: Io.File = .{ .handle = @intCast(rc), .flags = f.flags }; + errdefer duplicate.close(cat.io); + cat.reads[conn.index][slot] = req.tag; + cat.read_handles[conn.index][slot] = req.handle; + errdefer cat.reads[conn.index][slot] = 0; + try cat.read_groups[conn.index][slot].concurrent(cat.io, readTask, .{ cat, conn, req, duplicate, slot }); + } + fn readTask(cat: *Catalog, conn: *Runner.Conn, req: fs.Req, file: Io.File, slot: usize) void { + defer file.close(cat.io); + defer { + cat.mutex.lockUncancelable(cat.io); + cat.reads[conn.index][slot] = 0; + cat.mutex.unlock(cat.io); + } + var arena = std.heap.ArenaAllocator.init(A); + defer arena.deinit(); + const b = arena.allocator().alloc(u8, req.size) catch { + conn.replyAsync(&.{ .tag = req.tag, .status = .err, .ename = "OutOfMemory" }, "") catch {}; + return; + }; + const result = file.readPositional(cat.io, &.{b}, req.off) catch |err| switch (err) { + error.Unseekable => file.readStreaming(cat.io, &.{b}) catch |stream_err| switch (stream_err) { + error.EndOfStream => 0, + else => stream_err, + }, + else => err, + }; + if (result) |n| conn.replyAsync(&.{ .tag = req.tag }, b[0..n]) catch {} else |err| conn.replyAsync(&.{ .tag = req.tag, .status = .err, .ename = @errorName(err) }, "") catch {}; + } + fn replyError(conn: *Runner.Conn, tag: u64, err: anyerror) void { + const message = switch (err) { + error.FileNotFound => "not found", + error.AccessDenied, error.PermissionDenied => "permission denied", + error.PathAlreadyExists => "file exists", + else => @errorName(err), + }; + conn.reply(&.{ .tag = tag, .status = .err, .ename = message }, ""); + } + fn hostPath(cat: *Catalog, a: std.mem.Allocator, path: []const u8) ![]const u8 { + if (!safeRelative(path)) return error.AccessDenied; + // Compatibility alias is a namespace mapping, never a second service. + if (std.mem.eql(u8, path, "_9web")) return a.dupe(u8, cat.namespace); + if (std.mem.startsWith(u8, path, "_9web/")) return std.fmt.allocPrint(a, "{s}/{s}", .{ cat.namespace, path[6..] }); + return std.fmt.allocPrint(a, "/{s}", .{path}); + } + fn hostAttr(cat: *Catalog, a: std.mem.Allocator, path: []const u8) !fs.Attr { + const full = try cat.hostPath(a, path); + const st = Io.Dir.cwd().statFile(cat.io, full, .{}) catch |err| blk: { + // Keep dangling symlinks visible; an attempt to open reports the error. + if (err != error.FileNotFound) return err; + break :blk try Io.Dir.cwd().statFile(cat.io, full, .{ .follow_symlinks = false }); + }; + return .{ + .name = std.fs.path.basename(path), + .dir = st.kind == .directory, + .size = st.size, + .mtime = @intCast(@min(std.math.maxInt(u32), @max(0, st.mtime.toSeconds()))), + // The 9P engine has no POSIX uid/group/ACL context. The kernel is + // the authority for this export, operating as the service user. + .mode = if (st.kind == .directory) 0o777 else 0o666, + }; + } + fn hostOpen(cat: *Catalog, a: std.mem.Allocator, path: []const u8, mode: u8, truncate: bool) !Io.File { + // Pin the inode with O_PATH, inspect it without opening a device/FIFO, + // then reopen that exact inode. Symlinks and mount points are followed. + const ref = try Io.Dir.cwd().openFile(cat.io, try cat.hostPath(a, path), .{ .path_only = true }); + defer ref.close(cat.io); + const st = try ref.stat(cat.io); + if (st.kind != .file and st.kind != .directory) return error.SpecialFileNotSupported; + const fd_path = try std.fmt.allocPrintSentinel(a, "/proc/self/fd/{d}", .{ref.handle}, 0); + const linux = std.os.linux; + const flags: linux.O = .{ .ACCMODE = switch (mode & 3) { + 1 => .WRONLY, + 2 => .RDWR, + else => .RDONLY, + }, .CLOEXEC = true, .NONBLOCK = true, .NOCTTY = true, .TRUNC = truncate }; + const rc = linux.open(fd_path, flags, 0); + switch (linux.errno(rc)) { + .SUCCESS => return .{ .handle = @intCast(rc), .flags = .{ .nonblocking = true } }, + .ACCES, .PERM => return error.AccessDenied, + .NOENT => return error.FileNotFound, + .ISDIR => return error.IsDir, + .ROFS => return error.PermissionDenied, + else => return error.HostOpenFailed, + } + } + fn hostIo(cat: *Catalog, a: std.mem.Allocator, conn: *Runner.Conn, req: fs.Req, file: ?Io.File) !void { + const f = file orelse return error.FileNotOpen; + if (req.op == .write) { + f.writePositionalAll(cat.io, req.data, req.off) catch |err| switch (err) { + error.Unseekable => try f.writeStreamingAll(cat.io, req.data), + else => return err, + }; + conn.reply(&.{ .tag = req.tag, .written = @intCast(req.data.len) }, ""); + } else { + const b = try a.alloc(u8, req.size); + // A short read is meaningful for synthetic files and event streams. + const n = try f.readPositional(cat.io, &.{b}, req.off); + conn.reply(&.{ .tag = req.tag }, b[0..n]); + } + } + fn hostOperation(cat: *Catalog, a: std.mem.Allocator, conn: *Runner.Conn, req: fs.Req, path: []const u8, reply: *fs.Reply) !void { + const full = try cat.hostPath(a, path); + switch (req.op) { + .open => { + var slot: usize = 0; + while (slot < 512 and cat.files[conn.index][slot] != null) : (slot += 1) {} + if (slot == 512) return error.TooManyOpenFiles; + var next = path; + const f = if (req.create) blk: { + if (!safeName(req.data)) return error.AccessDenied; + next = try std.fs.path.join(a, &.{ path, req.data }); + if (cat.virtualPath(next) != null) return error.PermissionDenied; + const dest = try cat.hostPath(a, next); + if (req.perm & c9.dmdir != 0) { + try Io.Dir.cwd().createDir(cat.io, dest, .fromMode(req.perm & 0o777)); + break :blk try cat.hostOpen(a, next, req.omode, false); + } + break :blk try Io.Dir.cwd().createFile(cat.io, dest, .{ .read = (req.omode & 3) != 1, .exclusive = true, .permissions = .fromMode(req.perm & 0o777) }); + } else try cat.hostOpen(a, path, req.omode, false); + errdefer f.close(cat.io); + reply.attr = try cat.attr(a, next); + reply.handle = @intCast(slot + 1); + cat.files[conn.index][slot] = f; + }, + .release => { + if (req.opened and req.handle > 0 and req.handle <= 512) { + if (cat.files[conn.index][req.handle - 1]) |f| f.close(cat.io); + cat.files[conn.index][req.handle - 1] = null; + } + if (req.remove) { + if (path.len == 0) return error.PermissionDenied; + const st = try Io.Dir.cwd().statFile(cat.io, full, .{ .follow_symlinks = false }); + if (st.kind == .directory) try Io.Dir.cwd().deleteDir(cat.io, full) else try Io.Dir.cwd().deleteFile(cat.io, full); + } + }, + .setattr => { + if (req.set.name and (req.set.mode or req.set.mtime or req.set.length)) return error.CombinedRenameNotSupported; + if (req.set.name) { + if (path.len == 0 or !safeName(req.data)) return error.PermissionDenied; + const next = try std.fs.path.join(a, &.{ std.fs.path.dirname(path) orelse "", req.data }); + if (cat.virtualPath(next) != null) return error.PermissionDenied; + // Allocate replacement names before changing anything on disk. + var replacements: std.ArrayList(struct { index: usize, name: []const u8 }) = .empty; + for (cat.nodes.items, 0..) |n, ni| { + if (std.mem.eql(u8, n, path) or (std.mem.startsWith(u8, n, path) and n.len > path.len and n[path.len] == '/')) + try replacements.append(a, .{ .index = ni, .name = try std.fmt.allocPrint(A, "{s}{s}", .{ next, n[path.len..] }) }); + } + var renamed = false; + errdefer if (!renamed) { + for (replacements.items) |r| A.free(r.name); + }; + try Io.Dir.cwd().renamePreserve(full, .cwd(), try cat.hostPath(a, next), cat.io); + for (replacements.items) |r| { + A.free(cat.nodes.items[r.index]); + cat.nodes.items[r.index] = r.name; + } + renamed = true; + reply.attr = try cat.attr(a, next); + } else { + // OTRUNC must reach FUSE as an open flag, not ftruncate; + // synthetic 9P ctl files often do not implement setattr. + const f = try cat.hostOpen(a, path, if (req.truncate or req.set.length) 1 else 0, req.truncate); + defer f.close(cat.io); + if (req.set.length and !req.truncate) try f.setLength(cat.io, req.length); + if (req.set.mode) try f.setPermissions(cat.io, .fromMode(req.perm & 0o777)); + if (req.set.mtime) try f.setTimestamps(cat.io, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = @as(i96, req.mtime) * std.time.ns_per_s } } }); + reply.attr = try cat.attr(a, path); + } + }, + else => unreachable, + } + } + fn safeName(name: []const u8) bool { + return name.len > 0 and safeRelative(name) and std.mem.indexOfScalar(u8, name, '/') == null; + } + 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 = ""; + if (cat.virtualPath(path) == null and (req.op == .open or req.op == .release or req.op == .setattr)) { + try cat.hostOperation(a, conn, req, path, &reply); + conn.reply(&reply, ""); + return; + } + 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") or req.set.name or req.set.mode or req.set.mtime or (req.set.length and req.length != 0)) return error.PermissionDenied; + reply.attr = try cat.attr(a, path); + }, + .open => { + if (req.create) return error.PermissionDenied; + if ((req.omode & 3) != 0 and !std.mem.endsWith(u8, path, "/ctl")) return error.PermissionDenied; + const site_path = cat.sitePath(path); + if (site_path) |p| if (std.mem.startsWith(u8, p.rest, "public/")) { + const f = try openBeneath(cat.io, cat.sites[p.i].root, p.rest[7..]); + errdefer f.close(cat.io); + const st = try f.stat(cat.io); + if (st.kind == .directory) { + f.close(cat.io); + } else { + if (st.kind != .file) return error.PermissionDenied; + var slot: usize = 0; + while (slot < 512 and cat.files[conn.index][slot] != null) : (slot += 1) {} + if (slot == 512) return error.TooManyOpenFiles; + cat.files[conn.index][slot] = f; + reply.handle = @intCast(slot + 1); + } + }; + if (site_path != null and std.mem.eql(u8, site_path.?.rest, "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.remove) return error.PermissionDenied; + if (req.opened and req.handle != 0) { + const p = cat.sitePath(path); + if (p != null and std.mem.startsWith(u8, p.?.rest, "public/")) { + if (cat.files[conn.index][req.handle - 1]) |f| f.close(cat.io); + cat.files[conn.index][req.handle - 1] = null; + } else 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/")) { + if (req.handle == 0) return error.FileNotOpen; + const f = cat.files[conn.index][req.handle - 1] orelse return error.FileNotOpen; + 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"; +} diff --git a/web/client.zig b/web/client.zig index 2ec9163..54b010b 100644 --- a/web/client.zig +++ b/web/client.zig @@ -60,6 +60,10 @@ fn fail(message: []const u8) i32 { return -1; } fn submit(request: c9.Client.Request) i32 { + // take() lends reply bytes until its next call. JS has copied the result + // before submitting another request; release that frame now, before a + // full-msize reply arrives. Otherwise push() has less than msize free. + if (client.take() != null) return fail("UnexpectedBufferedReply"); _ = client.submit(request) catch |err| return fail(@errorName(err)); return 0; } diff --git a/web/discover.py b/web/discover.py new file mode 100644 index 0000000..d3d08be --- /dev/null +++ b/web/discover.py @@ -0,0 +1,93 @@ +"""One-shot Linux listener inventory for 9web. Never connects to a listener. + +Only socket-inode ownership establishes a process/listener relationship. Parent +commands can identify a launcher, but never turn an arbitrary Node socket into +HTTP. Output deliberately omits full argv/environment (which may hold secrets). +""" +import ipaddress +import json +import os +import socket +import sys + + +def recognize(argv): + if not argv: + return None + exe = os.path.basename(argv[0]) + args = argv[1:] + if exe in ('miniserve', '9web'): + return exe, 'https' if '--tls-cert' in args else 'http', 'recognized executable' + if exe.startswith('python') and any(args[i:i+2] == ['-m', 'http.server'] for i in range(len(args))): + return 'python http.server', 'http', 'python -m http.server' + if exe == 'typst' and 'watch' in args: + return 'typst watch', 'http', 'typst watch' + # Require a CLI entrypoint, not a substring anywhere in arbitrary arguments. + if exe in ('node', 'nodejs', 'bun') and args: + entry = args[0].replace('\\', '/') + if entry.endswith(('/vite/bin/vite.js', '/vite/bin/vite.mjs')): + return 'vite', 'https' if '--https' in args else 'http', 'Vite CLI entrypoint' + if '/wrangler/' in entry and 'dev' in args: + return 'wrangler dev', 'http', 'Wrangler dev entrypoint' + return None + + +def inventory(web_pid=None, web_ports=()): + owners = {} + for name in os.listdir('/proc'): + if not name.isdecimal(): + continue + base = '/proc/' + name + try: + argv = open(base + '/cmdline', 'rb').read(65536).decode(errors='replace').rstrip('\0').split('\0') + exe = os.path.basename(os.readlink(base + '/exe')) + cwd = os.readlink(base + '/cwd') + fds = os.listdir(base + '/fd') + except (OSError, ValueError): + continue + recognized = recognize([exe] + argv[1:]) + # 9web has both HTTP and 9P sockets. Its own configured listeners are + # supplied separately by the catalogue, never inferred from its name. + if exe == '9web': + recognized = None + for fd in fds: + try: + link = os.readlink(base + '/fd/' + fd) + except OSError: + continue + if link.startswith('socket:['): + owners.setdefault(link[8:-1], []).append(dict( + pid=int(name), process=exe, cwd=cwd, recognized=recognized)) + rows = [] + for table, family in [('tcp', socket.AF_INET), ('tcp6', socket.AF_INET6)]: + try: + lines = open('/proc/net/' + table).readlines()[1:] + except OSError: + continue + for line in lines: + fields = line.split() + if fields[3] != '0A': + continue + raw, port = fields[1].split(':') + packed = b''.join(int(raw[i:i+8], 16).to_bytes(4, sys.byteorder) for i in range(0, len(raw), 8)) + address = socket.inet_ntop(family, packed) + inode = fields[9] + processes = {p['pid']: p for p in owners.get(inode, [])} + recognized = [p for p in processes.values() if p['recognized']] + p = recognized[0] if recognized else next(iter(processes.values()), None) + rec = p['recognized'] if p else None + if web_pid in processes and int(port, 16) in web_ports: + p = processes[web_pid] + rec = ('9web', 'http', '9web known HTTP listener') + rows.append(dict(id=table + '-' + inode, address=address, port=int(port, 16), + protocol=rec[1] if rec else 'unknown', service=rec[0] if rec else None, + pid=p['pid'] if p else None, process=p['process'] if p else None, + cwd=p['cwd'] if p else None, + local=ipaddress.ip_address(address).is_loopback, + evidence=('socket inode ownership; ' + rec[2]) if rec else + ('socket inode ownership; unrecognized protocol' if p else 'process ownership unavailable'))) + return sorted(rows, key=lambda r: (r['port'], r['address'])) + + +if __name__ == '__main__': + print(json.dumps(inventory(int(sys.argv[1]), [int(p) for p in sys.argv[2].split(',')]) if len(sys.argv) == 3 else inventory())) diff --git a/web/httpfs.zig b/web/httpfs.zig index fa783e5..479dd09 100644 --- a/web/httpfs.zig +++ b/web/httpfs.zig @@ -23,9 +23,31 @@ const Request = c9.Client.Request; const max_components = 64; const name_bytes = 8192; +/// A website is a view of a subtree of the SAME upstream namespace. Index and +/// pretty-URL lookup are HTTP policy; all stat/open/read operations remain 9P. +pub fn website(m: anytype, request: *Server.Request, root: []const u8, path: []const u8, big: []u8, small: []u8) void { + var fs: FS(@TypeOf(m.*)) = .{ .m = m, .request = request, .big = big, .small = small, .website_mode = true }; + defer fs.cleanup(); + fs.serveWebsite(root, path) catch |err| switch (err) { + error.Handled => {}, + error.NotFound => fs.fail(.not_found, "Site has not been built, or file is missing.\n"), + error.BadPath => fs.fail(.bad_request, "Invalid path\n"), + error.Upstream, error.Disconnected => fs.fail(.bad_gateway, "9P upstream unavailable\n"), + else => fs.fail(.internal_server_error, "Internal error\n"), + }; +} + pub fn handle(m: anytype, request: *Server.Request, path: []const u8, big: []u8, small: []u8) void { + return handleOptions(m, request, path, big, small, false); +} + +pub fn control(m: anytype, request: *Server.Request, path: []const u8, big: []u8, small: []u8) void { + return handleOptions(m, request, path, big, small, true); +} + +fn handleOptions(m: anytype, request: *Server.Request, path: []const u8, big: []u8, small: []u8, legacy_control: bool) void { const M = @TypeOf(m.*); - var fs: FS(M) = .{ .m = m, .request = request, .big = big, .small = small }; + var fs: FS(M) = .{ .m = m, .request = request, .big = big, .small = small, .legacy_control = legacy_control }; fs.serve(path) catch |err| switch (err) { error.Handled => {}, error.NotFound => fs.fail(.not_found, "No such file or directory\n"), @@ -47,6 +69,9 @@ fn FS(comptime M: type) type { root_fid: ?u32 = null, leaf_fid: ?u32 = null, responded: bool = false, + website_mode: bool = false, + content_type: ?[]const u8 = null, + legacy_control: bool = false, fn rpcSmall(self: *Self, req: Request) !wire.Decoded { var r: M.Rpc = .{ .buf = self.small }; @@ -65,9 +90,10 @@ fn FS(comptime M: type) type { /// A 9P Rerror: map to a status and answer, then stop the request. fn rerr(self: *Self, ename: []const u8) anyerror { + if (self.website_mode and mapError(ename) == .not_found) return error.NotFound; var buf: [160]u8 = undefined; const body = std.fmt.bufPrint(&buf, "{s}\n", .{ename[0..@min(ename.len, 128)]}) catch "error\n"; - self.fail(mapError(ename), body); + self.fail(if (self.legacy_control) .bad_request else mapError(ename), body); return error.Handled; } @@ -136,6 +162,44 @@ fn FS(comptime M: type) type { // -- dispatch ------------------------------------------------------ + fn serveWebsite(self: *Self, root: []const u8, path: []const u8) !void { + if (self.request.head.method != .GET and self.request.head.method != .HEAD) { + self.fail(.method_not_allowed, "Use GET or HEAD\n"); + return; + } + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const a = arena.allocator(); + var storage: [name_bytes]u8 = undefined; + var names: [max_components][]const u8 = undefined; + var n = try decodePath(try std.fmt.allocPrint(a, "{s}/{s}", .{ root, path }), &storage, &names); + // Public documents only; controls and provider configuration must + // never become executable documents through the website view. + for (names[0..n]) |name| if (name[0] == '.' or std.mem.eql(u8, name, "_headers") or std.mem.eql(u8, name, "_redirects")) return error.NotFound; + var st = self.walkTo(names[0..n]) catch |err| blk: { + if (err != error.NotFound or n == 0 or std.fs.path.extension(names[n - 1]).len != 0) return err; + names[n - 1] = try std.fmt.allocPrint(a, "{s}.html", .{names[n - 1]}); + break :blk try self.walkTo(names[0..n]); + }; + if (st.qid.type & c9.qtdir != 0) { + const raw = std.mem.sliceTo(self.request.head.target, '?'); + if (!std.mem.endsWith(u8, raw, "/")) { + const query = self.request.head.target[raw.len..]; + const location = try std.fmt.allocPrint(a, "{s}/{s}", .{ raw, query }); + self.responded = true; + try self.request.respond("", .{ .status = .moved_permanently, .keep_alive = false, .extra_headers = &.{.{ .name = "location", .value = location }} }); + return; + } + if (n == names.len) return error.BadPath; + names[n] = "index.html"; + n += 1; + st = try self.walkTo(names[0..n]); + if (st.qid.type & c9.qtdir != 0) return error.NotFound; + } + self.content_type = @import("catalog.zig").mime(names[n - 1]); + return self.getFile(st); + } + fn serve(self: *Self, path: []const u8) !void { const rel = path[3..]; // after "/fs" const trailing = rel.len != 0 and rel[rel.len - 1] == '/'; @@ -192,17 +256,33 @@ fn FS(comptime M: type) type { if (opened.msg == .rerror) return self.rerr(opened.msg.rerror.ename); if (opened.msg != .ropen) return error.Upstream; - const range = self.parseRange(st.length); + const range = try self.parseRange(st.length); const status: Status = if (range.partial) .partial_content else .ok; var offset: u64 = range.start; + if (self.request.head.method == .HEAD) { + var length_buf: [24]u8 = undefined; + var range_buf: [64]u8 = undefined; + const range_header: []const std.http.Header = if (range.partial) &.{.{ .name = "content-range", .value = try std.fmt.bufPrint(&range_buf, "bytes {d}-{d}/{d}", .{ range.start, range.end - 1, st.length }) }} else &.{}; + var headers: std.ArrayList(std.http.Header) = .empty; + defer headers.deinit(std.heap.page_allocator); + try headers.appendSlice(std.heap.page_allocator, &.{ + .{ .name = "content-type", .value = self.content_type orelse "application/octet-stream" }, + .{ .name = "content-length", .value = try std.fmt.bufPrint(&length_buf, "{d}", .{range.end - range.start}) }, + .{ .name = "accept-ranges", .value = "bytes" }, + .{ .name = "x-content-type-options", .value = "nosniff" }, + }); + try headers.appendSlice(std.heap.page_allocator, range_header); + self.responded = true; + return self.request.respond("", .{ .status = status, .keep_alive = false, .transfer_encoding = .none, .extra_headers = headers.items }); + } const first = try self.readAt(leaf, offset, self.chunk()); if (first.msg == .rerror) return self.rerr(first.msg.rerror.ename); if (first.msg != .rread) return error.Upstream; - const ctype = sniff(first.msg.rread.data); + const ctype = self.content_type orelse sniff(first.msg.rread.data); var cr_buf: [64]u8 = undefined; - var headers: [4]std.http.Header = undefined; + var headers: [5]std.http.Header = undefined; var nh: usize = 0; headers[nh] = .{ .name = "content-type", .value = ctype }; nh += 1; @@ -210,6 +290,8 @@ fn FS(comptime M: type) type { nh += 1; headers[nh] = .{ .name = "cache-control", .value = "no-store" }; nh += 1; + headers[nh] = .{ .name = "x-content-type-options", .value = "nosniff" }; + nh += 1; if (range.partial) { headers[nh] = .{ .name = "content-range", .value = std.fmt.bufPrint(&cr_buf, "bytes {d}-{d}/{d}", .{ range.start, range.end - 1, st.length }) catch return error.Upstream }; nh += 1; @@ -231,7 +313,9 @@ fn FS(comptime M: type) type { if (sent >= limit or data.len == 0) break; offset += data.len; const r = try self.readAt(leaf, offset, self.chunk()); - if (r.msg != .rread) break; + // Do not terminate chunked HTTP successfully after a 9P + // error: the client must detect the incomplete response. + if (r.msg != .rread) return error.Upstream; data = r.msg.rread.data; if (data.len == 0) break; } @@ -294,16 +378,35 @@ fn FS(comptime M: type) type { fn listDir(self: *Self, st: wire.Stat) !void { const html = self.wantsHtml(); + var name_buf: [256]u8 = undefined; + const name = name_buf[0..@min(st.name.len, name_buf.len)]; + @memcpy(name, st.name[0..name.len]); const leaf = self.leaf_fid.?; const opened = try self.rpcSmall(.{ .open = .{ .fid = leaf, .mode = c9.oread } }); if (opened.msg == .rerror) return self.rerr(opened.msg.rerror.ename); if (opened.msg != .ropen) return error.Upstream; - // JSON/HTML is built into `big`; directory data is read into `small`. - var w: std.Io.Writer = .fixed(self.big); + // Read frames must fit the RPC scratch buffer. Stream the listing + // so large directories neither overflow `big` nor appear empty. + const count: u32 = @intCast(@min(self.chunk(), self.small.len - 11)); + var r: M.Rpc = .{ .buf = self.small }; + var got = try self.m.rpc(.{ .read = .{ .fid = leaf, .offset = 0, .count = count } }, &r); + if (got.msg == .rerror) return self.rerr(got.msg.rerror.ename); + if (got.msg != .rread) return error.Upstream; + self.responded = true; + var bw = try self.request.respondStreaming(self.big, .{ .respond_options = .{ + .status = .ok, + .keep_alive = false, + .extra_headers = &.{ + .{ .name = "content-type", .value = if (html) "text/html; charset=utf-8" else "application/json" }, + .{ .name = "cache-control", .value = "no-store" }, + .{ .name = "x-content-type-options", .value = "nosniff" }, + }, + } }); + const w = &bw.writer; if (html) { w.writeAll("<!doctype html><meta charset=utf-8><title>") catch return error.Upstream; - writeHtml(&w, st.name); + writeHtml(w, name); w.writeAll("</title><ul>") catch return error.Upstream; } else { w.writeByte('[') catch return error.Upstream; @@ -311,37 +414,31 @@ fn FS(comptime M: type) type { var offset: u64 = 0; var first = true; while (true) { - var r: M.Rpc = .{ .buf = self.small }; - const got = try self.m.rpc(.{ .read = .{ .fid = leaf, .offset = offset, .count = self.chunk() } }, &r); - if (got.msg != .rread) break; const data = got.msg.rread.data; if (data.len == 0) break; offset += data.len; var rest: []const u8 = data; while (rest.len >= 2) { const size: usize = @as(usize, std.mem.readInt(u16, rest[0..2], .little)) + 2; - if (size > rest.len) break; - const entry = c9.Stat.decode(rest[0..size]) catch break; + if (size > rest.len) return error.Upstream; + const entry = c9.Stat.decode(rest[0..size]) catch return error.Upstream; rest = rest[size..]; if (html) { w.writeAll("<li>") catch return error.Upstream; - writeHtml(&w, entry.name); + writeHtml(w, entry.name); if (entry.qid.type & c9.qtdir != 0) w.writeByte('/') catch {}; w.writeAll("</li>") catch return error.Upstream; } else { if (!first) w.writeByte(',') catch return error.Upstream; first = false; - writeEntryJson(&w, entry) catch return error.Upstream; + writeEntryJson(w, entry) catch return error.Upstream; } } + got = try self.m.rpc(.{ .read = .{ .fid = leaf, .offset = offset, .count = count } }, &r); + if (got.msg != .rread) return error.Upstream; } if (html) w.writeAll("</ul>") catch return error.Upstream else w.writeByte(']') catch return error.Upstream; - self.responded = true; - self.request.respond(w.buffered(), .{ .status = .ok, .keep_alive = false, .extra_headers = &.{ - .{ .name = "content-type", .value = if (html) "text/html; charset=utf-8" else "application/json" }, - .{ .name = "cache-control", .value = "no-store" }, - .{ .name = "x-content-type-options", .value = "nosniff" }, - } }) catch {}; + try bw.end(); } // -- writes -------------------------------------------------------- @@ -375,6 +472,10 @@ fn FS(comptime M: type) type { if (cr.msg != .rcreate) return error.Upstream; } const written = try self.streamBody(self.leaf_fid.?, write_off); + if (self.legacy_control) { + self.fail(.accepted, "queued\n"); + return; + } var buf: [48]u8 = undefined; const body = std.fmt.bufPrint(&buf, "{{\"written\":{d}}}\n", .{written}) catch "{}\n"; self.responded = true; @@ -424,6 +525,10 @@ fn FS(comptime M: type) type { while (true) { const n = br.readSliceShort(cbuf[0..maxw]) catch return error.Upstream; if (n == 0) break; + if (self.legacy_control and total + n > 128) { + self.fail(.bad_request, "Command too long\n"); + return error.Handled; + } var done: usize = 0; while (done < n) { const w = try self.rpcSmall(.{ .write = .{ .fid = fid, .offset = offset + done, .data = cbuf[done..n] } }); @@ -450,25 +555,35 @@ fn FS(comptime M: type) type { } const Range = struct { start: u64, end: u64, partial: bool }; - fn parseRange(self: *Self, length: u64) Range { + fn parseRange(self: *Self, length: u64) !Range { var it = self.request.iterateHeaders(); while (it.next()) |h| { if (!std.ascii.eqlIgnoreCase(h.name, "range")) continue; - if (!std.mem.startsWith(u8, h.value, "bytes=")) break; + if (!std.mem.startsWith(u8, h.value, "bytes=")) return self.badRange(); const spec = h.value[6..]; - const dash = std.mem.indexOfScalar(u8, spec, '-') orelse break; - const start = std.fmt.parseInt(u64, spec[0..dash], 10) catch break; + const dash = std.mem.indexOfScalar(u8, spec, '-') orelse return self.badRange(); + if (dash == 0) { + const suffix = std.fmt.parseInt(u64, spec[1..], 10) catch return self.badRange(); + if (suffix == 0 or length == 0) return self.badRange(); + return .{ .start = length - @min(length, suffix), .end = length, .partial = true }; + } + const start = std.fmt.parseInt(u64, spec[0..dash], 10) catch return self.badRange(); var end: u64 = length; if (dash + 1 < spec.len) { - const e = std.fmt.parseInt(u64, spec[dash + 1 ..], 10) catch break; - end = @min(e + 1, length); + const e = std.fmt.parseInt(u64, spec[dash + 1 ..], 10) catch return self.badRange(); + end = @min(e +| 1, length); } - if (start >= length or start >= end) break; + if (start >= length or start >= end) return self.badRange(); return .{ .start = start, .end = end, .partial = true }; } return .{ .start = 0, .end = length, .partial = false }; } + fn badRange(self: *Self) anyerror { + self.fail(.range_not_satisfiable, "Unsatisfiable range\n"); + return error.Handled; + } + fn wantsFollow(self: *Self) bool { if (std.mem.indexOf(u8, self.request.head.target, "follow=1") != null) return true; return self.acceptHas("text/event-stream"); diff --git a/web/main.zig b/web/main.zig index 7222d91..9c49b63 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), @@ -95,7 +99,7 @@ fn parseUpstream(allocator: std.mem.Allocator, text: []const u8) !mux.Address { if (std.mem.startsWith(u8, text, "tcp:")) return .{ .network = .{ .tcp = try Io.net.IpAddress.parseLiteral(text[4..]) } }; if (std.mem.startsWith(u8, text, "unix:")) - return .{ .network = .{ .unix = try allocator.dupeZ(u8, text[5..]) } }; + return .{ .network = .{ .unix = try allocator.dupeSentinel(u8, text[5..], 0) } }; if (std.mem.startsWith(u8, text, "file:")) return .{ .file = text[5..] }; return error.InvalidUpstream; @@ -105,7 +109,7 @@ fn parseServe(allocator: std.mem.Allocator, text: []const u8) !c9.transport.Addr if (std.mem.startsWith(u8, text, "tcp:")) return .{ .tcp = try Io.net.IpAddress.parseLiteral(text[4..]) }; if (std.mem.startsWith(u8, text, "unix:")) - return .{ .unix = try allocator.dupeZ(u8, text[5..]) }; + return .{ .unix = try allocator.dupeSentinel(u8, text[5..], 0) }; return error.InvalidServe; } @@ -120,6 +124,11 @@ 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 post_name: []const u8 = "9web"; + var explicit_upstream = false; var i: usize = 1; while (i < args.len) : (i += 1) { if (std.mem.eql(u8, args[i], "--help")) { @@ -127,6 +136,9 @@ 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] + \\ [--post NAME] (pure service name, default 9web, with filesystem /) + \\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 +148,40 @@ 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, "--post")) { + post_name = 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), init.minimal.environ.block.slice.ptr, post_name); + 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); @@ -151,7 +192,7 @@ pub fn main(init: std.process.Init) !void { }; defer m.stop(); - var listener = c9.transport.listen(io, .{ .tcp = address }, max_connections) catch |err| { + var listener = listenHttp(io, address) catch |err| { if (err == error.AddressInUse) { std.debug.print("9web: cannot listen on {s}: address already in use.\nUse --listen 127.0.0.1:0 to select a free port; the URL is printed at startup.\n", .{listen_text}); } else std.debug.print("9web: cannot listen on {s}: {s}\n", .{ listen_text, @errorName(err) }); @@ -165,14 +206,18 @@ pub fn main(init: std.process.Init) !void { if ((!std.mem.eql(u8, origin_uri.scheme, "https") and !std.mem.eql(u8, origin_uri.scheme, "http")) or origin_uri.host == null or origin_uri.user != null or origin_uri.password != null or 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 }, .{}); + const browser_config = if (cat != null) + try std.json.Stringify.valueAlloc(allocator, .{ .user = user, .tree = tree, .catalog = true }, .{}) + else + 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 +226,19 @@ pub fn main(init: std.process.Init) !void { defer group.cancel(io); try group.concurrent(io, MuxT.readerLoop, .{&m}); + // Optional compatibility listeners; prefix routes need no extra port. + if (cat) |c| { + for (c.sites, 0..) |site, si| { + const port = site.port orelse continue; + const l = try allocator.create(Io.net.Server); + var bind_address = address; + bind_address.setPort(port); + l.* = try listenHttp(io, bind_address); + 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 +274,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 +284,54 @@ pub fn main(init: std.process.Init) !void { } } +fn listenHttp(io: Io, address: Io.net.IpAddress) !Io.net.Server { + // HTTP's short connections leave TIME_WAIT sockets behind. Reuse addresses + // across service restarts, but do not share a live port between instances. + if (@import("builtin").os.tag != .linux) + return address.listen(io, .{ .kernel_backlog = max_connections }); + // std.Io's reuse_address also sets REUSEPORT. Configure only REUSEADDR + // before bind, then hand the socket back to std.Io for cancellable accepts. + const linux = std.os.linux; + var addr: std.c.sockaddr.storage = undefined; + var len = c9.transport.ipSockaddr(address, &addr); + const rc = linux.socket(addr.family, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, linux.IPPROTO.TCP); + if (linux.errno(rc) != .SUCCESS) return error.Socket; + const fd: i32 = @intCast(rc); + errdefer _ = linux.close(fd); + const on: c_int = 1; + if (linux.errno(linux.setsockopt(fd, linux.SOL.SOCKET, linux.SO.REUSEADDR, @ptrCast(&on), @sizeOf(c_int))) != .SUCCESS) + return error.SocketOption; + switch (linux.errno(linux.bind(fd, @ptrCast(&addr), len))) { + .SUCCESS => {}, + .ADDRINUSE => return error.AddressInUse, + else => return error.Bind, + } + if (linux.errno(linux.listen(fd, max_connections)) != .SUCCESS) return error.Listen; + if (linux.errno(linux.getsockname(fd, @ptrCast(&addr), &len)) != .SUCCESS) return error.SocketAddress; + var bound = address; + const actual: *const std.c.sockaddr.in = @ptrCast(&addr); // port has the same offset in sockaddr_in6 + bound.setPort(std.mem.bigToNative(u16, actual.port)); + return .{ .socket = .{ .handle = fd, .address = bound }, .options = {} }; +} + +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 +425,51 @@ 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.startsWith(u8, path, "/sites/")) { + const rest = path[7..]; + const end = std.mem.indexOfScalar(u8, rest, '/') orelse rest.len; + for (c.sites) |site| if (std.mem.eql(u8, rest[0..end], site.name)) { + return serveSitePath(s, &request, site, rest[end..]); + }; + return respond(&request, "Unknown site\n", "text/plain", .not_found); + } + if (std.mem.eql(u8, path, "/_cloud9/catalog.json")) { + if (request.head.method != .GET and request.head.method != .HEAD) return respond(&request, "Use GET\n", "text/plain", .method_not_allowed); + var route: [1024]u8 = undefined; + return httpfs.handle(app.m, &request, try std.fmt.bufPrint(&route, "/fs{s}/index.json", .{c.namespace}), &s.frame, &s.body); + } + 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) |site| if (std.mem.eql(u8, name, site.name)) { + var route: [1024]u8 = undefined; + request.head.method = .PUT; + return httpfs.control(app.m, &request, try std.fmt.bufPrint(&route, "/fs{s}/sites/{s}/ctl", .{ c.namespace, name }), &s.frame, &s.body); + }; + 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); } @@ -340,6 +483,7 @@ fn serveHttp(s: *Session) !void { return respond(&request, "Use GET\n", "text/plain", .method_not_allowed); if (std.mem.eql(u8, path, "/_cloud9/config.json")) return respond(&request, config.browser_config, "application/json", .ok); if (std.mem.eql(u8, path, "/_cloud9/app.mjs")) return respond(&request, @embedFile("static/app.mjs"), "text/javascript; charset=utf-8", .ok); + if (std.mem.eql(u8, path, "/_cloud9/catalog.mjs")) return respond(&request, @embedFile("static/catalog.mjs"), "text/javascript; charset=utf-8", .ok); if (std.mem.eql(u8, path, "/_cloud9/client.mjs")) return respond(&request, @embedFile("static/client.mjs"), "text/javascript; charset=utf-8", .ok); if (std.mem.eql(u8, path, "/_cloud9/style.css")) return respond(&request, @embedFile("static/style.css"), "text/css; charset=utf-8", .ok); if (std.mem.eql(u8, path, "/_cloud9/cloud9.wasm")) return respond(&request, @embedFile("client.wasm"), "application/wasm", .ok); @@ -375,3 +519,13 @@ fn wsSession(s: *Session, request: *std.http.Server.Request) !void { } } } + +fn serveSite(s: *Session, request: *std.http.Server.Request, site: catalog.Site) !void { + return serveSitePath(s, request, site, std.mem.sliceTo(request.head.target, '?')); +} + +fn serveSitePath(s: *Session, request: *std.http.Server.Request, site: catalog.Site, path: []const u8) !void { + var root_buf: [1024]u8 = undefined; + const root = try std.fmt.bufPrint(&root_buf, "{s}/sites/{s}/public", .{ s.app.catalog.?.namespace, site.name }); + httpfs.website(s.app.m, request, root, path, &s.frame, &s.body); +} diff --git a/web/prefix.py b/web/prefix.py new file mode 100755 index 0000000..06bf497 --- /dev/null +++ b/web/prefix.py @@ -0,0 +1,223 @@ +#!/usr/bin/env python3 +"""Optional build-time relocation of generated static output, never HTTP rewriting. + +HTML URL attributes and CSS URLs/URL selectors only. JavaScript, JSON, authored +text and normal build output are untouched. This is not a JavaScript compiler: +sites constructing absolute asset URLs in JS need their own base-path option. +Published previews are symlinks to complete generations; old generations are +retained so open readers and a failed next build cannot lose their output. +""" +import argparse +from html.parser import HTMLParser +import html as html_lib +from pathlib import Path +import os +import re +import shutil +import tempfile + + +def check_prefix(prefix): + if not re.fullmatch(r"/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]+/?", prefix): + raise ValueError("prefix must be an absolute path of named segments, e.g. /sites/cafe/") + return prefix.rstrip("/") + + +def url(value, prefix): + if value.startswith("/") and not value.startswith("//"): + return prefix + value + return value + + +def string_end(text, start): + quote = text[start] + i = start + 1 + while i < len(text): + if text[i] == "\\": + i += 2 + elif text[i] == quote: + return i + 1 + else: + i += 1 + raise ValueError("unterminated CSS string") + + +def css(text, prefix): + parts, i = [], 0 + while i < len(text): + start = i + if text.startswith("/*", i): + end = text.find("*/", i + 2) + i = len(text) if end < 0 else end + 2 + parts.append(text[start:i]) + elif text[i:i+4].lower() == "url(" and (i == 0 or not (text[i-1].isalnum() or text[i-1] in "_-")): + i += 4 + while i < len(text) and text[i].isspace(): + i += 1 + if i < len(text) and text[i] in "\"'": + end = string_end(text, i) + parts.extend((text[start:i+1], url(text[i+1:end-1], prefix), text[end-1:end])) + i = end + else: + end = text.find(")", i) + if end < 0: + raise ValueError("unterminated CSS url()") + parts.extend((text[start:i], url(text[i:end], prefix))) + i = end + elif text[i] in "\"'": + i = string_end(text, i) + # URL attribute selectors must continue matching relocated links. + context = text[:start] + is_url = re.search(r"(?:@import\s*|\[\s*(?:href|src|action)\s*=\s*)$", context, re.I) + value = text[start+1:i-1] + parts.append(text[start] + (url(value, prefix) if is_url else value) + text[i-1]) + else: + parts.append(text[i]) + i += 1 + return "".join(parts) + + +ATTR = re.compile(r"([\w:-]+)(\s*=\s*)(\"[^\"]*\"|'[^']*'|[^\s>]+)") +URL_ATTRS = {"href", "src", "poster", "action", "formaction", "cite", "data", "xlink:href"} + + +def srcset(text, prefix): + # Data URLs may contain commas; do not blindly split every comma. + parts, i = [], 0 + while i < len(text): + start = i + while i < len(text) and (text[i].isspace() or text[i] == ','): + i += 1 + parts.append(text[start:i]) + start = i + while i < len(text) and not text[i].isspace(): + i += 1 + value = text[start:i] + parts.append(url(value, prefix)) + if value.endswith(','): + continue + start = i + depth = 0 + while i < len(text): + ch = text[i] + if ch == '(': + depth += 1 + elif ch == ')': + depth -= 1 + i += 1 + if ch == ',' and depth == 0: + break + parts.append(text[start:i]) + return ''.join(parts) + + +class Document(HTMLParser): + def __init__(self, prefix): + super().__init__(convert_charrefs=False) + self.prefix, self.parts, self.raw = prefix, [], None + + def tag(self, tag, closed=False): + def attr(m): + name, eq, raw = m.groups() + quote = raw[0] if raw[0] in "\"'" else "" + value = raw[1:-1] if quote else raw + original = value + decoded = html_lib.unescape(value) + if name.lower() in URL_ATTRS: + value = url(decoded, self.prefix) + elif name.lower() == "style": + value = css(decoded, self.prefix) + elif name.lower() == "srcset": + value = srcset(decoded, self.prefix) + else: + return m[0] + value = html_lib.escape(value, quote=True) if value != decoded else original + return name + eq + quote + value + quote + self.parts.append(ATTR.sub(attr, self.get_starttag_text())) + if not closed and tag in ("script", "style"): + self.raw = tag + + def handle_starttag(self, tag, attrs): + self.tag(tag) + + def handle_startendtag(self, tag, attrs): + self.tag(tag, True) + + def handle_endtag(self, tag): + self.parts.append(f"</{tag}>") + if tag == self.raw: + self.raw = None + + def handle_data(self, data): + self.parts.append(css(data, self.prefix) if self.raw == "style" else data) + + def handle_entityref(self, name): + self.parts.append(f"&{name};") + + def handle_charref(self, name): + self.parts.append(f"&#{name};") + + def handle_comment(self, data): + self.parts.append(f"<!--{data}-->") + + def handle_decl(self, decl): + self.parts.append(f"<!{decl}>") + + def handle_pi(self, data): + self.parts.append(f"<?{data}>") + + +def html(text, prefix): + parser = Document(prefix) + parser.feed(text) + parser.close() + return "".join(parser.parts) + + +def publish(source, output, prefix): + prefix = check_prefix(prefix) + source, output = Path(source).resolve(), Path(output).absolute() + if not (source / "index.html").is_file(): + raise ValueError("source must contain an already-built index.html") + if source == output.resolve() or source in output.resolve().parents: + raise ValueError("preview output must be outside the normal build output") + if output.exists() and not output.is_symlink(): + raise ValueError("refusing to replace a non-symlink output; choose a new preview path") + output.parent.mkdir(parents=True, exist_ok=True) + stage = Path(tempfile.mkdtemp(prefix=f".{output.name}-build-", dir=output.parent)) + link = stage.with_name(stage.name + ".link") + try: + for path in source.rglob("*"): + if path.is_symlink(): + raise ValueError(f"symlink in build output: {path}") + target = stage / path.relative_to(source) + if path.is_dir(): + target.mkdir(exist_ok=True) + elif path.is_file(): + target.parent.mkdir(parents=True, exist_ok=True) + if path.suffix.lower() in (".html", ".css"): + text = path.read_text(encoding="utf-8") + transform = html if path.suffix.lower() == ".html" else css + target.write_text(transform(text, prefix), encoding="utf-8") + else: + shutil.copy2(path, target) + link.symlink_to(stage.name, target_is_directory=True) + os.replace(link, output) + except BaseException: + link.unlink(missing_ok=True) + shutil.rmtree(stage) + raise + print(f"preview: {output} at {prefix}/ (normal output unchanged)") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--url-prefix", required=True) + parser.add_argument("--output", required=True) + parser.add_argument("source") + args = parser.parse_args() + publish(args.source, args.output, args.url_prefix) + + +if __name__ == "__main__": + main() diff --git a/web/static/app.mjs b/web/static/app.mjs index 429bef7..33d5a08 100644 --- a/web/static/app.mjs +++ b/web/static/app.mjs @@ -1,8 +1,40 @@ import { Client } from './client.mjs'; +import { catalogViews } from './catalog.mjs'; const $ = id => document.getElementById(id); const text = new TextDecoder('utf-8', { fatal: true }); let client = null, current = null, currentPath = '/', busy = false, dirty = false; let configuration = null; +let activeView = 'files'; +const catalog = catalogViews(pathLink); + +async function loadConfiguration() { + if (!configuration) { + const response = await fetch('/_cloud9/config.json'); + if (!response.ok) throw new Error('Unable to load server settings. Reload to try again.'); + configuration = await response.json(); + for (const view of ['sites', 'services']) $(`view-${view}`).hidden = !configuration.catalog; + } + return configuration; +} +function locationView() { + const view = new URLSearchParams(location.search).get('view'); + return configuration?.catalog && ['sites', 'services'].includes(view) ? view : 'files'; +} +function viewURL(view, path = currentPath) { + return pathURL(path) + (view === 'files' ? (current?.stream && path === currentPath ? '?follow=1' : '') : `?view=${view}`); +} +function showView(view) { + activeView = view; + for (const name of ['files', 'sites', 'services']) { + $(`${name}-view`).hidden = view !== name; + const link = $(`view-${name}`); + link.classList.toggle('selected', view === name); + if (view === name) link.setAttribute('aria-current', 'page'); else link.removeAttribute('aria-current'); + link.href = viewURL(name); + } + document.title = `${view === 'files' ? currentPath : view} — 9web`; + catalog.show(view); +} function status(message, error = false) { $('status').textContent = message; @@ -11,13 +43,13 @@ function status(message, error = false) { function controls() { for (const id of ['go', 'reload', 'newfile', 'newdir', 'upload']) $(id).disabled = busy; $('save').disabled = busy || !current || current.stat.directory || $('editor').hidden || !dirty; - $('download').disabled = busy || !current || current.stat.directory; + $('download').disabled = busy || !current || current.stat.directory || current.stream; $('follow').disabled = busy || !current || current.stat.directory; $('rename').disabled = busy || currentPath === '/'; $('delete').disabled = busy || currentPath === '/'; $('editor').readOnly = busy; $('edit-status').textContent = dirty ? 'Unsaved changes.' : 'No unsaved changes.'; - document.querySelector('main').setAttribute('aria-busy', String(busy)); + document.querySelector('#files-view main').setAttribute('aria-busy', String(busy)); } async function action(operation) { if (busy) return; @@ -29,23 +61,19 @@ async function action(operation) { } async function connectedClient() { if (client && !client.closed) return client; - if (!configuration) { - const response = await fetch('/_cloud9/config.json'); - if (!response.ok) throw new Error('Unable to load server settings. Reload to try again.'); - configuration = await response.json(); - } + await loadConfiguration(); try { client = await Client.connect('/_cloud9/9p', configuration.user, configuration.tree); } catch { throw new Error('Unable to reach the file server. Reload to try again.'); } return client; } -async function readPath(path) { +async function readPath(path, metadataOnly = false) { const active = await connectedClient(); - try { return await active.readPath(path); } + try { return await active.readPath(path, metadataOnly); } catch (error) { // A timed-out/closed transport has lost its fids. Retry reads once on a // fresh session. Writes are never replayed after an uncertain result. if (!active.closed) throw error; - return (await connectedClient()).readPath(path); + return (await connectedClient()).readPath(path, metadataOnly); } } function normalizePath(path) { @@ -103,16 +131,18 @@ function breadcrumbs(path) { $('up').href = pathURL(parent); $('up').dataset.path = parent; } -async function navigate(path, historyMode = 'push') { +async function navigate(path, historyMode = 'push', follow = false) { path = normalizePath(path); + showView('files'); stopFollow(); status('Loading…'); - const result = await readPath(path); + const result = await readPath(path, follow); current = result; currentPath = path; dirty = false; + for (const view of ['files', 'sites', 'services']) $(`view-${view}`).href = viewURL(view); $('path').value = path; - const url = pathURL(path); + const url = pathURL(path) + (follow ? '?follow=1' : ''); if (historyMode === 'push' && location.pathname + location.search + location.hash !== url) history.pushState(null, '', url); - document.title = `${path} — cloud9`; + document.title = `${path} — 9web`; breadcrumbs(path); $('directory').hidden = !result.stat.directory; $('file').hidden = result.stat.directory; @@ -124,6 +154,11 @@ async function navigate(path, historyMode = 'push') { const permissions = document.createElement('td'), name = document.createElement('td'), size = document.createElement('td'); permissions.className = 'mode'; permissions.textContent = mode(entry); name.append(pathLink(entry.name + (entry.directory ? '/' : ''), child(path, entry.name), 'entry' + (entry.directory ? ' directory' : ''))); + if (!entry.directory) { + const followLink = pathLink('follow', child(path, entry.name), 'follow-link'); + followLink.dataset.follow = 'true'; followLink.href += '?follow=1'; + name.append(followLink); + } size.className = 'size'; size.textContent = entry.directory ? '—' : entry.length.toLocaleString(); row.append(permissions, name, size); rows.append(row); } @@ -133,7 +168,7 @@ async function navigate(path, historyMode = 'push') { } else { $('filename').textContent = result.stat.name; $('file-mode').textContent = mode(result.stat); - $('file-size').textContent = `${result.bytes.length.toLocaleString()} bytes`; + $('file-size').textContent = result.stream ? 'stream' : `${result.bytes.length.toLocaleString()} bytes`; let editable = true; try { if (result.bytes.includes(0)) throw new Error('Binary'); @@ -145,20 +180,31 @@ async function navigate(path, historyMode = 'push') { status('File loaded.'); } updateStat(result); - markTree(path); + if (follow && !result.stat.directory) startFollow(); } function mayLeave() { return !dirty || window.confirm('Discard unsaved changes?'); } document.addEventListener('click', event => { + const tab = event.target.closest('a[data-view]'); + if (tab && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey && !event.altKey) { + event.preventDefault(); + if (busy || tab.hidden) return; + const view = tab.dataset.view; + if (view === activeView) return; + history.pushState(null, '', viewURL(view)); + showView(view); + if (view === 'files' && !current) action(() => navigate(currentPath, 'none')); + return; + } const link = event.target.closest('a[data-path]'); if (!link || event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return; event.preventDefault(); - if (!busy && mayLeave()) action(() => navigate(link.dataset.path)); + if (!busy && mayLeave()) action(() => navigate(link.dataset.path, 'push', link.dataset.follow === 'true')); }); $('path-form').onsubmit = event => { event.preventDefault(); if (mayLeave()) action(() => navigate($('path').value)); }; -$('reload').onclick = () => { if (mayLeave()) action(() => navigate(current ? currentPath : locationPath(), 'none')); }; +$('reload').onclick = () => { if (mayLeave()) action(() => navigate(current ? currentPath : locationPath(), 'none', Boolean(current?.stream))); }; $('editor').oninput = () => { dirty = true; controls(); }; $('save').onclick = () => action(async () => { const bytes = new TextEncoder().encode($('editor').value); @@ -180,27 +226,23 @@ $('download').onclick = () => { setTimeout(() => URL.revokeObjectURL(url), 1000); }; window.addEventListener('popstate', () => { - if (busy || !mayLeave()) { history.pushState(null, '', pathURL(currentPath)); return; } - action(() => navigate(locationPath(), 'none')); + const view = locationView(), path = locationPath(); + if (busy || (view === 'files' && path !== currentPath && !mayLeave())) { history.pushState(null, '', viewURL(activeView)); return; } + showView(view); + const follow = new URLSearchParams(location.search).get('follow') === '1'; + if (view === 'files' && (!current || path !== currentPath || Boolean(current.stream) !== follow)) action(() => navigate(path, 'none', follow)); }); window.addEventListener('beforeunload', event => { if (dirty) { event.preventDefault(); event.returnValue = ''; } }); -// ---- HTTP /fs view: tree, stat panel, create/rename/delete/upload, follow ---- +// ---- HTTP /fs view: stat panel, create/rename/delete/upload, follow ---- // These operate over the gateway's HTTP mapping of the same 9P tree, so they // stay independent of the WebSocket editing session above. function fsURL(path) { return '/fs' + path.split('/').map(encodeURIComponent).join('/'); } function parentOf(path) { return path.replace(/\/[^/]+$/, '') || '/'; } function baseName(path) { return path.split('/').filter(Boolean).pop() || ''; } function dirContext() { return current && current.stat.directory ? currentPath : parentOf(currentPath); } -function sortEntries(a, b) { return Number(b.dir) - Number(a.dir) || a.name.localeCompare(b.name); } - -async function fetchDir(path) { - const response = await fetch(fsURL(path) + (path.endsWith('/') ? '' : '/'), { headers: { accept: 'application/json' } }); - if (!response.ok) throw new Error(`list failed: ${response.status}`); - return response.json(); -} function renderStat(rows) { const dl = $('stat'); @@ -233,72 +275,37 @@ function updateStat(result) { }).catch(() => {}); } -function treeItem(entry, path) { - const li = document.createElement('li'); - const node = document.createElement('span'); - node.className = 'node' + (entry.dir ? ' dir' : ''); - node.dataset.node = path; - const twist = document.createElement('span'); - twist.className = 'twist'; - twist.textContent = entry.dir ? '▸' : ' '; - node.append(twist, document.createTextNode(entry.name)); - li.append(node); - node.onclick = () => { - if (entry.dir) toggleTree(li, path, twist); - else if (!busy && mayLeave()) action(() => navigate(path)); - }; - return li; -} -async function toggleTree(li, path, twist) { - const open = li.querySelector(':scope > ul'); - if (open) { open.remove(); twist.textContent = '▸'; return; } - twist.textContent = '▾'; - const ul = document.createElement('ul'); - li.append(ul); - try { - for (const entry of (await fetchDir(path)).sort(sortEntries)) ul.append(treeItem(entry, child(path, entry.name))); - } catch (error) { ul.textContent = '(unavailable)'; } -} -async function loadTree() { - const root = $('filetree'); - try { - const entries = (await fetchDir('/')).sort(sortEntries); - root.replaceChildren(...entries.map(entry => treeItem(entry, '/' + entry.name))); - } catch { /* upstream not ready; leave the last tree in place */ } -} -function markTree(path) { - for (const node of $('filetree').querySelectorAll('.node.current')) node.classList.remove('current'); - const match = $('filetree').querySelector(`.node[data-node="${CSS.escape(path)}"]`); - if (match) match.classList.add('current'); -} let stream = null; -function stopFollow() { +function stopFollow(clear = true) { if (stream) { stream.close(); stream = null; } $('follow').classList.remove('active'); - $('stream').hidden = true; - $('stream').textContent = ''; + if (clear) { $('stream').hidden = true; $('stream').textContent = ''; } } function startFollow() { if (!current || current.stat.directory) return; + current.stream = true; + history.replaceState(null, '', viewURL('files')); + controls(); $('editor').hidden = true; $('binary').hidden = true; $('edit-actions').hidden = true; const view = $('stream'); view.hidden = false; view.textContent = ''; $('follow').classList.add('active'); + status('Following stream…'); stream = new EventSource(fsURL(currentPath) + '?follow=1'); stream.onmessage = event => { view.textContent += event.data + '\n'; view.scrollTop = view.scrollHeight; }; - stream.addEventListener('eof', stopFollow); - stream.onerror = () => { status('Stream ended.'); stopFollow(); }; + stream.addEventListener('eof', () => { stopFollow(false); status('End of stream.'); }); + stream.onerror = () => { status('Stream ended.'); stopFollow(false); }; } $('follow').onclick = () => { - if ($('follow').classList.contains('active')) { stopFollow(); action(() => navigate(currentPath, 'none')); } + if ($('follow').classList.contains('active')) { stopFollow(false); status('Stream stopped.'); } else startFollow(); }; async function mutate(fn, done) { if (busy) return; busy = true; controls(); - try { await fn(); await loadTree(); status(done); } + try { await fn(); status(done); } catch (error) { status(error.message, true); } finally { busy = false; controls(); } } @@ -357,4 +364,13 @@ $('upload-input').onchange = () => { $('upload-input').value = ''; }; -action(() => navigate(locationPath(), 'none')).then(loadTree); +async function start() { + try { + await loadConfiguration(); + currentPath = locationPath(); + const view = locationView(); + showView(view); + if (view === 'files') await action(() => navigate(currentPath, 'none', new URLSearchParams(location.search).get('follow') === '1')); + } catch (error) { status(error.message, true); } +} +start(); diff --git a/web/static/catalog.mjs b/web/static/catalog.mjs new file mode 100644 index 0000000..2897389 --- /dev/null +++ b/web/static/catalog.mjs @@ -0,0 +1,105 @@ +// Sites and services are views of the same 9web application. File/log links +// use app.mjs navigation; control writes use the normal HTTP-to-9P mapping. +const $ = id => document.getElementById(id); +const element = (tag, text = '') => { const e = document.createElement(tag); e.textContent = text; return e; }; +function url(address, port, protocol = 'http') { + let host = address; + if (!host || host === '0.0.0.0' || host === '::') host = location.hostname; + host = host.replace(/^\[|\]$/g, ''); + return `${protocol}://${host.includes(':') ? `[${host}]` : host}:${port}/`; +} + +export function catalogViews(pathLink) { + let active = 'files', timer = null, request = null, revision = 0, pending = new Set(); + const rows = new Map(); + let namespace = ''; + function status(view, text, error = false) { + const p = $(`${view}-status`); p.textContent = text; p.classList.toggle('error', error); + } + async function control(site, command) { + if (pending.has(site.name)) return; + pending.add(site.name); updateSite(site); + try { + const r = await fetch(`/fs${namespace}/sites/${encodeURIComponent(site.name)}/ctl`, { method: 'PUT', body: command }); + if (!r.ok) throw new Error(await r.text()); + status('sites', `${site.title}: ${command} requested.`); + } catch (error) { status('sites', error.message, true); } + finally { pending.delete(site.name); updateSite(site); if (active === 'sites') await refresh(false); } + } + function updateSite(site) { + let row = rows.get(site.name); + if (!row) { + const tr = element('tr'); tr.dataset.site = site.name; + const name = element('td'), phase = element('td'), finished = element('td'), actions = element('td'); + const link = element('a'); link.className = 'site-name'; + const root = element('span'); root.className = 'site-root'; name.append(link, root); + const tools = element('div'); tools.className = 'site-actions'; + const build = element('button', 'build'), watch = element('button', 'watch'), cancel = element('button', 'cancel'); + for (const b of [build, watch, cancel]) b.type = 'button'; + tools.append(build, watch, cancel, pathLink('files', `${namespace}/sites/${site.name}/public`), pathLink('log', `${namespace}/sites/${site.name}/log`)); + actions.append(tools); tr.append(name, phase, finished, actions); + row = { tr, link, root, phase, finished, build, watch, cancel }; rows.set(site.name, row); + $('sites-entries').append(tr); + } + const s = site.status; + row.link.textContent = site.title; row.link.href = site.port ? url('', site.port) : `/sites/${encodeURIComponent(site.name)}/`; row.root.textContent = site.root; + row.phase.textContent = `${s.phase}${s.watching ? ' · watch on' : ''}${s.queued ? ' · queued' : ''}${s.exit_code !== null ? ` · exit ${s.exit_code}` : ''}`; + row.phase.className = `site-phase${s.phase === 'failed' ? ' failed' : ''}`; + row.finished.textContent = s.finished ? new Date(s.finished * 1000).toLocaleString() : '—'; + row.build.disabled = pending.has(site.name) || !site.build; + row.watch.disabled = pending.has(site.name) || !site.watch; + row.watch.textContent = s.watching ? 'stop watching' : 'watch'; + row.cancel.disabled = pending.has(site.name) || (!s.pid && !s.queued && !s.watching); + row.build.onclick = () => control(site, 'build'); + row.watch.onclick = () => control(site, s.watching ? 'watch off' : 'watch on'); + row.cancel.onclick = () => control(site, 'cancel'); + } + function listenerRow(item, sites) { + const tr = element('tr'), name = element('td'); + if (item.protocol !== 'unknown') { + const site = item.service === '9web' && sites.find(s => s.port === item.port); + const a = element('a', site ? site.title : item.service); a.href = url(item.address, item.port, item.protocol); name.append(a); + } else name.textContent = item.process || 'Unknown'; + const address = element('td', `${item.address.includes(':') ? `[${item.address}]` : item.address}:${item.port}${item.local ? ' · local only' : ''}`); + address.className = 'listener-address'; + tr.append(name, address, element('td', item.pid ? `${item.process || ''} (${item.pid})` : 'Unavailable'), element('td', item.evidence)); + return tr; + } + async function refresh(showCount = true) { + if (active === 'files' || document.hidden) return; + request?.abort(); request = new AbortController(); + const ticket = ++revision, view = active; + $(`${view}-reload`).disabled = true; + try { + const r = await fetch('/_cloud9/catalog.json', { signal: request.signal }); + if (!r.ok) throw new Error(`Unable to load ${view}: HTTP ${r.status}`); + const data = await r.json(); + if (ticket !== revision || active !== view) return; + namespace = data.namespace || ''; + if (view === 'sites') { + const names = new Set(data.sites.map(s => s.name)); + for (const [name, row] of rows) if (!names.has(name)) { row.tr.remove(); rows.delete(name); } + for (const site of data.sites) updateSite(site); + $('sites-empty').hidden = data.sites.length !== 0; + if (showCount) status(view, `${data.sites.length} ${data.sites.length === 1 ? 'site' : 'sites'} · generated files are served continuously`); + } else { + const known = data.listeners.filter(x => x.protocol !== 'unknown'), unknown = data.listeners.filter(x => x.protocol === 'unknown'); + $('services-entries').replaceChildren(...known.map(x => listenerRow(x, data.sites))); + $('other-listeners-entries').replaceChildren(...unknown.map(x => listenerRow(x, data.sites))); + $('services-empty').hidden = known.length !== 0; + $('other-listeners-title').textContent = `Other listeners (${unknown.length})`; + status(view, data.discovery_error || `${known.length} HTTP services · refreshed every 5 seconds`, Boolean(data.discovery_error)); + } + } catch (error) { if (error.name !== 'AbortError' && ticket === revision) status(view, error.message, true); } + finally { if (ticket === revision) $(`${view}-reload`).disabled = false; } + } + function show(view) { + clearInterval(timer); request?.abort(); revision++; + active = view; + for (const name of ['sites', 'services']) $(`${name}-reload`).disabled = false; + if (view !== 'files') { refresh(); timer = setInterval(refresh, 5000); } + } + for (const name of ['sites', 'services']) $(`${name}-reload`).onclick = () => refresh(); + document.addEventListener('visibilitychange', () => { if (!document.hidden) refresh(); }); + return { show }; +} diff --git a/web/static/client.mjs b/web/static/client.mjs index 69f44d2..c012fc0 100644 --- a/web/static/client.mjs +++ b/web/static/client.mjs @@ -96,9 +96,10 @@ export class Client { if (live && !this.closed) await this.ask('clunk', 1); } } - readPath(path) { + readPath(path, metadataOnly = false) { return this.serial(() => this.withFile(path, async () => { const stat = JSON.parse(decoder.decode((await this.ask('stat', 1)).data)); + if (metadataOnly && !stat.directory) return { stat, entries: [], bytes: new Uint8Array(), stream: true }; const opened = await this.ask('open', 1, 0); const count = Math.min(this.wasm.max_read(), opened.number || Infinity); let offset = 0; diff --git a/web/static/index.html b/web/static/index.html index b012b3c..c392e65 100644 --- a/web/static/index.html +++ b/web/static/index.html @@ -3,17 +3,22 @@ <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> - <title>cloud9 — files</title> + <title>9web — files</title> <link rel="stylesheet" href="/_cloud9/style.css"> <script type="module" src="/_cloud9/app.mjs"></script> </head> <body> <header> - <a class="brand" href="/" data-path="/">cloud9</a> + <a class="brand" href="/" data-path="/">9web</a> <div class="description">A web interface to your file server.</div> </header> - <nav class="toolbar" aria-label="File actions"> - <a class="selected" href="/" data-path="/">files</a> + <nav class="toolbar" id="views" aria-label="Views"> + <a class="selected" id="view-files" href="/" data-view="files" aria-current="page">files</a> + <a id="view-sites" href="/?view=sites" data-view="sites" hidden>sites</a> + <a id="view-services" href="/?view=services" data-view="services" hidden>services</a> + </nav> + <div id="files-view"> + <nav class="toolbar file-tools" aria-label="File actions"> <button id="reload" type="button">reload</button> <button id="newfile" type="button">new file</button> <button id="newdir" type="button">new folder</button> @@ -28,10 +33,6 @@ </form> </nav> <div class="layout"> - <aside id="sidebar" aria-label="Tree"> - <div class="tree-head">tree</div> - <ul id="filetree" role="tree"></ul> - </aside> <main> <nav id="breadcrumbs" aria-label="Breadcrumb"><a href="/" data-path="/">root</a></nav> <div class="summary"> @@ -66,6 +67,41 @@ </section> </main> </div> - <footer>served by cloud9</footer> + </div> + <main id="sites-view" class="catalog-view" aria-label="Sites" hidden> + <div class="summary"> + <p id="sites-status" role="status" aria-live="polite">Loading sites…</p> + <button id="sites-reload" type="button">reload</button> + </div> + <div class="table-scroll"> + <table> + <thead><tr><th scope="col">Site</th><th scope="col">Status</th><th scope="col">Last build</th><th scope="col">Actions</th></tr></thead> + <tbody id="sites-entries"></tbody> + </table> + </div> + <p id="sites-empty" class="muted" hidden>No sites configured.</p> + </main> + <main id="services-view" class="catalog-view" aria-label="Services" hidden> + <div class="summary"> + <p id="services-status" role="status" aria-live="polite">Loading services…</p> + <button id="services-reload" type="button">reload</button> + </div> + <p class="muted">Listeners identified from process metadata, without endpoint probes. Local-only links work on this computer.</p> + <div class="table-scroll"> + <table> + <thead><tr><th scope="col">Service</th><th scope="col">Address</th><th scope="col">Process</th><th scope="col">Evidence</th></tr></thead> + <tbody id="services-entries"></tbody> + </table> + </div> + <p id="services-empty" class="muted" hidden>No HTTP services identified.</p> + <details id="other-listeners"> + <summary id="other-listeners-title">Other listeners</summary> + <div class="table-scroll"><table> + <thead><tr><th scope="col">Process</th><th scope="col">Address</th><th scope="col">PID</th><th scope="col">Evidence</th></tr></thead> + <tbody id="other-listeners-entries"></tbody> + </table></div> + </details> + </main> + <footer>served by 9web</footer> </body> </html> diff --git a/web/static/style.css b/web/static/style.css index 965406b..e17f1e5 100644 --- a/web/static/style.css +++ b/web/static/style.css @@ -8,6 +8,7 @@ body { margin: 0; padding: 22px 3%; } a { color: #07539b; text-decoration: none; } a:hover { text-decoration: underline; } +.follow-link { margin-left: 14px; font-size: 11px; color: #777; } header { padding: 0 8px 17px; } .brand { font-size: 30px; font-weight: bold; color: #222; letter-spacing: -1px; } .description { color: #777; margin-top: 1px; } @@ -26,6 +27,21 @@ header { padding: 0 8px 17px; } font-size: 15px; } .toolbar .selected { background: #ccc; color: #111; } +.file-tools { border-bottom: 1px solid #ddd; padding-top: 7px; } +.catalog-view { padding-top: 13px; } +.catalog-view .summary p { margin: 0; color: #777; } +.catalog-view .summary .error { color: #a22; } +.catalog-view td { padding-top: 8px; padding-bottom: 8px; } +.catalog-view .site-name { font-weight: bold; } +.catalog-view .site-root { display: block; color: #777; font: 12px/1.6 monospace; overflow-wrap: anywhere; } +.catalog-view .site-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 5px 9px; font-size: 12px; } +.catalog-view .site-phase { white-space: nowrap; } +.catalog-view .site-phase.failed { color: #a22; } +.catalog-view .listener-address { font-family: monospace; overflow-wrap: anywhere; } +.catalog-view .muted { color: #777; font-size: 12px; } +.table-scroll { overflow-x: auto; } +#other-listeners { margin-top: 20px; } +#other-listeners summary { cursor: pointer; padding-bottom: 8px; color: #777; } button, input, textarea { font: inherit; color: inherit; } button { cursor: pointer; @@ -66,17 +82,7 @@ textarea { display: block; width: 100%; min-height: 420px; resize: vertical; bor #binary { color: #777; padding: 16px 8px; } .actions { display: flex; align-items: center; gap: 12px; margin-top: 10px; font-size: 12px; } #edit-status { color: #777; } -.layout { display: flex; align-items: flex-start; gap: 16px; } -#sidebar { flex: 0 0 220px; border-right: 1px solid #e2e2e2; padding: 8px 10px 0 0; min-width: 0; } -.tree-head { color: #999; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; padding: 4px; } -#filetree, #filetree ul { list-style: none; margin: 0; padding: 0; } -#filetree ul { margin-left: 12px; } -#filetree li { line-height: 1.7; white-space: nowrap; } -#filetree .node { cursor: pointer; font-family: monospace; overflow-wrap: anywhere; } -#filetree .node.dir { font-weight: bold; } -#filetree .node.current { background: #ccc; } -#filetree .twist { display: inline-block; width: 12px; color: #999; } -.layout main { flex: 1 1 auto; min-width: 0; } +.layout main { min-width: 0; } #stat-panel { margin-top: 18px; border-top: 1px solid #eee; padding-top: 8px; } #stat-panel h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: #999; margin: 0 0 6px; } #stat { display: grid; grid-template-columns: max-content 1fr; gap: 2px 12px; font: 12px monospace; margin: 0; } @@ -90,6 +96,9 @@ footer { margin: 30px 8px 0; padding-top: 7px; border-top: 1px solid #ddd; text- a:focus-visible, button:focus-visible { outline: 2px solid #07539b; outline-offset: 2px; } [hidden] { display: none !important; } @media (max-width: 600px) { + #sites-view .site-root, #sites-view th:nth-child(3), #sites-view td:nth-child(3) { display: none; } + .catalog-view .site-phase { white-space: normal; } + #services-view table { min-width: 600px; } body { padding: 14px 10px; } header { padding-left: 3px; } .brand { font-size: 26px; } @@ -104,6 +113,4 @@ a:focus-visible, button:focus-visible { outline: 2px solid #07539b; outline-offs .summary { align-items: start; } .file-heading { gap: 7px 12px; } textarea { min-height: 350px; } - .layout { flex-direction: column; } - #sidebar { flex: none; width: 100%; border-right: 0; border-bottom: 1px solid #e2e2e2; max-height: 180px; overflow: auto; padding: 0 0 6px; } } |
