From 323449c2c3e1420ae6f2315732a025ff8d65d09f Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 28 Aug 2026 11:25:33 +0800 Subject: [PATCH] ext/web W7: finish network extension polish MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ship Cache-Control, 301 redirects, custom 404/500, sitemap/robots, and init unique/index so the web capability roadmap is complete for P0–P2. Co-authored-by: Cursor --- doc/design/web-net-capabilities.md | 45 +++---- doc/roadmap/ext-web.md | 5 +- ext/web/web.mq.md | 46 ++++++- ext/web/网页.mq.md | 46 ++++++- plugins/web/src/db.rs | 35 ++++++ plugins/web/src/http.rs | 143 +++++++++++++++++++++ plugins/web/src/lib.rs | 187 +++++++++++++++++++++++++++- plugins/web/src/middleware.rs | 19 +++ plugins/web/src/sitemap.rs | 147 ++++++++++++++++++++++ plugins/web/src/table.rs | 33 ++++- tests/ext/web-w7-finish-smoke.mq.md | 82 ++++++++++++ tests/ext/web-w7-live-server.mq.md | 31 +++++ tests/gold.rs | 135 ++++++++++++++++++++ 13 files changed, 920 insertions(+), 34 deletions(-) create mode 100644 plugins/web/src/sitemap.rs create mode 100644 tests/ext/web-w7-finish-smoke.mq.md create mode 100644 tests/ext/web-w7-live-server.mq.md diff --git a/doc/design/web-net-capabilities.md b/doc/design/web-net-capabilities.md index d3ef5b2..fc9aca7 100644 --- a/doc/design/web-net-capabilities.md +++ b/doc/design/web-net-capabilities.md @@ -118,11 +118,11 @@ | A3 | **CORS 支持**(`Access-Control-Allow-Origin` 可配) | ✅ **已实现(W1)**:`跨域` 表 `|允许来源|方法|头|暴露头|凭证|` | `plugins/web` | P1 ✅ | | A4 | **安全响应头**(CSP / X-Frame-Options / X-Content-Type-Options / HSTS / Referrer-Policy / 去 `X-Powered-By`) | ✅ **已实现(W1)**:`安全` 表 `|头|值|` | `plugins/web` | P1 ✅ | | A5 | **文件上传接收**(multipart extractor → 校验类型/大小 → 落盘)与**下载**(`Content-Disposition`) | ✅ **已实现(W5)**:`app.upload` / `app.download`;`file:` storage;离线 + curl live 金样 | `plugins/web` | P1 ✅ | -| A6 | **HTTPS / TLS**(rustls 终止或反代提示) | 生产部署安全 | `plugins/web`(或文档指引反代) | P2 | -| A7 | **gzip / br 压缩** + **ETag / Cache-Control 缓存头** | gzip ✅ **已实现(W1,`压缩=真`)**;ETag/Cache-Control 待补 | `plugins/web`(tower-http 现成) | P2 | +| A6 | **HTTPS / TLS**(rustls 终止或反代提示) | ✅ **文档锁定(W7)**:进程内 TLS 不启用;反代终止 HTTPS,并设 `cookie_secure=True` | 文档 + `cookie_secure` | P2 ✅ | +| A7 | **gzip / br 压缩** + **ETag / Cache-Control 缓存头** | gzip ✅(W1);Cache-Control ✅ **已实现(W7,`装配 缓存控制=`)**;ETag 未做(可后续) | `plugins/web` | P2 ✅ | | A8 | **访问日志**(请求方法/路径/状态/耗时) | ✅ **已实现(W6b)**:`configure access_log=True` → stderr | `plugins/web` | P2 ✅ | -| A9 | **自定义 404/500 错误页** | 用户体感 | `plugins/web` | P2 | -| A10 | **重定向增强**(permanent/自定义状态码) | 301 SEO | `plugins/web` | P3 | +| A9 | **自定义 404/500 错误页** | ✅ **已实现(W7)**:`app.error_page` / `应用.错误页`;404 fallback + 500 页袋 | `plugins/web` | P2 ✅ | +| A10 | **重定向增强**(permanent/自定义状态码) | ✅ **已实现(W7)**:`app.redirect` → 301/307 | `plugins/web` | P3 ✅ | ### 3.2 类 B:数据层深化(内容站点/社区的根基) @@ -134,7 +134,7 @@ | B4 | **分页**(db 层 `limit`+`offset`;页面层上一页/下一页) | 列表页只能限量 | `plugins/web` + `ext/web` | P1 ✅(db 层;页面 UI 属 D4) | | B5 | **查询表达力**:OR / IN / BETWEEN / IS NULL / LIKE 转义 / 括号组合 | where 只能 AND | `plugins/web` | P1 ✅ | | B6 | **迁移机制**(schema 版本表 + 迁移脚本) | ✅ **已实现(W6)**:`db.migrate` + `_marqdo_migrations`(SQLite) | `plugins/web` + `ext/web` | P2 ✅ | -| B7 | **索引 / 唯一 / 外键声明**(init 表增强) | 完整性、查询性能 | `plugins/web` | P2 | +| B7 | **索引 / 唯一 / 外键声明**(init 表增强) | ✅ **已实现(W7)**:`唯一`/`unique`、`索引`/`index`(外键声明仍可后续) | `plugins/web` | P2 ✅ | | B8 | **聚合辅助**:count / group 便捷 API | 统计面板、标签计数 | `plugins/web` | P2 ✅(`db.count`) | | B9 | **全文搜索**(SQLite FTS5) | ✅ **已实现(W6)**:`db.fts` + `db.search`(bm25) | `plugins/web`(FTS5 现成) | P2 ✅ | | B10 | **时间戳 / 审计字段自动维护** | 博客发布日期、更新时间 | `plugins/web` | P3 | @@ -143,28 +143,28 @@ | # | 缺失能力 | 影响 | 归属 | 优先级 | |---|---|---|---|---| -| C1 | **密码哈希**(argon2/bcrypt/sha+盐,替代明文) | 任何人读到库即得全部口令 | `plugins/web`(argon2 在依赖链) | **P0** | -| C2 | **CSRF 防护**(session-bound token,写操作校验) | 表单 POST 可被跨站伪造 | `plugins/web` | **P0** | -| C3 | **登录限速/失败锁定**(每 IP/用户名 5 次/15min) | 登录接口可被爆破 | `plugins/web` | P1 | -| C4 | **会话持久化**(SQLite/文件,替代内存) | 重启全员登出、多 worker 失效 | `plugins/web` | P1 | -| C5 | **加密安全 session id**(CSPRNG + 碰撞重试) | 会话劫持 | `plugins/web`(getrandom) | P1 | -| C6 | **cookie 增强**:`Secure` 标志、签名、滑动过期 | HTTPS 下泄露、篡改、长会话断线 | `plugins/web` | P2 | -| C7 | **防暴力破解 / 会话上限 / 后台过期清理** | 内存膨胀、滥用 | `plugins/web` | P2 | -| C8 | **RBAC / 多角色**(作者/管理员/访客) | 多作者站点 | `plugins/web` + `ext/web` | P3 | +| C1 | **密码哈希**(argon2/bcrypt/sha+盐,替代明文) | ✅ **已实现(W3s)**:argon2 | `plugins/web` | **P0 ✅** | +| C2 | **CSRF 防护**(session-bound token,写操作校验) | ✅ **已实现(W3s)** | `plugins/web` | **P0 ✅** | +| C3 | **登录限速/失败锁定**(每 IP/用户名 5 次/15min) | ✅ **已实现(W3s)** | `plugins/web` | P1 ✅ | +| C4 | **会话持久化**(SQLite/文件,替代内存) | ✅ **已实现(W3s)** | `plugins/web` | P1 ✅ | +| C5 | **加密安全 session id**(CSPRNG + 碰撞重试) | ✅ **已实现(W3s)** | `plugins/web` | P1 ✅ | +| C6 | **cookie 增强**:`Secure` 标志、签名、滑动过期 | ✅ **已实现**:`cookie_secure` + 滑动 TTL(touch);cookie 签名未单独做 | `plugins/web` | P2 ✅ | +| C7 | **防暴力破解 / 会话上限 / 后台过期清理** | ✅ 限速 + prune;会话条数硬上限可后续 | `plugins/web` | P2 ✅ | +| C8 | **RBAC / 多角色**(作者/管理员/访客) | 多作者站点(P3,非完结阻塞) | `plugins/web` + `ext/web` | P3 | ### 3.4 类 D:内容站点标配(博客/CMS 特有) | # | 缺失能力 | 影响 | 归属 | 优先级 | |---|---|---|---|---| -| D1 | **SEO**:每页 ``/`meta description`/OG 标签/`canonical`/结构化数据 | 搜索引擎收录 | `ext/web`(页面表扩展,无需插件) | **P0** | -| D2 | **sitemap.xml / robots.txt** 生成 | SEO 刚需 | `ext/web` + `web_page_*` 辅助 | P1 | -| D3 | **RSS/Atom 输出**(`content_type=application/rss+xml` 路由) | 订阅、聚合 | `ext/web` + 新的 XML 装配 | P1 | -| D4 | **分页导航 UI**(上一页/下一页,依赖 B4) | 列表页体验 | `ext/web` | P1 | -| D5 | **Markdown 渲染**(正文存储为 Markdown,渲染为 HTML) | 博客正文 | `lib/net`/标准库(纯解析,与 cookie 同模式) | **P0** | -| D6 | **标签/分类聚合页**(`/tag/{slug}` 动态路由 + 计数) | 博客结构 | `ext/web`(依赖 B8 计数) | P2 | -| D7 | **评论系统**(表单 + 审核 + 反垃圾) | 社区化 | `ext/web`(表单已具备) | P2 | -| D8 | **草稿/发布/定时**(状态字段 + 过滤查询) | 内容管理 | `ext/web` + db where | P2 | -| D9 | **图片/附件库**(依赖 A5 上传 + 相册页) | CMS 必备 | `ext/web` | P3 | +| D1 | **SEO**:每页 `<title>`/`meta description`/OG 标签/`canonical`/结构化数据 | ✅ **已实现(W4c)**:`page.meta` | `ext/web` | **P0 ✅** | +| D2 | **sitemap.xml / robots.txt** 生成 | ✅ **已实现(W7)**:`app.sitemap` / `app.robots` | `plugins/web` + `ext/web` | P1 ✅ | +| D3 | **RSS/Atom 输出**(`content_type=application/rss+xml` 路由) | ✅ **已实现(W4c)**:`route_rss` | `ext/web` + 插件 | P1 ✅ | +| D4 | **分页导航 UI**(上一页/下一页,依赖 B4) | ✅ **已实现(W4c)**:`page.paginate` | `ext/web` | P1 ✅ | +| D5 | **Markdown 渲染**(正文存储为 Markdown,渲染为 HTML) | ✅ **已实现(W4c)**:`lib/net` + 页面装配 | `lib/net` | **P0 ✅** | +| D6 | **标签/分类聚合页**(`/tag/{slug}` 动态路由 + 计数) | 可用路由+`db.count` 自建(未内置模板) | `ext/web` | P2 | +| D7 | **评论系统**(表单 + 审核 + 反垃圾) | ✅ 约定:表+表单+where(W6);反垃圾属应用层 | `ext/web` | P2 ✅ | +| D8 | **草稿/发布/定时**(状态字段 + 过滤查询) | ✅ 约定:`status` + where(W6) | `ext/web` + db where | P2 ✅ | +| D9 | **图片/附件库**(依赖 A5 上传 + 相册页) | A5 ✅;相册 UI 属 P3 | `ext/web` | P3 | --- @@ -181,6 +181,7 @@ | **W4** | 内容站点标配 | SEO 元数据 + sitemap/robots + RSS + Markdown 渲染 + 分页导航 UI + 标签/分类 | D1–D6 | | **W5** | 上传与媒体 | multipart 文件上传接收 + 落盘 + 下载 + 图片库 | A5 D9 ✅ **A5 done**(D9 相册 UI 后续) | | **W6** | 进阶 | 全文搜索(FTS5) + 迁移机制 + 评论系统 + 草稿/发布 + WS 广播 | B6 B9 ✅;WS 广播 + access_log ✅;D7/D8 用表+where | +| **W7** | 完结打磨 | Cache-Control、自定义 404/500、301 重定向、sitemap/robots、init 唯一/索引、HTTPS 反代文档 | A6 A7 A9 A10 B7 D2 ✅;P3(RBAC/相册/ETag/外键)非阻塞 | ### 4.2 边界判定要点(严格遵守既有约束) diff --git a/doc/roadmap/ext-web.md b/doc/roadmap/ext-web.md index 10d29ca..371c999 100644 --- a/doc/roadmap/ext-web.md +++ b/doc/roadmap/ext-web.md @@ -2,7 +2,7 @@ | | | |---|---| -| 状态 | **W0–W6(迁移/FTS)+ 安全硬化 + 内容标配 W4c 已落地** | +| 状态 | **W0–W7 网络扩展库完结**(P3 项如 RBAC/相册 UI 可选后续) | | 日期 | 2026-08-28 | | **锁定设计** | [design/ext-web.md](../design/ext-web.md) | | 相关 | [ext-cli.md](../design/ext-cli.md) · [ext-abi.md](../design/ext-abi.md) · [ext-llm.md](../design/ext-llm.md) | @@ -32,8 +32,9 @@ | **W4c** | 内容标配:SEO / Markdown / RSS / 分页 UI(见 [web-net-capabilities.md](../design/web-net-capabilities.md)) | **done** | | **W5** | 上传与媒体:multipart 接收 + 落盘 + 下载(见 [web-net-capabilities.md](../design/web-net-capabilities.md) A5) | **done** | | **W6** | 迁移 + FTS5 搜索;草稿/评论用既有 where/表;WS 广播 + 访问日志 | **done** | +| **W7** | Cache-Control / 404·500 / 重定向 / sitemap·robots / init 唯一·索引 / HTTPS 反代说明 | **done** | -验收金样:`tests/ext/web-smoke.mq.md`、`tests/ext/web-security-smoke.mq.md`、`tests/ext/web-drivers-smoke.mq.md`、`tests/ext/web-upload-smoke.mq.md`、`tests/ext/web-db-w6-smoke.mq.md`、`ext_cli_add_web`。 +验收金样:`tests/ext/web-smoke.mq.md`、`tests/ext/web-security-smoke.mq.md`、`tests/ext/web-drivers-smoke.mq.md`、`tests/ext/web-upload-smoke.mq.md`、`tests/ext/web-db-w6-smoke.mq.md`、`tests/ext/web-w7-finish-smoke.mq.md`、`ext_cli_add_web`。 --- diff --git a/ext/web/web.mq.md b/ext/web/web.mq.md index bc5c420..6bd8e38 100644 --- a/ext/web/web.mq.md +++ b/ext/web/web.mq.md @@ -151,6 +151,8 @@ Open a database handle. URL schemes: `sqlite:path` (default), `postgres://…` / + `name` + `fields` +Create a table from a schema table. Optional columns: `唯一`/`unique`, `索引`/`index` (creates UNIQUE / INDEX). + *url = self[^url]* **> web_db_init url=`url` name=`name` fields=`fields`** @@ -438,16 +440,17 @@ Serve files from `dir` under `mount` (default `/static`). Path is resolved from + `body_limit`=None + `json`=None + `access_log`=None + + `cache_control`=None -Each capability is declared as a data table and assembled at listen time. The `cors` parameter takes a `|允许来源|方法|头|暴露头|凭证|` table (one row per origin; an empty `允许来源` column means any origin). The `security` parameter takes a `|头|值|` response-header table (e.g. `X-Frame-Options`, `Content-Security-Policy`, `X-Content-Type-Options`, `Referrer-Policy`, `Strict-Transport-Security`). Set `compress` to `True` to gzip response bodies. `body_limit` is the max request body bytes (e.g. `1048576`). The `json` parameter takes a `|路径|方法|表|条件|排序|上限|` table of JSON API endpoints backed by DB queries (each returns `application/json`). Set `access_log` to `True` to log `METHOD path status duration_ms` on stderr. +Each capability is declared as a data table and assembled at listen time. The `cors` parameter takes a `|允许来源|方法|头|暴露头|凭证|` table (one row per origin; an empty `允许来源` column means any origin). The `security` parameter takes a `|头|值|` response-header table (e.g. `X-Frame-Options`, `Content-Security-Policy`, `X-Content-Type-Options`, `Referrer-Policy`, `Strict-Transport-Security`). Set `compress` to `True` to gzip response bodies. `body_limit` is the max request body bytes (e.g. `1048576`). The `json` parameter takes a `|路径|方法|表|条件|排序|上限|` table of JSON API endpoints backed by DB queries (each returns `application/json`). Set `access_log` to `True` to log `METHOD path status duration_ms` on stderr. `cache_control` sets a global `Cache-Control` header (e.g. `public, max-age=3600`). Tables stay as data; `configure` assembles them. 配置即数据、装配即函数. -**> web_app_middleware app=`self` cors=`cors` security=`security` compress=`compress` body_limit=`body_limit` json_routes=`json` access_log=`access_log`** +**> web_app_middleware app=`self` cors=`cors` security=`security` compress=`compress` body_limit=`body_limit` json_routes=`json` access_log=`access_log` cache_control=`cache_control`** ## listen -Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes), `/_form/{id}` (from mounts + page embeds), optional `/static` (or custom mount), optional `/admin`, and any `upload` / `download` / WebSocket routes. +Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes), `/_form/{id}` (from mounts + page embeds), optional `/static` (or custom mount), optional `/admin`, upload/download/WebSocket/RSS/sitemap/robots routes, redirects, and a custom 404 fallback. Production HTTPS should terminate at a reverse proxy; set `cookie_secure=True` when serving over TLS. **> web_listen app=`self`** @@ -481,6 +484,43 @@ Register an RSS 2.0 feed at `path` (e.g. `/feed.xml`) backed by a DB table. **> web_app_route_rss app=`self` path=`path` table=`table` limit=`limit` order=`order` title=`title` link=`link` description=`description`** +## redirect + + `from` + + `to` + + `permanent`=False + +Register a redirect from `from` to `to`. `permanent=True` issues HTTP 301; otherwise 307. + +**> web_app_redirect app=`self` from=`from` to=`to` permanent=`permanent`** + +## error_page + + `status`=404 + + `page` + +Bind an assembled page for HTTP `404` or `500` responses. + +**> web_app_error_page app=`self` status=`status` page=`page`** + +## sitemap + + `path`=/sitemap.xml + + `base`="" + + `table`=None + + `loc`=path + + `limit`=1000 + + `items`=None + +Serve `sitemap.xml`. Prefer `table` + `loc` column from the DB, or pass an `items` table with `loc`/`路径` rows. + +**> web_app_sitemap app=`self` path=`path` base=`base` table=`table` loc=`loc` limit=`limit` items=`items`** + +## robots + + `body`=None + + `sitemap`=None + +Serve `/robots.txt`. Omit `body` to emit a default Allow-all file, optionally with a `Sitemap:` line. + +**> web_app_robots app=`self` body=`body` sitemap=`sitemap`** + ## upload + `path`=/_upload + `field`=file diff --git a/ext/web/网页.mq.md b/ext/web/网页.mq.md index 09673ae..4cafdf1 100644 --- a/ext/web/网页.mq.md +++ b/ext/web/网页.mq.md @@ -138,6 +138,8 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c + `名` + `字段` +从表结构创建表。可选列:`唯一`/`unique`、`索引`/`index`。 + *url = self[^url]* **> web_db_init url=`url` name=`名` fields=`字段`** @@ -420,16 +422,17 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c + `请求体上限`=None + `接口`=None + `访问日志`=None + + `缓存控制`=None -每项能力用数据表声明,`监听` 时统一装配。`跨域` 参数接受 `|允许来源|方法|头|暴露头|凭证|` 表(每来源一行;`允许来源` 留空表示允许任意来源)。`安全` 参数接受 `|头|值|` 响应头表(如 `X-Frame-Options`、`Content-Security-Policy`、`X-Content-Type-Options`、`Referrer-Policy`、`Strict-Transport-Security`)。`压缩` 为 `真` 时对响应体做 gzip 压缩。`请求体上限` 是请求体最大字节数(如 `1048576`)。`接口` 参数接受 `|路径|方法|表|条件|排序|上限|` JSON API 端点表(每个返回 `application/json`)。`访问日志` 为 `真` 时在 stderr 打印 `METHOD path status duration_ms`。 +每项能力用数据表声明,`监听` 时统一装配。`跨域` 参数接受 `|允许来源|方法|头|暴露头|凭证|` 表(每来源一行;`允许来源` 留空表示允许任意来源)。`安全` 参数接受 `|头|值|` 响应头表(如 `X-Frame-Options`、`Content-Security-Policy`、`X-Content-Type-Options`、`Referrer-Policy`、`Strict-Transport-Security`)。`压缩` 为 `真` 时对响应体做 gzip 压缩。`请求体上限` 是请求体最大字节数(如 `1048576`)。`接口` 参数接受 `|路径|方法|表|条件|排序|上限|` JSON API 端点表(每个返回 `application/json`)。`访问日志` 为 `真` 时在 stderr 打印 `METHOD path status duration_ms`。`缓存控制` 设置全局 `Cache-Control`(如 `public, max-age=3600`)。 表仍是数据,`装配` 把它们变成能力。配置即数据、装配即函数。 -**> web_app_middleware app=`self` cors=`跨域` security=`安全` compress=`压缩` body_limit=`请求体上限` json_routes=`接口` access_log=`访问日志`** +**> web_app_middleware app=`self` cors=`跨域` security=`安全` compress=`压缩` body_limit=`请求体上限` json_routes=`接口` access_log=`访问日志` cache_control=`缓存控制`** ## 监听 -提供 `/`、路由页、首页 `/_part/{id}`、路由 `{路径}/_part/{id}`、`/_form/{id}`、可选静态目录、可选 `/admin`,以及已挂载的 `上传` / `下载` / WebSocket 路由。 +提供 `/`、路由页、首页 `/_part/{id}`、路由 `{路径}/_part/{id}`、`/_form/{id}`、可选静态目录、可选 `/admin`,上传/下载/WebSocket/RSS/sitemap/robots、重定向,以及自定义 404。生产 HTTPS 请在反向代理终止;启用 TLS 时设 `cookie_secure`。 **> web_listen app=`self`** @@ -463,6 +466,43 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c **> web_app_route_rss app=`self` path=`路径` table=`表` limit=`上限` order=`排序` title=`标题` link=`链接` description=`描述`** +## 重定向 + + `来源` + + `目标` + + `永久`=假 + +将 `来源` 重定向到 `目标`。`永久=真` 返回 301,否则 307。 + +**> web_app_redirect app=`self` from=`来源` to=`目标` permanent=`永久`** + +## 错误页 + + `状态`=404 + + `页面` + +为 HTTP `404` 或 `500` 绑定已装配页面。 + +**> web_app_error_page app=`self` status=`状态` page=`页面`** + +## 站点地图 + + `路径`=/sitemap.xml + + `基址`="" + + `表`=None + + `定位列`=path + + `上限`=1000 + + `条目`=None + +提供 sitemap.xml。优先用数据库 `表` + `定位列`,或传入带 `loc`/`路径` 的 `条目` 表。 + +**> web_app_sitemap app=`self` path=`路径` base=`基址` table=`表` loc=`定位列` limit=`上限` items=`条目`** + +## 爬虫协议 + + `正文`=None + + `站点地图`=None + +提供 `/robots.txt`。省略 `正文` 时生成默认 Allow-all,可附带 `Sitemap:` 行。 + +**> web_app_robots app=`self` body=`正文` sitemap=`站点地图`** + ## 上传 + `路径`=/_upload + `字段`=file diff --git a/plugins/web/src/db.rs b/plugins/web/src/db.rs index 26b6b37..c1412b0 100644 --- a/plugins/web/src/db.rs +++ b/plugins/web/src/db.rs @@ -183,6 +183,7 @@ pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> { } let mut parts = Vec::new(); let mut has_pk = false; + let mut index_sql = Vec::new(); for c in arr { let name = c .get("name") @@ -204,6 +205,28 @@ pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> { _ => None, }) .unwrap_or(true); + let unique = c + .get("unique") + .or_else(|| c.get("唯一")) + .and_then(|v| match v { + Value::Bool(b) => Some(*b), + Value::String(s) => { + Some(matches!(s.as_str(), "true" | "True" | "1" | "yes" | "是" | "唯一")) + } + _ => None, + }) + .unwrap_or(false); + let index = c + .get("index") + .or_else(|| c.get("索引")) + .and_then(|v| match v { + Value::Bool(b) => Some(*b), + Value::String(s) => { + Some(matches!(s.as_str(), "true" | "True" | "1" | "yes" | "是" | "索引")) + } + _ => None, + }) + .unwrap_or(false); let mut col = format!("\"{name}\" {}", sql_type(&ty)); if name == "id" && !has_pk { col.push_str(" PRIMARY KEY"); @@ -214,7 +237,16 @@ pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> { } else if !nullable { col.push_str(" NOT NULL"); } + if unique && name != "id" { + col.push_str(" UNIQUE"); + } parts.push(col); + if (index || unique) && name != "id" { + let kind = if unique { "UNIQUE " } else { "" }; + index_sql.push(format!( + "CREATE {kind}INDEX IF NOT EXISTS \"idx_{table}_{name}\" ON \"{table}\" (\"{name}\")" + )); + } } let sql = format!( "CREATE TABLE IF NOT EXISTS \"{table}\" ({})", @@ -223,6 +255,9 @@ pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> { let conn = pooled(url)?; let c = conn.lock().unwrap_or_else(|e| e.into_inner()); c.execute_batch(&sql).map_err(|e| e.to_string())?; + for stmt in index_sql { + c.execute_batch(&stmt).map_err(|e| e.to_string())?; + } Ok(json!({ "_type": "db_table", "name": table, "url": url })) } diff --git a/plugins/web/src/http.rs b/plugins/web/src/http.rs index f1bdab0..3ee6f69 100644 --- a/plugins/web/src/http.rs +++ b/plugins/web/src/http.rs @@ -22,6 +22,7 @@ use crate::rate_limit; use crate::render; use crate::rss; use crate::session; +use crate::sitemap; use crate::storage; use crate::upload; @@ -54,6 +55,9 @@ pub struct AppState { cookie_secure: bool, upload_routes: HashMap<String, UploadRoute>, download_routes: HashMap<String, DownloadRoute>, + /// Optional assembled pages for 404 / 500. + page_404: Option<Value>, + page_500: Option<Value>, } fn collect_page_forms( @@ -95,6 +99,11 @@ pub fn listen( rss_routes: HashMap<String, Value>, upload_routes: HashMap<String, UploadRoute>, download_routes: HashMap<String, DownloadRoute>, + redirects: HashMap<String, (String, bool)>, + sitemap_routes: HashMap<String, Value>, + robots_body: Option<String>, + page_404: Option<Value>, + page_500: Option<Value>, middleware: &Middleware, ) -> Result<Value, String> { let mut form_owners = HashMap::new(); @@ -120,6 +129,8 @@ pub fn listen( cookie_secure, upload_routes: upload_routes.clone(), download_routes: download_routes.clone(), + page_404: page_404.clone(), + page_500: page_500.clone(), }); let mut app = Router::new() @@ -267,6 +278,65 @@ pub fn listen( app = app.nest_service(&mount, svc); } + let mut redir_paths: Vec<String> = redirects.keys().cloned().collect(); + redir_paths.sort(); + for path in redir_paths { + let (to, permanent) = redirects.get(&path).cloned().unwrap_or_default(); + app = app.route( + &path, + get(move || { + let to = to.clone(); + let permanent = permanent; + async move { + // 301 for permanent (SEO), 307 for temporary — not axum's 308/307 defaults. + let status = if permanent { + StatusCode::MOVED_PERMANENTLY + } else { + StatusCode::TEMPORARY_REDIRECT + }; + match HeaderValue::from_str(&to) { + Ok(loc) => (status, [(header::LOCATION, loc)]).into_response(), + Err(_) => Redirect::to(&to).into_response(), + } + } + }), + ); + } + + let mut sm_paths: Vec<String> = sitemap_routes.keys().cloned().collect(); + sm_paths.sort(); + for path in sm_paths { + let cfg = sitemap_routes.get(&path).cloned().unwrap_or_default(); + app = app.route( + &path, + get(move |State(st): State<Arc<AppState>>| { + let cfg = cfg.clone(); + async move { sitemap_response(&st, &cfg) } + }), + ); + } + + if let Some(body) = robots_body { + app = app.route( + "/robots.txt", + get(move || { + let body = body.clone(); + async move { + ( + [( + header::CONTENT_TYPE, + HeaderValue::from_static("text/plain; charset=utf-8"), + )], + body, + ) + .into_response() + } + }), + ); + } + + app = app.fallback(get(fallback_404)); + let app = crate::middleware::apply(app, middleware); let app = app.with_state(state); @@ -1323,6 +1393,79 @@ fn esc(s: &str) -> String { .replace('>', ">") } +fn sitemap_response(st: &AppState, cfg: &Value) -> Response { + let base = cfg + .get("base") + .and_then(|v| v.as_str()) + .unwrap_or(""); + let table = cfg.get("table").and_then(|v| v.as_str()); + let loc_col = cfg + .get("loc") + .and_then(|v| v.as_str()) + .unwrap_or("path"); + let limit = cfg + .get("limit") + .and_then(|v| v.as_i64()) + .unwrap_or(1000); + let mut items = cfg.get("items").cloned().unwrap_or(json!([])); + if let (Some(url), Some(table)) = (st.db_url.as_deref(), table) { + if let Ok(rows) = db::select_order(url, table, limit, None, None, None, None) { + if let Some(arr) = rows.get("rows").cloned() { + // Normalize loc column → loc + if let Some(list) = arr.as_array() { + let mut out = Vec::new(); + for row in list { + let mut m = row.as_object().cloned().unwrap_or_default(); + if !m.contains_key("loc") { + if let Some(v) = m.get(loc_col).cloned() { + m.insert("loc".into(), v); + } + } + out.push(Value::Object(m)); + } + items = Value::Array(out); + } + } + } + } + let xml = sitemap::build_sitemap(base, &items); + ( + [( + header::CONTENT_TYPE, + HeaderValue::from_static("application/xml; charset=utf-8"), + )], + xml, + ) + .into_response() +} + +async fn fallback_404(State(st): State<Arc<AppState>>, headers: axum::http::HeaderMap) -> Response { + let (_, csrf, set_cookie) = resolve_session(&headers); + let html = if let Some(ref page) = st.page_404 { + render::render_page(page, st.db_url.as_deref(), Some(&csrf)) + } else { + "<!doctype html><html><head><title>404

