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. --- build.zig | 2 + docs/9web.service | 2 +- docs/catalog.example.json | 5 +- docs/catalog.md | 130 ++++++++++++++------- docs/namespace-sites-validation.md | 74 ++++++++++++ test/web/catalog.py | 18 +++ test/web/catalog_ui.mjs | 29 ++++- test/web/host_fs.py | 8 +- test/web/prefix.py | 73 ++++++++++++ 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 +- 15 files changed, 730 insertions(+), 185 deletions(-) create mode 100644 docs/namespace-sites-validation.md create mode 100644 test/web/prefix.py create mode 100755 web/prefix.py diff --git a/build.zig b/build.zig index 20ed0ce..be9c929 100644 --- a/build.zig +++ b/build.zig @@ -40,6 +40,8 @@ pub fn build(b: *std.Build) void { }) }); bridge.root_module.addAnonymousImport("client.wasm", .{ .root_source_file = wasm.getEmittedBin() }); b.installArtifact(bridge); + b.installFile("web/prefix.py", "bin/9web-prefix"); + b.step("prefix-test", "Test opt-in generated-site URL relocation").dependOn(&b.addSystemCommand(&.{ "python3", "test/web/prefix.py" }).step); if (target.result.os.tag == .linux) { const catalog_test = b.addSystemCommand(&.{ "python3", "test/web/catalog.py" }); catalog_test.addArtifactArg(bridge); diff --git a/docs/9web.service b/docs/9web.service index 7109935..a861671 100644 --- a/docs/9web.service +++ b/docs/9web.service @@ -3,7 +3,7 @@ Description=9web filesystem, sites and services After=network.target [Service] -ExecStart=%h/.local/bin/9ns --mntgen -- %h/.local/libexec/9web --listen [::]:8090 --catalog %h/.config/9web/sites.json --catalog-socket %t/9web/catalog.sock --state-dir %h/.local/state/9web +ExecStart=%h/.local/bin/9ns --mntgen -- %h/.local/libexec/9web --listen [::]:8090 --catalog %h/.config/9web/sites.json --catalog-socket %t/9web/catalog.sock --state-dir %h/.local/state/9web --post 9web Restart=on-failure RestartSec=2 RuntimeDirectory=9web diff --git a/docs/catalog.example.json b/docs/catalog.example.json index c5970f3..c20c54a 100644 --- a/docs/catalog.example.json +++ b/docs/catalog.example.json @@ -4,9 +4,8 @@ "name": "notes", "title": "My notes", "cwd": "/home/me/notes", - "root": "/home/me/notes/site/out", - "port": 8091, - "build": ["just", "site"], + "root": "/home/me/.local/state/9web/public/notes", + "build": ["python3", "site/build.py", "--url-prefix", "/sites/notes/", "--prefix-output", "/home/me/.local/state/9web/public/notes"], "inputs": ["index.typ", "lib", "notes", "site/build.py", "site/site.css"] } ] diff --git a/docs/catalog.md b/docs/catalog.md index 74062bb..bc308c2 100644 --- a/docs/catalog.md +++ b/docs/catalog.md @@ -8,8 +8,10 @@ unchanged; catalogue mode and an explicit upstream are mutually exclusive. Set the top-level registry field `"filesystem": "/"` to expose the whole workstation in the Files tab. The real `/` remains the root; the catalogue is -mounted at `/_9web`. Startup refuses an existing real `/_9web` rather than hiding -it. Without this opt-in field, the previous catalogue-only tree is unchanged. +posted separately as `9web`, visible at `/mnt/9p/9web` under `9ns --mntgen`. +`/_9web` is a compatibility path alias to that mount, not another backend. +Startup refuses an existing real `/_9web` rather than hiding it. +Without this opt-in field, the previous catalogue-only tree is unchanged. Only `/` is accepted: this export is not a restricted directory sandbox. Host files support reading, editing, uploads, mkdir, rename and removal through @@ -30,7 +32,9 @@ discovered by 9ns in the usual way. ## Configuration and serving See [catalog.example.json](catalog.example.json). Every site has a unique `name`, -`title`, absolute `cwd` and `root`, and a distinct nonzero HTTP `port`. Optional +`title`, absolute `cwd` and `root`. Sites live at `/sites//` on the gateway +port. An optional `port` (`?u16`, omitted by default) retains the old separate +listener for explicit compatibility; it must be nonzero and unique. Optional `build` and `watch` values are argument arrays, not shell command strings. Use an explicit `sh -c` only when a trusted recipe needs shell syntax. @@ -47,8 +51,11 @@ than advertising a site that is not actually served. All site listeners use the dashboard's bind address. `[::]` uses a dual-stack listener for all IPv6 and IPv4 interfaces; `0.0.0.0` restricts this to IPv4. -Each site occupies its own HTTP origin so root-relative `/assets/` and `/blog/` -URLs work without HTML rewriting. Directory URLs redirect to a trailing slash +HTTP roots map into `/mnt/9p/9web/sites//public` in the service namespace. +Website responses use the same mux, 9P walks, opens and reads as `/fs`, not a +second disk-serving implementation. Root-relative sites need an **opt-in build +prefix**, described below; there is no response rewriting or Referer/cookie +routing. Directory URLs redirect to a trailing slash and serve `index.html`; extensionless paths try `.html`. Static serving supports MIME types, HEAD, single byte ranges, and streaming large files. Symlink traversal, dotfiles, `..`, and encoded path separators are refused. Directory listings are @@ -100,25 +107,64 @@ Each new job replaces `/.log`. The UI and 9P expose its last Watch state and build status reset at service restart; generated output remains. No build or watch is started automatically. -Builders currently write to their configured output directories in place. -`9web` does **not** provide atomic publication or a last-good snapshot: a failed -build can leave partial output, and a file truncated during a response closes that -response. Builders needing atomic publication should generate a separate release -directory and arrange publication themselves. The served root can be a symlink; -symlinks below that root are refused. +Generic builders may still write in place. The optional prefix packaging step +below publishes complete preview generations instead. Public files retain their +disk descriptors for the lifetime of an open 9P fid, including across publication. +The served root can be a symlink; symlinks below that root are refused. -## 9P and HTTP controls +## Opt-in prefix builds + +`zig build` installs `zig-out/bin/9web-prefix` (Python 3). Put it on the build +process's PATH; the installed service uses `~/.local/bin/9web-prefix`. +The site's build option is **unset by default**. Authored content, source links, +scripts and default output URLs do not acquire a local prefix. + +The workstation build entry points accept: + +``` +--url-prefix /sites/NAME/ --prefix-output /absolute/preview/output +``` -The catalogue is itself a 9P backend, consumed by the ordinary gateway mux. -`--serve` and WebSocket clients see the same tree as direct socket clients: +- Entomology/Standards: `python3 site/build.py ...` +- Café: `mise exec -- sh build/build.sh ...` +- Pardes website: `mise exec -- python3 build/build.py ...` (also its build task) +- Café hub archive: `python3 build/static.py ...` + +They run the normal build, then package a **separate** output copy. For initial +publication of an existing build, add `--reuse-output` to skip compilation. +9web's configured build recipes pass the prefix explicitly; managed watch calls +the same recipe. The archive remains read-only in the deployed catalogue. + +The shared packaging tool only relocates generated HTML URL attributes and CSS +URLs/imports/URL selectors. Relative URLs, external URLs, fragments, text/code +examples, JavaScript and JSON are preserved. It does not pretend to understand +arbitrary JavaScript-generated URLs; future sites using those need their own +build-time base-path support. Worker/API routes are not implemented locally. + +Preview output is an atomically replaced symlink to a complete generation in +the same parent directory. A failed compile/package leaves the prior preview +published; deleted assets do not survive in a new generation. Old generation +directories are retained (no automatic garbage collection), so repeated builds +consume disk space. Normal output is never overwritten by prefix packaging. + +## 9P and HTTP controls -With `"filesystem": "/"`, prefix every catalogue path below with `/_9web`. -For example, the control URL becomes `/fs/_9web/sites/entomology/ctl` and its -mounted path is `/mnt/9p/web/_9web/sites/entomology/ctl`. UI links and controls +There is one supervisor in the 9web process, with a pure 9P service for site +files, controls and discovery. In host mode this is posted through the ordinary +per-user registry as `$XDG_RUNTIME_DIR/9p/9web` (`--post NAME` changes the name). +A host-only export has a separate runner, mutex, fid tables and pending reads; +it has no build/discovery loops. It exports the process's composed `/` through +the private `--catalog-socket` to the gateway mux. The host export is **not** +posted back inside its own mount, avoiding recursive exposure. + +With `"filesystem": "/"`, prefix every pure-service path below with `/mnt/9p/9web`. +For example, the control URL becomes `/fs/mnt/9p/9web/sites/entomology/ctl` and its +mounted path is `/mnt/9p/9web/sites/entomology/ctl`. UI links and controls select the prefix automatically from the catalogue snapshot's `namespace`. ``` /index.html +/index.json catalogue snapshot (sites, state, discovery) /sites//public/ complete generated output tree (read-only) /sites//ctl build | watch on | watch off | cancel /sites//status JSON state, PID, exit code, times, generation @@ -129,10 +175,10 @@ select the prefix automatically from the catalogue snapshot's `namespace`. ``` ``` -9ns --unix "$XDG_RUNTIME_DIR/9web/catalog.sock" --name web -- sh -echo build > /mnt/9p/web/sites/entomology/ctl -cat /mnt/9p/web/sites/entomology/status -cat /mnt/9p/web/sites/entomology/log +9ns --mntgen -- sh +echo build > /mnt/9p/9web/sites/entomology/ctl +cat /mnt/9p/9web/sites/entomology/status +cat /mnt/9p/9web/sites/entomology/log ``` Event opens receive the current state, then park until its revision changes. @@ -141,10 +187,11 @@ durable history of every transition. 9P flush cancels parked reads. State events also work through `/fs/sites//events?follow=1` as SSE. `status` is the finite snapshot to use for ordinary polling. -The sites and services views read `GET /_cloud9/catalog.json` while visible. +The sites and services views read `GET /_cloud9/catalog.json` while visible; +this is an HTTP-to-9P mapping to the posted service's `index.json`. Build controls use `PUT /fs/sites//ctl`, through the same HTTP-to-9P mapping as file editing. The earlier `POST /_cloud9/sites//ctl` endpoint remains -compatible for existing clients. Logs use the normal file viewer rather than a +compatible for existing clients and also writes the 9P control file. Logs use the normal file viewer rather than a separate dialog. Polling preserves site-row focus and pauses in background tabs. ## Passive discovery @@ -152,7 +199,7 @@ separate dialog. Polling preserves site-row focus and pauses in background tabs. Every five seconds, an embedded Python 3 inventory helper reads `/proc/net/tcp*`, joins socket inodes to process descriptors, and recognizes executable/argument patterns for miniserve, Python `http.server`, Typst watch, Vite and Wrangler dev. -9web advertises only its explicitly registered HTTP ports: its 9P listener is +9web identifies only its own known HTTP listening ports: its 9P listener is never labelled HTTP just because the owning executable is 9web. No TCP connections or HTTP probes are performed. Unknown or inaccessible process @@ -187,13 +234,16 @@ and the configured project build tools in PATH. Miniserve remains independent. This is a trusted-network operator interface: it has **no login or TLS**. Anyone who can reach the dashboard can read the catalogue and trigger configured jobs. +Sites under URL prefixes share the operator interface's origin: site scripts +can access filesystem writes and build controls. Prefixes are **not security +isolation**; only serve trusted code here. With `"filesystem": "/"`, they can additionally read or modify any file the service user can access, including credentials, configuration and source code, and operate control files under `/mnt/9p`. This can amount to arbitrary code execution as that user. Only enable this export for clients you fully trust. -Browser control requests reject foreign Origins; numeric Host validation and -separate site origins prevent served pages from using the dashboard as a same-origin -control API. This is not authentication. Bind to loopback or place an authenticated +Browser control requests reject foreign Origins and unexpected Hosts, but served +sites on this same origin are trusted clients too. This is not authentication. +Bind to loopback or place an authenticated gateway in front of it when those network permissions are inappropriate. No arbitrary command can be supplied to the catalogue build-control parser itself; its commands come from the local registry. A whole-host writable export removes @@ -246,8 +296,8 @@ There are three separate mechanisms, not one registration service: `$XDG_RUNTIME_DIR/9p/`. 9ns lists the registry and connects on a walk. No request is made to 9web; publication lifetime belongs to the producer. * Website roots and build recipes in this catalogue are startup configuration - in `sites.json`, not self-registration. The private catalogue socket itself - currently uses `listen`, not `listenPosted`. + in `sites.json`, not self-registration. The shared supervisor posts its pure + service using `listenPosted`; the private host adapter uses `listen`. * HTTP listener discovery observes `/proc` socket ownership and process metadata. It neither registers services nor probes HTTP endpoints. @@ -260,13 +310,12 @@ Original `mntgen` creates mountpoints on demand; 9ns's registry auto-connection is additional policy. Explicit configuration is not itself contrary to Plan 9: `ROOT/docs/man/6/namespace` specifies configured mount/bind/import composition. -The coherent next design step, **not implemented here**, is to expose a pure -site/build-control 9P view and compose it into the consumer's namespace, with -HTTP serving those same paths. Shared supervision can remain in one process; -no extra daemon is required per static site. Do not post the current mixed -host/catalogue root back under its own `/mnt/9p`: that creates recursion and -possible self-dependent waits. Arbitrary-upstream composition and unifying -the separate disk-based static-site HTTP handler remain outstanding. +The pure site/build-control view is now composed into the consumer's namespace, +and HTTP serves those same paths. Shared supervision remains in one process; +no extra daemon is required per static site. Only that pure view is posted, +never the host export. This also follows `ROOT/docs/man/8/httpd`: a namespace +webroot and explicit internal URL mappings, not a separate registration API. +Composition with an arbitrary explicit upstream remains outside this mode. ## Installed workstation configuration @@ -275,14 +324,17 @@ existing `/_cloud9/` asset/protocol URLs and library module names are retained for compatibility; they are not a different application name. The workstation registry enables `"filesystem": "/"`. Files opens the host -root, including the service's live `/mnt/9p` registry mount; the virtual catalogue -is under `/_9web`. The unit wraps the separately installed 9web binary with 9ns. +root, including the service's live `/mnt/9p` registry mount; the pure catalogue +is under `/mnt/9p/9web` (old `/_9web` paths still work). The unit wraps the separately installed 9web binary with 9ns. Ordinary `--upstream` mode is unchanged. Composition with an arbitrary explicit upstream is still not implemented; this mode composes the host filesystem with the catalogue, and existing 9P servers are available through `/mnt/9p`. -The initial workstation setup uses dashboard port 8090 and site ports 8091–8095: +The workstation now uses a **single port, 8090**, for the dashboard and sites: Entomology lab, 0x4200.cafe, Standards, Pardes website, and the older café hub archive. +The prior 8091–8095 listeners are no longer configured. Site URLs are +`/sites/entomology/`, `/sites/cafe/`, `/sites/standards/`, `/sites/pardes/`, and +`/sites/cafe-hub/`. Prefix output lives in `~/.local/state/9web/public/`. The archive is read-only (no build recipe registered). Other sites have explicit local build recipes and opt-in managed watches. Port 8081 remains miniserve's. Runtime config is `~/.config/9web/sites.json`; the service uses a separately diff --git a/docs/namespace-sites-validation.md b/docs/namespace-sites-validation.md new file mode 100644 index 0000000..64858fc --- /dev/null +++ b/docs/namespace-sites-validation.md @@ -0,0 +1,74 @@ +# Namespace-backed sites: implementation and validation + +## Design + +One 9web process supervises builds and posts a pure service as `9web` in the +normal per-user 9P registry. A separate host-only export consumes its composed +namespace through 9ns. Website HTTP, Files, catalogue metadata and controls use +the same 9P mux. The host export is never posted back into itself. + +Plan 9 references reviewed locally in Principia Softwarica: `srv(3)`, +`namespace(6)`, `mntgen(4)`, `exportfs(4)`, and `httpd(8)` (namespace webroot and +internal URL mappings). A conversational critique review caught public-file +reopening across publication and two HTML prefix parsing edge cases; all have +regression tests. Explicit recipe configuration remains policy, not a +self-registration protocol. Unix listener posting is a Linux adaptation of +channel publication, not identical `/srv` semantics. + +## Deployment + +- Enabled user unit `9web.service`, wrapped with `9ns --mntgen`. +- Sole HTTP listener `[::]:8090`, reachable using LAN IPv4 and IPv6. +- Existing 8091–8095 site listeners removed from configuration; miniserve untouched. +- Whole `/`, including `/mnt/9p`, retained in Files; pure service at `/mnt/9p/9web`. +- URLs `/sites/{entomology,cafe,standards,pardes,cafe-hub}/`. +- Preview generations under `~/.local/state/9web/public/`; normal output unchanged + by prefix packaging. Initial publication reused existing builds. +- `--url-prefix` is optional in project build commands and never enabled by + default. The service explicitly supplies it for builds and managed watch. +- Optional compatibility site port is represented as `?u16 = null`, not zero. +- Configuration backup: `~/.config/9web/sites.before-namespace.json`. + +The helper `9web-prefix` is installed by `zig build` and locally at +`~/.local/bin/9web-prefix`. It relocates generated HTML/CSS only. JavaScript, +JSON, authored content and source URLs are unchanged. The archive stays +read-only in the service. Old preview generations are retained, not garbage +collected. Same-origin site scripts are trusted operator code, not isolated +from writable files or build controls. Cloudflare APIs are not implemented. + +## Companion build commits + +| Project | Commit | Scope | +|---|---|---| +| Entomology lab | `da9d227` | Optional prefix/output/reuse flags in existing builder | +| Standards | `4ad43a0` | Same optional build flags | +| 0x4200.cafe | `2d47b2b` | Optional flags in build/build.sh; default/watch preserved | +| Pardes website | `a1da335` | Build entry point with optional prefix packaging | +| Café hub archive | `3e07a67` | Optional static archive build/packaging entry point | + +Existing unrelated project edits were not committed. No remote push. + +## Checks + +`zig build -Doptimize=ReleaseSafe catalog-test host-fs-test http-fs-test +catalog-ui-test mux-test prefix-test` passes. Coverage includes: + +- Index/pretty URLs, redirects with query preservation, MIME, HEAD and byte ranges. +- Identical website bytes through namespace/9P and HTTP; traversal rejection. +- Pinned public file reads when a new generation replaces its pathname. +- Scoped HTML/CSS prefixing, CSS selectors, entities, srcset/data URLs, unchanged + scripts/code examples/external URLs and normal-output hashes. +- Atomic publication, stale-asset exclusion and preservation on failure. +- Whole-host operations and mounted 9P controls, large directories, blocking + events and repeated flush/clunk without reader exhaustion. +- Existing Files/Sites/Services shell, mobile layout, editing and history. + +Live Chromium validation (`NINE_WEB_LIVE=http://127.0.0.1:8090 node +test/web/catalog_ui.mjs zig-out/bin/9web`) also passes: `/mnt/9p/9web`, agents, +zmx, `/usr/bin`, all five site landing pages, 22 same-origin images across +Entomology and the café, and café blog/pretty article navigation. LAN IPv4 and +IPv6 site requests return 200. An actual café build requested through +`/fs/mnt/9p/9web/sites/cafe/ctl` completed with exit code 0 and published a new +prefixed generation; no preview daemon was started. + +`zig build -Doptimize=ReleaseSafe e2e` passes all 32 original gateway scenarios. diff --git a/test/web/catalog.py b/test/web/catalog.py index e1b153e..87773d8 100644 --- a/test/web/catalog.py +++ b/test/web/catalog.py @@ -6,6 +6,7 @@ import json import os from pathlib import Path import socket +import shutil import struct import subprocess import sys @@ -145,8 +146,14 @@ def main(): assert http(site_port, '/nested/')[2] == b'nested page' assert http(site_port, '/', 'HEAD')[2] == b'' assert http(site_port, '/style.css', headers={'Range':'bytes=0-3'})[2] == b'body' + assert http(dashboard, '/sites/fixture/')[2] == b'

