From 7a07e24916cd5bc762996d81aa1faf550177cd6f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 12 Oct 2026 17:15:03 -0300 Subject: 9web: serve prefixed sites through the composed 9P namespace Post only the pure site/control/discovery tree, with a separate host-only adapter and no duplicate supervision. Serve websites, metadata and legacy controls through the ordinary 9P mux; retain whole-root filesystem browsing and compatibility paths. Pin public file descriptors across publication. Add opt-in build-time HTML/CSS prefix packaging with separate atomic preview generations and unchanged JavaScript/default output. Optional compatibility ports use ?u16. Deploy all five workstation sites on [::]:8090, with /mnt/9p/9web controls. Document Plan 9 references, companion build commits, shared-origin trust and live validation. Validated catalogue, host/FUSE, HTTP FS, mux, prefix and Chromium suites; original E2E 32 scenarios; all five live sites, blog navigation, IPv4/IPv6 and real cafe build through 9P. --- web/catalog.zig | 91 +++++++++++++++----- web/discover.py | 2 +- web/httpfs.zig | 124 ++++++++++++++++++++++++--- web/main.zig | 132 +++++++---------------------- web/prefix.py | 223 +++++++++++++++++++++++++++++++++++++++++++++++++ web/static/catalog.mjs | 2 +- 6 files changed, 438 insertions(+), 136 deletions(-) create mode 100755 web/prefix.py (limited to 'web') diff --git a/web/catalog.zig b/web/catalog.zig index 87349bb..d375973 100644 --- a/web/catalog.zig +++ b/web/catalog.zig @@ -17,7 +17,8 @@ pub const Site = struct { title: []const u8, cwd: []const u8, root: []const u8, - port: u16, + // 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. @@ -57,6 +58,9 @@ pub const Catalog = struct { 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)), @@ -82,26 +86,40 @@ pub const Catalog = struct { for (s.name) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '-' and ch != '_') return error.InvalidSiteName; if (names.contains(s.name)) return error.DuplicateSite; try names.put(s.name, {}); - for (parsed.value.sites[0..i]) |other| if (other.port == s.port) return error.DuplicatePort; + for (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.host_filesystem = parsed.value.filesystem != null; + 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) !void { - _ = try cat.runner.listen(.{ .unix = socket }, 16); + 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(); } @@ -140,11 +158,14 @@ pub const Catalog = struct { pub fn snapshot(cat: *Catalog, a: std.mem.Allocator) ![]const u8 { cat.mutex.lockUncancelable(cat.io); defer cat.mutex.unlock(cat.io); - const Row = struct { name: []const u8, title: []const u8, root: []const u8, port: u16, build: bool, watch: bool, status: State }; + 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 = if (cat.host_filesystem) "/_9web" else "" }, .{}); + return std.json.Stringify.valueAlloc(a, .{ .sites = rows, .listeners = listeners.value, .discovery_error = cat.discovery_error, .namespace = cat.namespace }, .{}); } fn discoverLoop(cat: *Catalog) void { @@ -155,7 +176,9 @@ pub const Catalog = struct { const pid = std.fmt.allocPrint(a, "{d}", .{std.os.linux.getpid()}) catch return; var ports: std.Io.Writer.Allocating = .init(a); ports.writer.print("{d}", .{cat.http_port}) catch return; - for (cat.sites) |site| ports.writer.print(",{d}", .{site.port}) catch return; + 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); @@ -320,8 +343,6 @@ pub const Catalog = struct { } fn virtualPath(cat: *Catalog, path: []const u8) ?[]const u8 { if (!cat.host_filesystem) return path; - if (std.mem.eql(u8, path, "_9web")) return ""; - if (std.mem.startsWith(u8, path, "_9web/")) return path[6..]; return null; } fn sitePath(cat: *Catalog, raw: []const u8) ?struct { i: usize, rest: []const u8 } { @@ -373,6 +394,7 @@ pub const Catalog = struct { 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, .{}); @@ -399,7 +421,7 @@ pub const Catalog = struct { 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 hostPath(a, raw), .{ .iterate = true }); + 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)); @@ -407,7 +429,7 @@ pub const Catalog = struct { sortNames(names.items); return names.items; }; - if (path.len == 0) try names.appendSlice(a, &.{ "index.html", "sites", "services", "listeners" }) else if (std.mem.eql(u8, path, "sites")) { + 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/")) { @@ -548,12 +570,15 @@ pub const Catalog = struct { }; conn.reply(&.{ .tag = tag, .status = .err, .ename = message }, ""); } - fn hostPath(a: std.mem.Allocator, path: []const u8) ![]const u8 { + 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 hostPath(a, path); + 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; @@ -572,7 +597,7 @@ pub const Catalog = struct { 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 hostPath(a, path), .{ .path_only = true }); + 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; @@ -609,7 +634,7 @@ pub const Catalog = struct { } } fn hostOperation(cat: *Catalog, a: std.mem.Allocator, conn: *Runner.Conn, req: fs.Req, path: []const u8, reply: *fs.Reply) !void { - const full = try hostPath(a, path); + const full = try cat.hostPath(a, path); switch (req.op) { .open => { var slot: usize = 0; @@ -620,7 +645,7 @@ pub const Catalog = struct { 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 hostPath(a, next); + 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); @@ -659,7 +684,7 @@ pub const Catalog = struct { errdefer if (!renamed) { for (replacements.items) |r| A.free(r.name); }; - try Io.Dir.cwd().renamePreserve(full, .cwd(), try hostPath(a, next), cat.io); + 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; @@ -706,7 +731,23 @@ pub const Catalog = struct { .open => { if (req.create) return error.PermissionDenied; if ((req.omode & 3) != 0 and !std.mem.endsWith(u8, path, "/ctl")) return error.PermissionDenied; - if (std.mem.endsWith(u8, path, "/events")) { + 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 }; @@ -719,7 +760,13 @@ pub const Catalog = struct { }, .release => { if (req.remove) return error.PermissionDenied; - if (req.opened and req.handle != 0) cat.cursors[conn.index][req.handle - 1].used = false; + 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 => { @@ -746,8 +793,8 @@ pub const Catalog = struct { return; } if (std.mem.startsWith(u8, p.rest, "public/")) { - const f = try openBeneath(cat.io, cat.sites[p.i].root, p.rest[7..]); - defer f.close(cat.io); + 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); diff --git a/web/discover.py b/web/discover.py index 8c750c1..d3d08be 100644 --- a/web/discover.py +++ b/web/discover.py @@ -78,7 +78,7 @@ def inventory(web_pid=None, web_ports=()): 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 registered HTTP listener') + 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, diff --git a/web/httpfs.zig b/web/httpfs.zig index 8af5793..31c4014 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; @@ -388,6 +470,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; @@ -437,6 +523,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] } }); @@ -463,25 +553,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 9d108c6..9c49b63 100644 --- a/web/main.zig +++ b/web/main.zig @@ -127,6 +127,7 @@ pub fn main(init: std.process.Init) !void { 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) { @@ -136,6 +137,7 @@ pub fn main(init: std.process.Init) !void { \\ [--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 \\ @@ -158,6 +160,10 @@ pub fn main(init: std.process.Init) !void { 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; } @@ -171,7 +177,7 @@ pub fn main(init: std.process.Init) !void { 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)); + 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; } @@ -220,13 +226,13 @@ pub fn main(init: std.process.Init) !void { defer group.cancel(io); try group.concurrent(io, MuxT.readerLoop, .{&m}); - // Each site gets its own HTTP root, preserving /assets and /blog links. - // These listeners belong to this same 9web process, not preview daemons. + // 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(site.port); + 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 }); @@ -435,21 +441,26 @@ fn serveHttp(s: *Session) !void { 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")) { - var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); - defer arena.deinit(); - return respond(&request, try c.snapshot(arena.allocator()), "application/json", .ok); + if (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, 0..) |site, si| if (std.mem.eql(u8, name, site.name)) { - var body_buf: [256]u8 = undefined; - const reader = try request.readerExpectContinue(&body_buf); - const command = reader.allocRemaining(std.heap.page_allocator, .limited(128)) catch return respond(&request, "Command too long\n", "text/plain", .bad_request); - defer std.heap.page_allocator.free(command); - c.control(si, command) catch |err| return respond(&request, @errorName(err), "text/plain", .bad_request); - return respond(&request, "queued\n", "text/plain", .accepted); + 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); } @@ -510,90 +521,11 @@ fn wsSession(s: *Session, request: *std.http.Server.Request) !void { } fn serveSite(s: *Session, request: *std.http.Server.Request, site: catalog.Site) !void { - if (request.head.method != .GET and request.head.method != .HEAD) return respond(request, "Use GET or HEAD\n", "text/plain", .method_not_allowed); - const raw = std.mem.sliceTo(request.head.target, '?'); - if (raw.len == 0 or raw[0] != '/') return respond(request, "Bad path\n", "text/plain", .bad_request); - var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); - defer arena.deinit(); - const a = arena.allocator(); - const decoded = try a.alloc(u8, raw.len); - var n: usize = 0; - var i: usize = 1; - while (i < raw.len) : (i += 1) { - var ch = raw[i]; - if (ch == '%') { - if (i + 2 >= raw.len) return respond(request, "Bad escape\n", "text/plain", .bad_request); - ch = std.fmt.parseInt(u8, raw[i + 1 .. i + 3], 16) catch return respond(request, "Bad escape\n", "text/plain", .bad_request); - if (ch == '/' or ch == '\\') return respond(request, "Bad escape\n", "text/plain", .bad_request); - i += 2; - } - decoded[n] = ch; - n += 1; - } - var rel: []const u8 = decoded[0..n]; - if (!catalog.safeRelative(rel)) return respond(request, "Forbidden path\n", "text/plain", .forbidden); - // Dotfiles and builder/provider configuration are not public documents. - var components = std.mem.tokenizeScalar(u8, rel, '/'); - while (components.next()) |component| if (component[0] == '.' or std.mem.eql(u8, component, "_headers") or std.mem.eql(u8, component, "_redirects")) return respond(request, "Not found\n", "text/plain", .not_found); - var file = catalog.openBeneath(s.app.io, site.root, rel) catch blk: { - // Pretty URLs such as /blog/post resolve to /blog/post.html. - if (std.fs.path.extension(rel).len != 0) return respond(request, "Not found\n", "text/plain", .not_found); - rel = try std.fmt.allocPrint(a, "{s}.html", .{rel}); - break :blk catalog.openBeneath(s.app.io, site.root, rel) catch return respond(request, "Not found\n", "text/plain", .not_found); - }; - var st = try file.stat(s.app.io); - if (st.kind == .directory) { - file.close(s.app.io); - if (!std.mem.endsWith(u8, raw, "/")) { - const location = try std.fmt.allocPrint(a, "{s}/", .{raw}); - return request.respond("", .{ .status = .moved_permanently, .keep_alive = false, .extra_headers = &.{.{ .name = "location", .value = location }} }); - } - rel = try std.fs.path.join(a, &.{ rel, "index.html" }); - file = catalog.openBeneath(s.app.io, site.root, rel) catch return respond(request, "Site has not been built, or index.html is missing.\n", "text/plain", .not_found); - st = try file.stat(s.app.io); - } - defer file.close(s.app.io); - if (st.kind != .file) return respond(request, "Not a regular file\n", "text/plain", .forbidden); - var start: u64 = 0; - var end = st.size; - var partial = false; - var headers = request.iterateHeaders(); - while (headers.next()) |h| if (std.ascii.eqlIgnoreCase(h.name, "range")) { - const text = h.value; - if (!std.mem.startsWith(u8, text, "bytes=") or std.mem.indexOfScalar(u8, text, ',') != null) return respond(request, "Unsupported range\n", "text/plain", .range_not_satisfiable); - const dash = std.mem.indexOfScalar(u8, text[6..], '-') orelse return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); - const left = text[6 .. 6 + dash]; - const right = text[7 + dash ..]; - if (left.len == 0) { - const suffix = std.fmt.parseInt(u64, right, 10) catch return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); - start = st.size - @min(st.size, suffix); - } else { - start = std.fmt.parseInt(u64, left, 10) catch return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); - if (right.len != 0) { - const last = std.fmt.parseInt(u64, right, 10) catch return respond(request, "Bad range\n", "text/plain", .range_not_satisfiable); - end = @min(st.size, last +| 1); - } - } - if (start >= end) return respond(request, "Unsatisfiable range\n", "text/plain", .range_not_satisfiable); - partial = true; - }; - var extra: std.ArrayList(std.http.Header) = .empty; - try extra.appendSlice(a, &.{ .{ .name = "content-type", .value = catalog.mime(rel) }, .{ .name = "cache-control", .value = "no-cache" }, .{ .name = "x-content-type-options", .value = "nosniff" }, .{ .name = "accept-ranges", .value = "bytes" } }); - if (partial) try extra.append(a, .{ .name = "content-range", .value = try std.fmt.allocPrint(a, "bytes {d}-{d}/{d}", .{ start, end - 1, st.size }) }); - if (request.head.method == .HEAD) { - try extra.append(a, .{ .name = "content-length", .value = try std.fmt.allocPrint(a, "{d}", .{end - start}) }); - return request.respond("", .{ .status = if (partial) .partial_content else .ok, .keep_alive = false, .transfer_encoding = .none, .extra_headers = extra.items }); - } - var bw = try request.respondStreaming(&s.body, .{ .content_length = end - start, .respond_options = .{ .status = if (partial) .partial_content else .ok, .keep_alive = false, .extra_headers = extra.items } }); - if (request.head.method != .HEAD) { - var offset = start; - while (offset < end) { - const count: usize = @intCast(@min(end - offset, s.frame.len)); - const got = try file.readPositionalAll(s.app.io, s.frame[0..count], offset); - if (got == 0) return error.EndOfStream; - try bw.writer.writeAll(s.frame[0..got]); - offset += got; - } - } - try bw.end(); + 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"") + 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"") + + def handle_decl(self, decl): + self.parts.append(f"") + + def handle_pi(self, data): + self.parts.append(f"") + + +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/catalog.mjs b/web/static/catalog.mjs index e110dee..2897389 100644 --- a/web/static/catalog.mjs +++ b/web/static/catalog.mjs @@ -42,7 +42,7 @@ export function catalogViews(pathLink) { $('sites-entries').append(tr); } const s = site.status; - row.link.textContent = site.title; row.link.href = url('', site.port); row.root.textContent = site.root; + 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() : '—'; -- cgit v1.3