ext/web P3: RBAC, audit timestamps, FK, gallery, ETag

Complete the remaining network-extension gaps so media sites can
gate by role, auto-stamp rows, declare foreign keys, browse uploads,
and conditional-get downloads.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-28 11:37:02 +08:00
1 parent 2d51f29199
commit c6360adccb
13 files changed
+938 -35

No files matched your search

+7 -6
View File
@@ -119,7 +119,7 @@
| 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 终止或反代提示) | ✅ **文档锁定(W7)**:进程内 TLS 不启用;反代终止 HTTPS,并设 `cookie_secure=True` | 文档 + `cookie_secure` | P2 ✅ |
| A7 | **gzip / br 压缩** + **ETag / Cache-Control 缓存头** | gzip ✅(W1);Cache-Control ✅ **已实现(W7,`装配 缓存控制=`)**;ETag 未做(可后续) | `plugins/web` | P2 ✅ |
| A7 | **gzip / br 压缩** + **ETag / Cache-Control 缓存头** | gzip ✅(W1);Cache-Control ✅(W7);ETag ✅ **已实现(P3,下载/相册 + If-None-Match→304)** | `plugins/web` | P2 ✅ |
| A8 | **访问日志**(请求方法/路径/状态/耗时) | ✅ **已实现(W6b)**:`configure access_log=True` → stderr | `plugins/web` | P2 ✅ |
| A9 | **自定义 404/500 错误页** | ✅ **已实现(W7)**:`app.error_page` / `应用.错误页`;404 fallback + 500 页袋 | `plugins/web` | P2 ✅ |
| A10 | **重定向增强**(permanent/自定义状态码) | ✅ **已实现(W7)**:`app.redirect` → 301/307 | `plugins/web` | P3 ✅ |
@@ -134,10 +134,10 @@
| 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 表增强) | ✅ **已实现(W7)**:`唯一`/`unique`、`索引`/`index`(外键声明仍可后续) | `plugins/web` | P2 ✅ |
| B7 | **索引 / 唯一 / 外键声明**(init 表增强) | ✅ **已实现(W7+P3)**:`唯一`/`索引`/`外键`(`posts.id` / `posts`) | `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 |
| B10 | **时间戳 / 审计字段自动维护** | ✅ **已实现(P3)**:表含 `created_at`/`updated_at` 时 insert/update 自动写入 | `plugins/web` | P3 ✅ |
### 3.3 类 C:安全硬化(能上线的前提)
@@ -150,7 +150,7 @@
| 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 |
| C8 | **RBAC / 多角色**(作者/管理员/访客) | ✅ **已实现(P3)**:用户表 `role`/`角色`;`app.gate`;`/admin*` 默认需 `admin` | `plugins/web` + `ext/web` | P3 ✅ |
### 3.4 类 D:内容站点标配(博客/CMS 特有)
@@ -164,7 +164,7 @@
| 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 |
| D9 | **图片/附件库**(依赖 A5 上传 + 相册页) | ✅ **已实现(P3)**:`app.gallery` HTML 相册 + A5 上传/下载 | `ext/web` + `plugins/web` | P3 ✅ |
---
@@ -181,7 +181,8 @@
| **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/外键)非阻塞 |
| **W7** | 完结打磨 | Cache-Control、自定义 404/500、301 重定向、sitemap/robots、init 唯一/索引、HTTPS 反代文档 | A6 A7 A9 A10 B7 D2 ✅ |
| **P3** | 真·网络扩展补齐 | 审计时间戳、外键、RBAC 门禁、媒体相册、下载 ETag | B10 C8 D9 + ETag/FK ✅ |
### 4.2 边界判定要点(严格遵守既有约束)
+3 -2
View File
@@ -2,7 +2,7 @@
| | |
|---|---|
| 状态 | **W0–W7 网络扩展库完结**(P3 项如 RBAC/相册 UI 可选后续) |
| 状态 | **W0–W7 + P3 完结**(审计/RBAC/相册/ETag/外键已落地) |
| 日期 | 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) |
@@ -33,8 +33,9 @@
| **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** |
| **P3** | 审计时间戳 / 外键 / RBAC / 相册 / ETag | **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`、`tests/ext/web-w7-finish-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`、`tests/ext/web-p3-smoke.mq.md`、`ext_cli_add_web`。
---
+23 -4
View File
@@ -151,7 +151,7 @@ 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).
Create a table from a schema table. Optional columns: `唯一`/`unique`, `索引`/`index` (creates UNIQUE / INDEX), `外键`/`fk`/`references` (e.g. `posts.id` or `posts(id)`). Columns named `created_at` / `updated_at` are filled automatically on insert/update when present.
*url = self[^url]*
**> web_db_init url=`url` name=`name` fields=`fields`**
@@ -467,10 +467,29 @@ Register a WebSocket endpoint at `path` (e.g. `/live`). `mode` is `echo` (defaul
+ `users`
+ `session_ttl`=3600
Keep the app's `admin=True`, and gate `/admin*` behind a login page. `users` is an admin-users table (`|username|password|` / `|用户名|密码|`); unauthenticated requests redirect to `/admin/login`.
Keep the app's `admin=True`, and gate `/admin*` behind a login page. `users` is an admin-users table (`|username|password|` / `|用户名|密码|`); optional `role` / `角色` column (`admin` / `author` / `visitor`). Unauthenticated requests redirect to `/admin/login`. Registers a default gate `/admin*` → `admin`.
**> web_app_auth app=`self` users=`users` session_ttl=`session_ttl`**
## gate
+ `path`
+ `roles`=admin
Require one of `roles` (CSV) for `path` (prefix or `path*`). Anonymous sessions count as `visitor`.
**> web_app_gate app=`self` path=`path` roles=`roles`**
## gallery
+ `path`=/gallery
+ `storage`
+ `prefix`=uploads/
+ `title`=Gallery
+ `download_base`=/_media
Serve an HTML media gallery listing objects under `prefix` in `storage`, linking through `download_base`.
**> web_app_gallery app=`self` path=`path` storage=`storage` prefix=`prefix` title=`title` download_base=`download_base`**
## route_rss
+ `path`
+ `table`
@@ -555,7 +574,7 @@ Session/auth helper. Constructs a config object; `login` validates against the u
+ `username`
+ `password`
Validate credentials against the users table and create a session. Returns `{ok, session_id, username}`.
Validate credentials against the users table and create a session. Returns `{ok, session_id, username, role}`.
*users = self[^users]*
*ttl = self[^session_ttl]*
@@ -564,7 +583,7 @@ Validate credentials against the users table and create a session. Returns `{ok,
## check
+ `session_id`
Returns `{ok, username}` when the session is valid.
Returns `{ok, username, role}` when the session is valid.
**> web_auth_check session_id=`session_id`**
+21 -2
View File
@@ -138,7 +138,7 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
+ `名`
+ `字段`
从表结构创建表。可选列:`唯一`/`unique`、`索引`/`index`。
从表结构创建表。可选列:`唯一`/`unique`、`索引`/`index`、`外键`/`fk`/`references`(如 `posts.id`)。若存在 `created_at` / `updated_at` 列,插入/更新时自动写入。
*url = self[^url]*
**> web_db_init url=`url` name=`名` fields=`字段`**
@@ -449,10 +449,29 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
+ `用户表`
+ `会话时长`=3600
在保持 `后台=真` 的同时,为 `/admin*` 加登录门禁。`用户表` 是管理员表(`|用户名|密码|`);未登录请求重定向到 `/admin/login`。
在保持 `后台=真` 的同时,为 `/admin*` 加登录门禁。`用户表` 为 `|用户名|密码|`(可选 `角色`:`admin`/`author`/`visitor`);未登录重定向到 `/admin/login`。默认登记门禁 `/admin*` → `admin`。
**> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长`**
## 门禁
+ `路径`
+ `角色`=admin
要求会话角色落在 `角色`(CSV)之一才可访问 `路径`(前缀或 `路径*`)。匿名视为 `visitor`。
**> web_app_gate app=`self` path=`路径` roles=`角色`**
## 相册
+ `路径`=/gallery
+ `存储`
+ `前缀`=uploads/
+ `标题`=相册
+ `下载基址`=/_media
列出 `存储` 下 `前缀` 对象,生成 HTML 相册页,链接走 `下载基址`。
**> web_app_gallery app=`self` path=`路径` storage=`存储` prefix=`前缀` title=`标题` download_base=`下载基址`**
## 路由订阅
+ `路径`
+ `表`
+107
View File
@@ -115,10 +115,76 @@ fn sql_type(t: &str) -> &'static str {
"int" | "integer" => "INTEGER",
"real" | "float" | "double" => "REAL",
"blob" => "BLOB",
"timestamp" | "datetime" | "timestamptz" | "审计" => "TEXT",
_ => "TEXT",
}
}
/// Parse `posts.id` / `posts(id)` / `posts` → (`posts`, `id`).
fn parse_fk(raw: &str) -> Result<(String, String), String> {
let s = raw.trim();
if s.is_empty() {
return Err("empty foreign key".into());
}
if let Some((t, rest)) = s.split_once('(') {
let col = rest.trim().trim_end_matches(')').trim();
let t = ident(t)?.to_string();
let col = if col.is_empty() {
"id".into()
} else {
ident(col)?.to_string()
};
return Ok((t, col));
}
if let Some((t, c)) = s.split_once('.') {
return Ok((ident(t)?.to_string(), ident(c)?.to_string()));
}
Ok((ident(s)?.to_string(), "id".into()))
}
fn utc_now_iso() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
// Keep it SQL-friendly ISO without extra deps.
let days = secs / 86400;
let tod = secs % 86400;
let h = tod / 3600;
let m = (tod % 3600) / 60;
let s = tod % 60;
// Civil date from days since 1970-01-01 (Howard Hinnant algorithm).
let z = days as i64 + 719468;
let era = if z >= 0 { z } else { z - 146096 } / 146097;
let doe = (z - era * 146097) as u64;
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
let y = yoe as i64 + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = doy - (153 * mp + 2) / 5 + 1;
let mo = if mp < 10 { mp + 3 } else { mp - 9 };
let y = if mo <= 2 { y + 1 } else { y };
format!("{y:04}-{mo:02}-{d:02}T{h:02}:{m:02}:{s:02}Z")
}
/// Column names present on `table` (SQLite).
fn column_names(url: &str, table: &str, txn_id: Option<&str>) -> Result<Vec<String>, String> {
let table = ident(table)?;
let conn = conn_for(url, txn_id)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
let sql = format!("PRAGMA table_info(\"{table}\")");
let mut stmt = c.prepare(&sql).map_err(|e| e.to_string())?;
let rows = stmt
.query_map([], |row| row.get::<_, String>(1))
.map_err(|e| e.to_string())?;
let mut out = Vec::new();
for r in rows {
out.push(r.map_err(|e| e.to_string())?);
}
Ok(out)
}
fn cell_str(v: &Value) -> String {
match v {
Value::Null => String::new(),
@@ -240,6 +306,15 @@ pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> {
if unique && name != "id" {
col.push_str(" UNIQUE");
}
let fk_raw = c
.get("fk")
.or_else(|| c.get("外键"))
.map(cell_str)
.unwrap_or_default();
if !fk_raw.is_empty() {
let (ref_table, ref_col) = parse_fk(&fk_raw)?;
col.push_str(&format!(" REFERENCES \"{ref_table}\"(\"{ref_col}\")"));
}
parts.push(col);
if (index || unique) && name != "id" {
let kind = if unique { "UNIQUE " } else { "" };
@@ -266,25 +341,47 @@ pub fn insert(url: &str, table: &str, rows: &Value, txn_id: Option<&str>) -> Res
return crate::db_pg::insert(url, table, rows, txn_id);
}
let table = ident(table)?;
let cols_present = column_names(url, table, txn_id)?;
let has_created = cols_present.iter().any(|c| c == "created_at");
let has_updated = cols_present.iter().any(|c| c == "updated_at");
let rows = crate::table::as_rows(rows);
let arr = rows.as_array().ok_or("rows must be a list")?;
let conn = conn_for(url, txn_id)?;
let c = conn.lock().unwrap_or_else(|e| e.into_inner());
let mut n = 0i64;
let now = utc_now_iso();
for row in arr {
let obj = row.as_object().ok_or("row must be a map")?;
let mut cols = Vec::new();
let mut placeholders = Vec::new();
let mut vals = Vec::new();
let mut seen_created = false;
let mut seen_updated = false;
for (k, v) in obj {
if k == "@" || k == "行" || k == "row" {
continue;
}
let _ = ident(k)?;
if k == "created_at" {
seen_created = true;
}
if k == "updated_at" {
seen_updated = true;
}
cols.push(format!("\"{k}\""));
placeholders.push("?");
vals.push(to_sql(v));
}
if has_created && !seen_created {
cols.push("\"created_at\"".into());
placeholders.push("?");
vals.push(rusqlite::types::Value::Text(now.clone()));
}
if has_updated && !seen_updated {
cols.push("\"updated_at\"".into());
placeholders.push("?");
vals.push(rusqlite::types::Value::Text(now.clone()));
}
if cols.is_empty() {
continue;
}
@@ -665,17 +762,27 @@ pub fn update(
}
let table = ident(table)?;
let cols_present = column_names(url, table, txn_id)?;
let has_updated = cols_present.iter().any(|c| c == "updated_at");
let obj = row.as_object().ok_or("row must be a map")?;
let mut sets = Vec::new();
let mut vals = Vec::new();
let mut seen_updated = false;
for (k, v) in obj {
if k == "id" {
continue;
}
let _ = ident(k)?;
if k == "updated_at" {
seen_updated = true;
}
sets.push(format!("\"{k}\" = ?"));
vals.push(to_sql(v));
}
if has_updated && !seen_updated {
sets.push("\"updated_at\" = ?".into());
vals.push(rusqlite::types::Value::Text(utc_now_iso()));
}
if sets.is_empty() {
return Err("nothing to update".into());
}
+142
View File
@@ -75,6 +75,7 @@ fn pg_type(t: &str) -> &'static str {
"real" | "float" | "double" => "DOUBLE PRECISION",
"blob" | "bytea" => "BYTEA",
"bool" | "boolean" => "BOOLEAN",
"timestamp" | "datetime" | "timestamptz" | "审计" => "TIMESTAMPTZ",
_ => "TEXT",
}
}
@@ -203,6 +204,71 @@ fn parse_where_simple(where_v: Option<&Value>) -> Result<(Vec<String>, Vec<Value
Ok((exprs, vals))
}
fn parse_fk(raw: &str) -> Result<(String, String), String> {
let s = raw.trim();
if s.is_empty() {
return Err("empty foreign key".into());
}
if let Some((t, rest)) = s.split_once('(') {
let col = rest.trim().trim_end_matches(')').trim();
let t = ident(t)?.to_string();
let col = if col.is_empty() {
"id".into()
} else {
ident(col)?.to_string()
};
return Ok((t, col));
}
if let Some((t, c)) = s.split_once('.') {
return Ok((ident(t)?.to_string(), ident(c)?.to_string()));
}
Ok((ident(s)?.to_string(), "id".into()))
}
fn utc_now_iso() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let days = secs / 86400;
let tod = secs % 86400;
let h = tod / 3600;
let m = (tod % 3600) / 60;
let s = tod % 60;
let z = days as i64 + 719468;
let era = if z >= 0 { z } else { z - 146096 } / 146097;
let doe = (z - era * 146097) as u64;
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
let y = yoe as i64 + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = doy - (153 * mp + 2) / 5 + 1;
let mo = if mp < 10 { mp + 3 } else { mp - 9 };
let y = if mo <= 2 { y + 1 } else { y };
format!("{y:04}-{mo:02}-{d:02}T{h:02}:{m:02}:{s:02}Z")
}
fn column_names(url: &str, table: &str, txn_id: Option<&str>) -> Result<Vec<String>, String> {
let table = ident(table)?;
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let rows = query_sql(
&mut c,
"SELECT column_name FROM information_schema.columns WHERE table_name = ? ORDER BY ordinal_position",
&[json!(table)],
)?;
Ok(rows
.into_iter()
.filter_map(|r| {
r.get("column_name")
.or_else(|| r.get("COLUMN_NAME"))
.and_then(|v| v.as_str())
.map(|s| s.to_string())
})
.collect())
}
pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> {
let table = ident(table)?;
let cols = crate::table::as_fields(fields);
@@ -212,6 +278,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")
@@ -235,6 +302,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 part = if name == "id" && !has_pk && pg_type(&ty) == "INTEGER" {
has_pk = true;
format!("\"{name}\" SERIAL PRIMARY KEY")
@@ -246,9 +335,27 @@ pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> {
if !nullable {
col.push_str(" NOT NULL");
}
if unique {
col.push_str(" UNIQUE");
}
let fk_raw = c
.get("fk")
.or_else(|| c.get("外键"))
.map(cell_str)
.unwrap_or_default();
if !fk_raw.is_empty() {
let (ref_table, ref_col) = parse_fk(&fk_raw)?;
col.push_str(&format!(" REFERENCES \"{ref_table}\"(\"{ref_col}\")"));
}
col
};
parts.push(part);
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}\" ({})",
@@ -257,30 +364,55 @@ pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> {
let conn = pooled(url)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
c.batch_execute(&sql).map_err(|e| e.to_string())?;
for stmt in index_sql {
let _ = c.batch_execute(&stmt);
}
Ok(json!({ "_type": "db_table", "name": table, "url": url }))
}
pub fn insert(url: &str, table: &str, rows: &Value, txn_id: Option<&str>) -> Result<Value, String> {
let table = ident(table)?;
let cols_present = column_names(url, table, txn_id).unwrap_or_default();
let has_created = cols_present.iter().any(|c| c == "created_at");
let has_updated = cols_present.iter().any(|c| c == "updated_at");
let rows = crate::table::as_rows(rows);
let arr = rows.as_array().ok_or("rows must be a list")?;
let conn = conn_for(url, txn_id)?;
let mut c = conn.lock().unwrap_or_else(|e| e.into_inner());
let mut n = 0i64;
let now = utc_now_iso();
for row in arr {
let obj = row.as_object().ok_or("row must be a map")?;
let mut cols = Vec::new();
let mut ph = Vec::new();
let mut vals = Vec::new();
let mut seen_created = false;
let mut seen_updated = false;
for (k, v) in obj {
if k == "@" || k == "行" || k == "row" {
continue;
}
let _ = ident(k)?;
if k == "created_at" {
seen_created = true;
}
if k == "updated_at" {
seen_updated = true;
}
cols.push(format!("\"{k}\""));
ph.push("?");
vals.push(v.clone());
}
if has_created && !seen_created {
cols.push("\"created_at\"".into());
ph.push("?");
vals.push(json!(now.clone()));
}
if has_updated && !seen_updated {
cols.push("\"updated_at\"".into());
ph.push("?");
vals.push(json!(now.clone()));
}
if cols.is_empty() {
continue;
}
@@ -374,17 +506,27 @@ pub fn update(
txn_id: Option<&str>,
) -> Result<Value, String> {
let table = ident(table)?;
let cols_present = column_names(url, table, txn_id).unwrap_or_default();
let has_updated = cols_present.iter().any(|c| c == "updated_at");
let obj = row.as_object().ok_or("row must be a map")?;
let mut sets = Vec::new();
let mut vals = Vec::new();
let mut seen_updated = false;
for (k, v) in obj {
if k == "id" || k == "@" || k == "行" || k == "row" {
continue;
}
let _ = ident(k)?;
if k == "updated_at" {
seen_updated = true;
}
sets.push(format!("\"{k}\" = ?"));
vals.push(v.clone());
}
if has_updated && !seen_updated {
sets.push("\"updated_at\" = ?".into());
vals.push(json!(utc_now_iso()));
}
if sets.is_empty() {
return Ok(json!({ "ok": true, "updated": 0 }));
}
+208 -12
View File
@@ -104,6 +104,8 @@ pub fn listen(
robots_body: Option<String>,
page_404: Option<Value>,
page_500: Option<Value>,
gates: Vec<(String, Vec<String>)>,
gallery_routes: HashMap<String, Value>,
middleware: &Middleware,
) -> Result<Value, String> {
let mut form_owners = HashMap::new();
@@ -254,9 +256,22 @@ pub fn listen(
let route_path = path.clone();
app = app.route(
&path,
get(move |State(st): State<Arc<AppState>>, Path(params): Path<HashMap<String, String>>| {
get(move |State(st): State<Arc<AppState>>, headers: axum::http::HeaderMap, Path(params): Path<HashMap<String, String>>| {
let route_path = route_path.clone();
async move { download_get(st, &route_path, params) }
async move { download_get(st, &route_path, params, &headers) }
}),
);
}
let mut gallery_paths: Vec<String> = gallery_routes.keys().cloned().collect();
gallery_paths.sort();
for path in gallery_paths {
let cfg = gallery_routes.get(&path).cloned().unwrap_or_default();
app = app.route(
&path,
get(move |State(st): State<Arc<AppState>>| {
let cfg = cfg.clone();
async move { gallery_page(&st, &cfg) }
}),
);
}
@@ -338,6 +353,17 @@ pub fn listen(
app = app.fallback(get(fallback_404));
let app = crate::middleware::apply(app, middleware);
let app = if !gates.is_empty() {
let gates_for_mw = gates.clone();
app.layer(axum::middleware::from_fn(
move |req: axum::http::Request<axum::body::Body>, next: axum::middleware::Next| {
let gates = gates_for_mw.clone();
async move { rbac_middleware(gates, req, next).await }
},
))
} else {
app
};
let app = app.with_state(state);
let addr: SocketAddr = format!("{host}:{port}")
@@ -726,6 +752,7 @@ fn download_get(
st: Arc<AppState>,
route_path: &str,
params: HashMap<String, String>,
headers: &axum::http::HeaderMap,
) -> Response {
let cfg = match st.download_routes.get(route_path) {
Some(c) => c.clone(),
@@ -745,13 +772,25 @@ fn download_get(
Err(e) => return json_err(StatusCode::INTERNAL_SERVER_ERROR, &e),
};
let (bytes, content_type, filename) = got;
let etag = weak_etag(&bytes);
if let Some(inm) = headers
.get(header::IF_NONE_MATCH)
.and_then(|v| v.to_str().ok())
{
if inm
.split(',')
.any(|t| t.trim().eq_ignore_ascii_case(&etag))
{
return StatusCode::NOT_MODIFIED.into_response();
}
}
let disp = if cfg.disposition.eq_ignore_ascii_case("inline") {
"inline"
} else {
"attachment"
};
let safe_name = filename.replace('"', "_");
let mut resp = (
(
StatusCode::OK,
[
(
@@ -764,12 +803,164 @@ fn download_get(
HeaderValue::from_str(&format!("{disp}; filename=\"{safe_name}\""))
.unwrap_or_else(|_| HeaderValue::from_static("attachment")),
),
(
header::ETAG,
HeaderValue::from_str(&etag).unwrap_or_else(|_| HeaderValue::from_static("W/\"0\"")),
),
],
bytes,
)
.into_response();
let _ = &mut resp;
resp
.into_response()
}
fn weak_etag(bytes: &[u8]) -> String {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for b in bytes {
h ^= u64::from(*b);
h = h.wrapping_mul(0x0100_0000_01b3);
}
format!("W/\"{h:x}-{}\"", bytes.len())
}
fn path_matches(path: &str, pattern: &str) -> bool {
let pat = pattern.trim_end_matches('*');
if pattern.ends_with('*') {
path == pat.trim_end_matches('/') || path.starts_with(pat)
} else {
path == pattern || path.starts_with(&format!("{pattern}/"))
}
}
async fn rbac_middleware(
gates: Vec<(String, Vec<String>)>,
req: axum::http::Request<axum::body::Body>,
next: axum::middleware::Next,
) -> Response {
let path = req.uri().path().to_string();
let cookie = req
.headers()
.get(header::COOKIE)
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
for (prefix, roles) in &gates {
if !path_matches(&path, prefix) {
continue;
}
// Login page itself must stay reachable.
if path == "/admin/login" || path.starts_with("/admin/login?") {
break;
}
let role = session::session_role(cookie.as_deref());
if !session::role_allowed(&role, roles) {
if role == "visitor" && path.starts_with("/admin") {
return Redirect::to("/admin/login").into_response();
}
return (
StatusCode::FORBIDDEN,
Html(format!(
"<!doctype html><html><body><h1>403 Forbidden</h1><p>Role `{role}` cannot access `{path}`.</p></body></html>"
)),
)
.into_response();
}
break;
}
next.run(req).await
}
fn gallery_page(_st: &AppState, cfg: &Value) -> Response {
let storage_url = cfg
.get("storage")
.and_then(|v| v.as_str())
.unwrap_or("");
let prefix = cfg
.get("prefix")
.and_then(|v| v.as_str())
.unwrap_or("");
let title = cfg
.get("title")
.and_then(|v| v.as_str())
.unwrap_or("Gallery");
let download_base = cfg
.get("download_base")
.and_then(|v| v.as_str())
.unwrap_or("/_media");
let listed = storage::list(storage_url, Some(prefix)).unwrap_or_else(|_| json!({ "keys": [] }));
let keys = listed
.get("keys")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default();
let mut items = String::new();
for k in &keys {
let key = k.as_str().unwrap_or("");
if key.is_empty() {
continue;
}
let href = format!(
"{}/{}",
download_base.trim_end_matches('/'),
key.trim_start_matches('/')
);
let name = key.rsplit('/').next().unwrap_or(key);
let lower = name.to_ascii_lowercase();
let is_img = lower.ends_with(".png")
|| lower.ends_with(".jpg")
|| lower.ends_with(".jpeg")
|| lower.ends_with(".gif")
|| lower.ends_with(".webp")
|| lower.ends_with(".svg");
if is_img {
items.push_str(&format!(
"<figure class=\"gal-item\"><a href=\"{h}\"><img src=\"{h}\" alt=\"{n}\"/></a><figcaption>{n}</figcaption></figure>",
h = esc(&href),
n = esc(name)
));
} else {
items.push_str(&format!(
"<figure class=\"gal-item\"><a class=\"gal-file\" href=\"{h}\">{n}</a></figure>",
h = esc(&href),
n = esc(name)
));
}
}
if items.is_empty() {
items = "<p class=\"gal-empty\">No media yet.</p>".into();
}
let html = format!(
r#"<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>{title}</title>
<style>
body{{font-family:system-ui,sans-serif;margin:0;padding:1.5rem;background:#f6f4ef;color:#1a1a1a}}
h1{{margin:0 0 1rem;font-size:1.5rem}}
.gal{{display:grid;grid-template-columns:repeat(auto-fill,minmax(140px,1fr));gap:1rem}}
.gal-item{{margin:0;background:#fff;border:1px solid #ddd;border-radius:6px;overflow:hidden}}
.gal-item img{{display:block;width:100%;height:120px;object-fit:cover}}
.gal-item figcaption,.gal-file{{display:block;padding:.5rem;font-size:.85rem;word-break:break-all}}
.gal-empty{{color:#666}}
</style></head>
<body><h1>{title}</h1><div class="gal">{items}</div></body></html>"#,
title = esc(title),
items = items
);
let etag = weak_etag(html.as_bytes());
(
StatusCode::OK,
[
(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
),
(
header::ETAG,
HeaderValue::from_str(&etag).unwrap_or_else(|_| HeaderValue::from_static("W/\"0\"")),
),
],
html,
)
.into_response()
}
fn admin_gate(st: &AppState, cookie_header: Option<&str>) -> Option<Response> {
@@ -779,7 +970,7 @@ fn admin_gate(st: &AppState, cookie_header: Option<&str>) -> Option<Response> {
if st.db_url.is_none() {
return Some(Html(admin_shell(st, "Admin", None, "<p class=\"flash err\">No database bound to this app.</p>")).into_response());
}
// Login-gated admin: require a valid session cookie.
// Login-gated admin: require a valid admin-role session.
if st.auth_users.is_some() && !admin_authed(st, cookie_header) {
return Some(
Redirect::to("/admin/login")
@@ -789,12 +980,16 @@ fn admin_gate(st: &AppState, cookie_header: Option<&str>) -> Option<Response> {
None
}
/// True when a valid admin session cookie is present.
/// True when a session with role `admin` is present (legacy users without role → admin).
fn admin_authed(_st: &AppState, cookie_header: Option<&str>) -> bool {
let Some(sid) = session::session_id_from_cookie(cookie_header) else {
return false;
};
session::session_get(&sid, "username").is_some()
if session::session_get(&sid, "username").is_none() {
return false;
}
let role = session::session_role(cookie_header);
session::role_allowed(&role, &["admin".into()])
}
async fn admin_login_get(State(st): State<Arc<AppState>>, headers: axum::http::HeaderMap) -> Response {
@@ -830,17 +1025,18 @@ async fn admin_login_post(
append_set_cookie(&mut resp, set_cookie);
return resp;
}
if session::check_credentials(users, &username, &password).is_none() {
let Some((user, role)) = session::check_credentials(users, &username, &password) else {
rate_limit::record_failure(&ip, &username);
let mut resp =
Html(login_page(&st, Some("Invalid username or password."), Some(&csrf)))
.into_response();
append_set_cookie(&mut resp, set_cookie);
return resp;
}
};
rate_limit::clear_success(&ip, &username);
let sid = session::session_new(Some(st.session_ttl));
session::session_set(&sid, "username", json!(username));
session::session_set(&sid, "username", json!(user));
session::session_set(&sid, "role", json!(role));
let mut resp = Redirect::to("/admin").into_response();
let _ = resp.headers_mut().try_append(
axum::http::header::SET_COOKIE,
+120
View File
@@ -1098,6 +1098,84 @@ web_ffi!(web_app_auth, |args: &Value| {
"auth".to_string(),
json!({ "users": users, "session_ttl": session_ttl }),
);
// Default RBAC: `/admin*` requires role `admin` when auth is configured.
let mut gates = obj
.get("gates")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default();
let has_admin_gate = gates.iter().any(|g| {
g.get("path")
.and_then(|v| v.as_str())
.is_some_and(|p| p == "/admin" || p == "/admin*")
});
if !has_admin_gate {
gates.push(json!({ "path": "/admin*", "roles": ["admin"] }));
obj.insert("gates".into(), Value::Array(gates));
}
Ok(app)
});
web_ffi!(web_app_gate, |args: &Value| {
let mut app = args.get("app").cloned().unwrap_or(json!({}));
let path = arg_str(args, "path")?.to_string();
let roles_raw = arg_str_opt(args, "roles")
.or_else(|| arg_str_opt(args, "角色"))
.unwrap_or("admin");
let roles: Vec<String> = session::parse_roles_csv(roles_raw);
let obj = app
.as_object_mut()
.ok_or_else(|| "app must be a map".to_string())?;
let mut gates = obj
.get("gates")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default();
gates.push(json!({ "path": path, "roles": roles }));
obj.insert("gates".into(), Value::Array(gates));
Ok(app)
});
web_ffi!(web_app_gallery, |args: &Value| {
let mut app = args.get("app").cloned().unwrap_or(json!({}));
let path = normalize_route_path(
arg_str(args, "path")
.or_else(|_| arg_str(args, "路径"))
.unwrap_or("/gallery"),
)?;
let storage_url = storage_url_arg(args, "storage")
.or_else(|_| storage_url_arg(args, "存储"))?;
let prefix = arg_str_opt(args, "prefix")
.or_else(|| arg_str_opt(args, "前缀"))
.unwrap_or("uploads/")
.to_string();
let title = arg_str_opt(args, "title")
.or_else(|| arg_str_opt(args, "标题"))
.unwrap_or("Gallery")
.to_string();
let download_base = arg_str_opt(args, "download_base")
.or_else(|| arg_str_opt(args, "media"))
.or_else(|| arg_str_opt(args, "下载基址"))
.unwrap_or("/_media")
.to_string();
let obj = app
.as_object_mut()
.ok_or_else(|| "app must be a map".to_string())?;
let mut routes = obj
.get("gallery_routes")
.and_then(|v| v.as_object())
.cloned()
.unwrap_or_default();
routes.insert(
path,
json!({
"storage": storage_url,
"prefix": prefix,
"title": title,
"download_base": download_base,
}),
);
obj.insert("gallery_routes".into(), Value::Object(routes));
Ok(app)
});
@@ -1342,6 +1420,8 @@ web_ffi!(web_listen, |args: &Value| {
robots_body,
page_404,
page_500,
gates,
gallery_routes,
middleware,
cookie_secure,
) = if args.get("page").is_some() || args.get("host").is_some() {
@@ -1394,6 +1474,8 @@ web_ffi!(web_listen, |args: &Value| {
None,
None,
None,
Vec::<(String, Vec<String>)>::new(),
HashMap::new(),
middleware::Middleware::default(),
cookie_secure,
)
@@ -1528,6 +1610,34 @@ web_ffi!(web_listen, |args: &Value| {
.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());
let mut gates: Vec<(String, Vec<String>)> = Vec::new();
if let Some(arr) = app.get("gates").and_then(|v| v.as_array()) {
for g in arr {
let path = g
.get("path")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
if path.is_empty() {
continue;
}
let roles = match g.get("roles") {
Some(Value::Array(a)) => a
.iter()
.filter_map(|v| v.as_str().map(|s| s.to_ascii_lowercase()))
.collect(),
Some(Value::String(s)) => session::parse_roles_csv(s),
_ => vec!["admin".into()],
};
gates.push((path, roles));
}
}
let mut gallery_routes = HashMap::new();
if let Some(obj) = app.get("gallery_routes").and_then(|v| v.as_object()) {
for (k, v) in obj {
gallery_routes.insert(k.clone(), v.clone());
}
}
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"
@@ -1595,6 +1705,8 @@ web_ffi!(web_listen, |args: &Value| {
robots_body,
page_404,
page_500,
gates,
gallery_routes,
middleware,
cookie_secure,
)
@@ -1628,6 +1740,8 @@ web_ffi!(web_listen, |args: &Value| {
robots_body,
page_404,
page_500,
gates,
gallery_routes,
&middleware,
)
});
@@ -1870,6 +1984,12 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
("web_password_hash", "password", web_password_hash as PluginFn),
("web_auth_new", "users,session_ttl", web_auth_new as PluginFn),
("web_app_auth", "app,users,session_ttl", web_app_auth as PluginFn),
("web_app_gate", "app,path,roles", web_app_gate as PluginFn),
(
"web_app_gallery",
"app,path,storage,prefix,title,download_base",
web_app_gallery as PluginFn,
),
(
"web_app_route_ws",
"app,path,echo,mode",
+53 -8
View File
@@ -376,12 +376,14 @@ pub fn session_cookie(id: &str, ttl_sec: u64, secure: bool) -> String {
}
/// Validate credentials; supports argon2 hashes or legacy plaintext in the password column.
pub fn check_credentials(users: &Value, username: &str, password: &str) -> Option<String> {
let rows = match users {
/// Returns `(username, role)`. Missing role column defaults to `admin` (legacy admin tables).
pub fn check_credentials(users: &Value, username: &str, password: &str) -> Option<(String, String)> {
let rows = crate::table::as_rows(users);
let rows = match rows {
Value::Array(a) => a,
_ => return None,
};
for row in rows {
for row in &rows {
let m = match row {
Value::Object(m) => m,
_ => continue,
@@ -393,12 +395,48 @@ pub fn check_credentials(users: &Value, username: &str, password: &str) -> Optio
.map(cell_str)
.unwrap_or_default();
if u == username && password::verify_password(password, &p) {
return Some(u);
let role = pick_cell(m, &["角色", "role", "roles"])
.map(cell_str)
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "admin".into());
return Some((u, role.to_ascii_lowercase()));
}
}
None
}
/// Session role (defaults to `visitor` when anonymous / missing).
pub fn session_role(cookie_header: Option<&str>) -> String {
let Some(sid) = session_id_from_cookie(cookie_header) else {
return "visitor".into();
};
match session_get(&sid, "role") {
Some(Value::String(r)) if !r.is_empty() => r.to_ascii_lowercase(),
_ => {
if session_get(&sid, "username").is_some() {
"admin".into()
} else {
"visitor".into()
}
}
}
}
pub fn role_allowed(role: &str, allowed: &[String]) -> bool {
if allowed.is_empty() {
return true;
}
let role = role.to_ascii_lowercase();
allowed.iter().any(|a| a.eq_ignore_ascii_case(&role))
}
pub fn parse_roles_csv(raw: &str) -> Vec<String> {
raw.split(',')
.map(|s| s.trim().to_ascii_lowercase())
.filter(|s| !s.is_empty())
.collect()
}
fn pick_cell<'a>(m: &'a Map<String, Value>, keys: &[&str]) -> Option<&'a Value> {
keys.iter().find_map(|k| m.get(*k))
}
@@ -456,10 +494,11 @@ pub fn abi_auth_login(args: &Value) -> Result<Value, String> {
.get("session_ttl")
.and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64)));
match check_credentials(&users, username, password) {
Some(_) => {
Some((u, role)) => {
let id = session_new(ttl);
session_set(&id, "username", json!(username));
Ok(json!({ "ok": true, "session_id": id, "username": username }))
session_set(&id, "username", json!(u));
session_set(&id, "role", json!(role));
Ok(json!({ "ok": true, "session_id": id, "username": u, "role": role }))
}
None => Ok(json!({ "ok": false })),
}
@@ -468,7 +507,13 @@ pub fn abi_auth_login(args: &Value) -> Result<Value, String> {
pub fn abi_auth_check(args: &Value) -> Result<Value, String> {
let id = arg_str(args, "session_id")?;
match session_get(id, "username") {
Some(Value::String(u)) => Ok(json!({ "ok": true, "username": u })),
Some(Value::String(u)) => {
let role = match session_get(id, "role") {
Some(Value::String(r)) => r,
_ => "admin".into(),
};
Ok(json!({ "ok": true, "username": u, "role": role }))
}
_ => Ok(json!({ "ok": false })),
}
}
+10 -1
View File
@@ -68,8 +68,9 @@ fn boolish(v: &Value) -> bool {
const UNIQUE_KEYS: &[&str] = &["唯一", "unique", "uniq"];
const INDEX_KEYS: &[&str] = &["索引", "index", "idx"];
const FK_KEYS: &[&str] = &["外键", "fk", "references", "ref", "引用"];
/// Schema table → `[{name,type,nullable,unique,index}, …]`
/// Schema table → `[{name,type,nullable,unique,index,fk}, …]`
pub fn as_fields(table: &Value) -> Value {
match table {
Value::Array(rows) => {
@@ -86,12 +87,17 @@ pub fn as_fields(table: &Value) -> Value {
let nullable = pick(m, NULL_KEYS).map(boolish).unwrap_or(true);
let unique = pick(m, UNIQUE_KEYS).map(boolish).unwrap_or(false);
let index = pick(m, INDEX_KEYS).map(boolish).unwrap_or(false);
let fk = pick(m, FK_KEYS)
.map(cell_str)
.filter(|s| !s.is_empty())
.unwrap_or_default();
out.push(json!({
"name": name,
"type": ty,
"nullable": nullable,
"unique": unique,
"index": index,
"fk": fk,
}));
}
}
@@ -103,6 +109,7 @@ pub fn as_fields(table: &Value) -> Value {
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 fks = pick(m, FK_KEYS).map(as_str_list).unwrap_or_default();
let mut out = Vec::new();
for (i, name) in names.iter().enumerate() {
if name.is_empty() {
@@ -121,12 +128,14 @@ pub fn as_fields(table: &Value) -> Value {
.get(i)
.map(|s| boolish(&Value::String(s.clone())))
.unwrap_or(false);
let fk = fks.get(i).cloned().unwrap_or_default();
out.push(json!({
"name": name,
"type": ty,
"nullable": nullable,
"unique": unique,
"index": index,
"fk": fk,
}));
}
Value::Array(out)
+23
View File
@@ -0,0 +1,23 @@
---
title: web P3 live — gallery HTML + download ETag
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
*blob = > web.storage url="file:web-fixtures/data/p3-live-blobs"*
> `blob`.put key="uploads/hello.txt" body="etag-body" content_type="text/plain"
*pg = > web.page title="p3-live"*
*app = > web.app page=`pg` port=18131*
*app = > `app`.download path="/_media/{*key}" storage=`blob` disposition="inline"*
*app = > `app`.gallery path="/gallery" storage=`blob` prefix="uploads/" title="P3 Gallery" download_base="/_media"*
> `app`.listen
+106
View File
@@ -0,0 +1,106 @@
---
title: web P3 smoke — audit timestamps, FK, RBAC, gallery (offline)
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
import json:lib/json.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/p3-smoke.db"*
> `store`.exec sql="DROP TABLE IF EXISTS comments"
> `store`.exec sql="DROP TABLE IF EXISTS posts"
`post_fields` =
| 字段 | 类型 | 可空 |
|------|------|------|
| id | int | 否 |
| title | text | 否 |
| created_at | timestamp | 是 |
| updated_at | timestamp | 是 |
> `store`.init name="posts" fields=`post_fields`
`comment_fields` =
| 字段 | 类型 | 可空 | 外键 |
|------|------|------|------|
| id | int | 否 | |
| post_id | int | 否 | posts |
| body | text | 否 | |
> `store`.init name="comments" fields=`comment_fields`
`rows` =
| @ | title |
|---|-------|
| 1 | Hello P3 |
> `store`.insert table="posts" rows=`rows`
*got = > `store`.get table="posts" id=1*
*ca = got[^created_at]*
1. `ca`
> print text=audit-insert-ok
2. *
> print text=audit-insert-fail
*upd = > json.parse text={"title":"Hello P3b"}*
> `store`.update table="posts" id=1 row=`upd`
*got2 = > `store`.get table="posts" id=1*
*ua2 = got2[^updated_at]*
1. `ua2`
> print text=audit-update-ok
2. *
> print text=audit-update-fail
*fk = > `store`.query sql="SELECT sql FROM sqlite_master WHERE type='table' AND name='comments'"*
*fkddl = fk[^rows][^1][^sql]*
1. `fkddl`
> print text=fk-ok
2. *
> print text=fk-fail
`users` =
| username | password | role |
|----------|----------|------|
| admin | secret | admin |
| alice | secret | author |
*login = > web_auth_login username="alice" password="secret" users=`users` session_ttl=120*
*role = login[^role]*
1. `role` == "author"
> print text=rbac-login-ok
2. *
> print text=rbac-login-fail
*pg = > web.page title="p3"*
*app = > web.app page=`pg` port=18130*
*app = > `app`.auth users=`users`*
*app = > `app`.gate path="/write*" roles="admin,author"*
*gates = app[^gates]*
*ng = > len value=`gates`*
1. `ng` >= 2
> print text=rbac-gate-ok
2. *
> print text=rbac-gate-fail
*blob = > web.storage url="file:web-fixtures/data/p3-gallery"*
> `blob`.put key="uploads/a.txt" body="hi" content_type="text/plain"
*app = > `app`.gallery path="/gallery" storage=`blob` prefix="uploads/" title="Media" download_base="/_media"*
*gal = app[^gallery_routes]*
*g = gal[^/gallery]*
1. `g`
> print text=gallery-ok
2. *
> print text=gallery-fail
+115
View File
@@ -2113,6 +2113,121 @@ fn ext_web_w7_live() {
let _ = child.wait();
}
#[test]
fn ext_web_p3_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-p3-smoke.mq.md",
"audit-insert-ok
audit-update-ok
fk-ok
rbac-login-ok
rbac-gate-ok
gallery-ok",
);
}
#[test]
fn ext_web_p3_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-p3-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 p3 live server");
let base = "http://127.0.0.1:18131";
let ready = std::time::Instant::now();
loop {
if ready.elapsed().as_secs() > 10 {
let _ = child.kill();
panic!("p3 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));
}
let out = Command::new("curl")
.args(["-s", &format!("{base}/gallery")])
.output()
.expect("curl gallery");
let gal = String::from_utf8_lossy(&out.stdout);
assert!(
gal.contains("P3 Gallery") && gal.contains("hello.txt"),
"gallery={gal}"
);
let out = Command::new("curl")
.args([
"-s",
"-D",
"-",
"-o",
"/dev/null",
&format!("{base}/_media/uploads/hello.txt"),
])
.output()
.expect("curl media");
let headers_raw = String::from_utf8_lossy(&out.stdout);
let headers = headers_raw.to_ascii_lowercase();
assert!(headers.contains("etag:"), "media headers={headers}");
let etag = headers_raw
.lines()
.find(|l| l.to_ascii_lowercase().starts_with("etag:"))
.map(|l| l.split_once(':').map(|(_, v)| v.trim().trim_matches('\r').to_string()).unwrap_or_default())
.expect("etag line");
let out = Command::new("curl")
.args([
"-s",
"-o",
"/dev/null",
"-w",
"%{http_code}",
"-H",
&format!("If-None-Match: {etag}"),
&format!("{base}/_media/uploads/hello.txt"),
])
.output()
.expect("curl 304");
assert_eq!(
String::from_utf8_lossy(&out.stdout),
"304",
"expected 304 for If-None-Match"
);
let _ = child.kill();
let _ = child.wait();
}
#[test]
fn ext_web_ws_broadcast_live() {
use futures_util::{SinkExt, StreamExt};