catalog fixture

' + assert http(dashboard, '/sites/fixture/pretty')[2] == b'pretty page' + assert http(dashboard, '/sites/fixture/nested?x=1')[1]['location'] == '/sites/fixture/nested/?x=1' + assert http(dashboard, '/sites/fixture/style.css', headers={'Range':'bytes=-2'})[0] == 206 + assert http(dashboard, '/sites/fixture/style.css', headers={'Range':'bytes=999999-'})[0] == 416 for path in ['/../secret', '/%2e%2e/secret', '/escape', '/escape-dir/secret', '/.git/config', '/%2fetc/passwd']: assert http(site_port, path)[0] in (400,403,404), path + assert http(dashboard, '/sites/fixture' + path)[0] in (400,403,404), path assert http(dashboard, headers={'Host':'attacker.invalid'})[0] == 403 ctl = '/_cloud9/sites/fixture/ctl' assert http(dashboard, ctl, 'POST', 'build', {'Origin':'https://attacker.invalid'})[0] == 403 @@ -199,6 +206,17 @@ def main(): nine.write('sites/fixture/ctl', 'build') eventually(lambda: json.loads(nine.read('sites/fixture/status'))['phase'] == 'failed') assert http(site_port)[0] == 200 + # A published generation replacement must not splice an open fid. + saved = (public/'style.css').read_bytes() + nine.walk('sites/fixture/public/style.css') + nine.rpc(112, struct.pack('Fixture'); await fs.writeFile(path.join(work, 'large.txt'), 'x'.repeat(180000)); - const dashboard = await port(), sitePort = await port(), debugPort = await port(); + const dashboard = await port(), debugPort = await port(); const runtime = path.join(work, 'runtime'); await fs.mkdir(path.join(runtime, '9p'), { recursive: true }); start(path.resolve('zig-out/bin/9proc-demo'), ['--unix', path.join(runtime, '9p', 'fixture')]); await wait(() => fs.stat(path.join(runtime, '9p', 'fixture')), '9P fixture posted'); - await fs.writeFile(path.join(work, 'config.json'), JSON.stringify({ filesystem: '/', sites: [{ name: 'fixture', title: 'Fixture notes', cwd: work, root, port: sitePort, build: ['sh', '-c', 'echo integrated-build'], inputs: ['public/index.html'] }] })); + await fs.writeFile(path.join(work, 'config.json'), JSON.stringify({ filesystem: '/', sites: [{ name: 'fixture', title: 'Fixture notes', cwd: work, root, build: ['sh', '-c', 'echo integrated-build'], inputs: ['public/index.html'] }] })); const startWeb = () => start(path.resolve('zig-out/bin/9ns'), ['--mntgen', '--', binary, '--listen', `127.0.0.1:${dashboard}`, '--catalog', path.join(work, 'config.json'), '--catalog-socket', path.join(work, 'catalog.sock'), '--state-dir', path.join(work, 'state')], { ...process.env, XDG_RUNTIME_DIR: runtime }); const web = startWeb(); const url = `http://127.0.0.1:${dashboard}`; @@ -106,7 +106,7 @@ try { await fs.writeFile('.zig-cache/catalog-ui/files.png', Buffer.from((await cdp('Page.captureScreenshot', { format: 'png' })).data, 'base64')); await view('sites'); await readySites(); assert.equal(await evaluate(`document.title`), 'sites — 9web'); - assert.equal(await evaluate(`document.querySelector('#sites-entries .site-name').href`), `http://127.0.0.1:${sitePort}/`); + assert.equal(await evaluate(`document.querySelector('#sites-entries .site-name').href`), `${url}/sites/fixture/`); assert.equal(await evaluate(`document.querySelectorAll('link[rel=stylesheet]').length`), 1); assert.equal(await evaluate(`document.querySelector('link[rel=stylesheet]').getAttribute('href')`), '/_cloud9/style.css'); // Build button writes through the same /fs tree as the normal file browser. @@ -161,16 +161,35 @@ try { web.kill('SIGTERM'); await once(web, 'exit'); await fs.unlink(path.join(work, 'catalog.sock')); startWeb(); - await wait(async () => (await fetch(url)).ok && (await fetch(`http://127.0.0.1:${sitePort}`)).ok, 'immediate restart with recent HTTP traffic'); + await wait(async () => (await fetch(url)).ok && (await fetch(`${url}/sites/fixture/`)).ok, 'immediate restart with recent HTTP traffic'); // Opt-in, read-only smoke navigation against an installed workstation. if (process.env.NINE_WEB_LIVE) { - for (const pathname of ['/mnt/9p', '/mnt/9p/agents', '/mnt/9p/agents/codex/history', '/mnt/9p/zmx/daybreak/command', '/mnt/9p/zmx/daybreak/cwd', '/usr/bin']) { + for (const pathname of ['/mnt/9p', '/mnt/9p/9web', '/mnt/9p/9web/sites/entomology', '/mnt/9p/agents', '/mnt/9p/agents/codex/history', '/mnt/9p/zmx/daybreak/command', '/mnt/9p/zmx/daybreak/cwd', '/usr/bin']) { await cdp('Page.navigate', { url: process.env.NINE_WEB_LIVE + pathname }); await wait(() => evaluate(`document.getElementById('path')?.value === ${JSON.stringify(pathname)} && (document.getElementById('status').textContent === 'File loaded.' || document.getElementById('status').textContent.includes('entries'))`), 'live navigation: ' + pathname, 30000).catch(async error => { throw new Error(error.message + ': ' + await evaluate(`document.getElementById('status')?.textContent`)); }); console.log('live browser navigation passed:', pathname); } + for (const site of ['entomology', 'cafe', 'standards', 'pardes', 'cafe-hub']) { + const target = `${process.env.NINE_WEB_LIVE}/sites/${site}/`; + await cdp('Page.navigate', { url: target }); + await wait(() => evaluate(`location.href === ${JSON.stringify(target)} && document.readyState === 'complete' && document.body.textContent.length > 50`), 'live website: ' + site, 60000); + const images = await evaluate(`Array.from(document.images).filter(i => new URL(i.src).origin === location.origin && i.getAttribute('loading') !== 'lazy').map(i => ({src:i.src, ok:i.complete && i.naturalWidth > 0}))`); + assert.deepEqual(images.filter(i => !i.ok), [], `${site}: failed same-origin images`); + const unprefixed = await evaluate(`Array.from(document.querySelectorAll('a[href],img[src],script[src],link[href]')).map(e => e.getAttribute('href') || e.getAttribute('src')).filter(u => u?.startsWith('/') && !u.startsWith('//') && !u.startsWith('/sites/${site}/'))`); + assert.deepEqual(unprefixed, [], `${site}: unresolved absolute URLs`); + console.log('live prefixed website passed:', site, `(${images.length} local images)`); + if (site === 'cafe') { + await cdp('Page.navigate', { url: target + 'blog/' }); + await wait(() => evaluate(`location.pathname === '/sites/cafe/blog/' && document.readyState === 'complete' && document.querySelectorAll('a[href^="/sites/cafe/blog/"]').length > 0`), 'cafe blog navigation', 30000); + const article = await evaluate(`Array.from(document.querySelectorAll('a[href^="/sites/cafe/blog/"]')).map(a=>a.href).find(h=>new URL(h).pathname !== '/sites/cafe/blog/')`); + assert.ok(article); + await cdp('Page.navigate', { url: article }); + await wait(() => evaluate(`location.href === ${JSON.stringify(article)} && document.readyState === 'complete' && document.body.textContent.length > 100`), 'cafe pretty article', 30000); + console.log('live cafe blog and pretty article passed:', article); + } + } } console.log('catalog_ui: shared shell/style, tab history, file/log navigation, build control, focus, dirty edits and mobile layout passed'); } catch (e) { diff --git a/test/web/host_fs.py b/test/web/host_fs.py index 7a55eec..ea39cc9 100644 --- a/test/web/host_fs.py +++ b/test/web/host_fs.py @@ -137,7 +137,13 @@ def main(): eventually(lambda: b'host-build' in http(p, '/fs/_9web/sites/fixture/log')[2]) assert http(config['sites'][0]['port'])[2] == b'site still works' assert http(p, '/_cloud9/catalog.json')[0] == 200 - assert json.loads(http(p, '/_cloud9/catalog.json')[2])['namespace'] == '/_9web' + assert json.loads(http(p, '/_cloud9/catalog.json')[2])['namespace'] == '/mnt/9p/9web' + assert http(p, '/fs/mnt/9p/9web/sites/fixture/public/index.html')[2] == b'site still works' + assert http(p, '/sites/fixture/')[2] == b'site still works' + pure = Nine(str(registry/'9web')) + assert pure.list('sites') == ['fixture'] + assert pure.read('sites/fixture/public/index.html') == b'site still works' + pure.s.close() assert http(p, target+'/renamed', 'DELETE')[0] == 204 assert http(p, target, 'DELETE')[0] == 204 nine.s.close() diff --git a/test/web/prefix.py b/test/web/prefix.py new file mode 100644 index 0000000..68fe974 --- /dev/null +++ b/test/web/prefix.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +import hashlib +import importlib.util +from pathlib import Path +import tempfile + +spec = importlib.util.spec_from_file_location('prefix', Path(__file__).resolve().parents[2] / 'web/prefix.py') +p = importlib.util.module_from_spec(spec) +spec.loader.exec_module(p) +prefix = '/sites/fixture' +original = ''' +go +externalexternal +fragment +relative
+href="/example" + + +''' +rewritten = p.html(original, prefix) +assert 'srcset=" /sites/fixture/a.png 1x, /sites/fixture/b.png 2x"' in p.html('', prefix) +assert 'url("/sites/fixture/a.png")' in p.html('
', prefix) +assert p.srcset('data:image/svg+xml,/untouched 1x, /a.png 2x', prefix) == 'data:image/svg+xml,/untouched 1x, /sites/fixture/a.png 2x' +for piece in ['href="/sites/fixture/blog/post?q=1&x=2#part"', + 'srcset="/sites/fixture/one.png 1x, /sites/fixture/two.png 2x"', + 'url(/sites/fixture/fonts/a.woff2)', 'a[href="/sites/fixture/blog/"]', + '@import "/sites/fixture/main.css"']: + assert piece in rewritten, piece +for piece in ['/* url(/comment) */', 'content:"/ordinary"', 'href="/example"', + "fetch('/api/pokes')", "const s = '/assets/unmodified.js'", '{"url":"/unmodified"}', + '', 'href="../page.html"', 'href="#part"', + 'href="//elsewhere/foo"', 'href="https://other/x"', 'src="data:image/png;base64,abcd"', + 'url("data:image/svg+xml,%3Csvg viewBox=\'0 0 10 10\'%3E")']: + assert piece in rewritten, piece +for invalid in ['', '/', '//host', '/a/../b', '/a?b', '/a#b', 'http://a']: + try: + p.check_prefix(invalid) + raise AssertionError(invalid) + except ValueError: + pass +with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + source, out = root/'normal', root/'preview' + source.mkdir() + (source/'index.html').write_text(original) + (source/'app.js').write_text("fetch('/api/pokes')") + def hashes(): + return {str(f.relative_to(source)): hashlib.sha256(f.read_bytes()).hexdigest() for f in source.rglob('*') if f.is_file()} + before = hashes() + p.publish(source, out, prefix) + assert hashes() == before + assert out.is_symlink() and (out/'index.html').read_text() == rewritten + assert (out/'app.js').read_bytes() == (source/'app.js').read_bytes() + previous = out.resolve() + (source/'app.js').unlink() + p.publish(source, out, prefix) + assert out.resolve() != previous and previous.exists() + assert not (out/'app.js').exists(), 'stale files must not leak into next generation' + (source/'escape').symlink_to('/etc/hostname') + current = out.resolve() + try: + p.publish(source, out, prefix) + raise AssertionError('symlink accepted') + except ValueError: + pass + assert out.resolve() == current, 'failed generation changed published output' +print('prefix: scoped HTML/CSS, unchanged JS/text/source, atomic publication and failure preservation passed') 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