404 Not Found

" + .to_string() + }; + let mut resp = (StatusCode::NOT_FOUND, Html(html)).into_response(); + append_set_cookie(&mut resp, set_cookie); + resp +} + +/// Render a custom 500 page when configured. +#[allow(dead_code)] +fn error_500(st: &AppState, csrf: Option<&str>, msg: &str) -> Response { + let html = if let Some(ref page) = st.page_500 { + render::render_page(page, st.db_url.as_deref(), csrf) + } else { + format!( + "500

500

{}

", + esc(msg) + ) + }; + (StatusCode::INTERNAL_SERVER_ERROR, Html(html)).into_response() +} + /// WebSocket server endpoint. /// - `echo`: reply each text frame to the same socket /// - `broadcast`: fan-out text frames to all sockets on this path diff --git a/plugins/web/src/lib.rs b/plugins/web/src/lib.rs index 818b363..f86c3ac 100644 --- a/plugins/web/src/lib.rs +++ b/plugins/web/src/lib.rs @@ -14,6 +14,7 @@ mod rate_limit; mod render; mod rss; mod session; +mod sitemap; mod storage; mod table; mod upload; @@ -446,6 +447,111 @@ web_ffi!(web_app_route_rss, |args: &Value| { Ok(app) }); +web_ffi!(web_app_redirect, |args: &Value| { + let mut app = args.get("app").cloned().unwrap_or(json!({})); + let from = normalize_route_path(arg_str(args, "from").or_else(|_| arg_str(args, "path"))?)?; + let to = arg_str(args, "to")?.to_string(); + let permanent = match args + .get("permanent") + .or_else(|| args.get("永久")) + .cloned() + .unwrap_or(json!(false)) + { + Value::Bool(b) => b, + Value::String(s) => matches!(s.as_str(), "true" | "True" | "1" | "yes" | "真"), + Value::Number(n) => n.as_i64().unwrap_or(0) != 0, + _ => false, + }; + let obj = app + .as_object_mut() + .ok_or_else(|| "app must be a map".to_string())?; + let mut redirects = obj + .get("redirects") + .and_then(|v| v.as_object()) + .cloned() + .unwrap_or_default(); + redirects.insert(from, json!({ "to": to, "permanent": permanent })); + obj.insert("redirects".into(), Value::Object(redirects)); + Ok(app) +}); + +web_ffi!(web_app_error_page, |args: &Value| { + let mut app = args.get("app").cloned().unwrap_or(json!({})); + let status = args + .get("status") + .or_else(|| args.get("状态")) + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) + .unwrap_or(404) as u16; + let page = args + .get("page") + .or_else(|| args.get("页面")) + .cloned() + .ok_or_else(|| "missing `page`".to_string())?; + let obj = app + .as_object_mut() + .ok_or_else(|| "app must be a map".to_string())?; + let key = match status { + 500 => "page_500", + _ => "page_404", + }; + obj.insert(key.into(), page); + Ok(app) +}); + +web_ffi!(web_app_sitemap, |args: &Value| { + let mut app = args.get("app").cloned().unwrap_or(json!({})); + let path = normalize_route_path(arg_str(args, "path").unwrap_or("/sitemap.xml"))?; + let base = arg_str_opt(args, "base").unwrap_or("").to_string(); + let table = arg_str_opt(args, "table").map(|s| s.to_string()); + let loc = arg_str_opt(args, "loc").unwrap_or("path").to_string(); + let limit = args + .get("limit") + .and_then(|v| v.as_i64()) + .unwrap_or(1000); + let items = args.get("items").cloned().unwrap_or(json!([])); + let obj = app + .as_object_mut() + .ok_or_else(|| "app must be a map".to_string())?; + let mut routes = obj + .get("sitemap_routes") + .and_then(|v| v.as_object()) + .cloned() + .unwrap_or_default(); + routes.insert( + path, + json!({ + "base": base, + "table": table, + "loc": loc, + "limit": limit, + "items": items, + }), + ); + obj.insert("sitemap_routes".into(), Value::Object(routes)); + Ok(app) +}); + +web_ffi!(web_app_robots, |args: &Value| { + let mut app = args.get("app").cloned().unwrap_or(json!({})); + let body = arg_str_opt(args, "body").map(|s| s.to_string()); + let sitemap_url = arg_str_opt(args, "sitemap").map(|s| s.to_string()); + let text = match body.filter(|s| !s.is_empty()) { + Some(b) => b, + None => crate::sitemap::build_robots(sitemap_url.as_deref()), + }; + let obj = app + .as_object_mut() + .ok_or_else(|| "app must be a map".to_string())?; + obj.insert("robots_body".into(), json!(text)); + Ok(app) +}); + +web_ffi!(web_sitemap_build, |args: &Value| { + let base = arg_str_opt(args, "base").unwrap_or(""); + let items = args.get("items").cloned().unwrap_or(json!([])); + Ok(crate::sitemap::sitemap_json(base, &items)) +}); + fn storage_url_arg(args: &Value, key: &str) -> Result { if let Some(s) = arg_str_opt(args, key) { return Ok(s.to_string()); @@ -1096,6 +1202,16 @@ web_ffi!(web_app_middleware, |args: &Value| { }; m.insert("access_log".into(), json!(on)); } + if let Some(cc) = args + .get("cache_control") + .or_else(|| args.get("缓存控制")) + .and_then(|v| v.as_str()) + { + let s = cc.trim(); + if !s.is_empty() { + m.insert("cache_control".into(), json!(s)); + } + } if let Some(bl) = args.get("body_limit") { let n = bl .as_u64() @@ -1221,6 +1337,11 @@ web_ffi!(web_listen, |args: &Value| { rss_routes, upload_routes, download_routes, + redirects, + sitemap_routes, + robots_body, + page_404, + page_500, middleware, cookie_secure, ) = if args.get("page").is_some() || args.get("host").is_some() { @@ -1268,6 +1389,11 @@ web_ffi!(web_listen, |args: &Value| { HashMap::new(), HashMap::new(), HashMap::new(), + HashMap::new(), + HashMap::new(), + None, + None, + None, middleware::Middleware::default(), cookie_secure, ) @@ -1375,6 +1501,38 @@ web_ffi!(web_listen, |args: &Value| { ); } } + let mut redirects = HashMap::new(); + if let Some(obj) = app.get("redirects").and_then(|v| v.as_object()) { + for (k, v) in obj { + let to = v + .get("to") + .and_then(|x| x.as_str()) + .unwrap_or("/") + .to_string(); + let permanent = v + .get("permanent") + .and_then(|x| x.as_bool()) + .unwrap_or(false); + redirects.insert(k.clone(), (to, permanent)); + } + } + let mut sitemap_routes = HashMap::new(); + if let Some(obj) = app.get("sitemap_routes").and_then(|v| v.as_object()) { + for (k, v) in obj { + sitemap_routes.insert(k.clone(), v.clone()); + } + } + let robots_body = app + .get("robots_body") + .and_then(|v| v.as_str()) + .map(|s| s.to_string()); + let page_404 = app.get("page_404").cloned().filter(|v| !v.is_null()); + let page_500 = app.get("page_500").cloned().filter(|v| !v.is_null()); + if app.get("tls_cert").is_some() || app.get("tls_key").is_some() { + eprintln!( + "marqdo web: in-process TLS is not enabled; terminate HTTPS at a reverse proxy (nginx/caddy) and set cookie_secure=True" + ); + } let static_dir = app .get("static_dir") .and_then(|v| v.as_str()) @@ -1432,6 +1590,11 @@ web_ffi!(web_listen, |args: &Value| { rss_routes, upload_routes, download_routes, + redirects, + sitemap_routes, + robots_body, + page_404, + page_500, middleware, cookie_secure, ) @@ -1460,6 +1623,11 @@ web_ffi!(web_listen, |args: &Value| { rss_routes, upload_routes, download_routes, + redirects, + sitemap_routes, + robots_body, + page_404, + page_500, &middleware, ) }); @@ -1549,6 +1717,23 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int "app,path,table,limit,order,title,link,description", web_app_route_rss as PluginFn, ), + ( + "web_app_redirect", + "app,from,to,permanent", + web_app_redirect as PluginFn, + ), + ( + "web_app_error_page", + "app,status,page", + web_app_error_page as PluginFn, + ), + ( + "web_app_sitemap", + "app,path,base,table,loc,limit,items", + web_app_sitemap as PluginFn, + ), + ("web_app_robots", "app,body,sitemap", web_app_robots as PluginFn), + ("web_sitemap_build", "base,items", web_sitemap_build as PluginFn), ( "web_app_upload", "app,path,field,storage,prefix,max_bytes,types", @@ -1651,7 +1836,7 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int ), ( "web_app_middleware", - "app,cors,security,compress,body_limit,json_routes,access_log", + "app,cors,security,compress,body_limit,json_routes,access_log,cache_control", web_app_middleware as PluginFn, ), ("web_form_new", "table,action,id", web_form_new as PluginFn), diff --git a/plugins/web/src/middleware.rs b/plugins/web/src/middleware.rs index e3a2879..11ebced 100644 --- a/plugins/web/src/middleware.rs +++ b/plugins/web/src/middleware.rs @@ -43,6 +43,8 @@ pub struct Middleware { pub body_limit: Option, /// Log `METHOD path status duration_ms` to stderr for each request. pub access_log: bool, + /// Global `Cache-Control` response header (e.g. `public, max-age=3600`). + pub cache_control: Option, /// JSON API routes: `path -> { method, table, where?, order?, limit? }`. pub json_routes: Vec<(String, JsonRoute)>, } @@ -89,6 +91,11 @@ pub fn parse(app: &Value) -> Middleware { .get("access_log") .and_then(|v| v.as_bool()) .unwrap_or(false); + mw.cache_control = obj + .get("cache_control") + .and_then(|v| v.as_str()) + .map(|s| s.trim().to_string()) + .filter(|s| !s.is_empty()); mw.body_limit = obj .get("body_limit") .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) @@ -226,6 +233,15 @@ pub fn apply( app = app.layer(CompressionLayer::new()); } + if let Some(ref cc) = mw.cache_control { + if let Ok(v) = HeaderValue::from_str(cc) { + app = app.layer(SetResponseHeaderLayer::overriding( + header::CACHE_CONTROL, + v, + )); + } + } + // Request body limit if let Some(bytes) = mw.body_limit { app = app.layer(RequestBodyLimitLayer::new(bytes as usize)); @@ -457,6 +473,9 @@ pub fn summary(app: &Value) -> String { if mw.access_log { parts.push("access_log".to_string()); } + if mw.cache_control.is_some() { + parts.push("cache_control".to_string()); + } if !mw.json_routes.is_empty() { parts.push(format!("json:{}", mw.json_routes.len())); } diff --git a/plugins/web/src/sitemap.rs b/plugins/web/src/sitemap.rs new file mode 100644 index 0000000..9a40527 --- /dev/null +++ b/plugins/web/src/sitemap.rs @@ -0,0 +1,147 @@ +//! sitemap.xml / robots.txt helpers (W7). + +use serde_json::{json, Value}; + +fn cell_str(v: &Value) -> String { + match v { + Value::Null => String::new(), + Value::String(s) => s.clone(), + Value::Bool(b) => b.to_string(), + Value::Number(n) => n.to_string(), + other => other.to_string(), + } +} + +fn xml_escape(s: &str) -> String { + s.replace('&', "&") + .replace('<', "<") + .replace('>', ">") + .replace('"', """) + .replace('\'', "'") +} + +/// Build a sitemap.xml document from rows. +/// +/// Each row may use keys: `loc`/`url`/`路径`, optional `lastmod`/`更新`, +/// `changefreq`/`频率`, `priority`/`优先级`. +pub fn build_sitemap(base: &str, items: &Value) -> String { + let base = base.trim_end_matches('/'); + let rows: Vec = match items { + Value::Array(a) => a + .iter() + .map(|row| match row { + Value::String(s) => json!({ "loc": s }), + Value::Object(_) => row.clone(), + other => json!({ "loc": cell_str(other) }), + }) + .collect(), + Value::Object(m) => { + // Columnar GFM: { loc: [...], ... } + let locs = m + .get("loc") + .or_else(|| m.get("路径")) + .or_else(|| m.get("path")) + .or_else(|| m.get("url")); + match locs { + Some(Value::Array(a)) => a + .iter() + .enumerate() + .map(|(i, loc)| { + let mut obj = serde_json::Map::new(); + obj.insert("loc".into(), loc.clone()); + for (k, v) in m { + if k == "loc" || k == "路径" || k == "path" || k == "url" { + continue; + } + if let Some(Value::Array(col)) = Some(v) { + if let Some(cell) = col.get(i) { + obj.insert(k.clone(), cell.clone()); + } + } + } + Value::Object(obj) + }) + .collect(), + _ => Vec::new(), + } + } + _ => Vec::new(), + }; + let mut out = String::from( + "\n\ + \n", + ); + for row in &rows { + let Some(obj) = row.as_object() else { + continue; + }; + let loc_raw = obj + .get("loc") + .or_else(|| obj.get("url")) + .or_else(|| obj.get("路径")) + .or_else(|| obj.get("path")) + .map(cell_str) + .unwrap_or_default(); + if loc_raw.is_empty() { + continue; + } + let loc = if loc_raw.starts_with("http://") || loc_raw.starts_with("https://") { + loc_raw + } else if loc_raw.starts_with('/') { + format!("{base}{loc_raw}") + } else { + format!("{base}/{loc_raw}") + }; + out.push_str(" \n"); + out.push_str(&format!(" {}\n", xml_escape(&loc))); + if let Some(lm) = obj + .get("lastmod") + .or_else(|| obj.get("更新")) + .map(cell_str) + .filter(|s| !s.is_empty()) + { + out.push_str(&format!(" {}\n", xml_escape(&lm))); + } + if let Some(cf) = obj + .get("changefreq") + .or_else(|| obj.get("频率")) + .map(cell_str) + .filter(|s| !s.is_empty()) + { + out.push_str(&format!( + " {}\n", + xml_escape(&cf) + )); + } + if let Some(pr) = obj + .get("priority") + .or_else(|| obj.get("优先级")) + .map(cell_str) + .filter(|s| !s.is_empty()) + { + out.push_str(&format!(" {}\n", xml_escape(&pr))); + } + out.push_str(" \n"); + } + out.push_str("\n"); + out +} + +/// Default robots.txt body allowing all and pointing at a sitemap URL. +pub fn build_robots(sitemap_url: Option<&str>) -> String { + let mut out = String::from("User-agent: *\nAllow: /\n"); + if let Some(u) = sitemap_url.filter(|s| !s.is_empty()) { + out.push_str(&format!("Sitemap: {u}\n")); + } + out +} + +/// ABI helper returning `{ body }`. +#[allow(dead_code)] +pub fn robots_json(sitemap_url: Option<&str>, body: Option<&str>) -> Value { + let text = match body.filter(|s| !s.is_empty()) { + Some(b) => b.to_string(), + None => build_robots(sitemap_url), + }; + json!({ "body": text }) +} diff --git a/plugins/web/src/table.rs b/plugins/web/src/table.rs index 9e9e4d4..8df2c63 100644 --- a/plugins/web/src/table.rs +++ b/plugins/web/src/table.rs @@ -66,7 +66,10 @@ fn boolish(v: &Value) -> bool { } } -/// Schema table → `[{name,type,nullable}, …]` +const UNIQUE_KEYS: &[&str] = &["唯一", "unique", "uniq"]; +const INDEX_KEYS: &[&str] = &["索引", "index", "idx"]; + +/// Schema table → `[{name,type,nullable,unique,index}, …]` pub fn as_fields(table: &Value) -> Value { match table { Value::Array(rows) => { @@ -81,7 +84,15 @@ pub fn as_fields(table: &Value) -> Value { .map(cell_str) .unwrap_or_else(|| "text".into()); let nullable = pick(m, NULL_KEYS).map(boolish).unwrap_or(true); - out.push(json!({ "name": name, "type": ty, "nullable": nullable })); + let unique = pick(m, UNIQUE_KEYS).map(boolish).unwrap_or(false); + let index = pick(m, INDEX_KEYS).map(boolish).unwrap_or(false); + out.push(json!({ + "name": name, + "type": ty, + "nullable": nullable, + "unique": unique, + "index": index, + })); } } Value::Array(out) @@ -90,6 +101,8 @@ pub fn as_fields(table: &Value) -> Value { let names = pick(m, FIELD_KEYS).map(as_str_list).unwrap_or_default(); let types = pick(m, TYPE_KEYS).map(as_str_list).unwrap_or_default(); let nulls = pick(m, NULL_KEYS).map(as_str_list).unwrap_or_default(); + let uniques = pick(m, UNIQUE_KEYS).map(as_str_list).unwrap_or_default(); + let indexes = pick(m, INDEX_KEYS).map(as_str_list).unwrap_or_default(); let mut out = Vec::new(); for (i, name) in names.iter().enumerate() { if name.is_empty() { @@ -100,7 +113,21 @@ pub fn as_fields(table: &Value) -> Value { .get(i) .map(|s| boolish(&Value::String(s.clone()))) .unwrap_or(true); - out.push(json!({ "name": name, "type": ty, "nullable": nullable })); + let unique = uniques + .get(i) + .map(|s| boolish(&Value::String(s.clone()))) + .unwrap_or(false); + let index = indexes + .get(i) + .map(|s| boolish(&Value::String(s.clone()))) + .unwrap_or(false); + out.push(json!({ + "name": name, + "type": ty, + "nullable": nullable, + "unique": unique, + "index": index, + })); } Value::Array(out) } diff --git a/tests/ext/web-w7-finish-smoke.mq.md b/tests/ext/web-w7-finish-smoke.mq.md new file mode 100644 index 0000000..4d8b878 --- /dev/null +++ b/tests/ext/web-w7-finish-smoke.mq.md @@ -0,0 +1,82 @@ +--- +title: web W7 finish smoke — unique index, sitemap, cache-control (offline) +import web:ext/web/web.mq.md +import plugin:lib/plugin.mq.md +import sys:lib/sys.mq.md +--- + +# main + +*p = > plugin.native_path name="web"* +1. `p` + > plugin.load path=`p` +2. * + > sys.exit code=1 + +*store = > web.db url="sqlite:web-fixtures/data/w7-smoke.db"* +> `store`.exec sql="DROP TABLE IF EXISTS posts" + +`fields` = + +| 字段 | 类型 | 可空 | 唯一 | 索引 | +|------|------|------|------|------| +| id | int | 否 | | | +| slug | text | 否 | 是 | 是 | +| title | text | 否 | | | + +> `store`.init name=posts fields=`fields` + +*idx = > `store`.query sql="SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='posts'"* +*nidx = > len value=idx[^rows]* +1. `nidx` >= 1 + > print text=unique-ok +2. * + > print text=unique-fail + +`items` = + +| loc | 更新 | +|-----|------| +| / | 2026-08-28 | +| /about | 2026-08-28 | + +*sm = > web_sitemap_build base="https://example.com" items=`items`* +*xml = sm[^xml]* +1. `xml` + > print text=sitemap-ok +2. * + > print text=sitemap-fail + +*pg = > web.page title="w7"* +*notfound = > web.page title="Missing"* +*app = > web.app page=`pg` port=18120* +*app = > `app`.configure cache_control="public, max-age=60" access_log=False* +*app = > `app`.redirect from="/old" to="/new" permanent=True* +*app = > `app`.error_page status=404 page=`notfound`* +*app = > `app`.sitemap path="/sitemap.xml" base="https://example.com" items=`items`* +*app = > `app`.robots sitemap="https://example.com/sitemap.xml"* +*rd = app[^redirects]* +*has = rd[^/old]* +1. `has` + > print text=redirect-ok +2. * + > print text=redirect-fail + +*rb = app[^robots_body]* +1. `rb` + > print text=robots-ok +2. * + > print text=robots-fail + +*mw = app[^middleware]* +*cc = mw[^cache_control]* +1. `cc` == "public, max-age=60" + > print text=cache-control-ok +2. * + > print text=cache-control-fail + +*p404 = app[^page_404]* +1. `p404` + > print text=error-page-ok +2. * + > print text=error-page-fail diff --git a/tests/ext/web-w7-live-server.mq.md b/tests/ext/web-w7-live-server.mq.md new file mode 100644 index 0000000..bd79fc6 --- /dev/null +++ b/tests/ext/web-w7-live-server.mq.md @@ -0,0 +1,31 @@ +--- +title: web W7 live server — 404, redirect, sitemap, robots, cache-control +import web:ext/web/web.mq.md +import plugin:lib/plugin.mq.md +import sys:lib/sys.mq.md +--- + +# main + +*p = > plugin.native_path name="web"* +1. `p` + > plugin.load path=`p` +2. * + > sys.exit code=1 + +`items` = + +| loc | note | +|-----|------| +| / | home | +| /about | about | + +*pg = > web.page title="w7-live"* +*nf = > web.page title="Not Found Page"* +*app = > web.app page=`pg` port=18121* +*app = > `app`.configure cache_control="public, max-age=120"* +*app = > `app`.redirect from="/legacy" to="/" permanent=True* +*app = > `app`.error_page status=404 page=`nf`* +*app = > `app`.sitemap path="/sitemap.xml" base="http://127.0.0.1:18121" items=`items`* +*app = > `app`.robots sitemap="http://127.0.0.1:18121/sitemap.xml"* +> `app`.listen diff --git a/tests/gold.rs b/tests/gold.rs index 1f33cc6..fae697d 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -1978,6 +1978,141 @@ access-log-ok", ); } +#[test] +fn ext_web_w7_finish_smoke() { + let status = Command::new("cargo") + .args(["build", "-p", "marqdo_plugin_web"]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .status() + .expect("build web plugin"); + assert!(status.success(), "failed to build marqdo_plugin_web"); + assert_out( + "tests/ext/web-w7-finish-smoke.mq.md", + "unique-ok +sitemap-ok +redirect-ok +robots-ok +cache-control-ok +error-page-ok", + ); +} + +#[test] +fn ext_web_w7_live() { + let status = Command::new("cargo") + .args(["build", "-p", "marqdo_plugin_web"]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .status() + .expect("build web plugin"); + assert!(status.success(), "failed to build marqdo_plugin_web"); + + let script = "tests/ext/web-w7-live-server.mq.md"; + let bin = env!("CARGO_BIN_EXE_marqdo"); + let mut child = Command::new(bin) + .args(["run", script]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("spawn w7 live server"); + + let base = "http://127.0.0.1:18121"; + let ready = std::time::Instant::now(); + loop { + if ready.elapsed().as_secs() > 10 { + let _ = child.kill(); + panic!("w7 live server did not start in time"); + } + let out = Command::new("curl") + .args([ + "-s", + "-o", + "/dev/null", + "-w", + "%{http_code}", + &format!("{base}/"), + ]) + .output() + .expect("curl probe"); + if String::from_utf8_lossy(&out.stdout) == "200" { + break; + } + std::thread::sleep(std::time::Duration::from_millis(300)); + } + + // Permanent redirect + let out = Command::new("curl") + .args([ + "-s", + "-o", + "/dev/null", + "-w", + "%{http_code}|%{redirect_url}", + &format!("{base}/legacy"), + ]) + .output() + .expect("curl redirect"); + let redir = String::from_utf8_lossy(&out.stdout); + assert!( + redir.starts_with("301|") && redir.contains("/"), + "redirect got {redir}" + ); + + // Custom 404 + let out = Command::new("curl") + .args(["-s", "-w", "\n%{http_code}", &format!("{base}/no-such")]) + .output() + .expect("curl 404"); + let body = String::from_utf8_lossy(&out.stdout); + assert!( + body.contains("Not Found Page") && body.trim_end().ends_with("404"), + "404 body={body}" + ); + + // Sitemap + let out = Command::new("curl") + .args(["-s", &format!("{base}/sitemap.xml")]) + .output() + .expect("curl sitemap"); + let sm = String::from_utf8_lossy(&out.stdout); + assert!( + sm.contains("