From 7645dd4c1fef9cf1d00b982dbf4620329c6cf56e Mon Sep 17 00:00:00 2001 From: cflmy Date: Tue, 25 Aug 2026 14:58:46 +0800 Subject: [PATCH] Extend network stack: session/auth, WebSocket, cookie/multipart primitives MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the network-extension wave to lib/net and the web ABI plugin: - lib/net cookie_parse / multipart_parse host primitives (EN/ZH) - plugins/web session store + /admin login gate + WebSocket client/server - ext/web author surface (EN/ZH) for session, auth, ws - form update flow and page-level validation echo tests - fix string escapes: \xHH, \u{...}, unknown escapes kept verbatim - map Chinese component slots (导航/侧栏/页脚) so sidebar renders - resolve relative static/db paths against the entry script dir (entry_dir) - new tutorial chapter 14, EN/ZH example sites, and gold tests Co-authored-by: Cursor --- .gitignore | 3 + Cargo.lock | 286 +++++++++++++- doc/design/ext-web-net.md | 285 ++++++++++++++ doc/design/markdown-mapping.md | 5 +- doc/tutorial/03-变量与赋值.md | 5 + doc/tutorial/12-标准库.md | 25 +- doc/tutorial/14-网络与Web扩展.md | 205 ++++++++++ doc/tutorial/README.md | 1 + examples/web-net-site-zh/README.md | 19 + .../web-net-site-zh/components/foot.mq.md | 10 + examples/web-net-site-zh/components/nav.mq.md | 15 + .../web-net-site-zh/components/side.mq.md | 15 + examples/web-net-site-zh/db/articles.mq.md | 23 ++ examples/web-net-site-zh/db/index.mq.md | 19 + examples/web-net-site-zh/index.mq.md | 111 ++++++ examples/web-net-site-zh/public/live.js | 40 ++ examples/web-net-site-zh/styles/shell.mq.md | 42 ++ examples/web-net-site/README.md | 19 + examples/web-net-site/components/foot.mq.md | 10 + examples/web-net-site/components/nav.mq.md | 15 + examples/web-net-site/components/side.mq.md | 15 + examples/web-net-site/db/articles.mq.md | 23 ++ examples/web-net-site/db/index.mq.md | 19 + examples/web-net-site/index.mq.md | 129 +++++++ examples/web-net-site/public/live.js | 40 ++ examples/web-net-site/styles/shell.mq.md | 42 ++ ext/web/web.mq.md | 94 ++++- ext/web/网页.mq.md | 94 ++++- lib/net.mq.md | 16 + lib/网络.mq.md | 16 + plugins/web/Cargo.toml | 4 +- plugins/web/src/http.rs | 205 +++++++++- plugins/web/src/lib.rs | 250 +++++++++++- plugins/web/src/session.rs | 353 +++++++++++++++++ plugins/web/src/table.rs | 14 +- plugins/web/src/ws.rs | 132 +++++++ src/host/dispatch.rs | 12 + src/host/net.rs | 362 ++++++++++++++++++ src/host/plugin.rs | 14 + src/parse/expr.rs | 127 +++++- tests/ext/web-admin-smoke.mq.md | 7 + tests/ext/web-entrydir-smoke.mq.md | 39 ++ tests/ext/web-net-smoke.mq.md | 148 +++++++ tests/gold.rs | 98 ++++- tests/keywords/quoted-string.mq.md | 13 +- tests/lib/net-cookie.mq.md | 17 + tests/lib/net-multipart.mq.md | 19 + 47 files changed, 3380 insertions(+), 75 deletions(-) create mode 100644 doc/design/ext-web-net.md create mode 100644 doc/tutorial/14-网络与Web扩展.md create mode 100644 examples/web-net-site-zh/README.md create mode 100644 examples/web-net-site-zh/components/foot.mq.md create mode 100644 examples/web-net-site-zh/components/nav.mq.md create mode 100644 examples/web-net-site-zh/components/side.mq.md create mode 100644 examples/web-net-site-zh/db/articles.mq.md create mode 100644 examples/web-net-site-zh/db/index.mq.md create mode 100644 examples/web-net-site-zh/index.mq.md create mode 100644 examples/web-net-site-zh/public/live.js create mode 100644 examples/web-net-site-zh/styles/shell.mq.md create mode 100644 examples/web-net-site/README.md create mode 100644 examples/web-net-site/components/foot.mq.md create mode 100644 examples/web-net-site/components/nav.mq.md create mode 100644 examples/web-net-site/components/side.mq.md create mode 100644 examples/web-net-site/db/articles.mq.md create mode 100644 examples/web-net-site/db/index.mq.md create mode 100644 examples/web-net-site/index.mq.md create mode 100644 examples/web-net-site/public/live.js create mode 100644 examples/web-net-site/styles/shell.mq.md create mode 100644 plugins/web/src/session.rs create mode 100644 plugins/web/src/ws.rs create mode 100644 tests/ext/web-entrydir-smoke.mq.md create mode 100644 tests/ext/web-net-smoke.mq.md create mode 100644 tests/lib/net-cookie.mq.md create mode 100644 tests/lib/net-multipart.mq.md diff --git a/.gitignore b/.gitignore index d03b177..d591c6c 100644 --- a/.gitignore +++ b/.gitignore @@ -46,5 +46,8 @@ plugins/*/tmp/ # web smoke/example DBs (cwd-relative or fixture data dirs) /web-fixtures/ /tests/ext/web-fixtures/data/ +/tests/ext/entrydir-fixtures/data/ /examples/web-site/data/ /examples/web-site-zh/data/ +/examples/web-net-site/data/ +/examples/web-net-site-zh/data/ diff --git a/Cargo.lock b/Cargo.lock index d36da4f..383c977 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -134,6 +134,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" dependencies = [ "axum-core", + "base64", "bytes", "form_urlencoded", "futures-util", @@ -152,8 +153,10 @@ dependencies = [ "serde_json", "serde_path_to_error", "serde_urlencoded", + "sha1", "sync_wrapper", "tokio", + "tokio-tungstenite 0.29.0", "tower", "tower-layer", "tower-service", @@ -191,6 +194,15 @@ version = "2.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bstr" version = "1.13.0" @@ -208,6 +220,12 @@ version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.12.1" @@ -299,6 +317,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.5.0" @@ -308,12 +335,38 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "data-encoding" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" + [[package]] name = "difflib" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "doc-comment" version = "0.3.4" @@ -391,6 +444,17 @@ version = "0.3.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2cd50c473c80f6d7c3670a752354b8e569b1a7cbfdc0419ec88e5edad85e0dc7" +[[package]] +name = "futures-macro" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d6d3cde68c518367be28956066ddfef33813991b77a55005a69dae04bf3b10b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "futures-sink" version = "0.3.34" @@ -410,11 +474,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a77a90a256fce34da66415271e30f94ee91c57b04b8a2c042d9cf3220179deaa" dependencies = [ "futures-core", + "futures-macro", + "futures-sink", "futures-task", "pin-project-lite", "slab", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getopts" version = "0.2.24" @@ -435,6 +511,18 @@ dependencies = [ "wasi", ] +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", +] + [[package]] name = "hashbrown" version = "0.14.5" @@ -690,7 +778,7 @@ dependencies = [ "predicates", "pulldown-cmark", "serde_json", - "thiserror", + "thiserror 1.0.69", "tiny_http", "ureq", "zeroize", @@ -724,10 +812,12 @@ name = "marqdo_plugin_web" version = "0.1.0" dependencies = [ "axum", + "futures-util", "rusqlite", "serde", "serde_json", "tokio", + "tokio-tungstenite 0.24.0", "tower-http", ] @@ -825,6 +915,15 @@ version = "0.3.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e" +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "predicates" version = "3.1.0" @@ -892,6 +991,71 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rand" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22f6172bdec972074665ed81ed53b71da00bfc44b65a753cfde883ec4c702a1a" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + [[package]] name = "regex" version = "1.13.1" @@ -929,7 +1093,7 @@ checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" dependencies = [ "cc", "cfg-if", - "getrandom", + "getrandom 0.2.17", "libc", "untrusted", "windows-sys 0.52.0", @@ -1061,6 +1225,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shlex" version = "2.0.1" @@ -1157,7 +1332,16 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f" +dependencies = [ + "thiserror-impl 2.0.20", ] [[package]] @@ -1171,6 +1355,17 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "thiserror-impl" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.3", +] + [[package]] name = "tiny_http" version = "0.12.0" @@ -1225,6 +1420,30 @@ dependencies = [ "syn 3.0.3", ] +[[package]] +name = "tokio-tungstenite" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edc5f74e248dc973e0dbb7b74c7e0d6fcc301c694ff50049504004ef4d0cdcd9" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite 0.24.0", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f72a05e828585856dacd553fba484c242c46e391fb0e58917c942ee9202915c" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite 0.29.0", +] + [[package]] name = "tokio-util" version = "0.7.19" @@ -1311,6 +1530,46 @@ dependencies = [ "once_cell", ] +[[package]] +name = "tungstenite" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18e5b8366ee7a95b16d32197d0b2604b43a0be89dc5fac9f8e96ccafbaedda8a" +dependencies = [ + "byteorder", + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "rand 0.8.7", + "sha1", + "thiserror 1.0.69", + "utf-8", +] + +[[package]] +name = "tungstenite" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c01152af293afb9c7c2a57e4b559c5620b421f6d133261c60dd2d0cdb38e6b8" +dependencies = [ + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "rand 0.9.5", + "sha1", + "thiserror 2.0.20", +] + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + [[package]] name = "unicase" version = "2.9.0" @@ -1385,6 +1644,12 @@ dependencies = [ "serde", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "utf8_iter" version = "1.0.4" @@ -1424,6 +1689,15 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + [[package]] name = "wasm-bindgen" version = "0.2.127" @@ -1628,6 +1902,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + [[package]] name = "zerocopy" version = "0.8.56" diff --git a/doc/design/ext-web-net.md b/doc/design/ext-web-net.md new file mode 100644 index 0000000..9b12758 --- /dev/null +++ b/doc/design/ext-web-net.md @@ -0,0 +1,285 @@ +# 网络扩展波次:`ext/web` 鉴权 · WebSocket · 标准库网络原语 + +| | | +|---|---| +| 状态 | **Draft(本波次锁定设计)** | +| 日期 | 2026-08-24 | +| 相关 | [ext-web.md](ext-web.md) · [ext-abi.md](ext-abi.md) · [stdlib-modules.md](stdlib-modules.md) · [ext-cli.md](ext-cli.md) | +| 目标 | 补齐 `ext/web` 登录鉴权(session/cookie)、WebSocket 端点;为网络扩展所需的**基础设施解析原语**适当补强 `lib/net`(标准库)。 | + +--- + +## 0. 一句话 + +**领域能力(鉴权、WebSocket 服务器)全部走 ABI 插件 `plugins/web`;纯解析基础设施(cookie / multipart 解析、WS 客户端)按需进入标准库 `lib/net`,因为标准库需要支撑扩展库开发。** + +--- + +## 1. 边界判定:什么进标准库 `lib/net`,什么进扩展 + +### 1.1 跨语言调研结论 + +| 语言 | 标准库 | 扩展/生态 | 边界准则 | +|------|--------|----------|---------| +| Python | `urllib`(URL/基础 HTTP)、`http.cookies`(**cookie 解析器在标准库**) | `requests`/`httpx`(session/连接池/便利层) | 标准库 = 基础设施原语(解析器);便利层 = 第三方 | +| Node.js | `http`/`https`/`http2`(低层流式,不解析 body) | express、ws、formidable | 足够 fancy 就不进核心;核心只放几乎人人要用的低层原语;**先 userland 验证再拉进 core** | +| Go | `net/http`(服务器+客户端都在标准库)、`net/http/cookiejar`(**RFC 6265 cookie jar 在标准库**) | `golang.org/x/net`(publicsuffix 等演进性) | HTTP 是「与 stdin 同级」的核心能力;**cookie 解析/管理进标准库**;持久化等演进性能力在外 | +| Rust | 极小 std(无 HTTP) | `http`(词汇)→ `hyper`(传输)→ `reqwest`(便利) | 分层;HTTP 因生态多样不进 std | + +**共识**: +1. **cookie 解析/管理(RFC 6265)**在 Python/Go 都是**标准库**(`http.cookies`、`net/http/cookiejar`)。 +2. **multipart/form-data 解析**在 Go 是标准库(`net/http`),在 Python 是第三方 `multipart`。 +3. **WebSocket** 几乎总在扩展/第三方(Node `ws`、Python `websockets`、Go `gorilla/websocket`),**很少进语言标准库**。 +4. **session 是领域级便利**,几乎所有语言都在框架层(express-session、Django session),不在语言标准库。 + +### 1.2 Marqdo 判定(锁定) + +| 能力 | 类型 | 归属 | 理由 | +|------|------|------|------| +| `cookie.parse` / `cookie.parse_response` | 基础设施解析 | **`lib/net`(标准库)** | 对齐 Go/Python 标准库;`ext/web` 鉴权依赖 | +| `multipart.parse` | 基础设施解析 | **`lib/net`(标准库)** | 对齐 Go 标准库;`ext/web` 文件上传依赖 | +| `ws.connect`(客户端) | 基础连接 | **`plugins/web`(ABI)+ `ext/web`** | WS 栈(tokio-tungstenite)已在 web 插件依赖链;核心 `Cargo.toml` 锁定 rustc 1.81 不宜再引入 WS 栈;符合「尽量只 ABI」 | +| `web.session_*`(session 管理) | 领域 | **`plugins/web`(ABI)+ `ext/web`** | session 是框架层便利,不进标准库 | +| `/admin` 登录鉴权 | 领域 | **`plugins/web`(ABI)+ `ext/web`** | 网站领域能力 | +| `web_ws`(服务器端点) | 领域 | **`plugins/web`(ABI)+ `ext/web`** | WebSocket 服务器是网站能力 | + +**实现约束(用户要求)**: +- 标准库新增的**解析原语**(cookie/multipart)是纯函数、无网络 I/O,作为 L0.5 宿主原语进 `src/host/`(与 `url_encode` 同模式),由 `lib/net` 薄包装——这符合「标准库需要支持扩展库开发」。 +- **涉及网络 I/O 的 WS 能力**(客户端 + 服务器端点)一律进 `plugins/web`(ABI v2),由 `ext/web` / `ext/web/网页.mq.md` 包装。核心运行时(rustc 1.81 锁定)**不再新增** WS 栈依赖。 +- **网站领域能力**(session、鉴权中间件)一律进 `plugins/web`(ABI v2),由 `ext/web` 包装。 +- **禁止**:为这些能力修改核心语法;禁止在 `src/host/` 塞领域逻辑。 + +--- + +## 2. 标准库 `lib/net` 新增(基础设施原语) + +### 2.1 `cookie.parse` / `解析Cookie` +- 形参:`text`(`Cookie:` 请求头或 `Set-Cookie:` 响应头) +- 结果:`[{name, value, path, domain, expires, max_age, secure, http_only, same_site}]` +- 行为:RFC 6265 解析;`Set-Cookie` 多 cookie 分条;无属性则字段为空。 + +**英文(`lib/net.mq.md`):** +```markdown +## cookie_parse + + `text` + +**> host_cookie_parse text=`text`** +``` + +**中文(`lib/网络.mq.md`):** +```markdown +## 解析Cookie + + `内容` + +**> host_cookie_parse text=`内容`** +``` + +### 2.2 `multipart.parse` / `解析多部分` +- 形参:`body`(文本)、`boundary` +- 结果:`[{name, filename, content_type, value}]`(`value` 为字段值;文件字段用 `filename` + `content_type` + `value`(base64 或文本)) +- 行为:解析 `multipart/form-data` 正文;字段名 → value;文件字段带 filename。 + +**英文:** +```markdown +## multipart_parse + + `body` + + `boundary` + +**> host_multipart_parse body=`body` boundary=`boundary`** +``` + +**中文:** +```markdown +## 解析多部分 + + `正文` + + `边界` + +**> host_multipart_parse body=`正文` boundary=`边界`** +``` + +### 2.3 WebSocket 客户端 — 见 §3.4(`plugins/web` ABI) + +WS 客户端为领域能力,放 `plugins/web`(ABI)而非标准库,理由见 §1.2。`lib/net` 不新增 WS 客户端包装,避免标准库依赖 ext 插件。 + +--- + +## 3. `plugins/web`(ABI v2)新增:领域能力 + +### 3.1 Session 存储(`web_session_*`) +插件进程内 `static` 内存存储(`HashMap>`),与 `web_listen` 同生命周期。 + +| ABI 名 | 形参 | 结果 | +|--------|------|------| +| `web_session_new` | 可选 `ttl_sec` | `{id}`(新 session id) | +| `web_session_set` | `id`, `key`, `value` | `{ok:true}` | +| `web_session_get` | `id`, `key` | 值或 `{ok:false}` | +| `web_session_del` | `id`, `key` | `{ok:true}` | +| `web_session_destroy` | `id` | `{ok:true}` | + +### 3.2 `/admin` 登录鉴权(`web_auth_*`) +- 作者面:`web.auth` 类(`网页.鉴权`),形参 `admin_users`(`|用户名|密码|` 表)、可选 `session_ttl`。 +- `listen` 时若配置了 auth:`/admin*` 未登录 → 跳转 `/admin/login`;登录页 `POST` → 校验 `admin_users` 表 → 写 session cookie → 重定向。 +- 登出:`/admin/logout` → 销毁 session。 + +| ABI 名 | 形参 | 结果 | +|--------|------|------| +| `web_auth_login` | `username`, `password`, `users`, `session_ttl` | `{ok:true, session_id}` 或 `{ok:false}` | +| `web_auth_check` | `session_id`, `users` | `{ok:true}` 或 `{ok:false}` | +| `web_auth_logout` | `session_id` | `{ok:true}` | + +### 3.3 WebSocket 服务器端点(`web_ws_route`) +- 作者面:`app.route_ws` / `应用.路由实时`,形参 `path`、可选 `echo=True`。 +- `listen` 时 `{path}` 升级为 WS:`echo=True` 回发原文;否则接收并丢弃(扩展点)。 + +| ABI 名 | 形参 | 结果 | +|--------|------|------| +| `web_app_route_ws` | `app`, `path`, `echo` | `{app}`(登记 WS 端点) | + +### 3.4 WebSocket 客户端(`web_ws_connect`) +- 作者面:`web.ws` / `网页.实时`,形参 `url`、`message`、可选 `headers`、`timeout_sec`。 +- 行为:客户端连接一次 WS,发 `message`,收**全部**服务器回文后关闭。**单请求-响应式**(非长驻),与 Marqdo 同步模型一致。 + +| ABI 名 | 形参 | 结果 | +|--------|------|------| +| `web_ws_connect` | `url`, `message`, `headers`(map), `timeout_sec` | `{ok:true, messages:[…]}` 或 `{ok:false, error}` | + +**英文(`ext/web/web.mq.md`):** +```markdown +# ws + + `timeout_sec`=30 + +## connect + + `url` + + `message` + + `headers`=None +``` + +**中文(`ext/web/网页.mq.md`):** +```markdown +# 实时 + + `超时秒`=30 + +## 连接 + + `地址` + + `消息` + + `headers`=None +``` + +> `plugins/web` 用 `tokio-tungstenite`(与 axum WS 同栈,版本兼容 rustc 1.81)。 + +--- + +## 4. `ext/web` 作者面新增 + +### 4.1 英文面(`web.mq.md`) + +```markdown +# auth + + `users` # |用户名|密码| 表 + + `session_ttl`=3600 + +## login + + `username` + + `password` + +## check + + `session_id` + +## logout + + `session_id` +``` + +`# app` 新增 `## route_ws`: +```markdown +## route_ws + + `path` + + `echo`=True +``` + +### 4.2 中文面(`网页.mq.md`) + +```markdown +# 鉴权 + + `用户表` + + `会话时长`=3600 + +## 登录 + + `用户名` + + `密码` + +## 校验 + + `会话id` + +## 登出 + + `会话id` +``` + +`# 应用` 新增 `## 路由实时`: +```markdown +## 路由实时 + + `路径` + + `回显`=真 +``` + +### 4.3 用法示例(英文) + +```markdown +# main + +`admins` = +| 用户名 | 密码 | +|--------|------| +| admin | secret | + +*store = > db.open * +*page = > web.page title="Site" * +*app = > web.app page=`page` db=`store` admin=True host=127.0.0.1 port=18081 * +*app = > `app`.route_ws path="/live" echo=True * +*app = > web.auth users=`admins` * +> `app`.listen + +*echo = > web.ws.connect url="ws://127.0.0.1:18081/live" message="hi" * +``` + +--- + +## 5. form 完善(现有功能验证 + 补测试) + +现状已具备(无需改代码,需验证): +- `action=update`:hidden `id` + `db.update`(form.rs `from_schema`/`submit` 已实现) +- 校验失败页级回显:`submit_and_respond` → `render_page_ex`(http.rs 已实现) + +本波次为 update 流程和页级回显补 gold 测试,确认端到端可用。 + +--- + +## 6. 测试与验收 + +| 测试 | 内容 | +|------|------| +| `tests/ext/web-net-smoke.mq.md` | session new/set/get/del + auth login/check/logout + `route_ws`/`app.auth` 登记 + ws 连接失败路径(离线) | +| `tests/lib/net-cookie.mq.md` | `cookie_parse` 解析 | +| `tests/lib/net-multipart.mq.md` | `multipart_parse` 解析 | +| `tests/ext/web-admin-smoke.mq.md` | form `action=update` 全流程 + 校验失败 `errors` 回显 | +| 现有 gold | 全量回归(树遍历 + 字节码) | + +验收:`cargo test --test gold` 全绿(除网络 live),双后端一致。 + +### 6.1 字典操作约束(作者面) + +Marqdo 原生字典操作:GFM 横表**构造**、脚注取元 `变量[^键]` **读取**。`ext/web` 作者面**不使用** `json.get` / `json.set` 做字典读取/改写——所有字段读取一律 `self[^键]`(或实例变量 `[^键]`),避免"用 json 标准库做字典操作"的反模式。`json.parse` / `json.stringify` 仅用于文本 ↔ 结构化数据的序列化转换(如 HTTP body 解析),不用于本地字典读写。 + +### 6.2 相对路径按入口脚本解析(host_query `entry_dir`) + +`static_dir`(如 `public`)与相对 db 路径(如 `sqlite:data/site.db`)此前按**进程 cwd** 解析——从仓库根目录跑 `examples/*/index.mq.md` 时,`/static` 404、db 会建到根目录,这是错误行为。 + +本波次修复:宿主新增 `host_query("entry_dir")`(返回入口 `.mq.md` 所在目录的绝对路径,回退 cwd),`plugins/web` 的 `web_listen` 将相对 `static_dir`、`db_url_of` 将相对 db 路径统一 `entry_dir().join(...)` 到绝对路径。于是无论从仓库根目录还是示例目录运行,静态资源与数据库都落在**脚本目录**下。绝对路径不受影响;`host_query` 不可用时回退旧 cwd 行为。 + +--- + +## 7. 非本波次 + +- Postgres / Redis / S3 驱动(roadmap W4,另行) +- WS 广播房间、多客户端(先 echo 单连接) +- session 持久化到 SQLite / 文件(先内存) +- 复杂权限 / RBAC \ No newline at end of file diff --git a/doc/design/markdown-mapping.md b/doc/design/markdown-mapping.md index bfecefe..6a039ae 100644 --- a/doc/design/markdown-mapping.md +++ b/doc/design/markdown-mapping.md @@ -190,8 +190,11 @@ Markdown **标记即语法**。输出**不是**语言架构原语,而是内置 | `\r` | 回车 | | `\\` | `\` | | `\"` | `"` | +| `\'` | `'` | +| `\xHH` | 十六进制字符(如 `\x22` → `"`) | +| `\u{...}` | Unicode 码点(如 `\u{4e2d}` → `中`) | -引号内仍可用 `` `变量` `` 插值。字面反斜杠+n 写 `\\n`(裸 token `\\n` 则就是两个字符)。 +引号内仍可用 `` `变量` `` 插值。字面反斜杠+n 写 `\\n`(裸 token `\\n` 则就是两个字符)。未知转义(如 `\q`)按字面保留,不报错。 --- diff --git a/doc/tutorial/03-变量与赋值.md b/doc/tutorial/03-变量与赋值.md index 62f8b94..a88809d 100644 --- a/doc/tutorial/03-变量与赋值.md +++ b/doc/tutorial/03-变量与赋值.md @@ -76,6 +76,11 @@ | `\r` | 回车 | | `\\` | 反斜杠 `\` | | `\"` | 双引号 `"` | +| `\'` | 单引号 `'` | +| `\xHH` | 十六进制字符(如 `\x22` → `"`) | +| `\u{...}` | Unicode 码点(如 `\u{4e2d}` → `中`) | + +未知转义(如 `\q`)**按字面保留**(`\q` → `\q`),不报错。 ### 变量插值 diff --git a/doc/tutorial/12-标准库.md b/doc/tutorial/12-标准库.md index e69bba4..695a0dd 100644 --- a/doc/tutorial/12-标准库.md +++ b/doc/tutorial/12-标准库.md @@ -35,7 +35,7 @@ Marqdo 内置函数无需导入即可使用;更丰富的能力通过导入 `li | `lib/plugin.mq.md` | `lib/插件.mq.md` | 加载原生 ABI 插件 | | `lib/time.mq.md` | `lib/时间.mq.md` | 时间 / 格式化 | | `lib/net.mq.md` | `lib/网络.mq.md` | HTTP(S) | -| `lib/json.mq.md` | — | JSON 解析 / 序列化 / 取元 | +| `lib/json.mq.md` | — | JSON 解析 / 序列化 | | `lib/math.mq.md` | `lib/数学.mq.md` | 数值、三角函数、随机、公式、绘图 | | `lib/foreign.mq.md` | `lib/外联.mq.md` | 运行异种语言代码块 | @@ -43,6 +43,17 @@ Marqdo 内置函数无需导入即可使用;更丰富的能力通过导入 `li ## 12.3 JSON 模块示例 +`json` 模块负责**文本 ↔ 结构化数据**的序列化转换,以及把文本字符串里的 `\n` 等转义还原: + +| 函数 | 作用 | +|------|------| +| `json.parse` | 解析 JSON 文本 | +| `json.stringify` | 序列化为 JSON 文本 | +| `json.get` | 取字段(键) | +| `json.keys` | 取所有键 | +| `json.set` | 设字段 | +| `json.append` | 向列表追加 | + ```markdown --- import json:lib/json.mq.md @@ -51,13 +62,14 @@ import json:lib/json.mq.md # main *obj = > json.parse text={"a":1}* -*a = > json.get value=`obj` key="a"* -> print text=`a` # 1 - *out = > json.stringify value=`obj`* > print text=`out` # {"a":1} + +*nl = > json.parse text={"n":"\n"}* +> print text=第一行`nl`[^n]第二行 # 换行还原 ``` +> **字典操作**:本地字典的读取用脚注取元 `变量[^键]`,**不要**用 `json.get`。`json.get` / `json.set` 仅用于跨文本边界的序列化场景(例如与外部 JSON API 交换数据时)。构造字典用 GFM 横表(见[第 9 章](./09-集合.md))。 `json` 常用函数: | 函数 | 作用 | @@ -154,4 +166,7 @@ import llm:ext/llm.mq.md ## 12.8 下一步 -你已经会使用标准库了。最后一章学习[第 13 章:命令行工具](./13-命令行工具.md)。 +你已经会使用标准库了。继续学习: + +- [第 13 章:命令行工具](./13-命令行工具.md) —— 运行、调试、扩展管理 +- [第 14 章:网络与 Web 扩展](./14-网络与Web扩展.md) —— `lib/net` 与 `ext/web` 实战 diff --git a/doc/tutorial/14-网络与Web扩展.md b/doc/tutorial/14-网络与Web扩展.md new file mode 100644 index 0000000..3002561 --- /dev/null +++ b/doc/tutorial/14-网络与Web扩展.md @@ -0,0 +1,205 @@ +# 第 14 章:网络与 Web 扩展 + +Marqdo 的网络能力分两层:**标准库 `lib/net`**(HTTP 客户端、cookie / multipart 解析)与**官方扩展 `ext/web`**(网页应用、数据库、表单、登录鉴权、WebSocket)。分层的依据是「代码即文档」的可读性——基础、通用、低依赖的进标准库;领域性强、依赖重、只在 Web 场景才用的进扩展。 + +## 14.1 概览 + +| 能力 | 位置 | 典型函数 | +|------|------|----------| +| HTTP(S) 客户端 | `lib/net` | `http_get` / `http_post` / `http_request` | +| Cookie 解析 | `lib/net` | `cookie_parse` | +| multipart/form-data 解析 | `lib/net` | `multipart_parse` | +| 网页应用 / 路由 / 静态 | `ext/web` | `web.page` / `web.app` | +| SQLite 数据库 | `ext/web` | `web.db` | +| 表单 | `ext/web` | `web.form` | +| 登录鉴权(session) | `ext/web` | `web.auth` / `app.auth` | +| WebSocket 客户端 / 端点 | `ext/web` | `web.ws` / `app.route_ws` | + +```bash +# 运行使用 ext/web 的程序前,先安装扩展 +marqdo ext add web +``` + +## 14.2 HTTP 客户端(`lib/net`) + +导入 `lib/net.mq.md`(中文 `lib/网络.mq.md`),用 `http_get` / `http_post` 发请求: + +```markdown +--- +import net:lib/net.mq.md +--- + +# main + +*resp = > net.http_get url="https://api.example.com/status"* +> print text=`resp`[^status] +``` + +`http_post` 默认以 JSON 提交,可用 `content_type=` / `headers=` 覆盖: + +```markdown +*body = > net.http_post url="https://api.example.com/echo" body={"msg":"hi"}* +> print text=`body` +``` + +> **字典操作**:返回值是字典(如 `resp`),直接取元 `resp[^键]`,不要用 `json.get`。 + +### Cookie 解析 + +`cookie_parse` 把 `Cookie` 请求头或 `Set-Cookie` 响应头解析成列表: + +```markdown +*req = > net.cookie_parse text="session=abc123; theme=dark"* +> print text=`req`[^1][^name] # session +> print text=`req`[^1][^value] # abc123 + +*resp = > net.cookie_parse text="id=42; Path=/; HttpOnly; SameSite=Lax" is_response=True* +> print text=`resp`[^1][^http_only] # True +``` + +### multipart 解析 + +`multipart_parse` 解析 `multipart/form-data` 正文(给定 `boundary`): + +```markdown +*parts = > net.multipart_parse body=`body` boundary="----WebKitFormBoundary"* +*field = parts[^1]* +> print text=`field`[^name] +``` + +## 14.3 网页应用(`ext/web`) + +`ext/web` 用类 + 方法装配页面。导入 `web:ext/web/web.mq.md`(中文 `网页:ext/web/网页.mq.md`)。 + +### 页面、组件、主体 + +```markdown +--- +import web:ext/web/web.mq.md +--- + +# main + +`首页` = + +| 组件 | 样式 | +|------|------| +| nav.`导航` | shell.`顶栏` | + +*page = > web.page title="我的站点" intro="

你好

"* +*page = > `page`.compose_components components=`首页`* +*html = > `page`.render* +> print text=`html` +``` + +### 应用 + 路由 + 监听 + +```markdown +*app = > web.app page=`page` host=127.0.0.1 port=18081* +*app = > `app`.route path="/about" page=`关于`* +> web_listen app=`app` +``` + +`listen` 提供 `/`、路由页、表单端点 `/_form/{id}`、可选静态目录与 `/admin`。 + +### 数据库(SQLite) + +```markdown +*store = > web.db url="sqlite:site.db"* +> `store`.init name=articles fields=`字段表` +> `store`.insert table=articles rows=`数据` +*rows = > `store`.select table=articles limit=10* +- [行](rows) + > print text=`行`[^title] +``` + +## 14.4 登录鉴权(session / cookie) + +给 `/admin*` 加登录门禁:`app.auth` 挂用户表,未登录重定向到 `/admin/login`。 + +```markdown +`管理员` = + +| 行 | 用户名 | 密码 | +|----|--------|------| +| 1 | admin | secret | +| 2 | 站长 | pw123 | + +*app = > web.app page=`page` admin=True* +*app = > `app`.auth users=`管理员` session_ttl=3600* +> web_listen app=`app` +``` + +`session_ttl` 是会话有效期(秒),过期后重新登录。 + +**独立鉴权工具** `web.auth`:登录、校验、登出(不依赖页面),返回 `{ok, session_id, username}`: + +```markdown +*auth = > web.auth users=`管理员` session_ttl=3600* +*login = > `auth`.login username="admin" password="secret"* +1. `login`[^ok] + > print text=登录成功:`login`[^username] +2. * + > print text=登录失败 + +*sid = login[^session_id]* +*check = > `auth`.check session_id=`sid`* +> print text=`check`[^username] + +> `auth`.logout session_id=`sid` +``` + +## 14.5 WebSocket + +`app.route_ws` 注册端点,`web.ws.connect` 单次请求–响应: + +```markdown +*app = > `app`.route_ws path="/live" echo=True* +> web_listen app=`app` +``` + +客户端(另开终端): + +```markdown +*ws = > web.ws timeout_sec=30* +*echo = > `ws`.connect url="ws://127.0.0.1:18081/live" message="hi"* +1. `echo`[^ok] + > print text=`echo`[^messages] +2. * + > print text=连接失败:`echo`[^error] +``` + +## 14.6 字典操作:用表格与取元,不用 `json.get`/`json.set` + +Marqdo 用 **GFM 表格**构造字典、用 **脚注取元 `变量[^键]`** 读取字典——这是语言原生能力,是「代码即文档」的体现。**不要**用 `json.get` / `json.set` 读写本地字典(那只用于文本 ↔ 结构化的序列化,如 `json.parse` / `json.stringify`)。 + +构造字典(横表,≥2 列 + 1 行): + +```markdown +`配置` = + +| 主机 | 端口 | +|------|------| +| 127.0.0.1 | 18081 | +``` + +读取字典(取元): + +```markdown +> print text=`配置`[^主机] # 127.0.0.1 +> print text=`配置`[^端口] # 18081 +``` + +读取函数返回的字典字段,同样直接取元: + +```markdown +*login = > `auth`.login username="admin" password="secret"* +> print text=`login`[^ok] # 而不是 json.get value=`login` key="ok" +``` + +## 14.7 下一步 + +你已经掌握 Marqdo 的网络与 Web 能力。继续探索: + +- [第 13 章:命令行工具](./13-命令行工具.md) —— `marqdo ext` 管理扩展 +- 设计文档:[`doc/design/ext-web-net.md`](../design/ext-web-net.md) —— 本波次网络扩展的完整设计与边界判定 diff --git a/doc/tutorial/README.md b/doc/tutorial/README.md index 6d9484e..186af90 100644 --- a/doc/tutorial/README.md +++ b/doc/tutorial/README.md @@ -48,6 +48,7 @@ |------|------|-------------| | [第 12 章:标准库](./12-标准库.md) | 内置函数、`lib/` 模块 | 直接使用官方能力 | | [第 13 章:命令行工具](./13-命令行工具.md) | `run` / `view` / `debug` / `catalog` | 运行、浏览、调试、发布 | +| [第 14 章:网络与 Web 扩展](./14-网络与Web扩展.md) | `lib/net`、`ext/web`、鉴权、WebSocket | 构建 HTTP 客户端与网页应用 | --- diff --git a/examples/web-net-site-zh/README.md b/examples/web-net-site-zh/README.md new file mode 100644 index 0000000..2e53745 --- /dev/null +++ b/examples/web-net-site-zh/README.md @@ -0,0 +1,19 @@ +# examples/web-net-site-zh + +中文作者面示例:只导入 `ext/web/网页.mq.md`(不用英文 `web.mq.md`),并用 `lib/网络.mq.md` 的解析原语。 + +```bash +cargo build -p marqdo_plugin_web +cargo run -- examples/web-net-site-zh/index.mq.md +``` + +从仓库根目录或示例目录运行都可以:`static_dir`(如 `public`)与相对 db 路径(如 `data/site-net-zh.db`)都会基于**入口脚本所在目录**解析,而不是终端的当前目录。 + +打开 http://127.0.0.1:18084/: + +- `/` — SQLite 文章列表 + `/live` WebSocket 实时小部件(`public/live.js`) +- `/about` +- `/new` — 表单 POST 到 `/_form/article` +- `/tools` — `lib/net`(`解析Cookie` / `解析多部分`)的解析演示 +- `/admin` — 会话门禁后台,用 `admin` / `secret` 登录 +- `/live` — WebSocket 回显端点(首页小部件或 `网页.实时` 的 `连接` 接入) diff --git a/examples/web-net-site-zh/components/foot.mq.md b/examples/web-net-site-zh/components/foot.mq.md new file mode 100644 index 0000000..2f7ddc6 --- /dev/null +++ b/examples/web-net-site-zh/components/foot.mq.md @@ -0,0 +1,10 @@ +## 页脚 + +`页脚` = + +| 属性 | 值 | 样式 | +|------|-----|------| +| Marqdo 网络扩展示例 | https://github.com | | +| 文档 | /tools | | + +**`页脚`** \ No newline at end of file diff --git a/examples/web-net-site-zh/components/nav.mq.md b/examples/web-net-site-zh/components/nav.mq.md new file mode 100644 index 0000000..3ac12d9 --- /dev/null +++ b/examples/web-net-site-zh/components/nav.mq.md @@ -0,0 +1,15 @@ +## 导航 + +站点顶部导航。`后台` 指向需要登录的 `/admin`。 + +`导航` = + +| 属性 | 值 | 样式 | +|------|-----|------| +| 首页 | / | | +| 关于 | /about | | +| 发布 | /new | | +| 工具 | /tools | | +| 后台 | /admin | | + +**`导航`** \ No newline at end of file diff --git a/examples/web-net-site-zh/components/side.mq.md b/examples/web-net-site-zh/components/side.mq.md new file mode 100644 index 0000000..b2a04e4 --- /dev/null +++ b/examples/web-net-site-zh/components/side.mq.md @@ -0,0 +1,15 @@ +## 侧栏 + +本示例展示了哪些特性。 + +`侧栏` = + +| 属性 | 值 | 样式 | +|------|-----|------| +| 文章 | / | | +| WebSocket | /#live | | +| 登录鉴权 | /admin | | +| 表单 | /new | | +| lib/net | /tools | | + +**`侧栏`** \ No newline at end of file diff --git a/examples/web-net-site-zh/db/articles.mq.md b/examples/web-net-site-zh/db/articles.mq.md new file mode 100644 index 0000000..68697a1 --- /dev/null +++ b/examples/web-net-site-zh/db/articles.mq.md @@ -0,0 +1,23 @@ +## 结构 + +`字段` = + +| 字段 | 类型 | 可空 | +|------|------|------| +| id | integer | false | +| title | text | false | +| body | text | true | + +**`字段`** + +## 种子 + +`行` = + +| id | title | body | +|----|-------|------| +| 1 | 欢迎来到网络扩展站 | 首页列表来自 SQLite。 | +| 2 | 登录门禁后台 | /admin 页面需要 session 会话。 | +| 3 | WebSocket 实时 | /live 端点会把你的消息实时回显。 | + +**`行`** \ No newline at end of file diff --git a/examples/web-net-site-zh/db/index.mq.md b/examples/web-net-site-zh/db/index.mq.md new file mode 100644 index 0000000..0101e3f --- /dev/null +++ b/examples/web-net-site-zh/db/index.mq.md @@ -0,0 +1,19 @@ +--- +title: db/index +description: 打开 sqlite、建表、幂等种子。 +导入 网页:ext/web/网页.mq.md +import articles:articles.mq.md +--- + +## 打开 + +*store = > 网页.数据库 地址="sqlite:data/site-net-zh.db"* +*字段 = > articles.结构* +> `store`.初始化 名=articles 字段=`字段` +*行 = > store.查询 表="articles" 上限=1* +1. `行` + **store** +2. * + *种子 = > articles.种子* + > `store`.插入 表=articles 行=`种子` + **store** \ No newline at end of file diff --git a/examples/web-net-site-zh/index.mq.md b/examples/web-net-site-zh/index.mq.md new file mode 100644 index 0000000..c07ef37 --- /dev/null +++ b/examples/web-net-site-zh/index.mq.md @@ -0,0 +1,111 @@ +--- +title: 网络扩展站(中文) +description: 首页 + /about + /new(表单)+ /tools(lib/net)+ /admin(登录鉴权)+ /live(WebSocket)。 +导入 网页:ext/web/网页.mq.md +import net:lib/网络.mq.md +import text:lib/text.mq.md +import shell:styles/shell.mq.md +import nav:components/nav.mq.md +import side:components/side.mq.md +import foot:components/foot.mq.md +import articles:db/articles.mq.md +import db:db/index.mq.md +--- + +# main + +`首页` = + +| 组件 | 样式 | +|------|------| +| nav.`导航` | shell.`顶栏` | +| side.`侧栏` | shell.`侧栏面板` | +| foot.`页脚` | | + +`列表` = + +| 属性 | 值 | 样式 | +|------|-----|------| +| title | articles.`articles`.title | shell.`卡片标题` | +| body | articles.`articles`.body | shell.`卡片正文` | + +`文章字段` = + +| 字段 | 标签 | 类型 | 必填 | 默认 | +|------|------|------|------|------| +| title | 标题 | text | true | | +| body | 正文 | textarea | false | | + +`文章规则` = + +| 字段 | 规则 | 消息 | +|------|------|------| +| title | required | 标题不能为空 | +| title | max:120 | 标题太长 | +| body | max:8000 | 正文太长 | + +*store = > db.打开* +*page = > 网页.页面 标题="网络扩展站" 引言="

网络扩展站

SQLite 文章列表、WebSocket 实时小部件、登录门禁后台、以及标准库 lib/net 的解析工具。

实时 WebSocket

connecting…

"* +*page = > page.组件装配 组件=`首页`* +*page = > page.主体装配 主体=`列表`* + +*about = > 网页.页面 标题="关于" 引言="

关于

演示网络扩展后的 web 库:会话、登录鉴权、WebSocket、multipart/cookie 解析。

"* +*about = > about.组件装配 组件=`首页`* + + + +*解析请求 = > net.解析Cookie 内容="session=abc123; theme=dark"* +*请求块 = 解析请求[^1]* + +*解析响应 = > net.解析Cookie 内容="id=42; Path=/; HttpOnly; SameSite=Lax" 是响应头=True* +*响应块 = 解析响应[^1]* + +`边界` = ----b + +`正文` = "--`边界`\nContent-Disposition: form-data; name=\"title\"\n\n你好\n--`边界`\nContent-Disposition: form-data; name=\"封面\"; filename=\"pic.png\"\nContent-Type: image/png\n\n<字节>\n--`边界`--\n" + +*多部分 = > net.解析多部分 正文=`正文` 边界=`边界`* +*字段块 = 多部分[^1]* +*文件块 = 多部分[^2]* + +`工具段` = + +| 段 | +|----| +|

网络工具

由 lib/net 提供的解析演示:解析Cookie 与 解析多部分。

| +|

Cookie 请求头

| +| `请求块`[^name]=`请求块`[^value] | +|

Set-Cookie 响应头

| +| `响应块`[^name]=`响应块`[^value] · HttpOnly=`响应块`[^http_only] | +|

多部分字段

| +| `字段块`[^name]=`字段块`[^value] | +|

多部分文件

| +| `文件块`[^name] · `文件块`[^filename] · `文件块`[^content_type] | +|

| + +*工具引言 = > text.str_join xs=`工具段` sep=""* +*tools = > 网页.页面 标题="工具" 引言=`工具引言`* +*tools = > tools.组件装配 组件=`首页`* + +*article_form = > 网页.表单 表="articles" 动作="插入"* +*article_form = > article_form.字段 字段=`文章字段`* +*article_form = > article_form.规则 规则=`文章规则`* + +*new = > 网页.页面 标题="发布" 引言="

发布

表单插入到 articles 表。

"* +*new = > new.组件装配 组件=`首页`* +*new = > new.表单装配 id="article" 表单=article_form* + +`管理员` = + +| 行 | 用户名 | 密码 | +|----|--------|------| +| 1 | admin | secret | + +*app = > 网页.应用 页面=page 数据库=store 后台=True 主机="127.0.0.1" 端口=18084* +*app = > app.路由 路径="/about" 页面=about* +*app = > app.路由 路径="/new" 页面=new* +*app = > app.路由 路径="/tools" 页面=tools* +*app = > app.静态 目录="public" 挂载="/static"* +*app = > app.路由实时 路径="/live" 回显=True* +*app = > app.鉴权 用户表=`管理员` 会话时长=3600* +> `app`.监听 \ No newline at end of file diff --git a/examples/web-net-site-zh/public/live.js b/examples/web-net-site-zh/public/live.js new file mode 100644 index 0000000..cfd2d03 --- /dev/null +++ b/examples/web-net-site-zh/public/live.js @@ -0,0 +1,40 @@ +// /live WebSocket 端点的小部件。 +// 连接 ws://host/live,发送输入,展示回显。 +(function () { + var out = document.getElementById("live-out"); + var input = document.getElementById("live-msg"); + var btn = document.getElementById("live-send"); + var status = document.getElementById("live-status"); + if (!out || !input || !btn || !status) return; + + var proto = location.protocol === "https:" ? "wss" : "ws"; + var ws = new WebSocket(proto + "://" + location.host + "/live"); + status.textContent = "connecting…"; + + ws.onopen = function () { + status.textContent = "connected"; + btn.disabled = false; + }; + ws.onmessage = function (ev) { + var p = document.createElement("p"); + p.textContent = "echo: " + ev.data; + out.appendChild(p); + out.scrollTop = out.scrollHeight; + }; + ws.onclose = function () { + status.textContent = "closed"; + btn.disabled = true; + }; + ws.onerror = function () { + status.textContent = "error"; + btn.disabled = true; + }; + + btn.onclick = function () { + var m = input.value.trim(); + if (m) ws.send(m); + }; + input.addEventListener("keydown", function (e) { + if (e.key === "Enter") btn.onclick(); + }); +})(); diff --git a/examples/web-net-site-zh/styles/shell.mq.md b/examples/web-net-site-zh/styles/shell.mq.md new file mode 100644 index 0000000..84027f6 --- /dev/null +++ b/examples/web-net-site-zh/styles/shell.mq.md @@ -0,0 +1,42 @@ +## 顶栏 + +`顶栏` = + +| 属性 | 值 | +|------|-----| +| background | #ffffff | +| border-bottom | 1px solid #e7e5e4 | + +**`顶栏`** + +## 侧栏面板 + +`侧栏面板` = + +| 属性 | 值 | +|------|-----| +| background | #f5f5f4 | + +**`侧栏面板`** + +## 卡片标题 + +`卡片标题` = + +| 属性 | 值 | +|------|-----| +| font-size | 1.15rem | +| margin | 0 0 0.35rem | + +**`卡片标题`** + +## 卡片正文 + +`卡片正文` = + +| 属性 | 值 | +|------|-----| +| color | #57534e | +| margin | 0 | + +**`卡片正文`** \ No newline at end of file diff --git a/examples/web-net-site/README.md b/examples/web-net-site/README.md new file mode 100644 index 0000000..124863c --- /dev/null +++ b/examples/web-net-site/README.md @@ -0,0 +1,19 @@ +# examples/web-net-site + +Demonstrates the network-extended `ext/web` + `lib/net` surface (design `doc/design/ext-web-net.md`). + +```bash +cargo build -p marqdo_plugin_web +cargo run -- examples/web-net-site/index.mq.md +``` + +Then open http://127.0.0.1:18083/: + +- `/` — articles from SQLite plus a live WebSocket widget (`/live`, served by `public/live.js`) +- `/about` +- `/new` — form POST to `/_form/article` +- `/tools` — parsing demos powered by `lib/net`: `net.cookie_parse` and `net.multipart_parse` +- `/admin` — session-gated admin; sign in with `admin` / `secret` +- `/live` — WebSocket echo endpoint (try it from the home widget, or with `web.ws.connect`) + +You may run it from the repo root or from this directory: relative `static_dir` (e.g. `public`) and relative db paths (e.g. `data/site-net.db`) resolve against the **entry script's directory**, not the terminal's current working directory. diff --git a/examples/web-net-site/components/foot.mq.md b/examples/web-net-site/components/foot.mq.md new file mode 100644 index 0000000..ca3554b --- /dev/null +++ b/examples/web-net-site/components/foot.mq.md @@ -0,0 +1,10 @@ +## foot + +`foot` = + +| 属性 | 值 | 样式 | +|------|-----|------| +| Marqdo web-net example | https://github.com | | +| Docs | /tools | | + +**`foot`** \ No newline at end of file diff --git a/examples/web-net-site/components/nav.mq.md b/examples/web-net-site/components/nav.mq.md new file mode 100644 index 0000000..1586fab --- /dev/null +++ b/examples/web-net-site/components/nav.mq.md @@ -0,0 +1,15 @@ +## nav + +Site top navigation. The Admin tab points at the login-gated admin page. + +`nav` = + +| 属性 | 值 | 样式 | +|------|-----|------| +| Home | / | | +| About | /about | | +| Publish | /new | | +| Tools | /tools | | +| Admin | /admin | | + +**`nav`** diff --git a/examples/web-net-site/components/side.mq.md b/examples/web-net-site/components/side.mq.md new file mode 100644 index 0000000..fa3df07 --- /dev/null +++ b/examples/web-net-site/components/side.mq.md @@ -0,0 +1,15 @@ +## side + +Sidebar index of what this example demonstrates. + +`side` = + +| 属性 | 值 | 样式 | +|------|-----|------| +| Articles | / | | +| WebSocket | /#live | | +| Auth | /admin | | +| Forms | /new | | +| net lib | /tools | | + +**`side`** \ No newline at end of file diff --git a/examples/web-net-site/db/articles.mq.md b/examples/web-net-site/db/articles.mq.md new file mode 100644 index 0000000..cf24f06 --- /dev/null +++ b/examples/web-net-site/db/articles.mq.md @@ -0,0 +1,23 @@ +## schema + +`fields` = + +| 字段 | 类型 | 可空 | +|------|------|------| +| id | integer | false | +| title | text | false | +| body | text | true | + +**`fields`** + +## seed + +`rows` = + +| id | title | body | +|----|-------|------| +| 1 | Welcome to the web-net site | Home page lists rows from SQLite. | +| 2 | Login-gated admin | The /admin page requires a session cookie. | +| 3 | WebSocket live | The /live endpoint echoes your message in real time. | + +**`rows`** \ No newline at end of file diff --git a/examples/web-net-site/db/index.mq.md b/examples/web-net-site/db/index.mq.md new file mode 100644 index 0000000..82dfd59 --- /dev/null +++ b/examples/web-net-site/db/index.mq.md @@ -0,0 +1,19 @@ +--- +title: db/index +description: Open sqlite, init articles, seed once. +import web:ext/web/web.mq.md +import articles:articles.mq.md +--- + +## open + +*store = > web.db url="sqlite:data/site-net.db"* +*fields = > articles.schema* +> `store`.init name=articles fields=`fields` +*rows = > store.select table="articles" limit=1* +1. `rows` + **store** +2. * + *seed = > articles.seed* + > `store`.insert table=articles rows=`seed` + **store** \ No newline at end of file diff --git a/examples/web-net-site/index.mq.md b/examples/web-net-site/index.mq.md new file mode 100644 index 0000000..3565db7 --- /dev/null +++ b/examples/web-net-site/index.mq.md @@ -0,0 +1,129 @@ +--- +title: web-net-site +description: Home + /about + /new (form) + /tools (net lib) + /admin (auth) + /live (WebSocket). +import web:ext/web/web.mq.md +import net:lib/net.mq.md +import text:lib/text.mq.md +import shell:styles/shell.mq.md +import nav:components/nav.mq.md +import side:components/side.mq.md +import foot:components/foot.mq.md +import articles:db/articles.mq.md +import db:db/index.mq.md +--- + +# main + +`home` = + +| 组件 | 样式 | +|------|------| +| nav.`nav` | shell.`topnav` | +| side.`side` | shell.`side_panel` | +| foot.`foot` | | + +`index` = + +| 属性 | 值 | 样式 | +|------|-----|------| +| title | articles.`articles`.title | shell.`card_title` | +| body | articles.`articles`.body | shell.`card_body` | + +Home intro also embeds the live WebSocket widget (HTML is served raw). + +*store = > db.open* +*page = > web.page title="Marqdo Net Site" intro="

Net Site

Articles from SQLite, a live WebSocket widget, login-gated admin, and net-library parsing tools.

Live WebSocket

connecting…

"* +*page = > page.compose_components components=home* +*page = > page.compose_main main=index* + +About page: same shell. + +*about = > web.page title="About" intro="

About

Demonstrates the network-extended web library: sessions, auth, WebSocket, multipart/cookie parsing.

"* +*about = > about.compose_components components=home* + + + +Parse a `Cookie` request header with the standard library. + +*parsed_req = > net.cookie_parse text="session=abc123; theme=dark"* +*req_cookie = parsed_req[^1]* + +Parse a `Set-Cookie` response header. + +*parsed_resp = > net.cookie_parse text="id=42; Path=/; HttpOnly; SameSite=Lax" is_response=True* +*resp_cookie = parsed_resp[^1]* + +Parse a `multipart/form-data` body. + +`_b` = net-boundary + +`_body` = "--`_b`\nContent-Disposition: form-data; name=\"title\"\n\nHello\n--`_b`\nContent-Disposition: form-data; name=\"cover\"; filename=\"pic.png\"\nContent-Type: image/png\n\n\n--`_b`--\n" + +*mp = > net.multipart_parse body=`_body` boundary=`_b`* +*mp_title = mp[^1]* +*mp_cover = mp[^2]* + +Build the tools page as one HTML intro (intro is served raw). + +`tools_parts` = + +| 段 | +|----| +|

Net tools

Parsing demos powered by lib/net: cookie_parse and multipart_parse.

| +|

Cookie request

| +| `req_cookie`[^name]=`req_cookie`[^value] | +|

Set-Cookie response

| +| `resp_cookie`[^name]=`resp_cookie`[^value] · HttpOnly=`resp_cookie`[^http_only] | +|

Multipart field

| +| `mp_title`[^name]=`mp_title`[^value] | +|

Multipart file

| +| `mp_cover`[^name] · `mp_cover`[^filename] · `mp_cover`[^content_type] | +|

| + +*tools_intro = > text.str_join xs=`tools_parts` sep=""* +*tools = > web.page title="Tools" intro=`tools_intro`* +*tools = > tools.compose_components components=home* + +Article create form: field table + rules table. + +`article_fields` = + +| 字段 | 标签 | 类型 | 必填 | 默认 | +|------|------|------|------|------| +| title | Title | text | true | | +| body | Body | textarea | false | | + +`article_rules` = + +| 字段 | 规则 | 消息 | +|------|------|------| +| title | required | Title is required | +| title | max:120 | Title is too long | +| body | max:8000 | Body is too long | + +*article_form = > web.form table="articles" action="insert"* +*article_form = > article_form.fields fields=article_fields* +*article_form = > article_form.rules rules=article_rules* + +New article page: form embedded in the main slot. + +*new = > web.page title="Publish" intro="

Publish

Form inserts into articles.

"* +*new = > new.compose_components components=home* +*new = > new.compose_form id="article" form=article_form* + +Admin users table (english + chinese column headers both work). + +`admins` = + +| 行 | username | password | +|----|----------|----------| +| 1 | admin | secret | + +*app = > web.app page=page db=store admin=True host="127.0.0.1" port=18083* +*app = > app.route path="/about" page=about* +*app = > app.route path="/new" page=new* +*app = > app.route path="/tools" page=tools* +*app = > app.static dir="public" mount="/static"* +*app = > app.route_ws path="/live" echo=True* +*app = > app.auth users=`admins` session_ttl=3600* +> `app`.listen \ No newline at end of file diff --git a/examples/web-net-site/public/live.js b/examples/web-net-site/public/live.js new file mode 100644 index 0000000..74dd8c9 --- /dev/null +++ b/examples/web-net-site/public/live.js @@ -0,0 +1,40 @@ +// Live widget for the /live WebSocket endpoint. +// Connects to ws://host/live, sends the input, shows the echo. +(function () { + var out = document.getElementById("live-out"); + var input = document.getElementById("live-msg"); + var btn = document.getElementById("live-send"); + var status = document.getElementById("live-status"); + if (!out || !input || !btn || !status) return; + + var proto = location.protocol === "https:" ? "wss" : "ws"; + var ws = new WebSocket(proto + "://" + location.host + "/live"); + status.textContent = "connecting…"; + + ws.onopen = function () { + status.textContent = "connected"; + btn.disabled = false; + }; + ws.onmessage = function (ev) { + var p = document.createElement("p"); + p.textContent = "echo: " + ev.data; + out.appendChild(p); + out.scrollTop = out.scrollHeight; + }; + ws.onclose = function () { + status.textContent = "closed"; + btn.disabled = true; + }; + ws.onerror = function () { + status.textContent = "error"; + btn.disabled = true; + }; + + btn.onclick = function () { + var m = input.value.trim(); + if (m) ws.send(m); + }; + input.addEventListener("keydown", function (e) { + if (e.key === "Enter") btn.onclick(); + }); +})(); diff --git a/examples/web-net-site/styles/shell.mq.md b/examples/web-net-site/styles/shell.mq.md new file mode 100644 index 0000000..15cd41b --- /dev/null +++ b/examples/web-net-site/styles/shell.mq.md @@ -0,0 +1,42 @@ +## topnav + +`topnav` = + +| 属性 | 值 | +|------|-----| +| background | #ffffff | +| border-bottom | 1px solid #e7e5e4 | + +**`topnav`** + +## side_panel + +`side_panel` = + +| 属性 | 值 | +|------|-----| +| background | #f5f5f4 | + +**`side_panel`** + +## card_title + +`card_title` = + +| 属性 | 值 | +|------|-----| +| font-size | 1.15rem | +| margin | 0 0 0.35rem | + +**`card_title`** + +## card_body + +`card_body` = + +| 属性 | 值 | +|------|-----| +| color | #57534e | +| margin | 0 | + +**`card_body`** diff --git a/ext/web/web.mq.md b/ext/web/web.mq.md index 2584dee..c700f68 100644 --- a/ext/web/web.mq.md +++ b/ext/web/web.mq.md @@ -3,7 +3,6 @@ title: ext/web/web description: Official web site classes (English). Tables + methods; no bag glue. import plugin:lib/plugin.mq.md import sys:lib/sys.mq.md -import json:lib/json.mq.md --- ## ensure_plugin @@ -53,7 +52,7 @@ Embed a form into the page main slot. `listen` auto-registers `GET|POST /_form/{ Offline HTML for tests / previews. 1. `db` - *url = > json.get value=`db` key="url"* + *url = db[^url]* **> web_render page=`self` url=`url`** 2. * **> web_render page=`self`** @@ -82,14 +81,14 @@ Style tables live as `##` exports in site style modules; compose resolves them b + `name` + `fields` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_init url=`url` name=`name` fields=`fields`** ## insert + `table` + `rows` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_insert url=`url` table=`table` rows=`rows`** ## select @@ -99,15 +98,15 @@ Style tables live as `##` exports in site style modules; compose resolves them b Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作|值|` (`=` `!=` `>` `>=` `<` `<=` `like`). -*url = > json.get value=`self` key="url"* +*url = self[^url]* *r = > web_db_select url=`url` table=`table` where=`where` limit=`limit`* -**> json.get value=`r` key="rows"** +**r[^rows]** ## get + `table` + `id` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_get url=`url` table=`table` id=`id`** ## update @@ -115,21 +114,21 @@ Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作 + `id` + `row` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_update url=`url` table=`table` id=`id` row=`row`** ## delete + `table` + `id` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_delete url=`url` table=`table` id=`id`** ## exec + `sql` + `args`=None -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_exec url=`url` sql=`sql` args=`args`** # form @@ -169,7 +168,7 @@ Field table + rules table; submit writes through `# db`. + `data` + `db` -*url = > json.get value=`db` key="url"* +*url = db[^url]* **> web_form_submit form=`self` data=`data` url=`url`** # app @@ -211,3 +210,76 @@ Serve files from `dir` under `mount` (default `/static`). Path is resolved from Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes), `/_form/{id}` (from mounts + page embeds), optional `/static` (or custom mount), and optional `/admin`. **> web_listen app=`self`** + +## route_ws + + `path` + + `echo`=True + +Register a WebSocket endpoint at `path` (e.g. `/live`). With `echo=True` (default) the server replies to each received text frame; otherwise frames are drained. Connect from a client with `web.ws.connect`. + +**> web_app_route_ws app=`self` path=`path` echo=`echo`** + +## auth + + `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`. + +**> web_app_auth app=`self` users=`users` session_ttl=`session_ttl`** + +# auth + + `users` + + `session_ttl`=3600 + +Session/auth helper. Constructs a config object; `login` validates against the users table. To gate `/admin` on this app, use `app.auth users=…` instead. + +> ensure_plugin +**> web_auth_new users=`users` session_ttl=`session_ttl`** + +## login + + `username` + + `password` + +Validate credentials against the users table and create a session. Returns `{ok, session_id, username}`. + +*users = self[^users]* +*ttl = self[^session_ttl]* +**> web_auth_login username=`username` password=`password` users=`users` session_ttl=`ttl`** + +## check + + `session_id` + +Returns `{ok, username}` when the session is valid. + +**> web_auth_check session_id=`session_id`** + +## logout + + `session_id` + +Destroy the session. + +**> web_auth_logout session_id=`session_id`** + +# ws + + `timeout_sec`=30 + +WebSocket client helper. + +> ensure_plugin +`out` = + +| timeout_sec | _type | +|-------------|-------| +| `timeout_sec` | ws | + +**out** + +## connect + + `url` + + `message`="" + + `headers`=None + +Single request–response: connect to `url`, send `message`, collect all server text replies, close. Returns `{ok, messages}`. + +*timeout = self[^timeout_sec]* +**> web_ws_connect url=`url` message=`message` headers=`headers` timeout_sec=`timeout`** diff --git a/ext/web/网页.mq.md b/ext/web/网页.mq.md index 48bedcc..4e33290 100644 --- a/ext/web/网页.mq.md +++ b/ext/web/网页.mq.md @@ -3,7 +3,6 @@ title: ext/web/网页 description: 官方网站类(中文)。表格 + 方法;不写袋式胶水。 import plugin:lib/plugin.mq.md import sys:lib/sys.mq.md -import json:lib/json.mq.md --- ## 确保插件 @@ -47,7 +46,7 @@ import json:lib/json.mq.md + `数据库`=None 1. `数据库` - *url = > json.get value=`数据库` key="url"* + *url = 数据库[^url]* **> web_render page=`self` url=`url`** 2. * **> web_render page=`self`** @@ -74,14 +73,14 @@ import json:lib/json.mq.md + `名` + `字段` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_init url=`url` name=`名` fields=`字段`** ## 插入 + `表` + `行` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_insert url=`url` table=`表` rows=`行`** ## 查询 @@ -91,15 +90,15 @@ import json:lib/json.mq.md 简单条件:单行列→值映射(AND `=`),或 `|字段|操作|值|` 表(`=` `!=` `>` `>=` `<` `<=` `like`)。 -*url = > json.get value=`self` key="url"* +*url = self[^url]* *r = > web_db_select url=`url` table=`表` where=`条件` limit=`上限`* -**> json.get value=`r` key="rows"** +**r[^rows]** ## 获取 + `表` + `id` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_get url=`url` table=`表` id=`id`** ## 更新 @@ -107,21 +106,21 @@ import json:lib/json.mq.md + `id` + `行` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_update url=`url` table=`表` id=`id` row=`行`** ## 删除 + `表` + `id` -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_delete url=`url` table=`表` id=`id`** ## 执行 + `sql` + `参数`=None -*url = > json.get value=`self` key="url"* +*url = self[^url]* **> web_db_exec url=`url` sql=`sql` args=`参数`** # 表单 @@ -161,7 +160,7 @@ import json:lib/json.mq.md + `数据` + `数据库` -*url = > json.get value=`数据库` key="url"* +*url = 数据库[^url]* **> web_form_submit form=`self` data=`数据` url=`url`** # 应用 @@ -201,3 +200,76 @@ import json:lib/json.mq.md 提供 `/`、路由页、首页 `/_part/{id}`、路由 `{路径}/_part/{id}`、`/_form/{id}`、可选静态目录,以及可选 `/admin`。 **> web_listen app=`self`** + +## 路由实时 + + `路径` + + `回显`=真 + +在 `路径`(如 `/live`)注册 WebSocket 端点。`回显=真`(默认)时服务器对每条收到的文本帧原样回复;否则仅读取丢弃。客户端用 `网页.实时` 的 `连接` 接入。 + +**> web_app_route_ws app=`self` path=`路径` echo=`回显`** + +## 鉴权 + + `用户表` + + `会话时长`=3600 + +在保持 `后台=真` 的同时,为 `/admin*` 加登录门禁。`用户表` 是管理员表(`|用户名|密码|`);未登录请求重定向到 `/admin/login`。 + +**> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长`** + +# 鉴权 + + `用户表` + + `会话时长`=3600 + +会话/登录工具类。`登录` 用 `用户表` 校验。要给应用加 `/admin` 门禁,用 `应用` 的 `鉴权` 方法。 + +> 确保插件 +**> web_auth_new users=`用户表` session_ttl=`会话时长`** + +## 登录 + + `用户名` + + `密码` + +用用户表校验并创建会话。返回 `{ok, session_id, username}`。 + +*users = self[^users]* +*ttl = self[^session_ttl]* +**> web_auth_login username=`用户名` password=`密码` users=`users` session_ttl=`ttl`** + +## 校验 + + `会话id` + +会话有效时返回 `{ok, username}`。 + +**> web_auth_check session_id=`会话id`** + +## 登出 + + `会话id` + +销毁会话。 + +**> web_auth_logout session_id=`会话id`** + +# 实时 + + `超时秒`=30 + +WebSocket 客户端工具。 + +> 确保插件 +`out` = + +| timeout_sec | _type | +|-------------|-------| +| `超时秒` | ws | + +**out** + +## 连接 + + `地址` + + `消息`="" + + `headers`=None + +单次请求-响应:连接 `地址`,发送 `消息`,收集服务器全部文本回复后关闭。返回 `{ok, messages}`。 + +*timeout = self[^timeout_sec]* +**> web_ws_connect url=`地址` message=`消息` headers=`headers` timeout_sec=`timeout`** diff --git a/lib/net.mq.md b/lib/net.mq.md index 0700fcc..806ce58 100644 --- a/lib/net.mq.md +++ b/lib/net.mq.md @@ -55,3 +55,19 @@ Optional `body=` / `content_type=` / `headers=`. + `text` **> host_url_encode text=`text`** + +## cookie_parse + + `text` + + `is_response`=False + +Parse a `Cookie` request header (default) or one or more `Set-Cookie` response headers (`is_response=True`) into a list of `{name, value, path, domain, expires, max_age, secure, http_only, same_site}`. + +**> host_cookie_parse text=`text` is_response=`is_response`** + +## multipart_parse + + `body` + + `boundary` + +Parse a `multipart/form-data` request body (given its `boundary`) into a list of `{name, filename?, content_type?, value}` parts. + +**> host_multipart_parse body=`body` boundary=`boundary`** diff --git a/lib/网络.mq.md b/lib/网络.mq.md index 0c574ef..e7c98e4 100644 --- a/lib/网络.mq.md +++ b/lib/网络.mq.md @@ -55,3 +55,19 @@ POST 并消费 OpenAI 兼容 SSE。返回 `{status, events}`(`delta` / `done` + `内容` **> host_url_encode text=`内容`** + +## 解析Cookie + + `内容` + + `是响应头`=False + +解析 `Cookie` 请求头(默认)或一个/多个 `Set-Cookie` 响应头(`是响应头=真`)为 `{name, value, path, domain, expires, max_age, secure, http_only, same_site}` 列表。 + +**> host_cookie_parse text=`内容` is_response=`是响应头`** + +## 解析多部分 + + `正文` + + `边界` + +解析 `multipart/form-data` 请求体(给定 `boundary`)为 `{name, filename?, content_type?, value}` 部件列表。 + +**> host_multipart_parse body=`正文` boundary=`边界`** diff --git a/plugins/web/Cargo.toml b/plugins/web/Cargo.toml index 00a2c88..d1217c4 100644 --- a/plugins/web/Cargo.toml +++ b/plugins/web/Cargo.toml @@ -14,6 +14,8 @@ crate-type = ["cdylib"] serde = { version = "1", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "sync", "time", "signal"] } -axum = "0.8" +axum = { version = "0.8", features = ["ws"] } tower-http = { version = "0.6", features = ["fs"] } rusqlite = { version = "0.32", features = ["bundled"] } +tokio-tungstenite = "0.24" +futures-util = "0.3" diff --git a/plugins/web/src/http.rs b/plugins/web/src/http.rs index 5a0b2fa..398f8ac 100644 --- a/plugins/web/src/http.rs +++ b/plugins/web/src/http.rs @@ -18,6 +18,7 @@ use tower_http::services::ServeDir; use crate::db; use crate::form; use crate::render; +use crate::session; #[derive(Clone)] struct AppState { @@ -28,6 +29,9 @@ struct AppState { /// Page that owns an embedded form (for re-render on validation errors). form_owners: HashMap, routes: HashMap, + /// Admin users table (`|用户名|密码|`) when login-gated admin is configured. + auth_users: Option, + session_ttl: u64, } fn collect_page_forms( @@ -62,12 +66,16 @@ pub fn listen( routes: HashMap, static_dir: Option, static_mount: &str, + auth_users: Option, + session_ttl: u64, + ws_routes: HashMap, ) -> Result { let mut form_owners = HashMap::new(); collect_page_forms(page, &mut forms, &mut form_owners); for p in routes.values() { collect_page_forms(p, &mut forms, &mut form_owners); } + session::reset(session_ttl); let state = Arc::new(AppState { page: page.clone(), db_url: db_url.map(|s| s.to_string()), @@ -75,6 +83,8 @@ pub fn listen( forms, form_owners, routes: routes.clone(), + auth_users, + session_ttl, }); let mut app = Router::new() @@ -82,6 +92,8 @@ pub fn listen( .route("/_part/{id}", get(home_part)) .route("/_form/{id}", get(form_get).post(form_post)) .route("/admin", get(admin_home)) + .route("/admin/login", get(admin_login_get).post(admin_login_post)) + .route("/admin/logout", get(admin_logout)) .route("/admin/{table}", get(admin_table)) .route("/admin/{table}/new", get(admin_new_get).post(admin_new_post)) .route( @@ -99,6 +111,19 @@ pub fn listen( app = app.route(&part_path, get(routed_part)); } + // WebSocket endpoints (`ws://…/{path}`), echo-mode by default. + let mut ws_paths: Vec = ws_routes.keys().cloned().collect(); + ws_paths.sort(); + for path in ws_paths { + let echo = ws_routes.get(&path).copied().unwrap_or(true); + app = app.route( + &path, + axum::routing::get(move |ws: axum::extract::WebSocketUpgrade| { + ws_upgrade(ws, echo) + }), + ); + } + let mount = normalize_static_mount(static_mount); if let Some(dir) = static_dir { if !dir.is_dir() { @@ -294,16 +319,142 @@ fn submit_and_respond( } } -fn admin_gate(st: &AppState) -> Option { +fn cookie_from_headers(headers: &axum::http::HeaderMap) -> Option<&str> { + headers + .get(axum::http::header::COOKIE) + .and_then(|v| v.to_str().ok()) +} + +fn admin_gate(st: &AppState, cookie_header: Option<&str>) -> Option { if !st.admin { return Some(Html(admin_shell(st, "Admin", None, "

Admin is disabled for this app.

")).into_response()); } if st.db_url.is_none() { return Some(Html(admin_shell(st, "Admin", None, "

No database bound to this app.

")).into_response()); } + // Login-gated admin: require a valid session cookie. + if st.auth_users.is_some() && !admin_authed(st, cookie_header) { + return Some( + Redirect::to("/admin/login") + .into_response(), + ); + } None } +/// True when a valid admin session cookie is present. +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() +} + +#[derive(Deserialize)] +struct LoginForm { + username: Option, + password: Option, +} + +async fn admin_login_get(State(st): State>) -> Response { + // Already logged in → straight to admin home. + if st.auth_users.is_none() { + return Redirect::to("/admin").into_response(); + } + Html(login_page(&st, None)).into_response() +} + +async fn admin_login_post( + State(st): State>, + Form(posted): Form, +) -> Response { + let Some(users) = &st.auth_users else { + return Redirect::to("/admin").into_response(); + }; + let username = posted.username.unwrap_or_default(); + let password = posted.password.unwrap_or_default(); + if session::check_credentials(users, &username, &password).is_none() { + return Html(login_page(&st, Some("Invalid username or password."))).into_response(); + } + let sid = session::session_new(Some(st.session_ttl)); + session::session_set(&sid, "username", json!(username)); + let mut resp = Redirect::to("/admin").into_response(); + if let Ok(h) = resp.headers_mut().try_append( + axum::http::header::SET_COOKIE, + session::session_cookie(&sid, st.session_ttl) + .parse() + .unwrap(), + ) { + let _ = h; + } + resp +} + +fn login_page(_st: &AppState, error: Option<&str>) -> String { + let err = match error { + Some(e) => format!("

{}

", esc(e)), + None => String::new(), + }; + format!( + r#" + + + +Admin Login + + + + +"#, + err = err, + ) +} + +async fn admin_logout( + State(_st): State>, + headers: axum::http::HeaderMap, +) -> Response { + if let Some(sid) = session::session_id_from_cookie( + headers + .get(axum::http::header::COOKIE) + .and_then(|v| v.to_str().ok()), + ) { + session::session_destroy(&sid); + } + let mut resp = Redirect::to("/admin/login").into_response(); + if let Ok(h) = resp.headers_mut().try_append( + axum::http::header::SET_COOKIE, + "marqdo_sid=; HttpOnly; Path=/; Max-Age=0; SameSite=Lax" + .parse() + .unwrap(), + ) { + let _ = h; + } + resp +} + fn flash_html(flash: Option<&str>) -> String { match flash { Some("created") => "
Row created.
".into(), @@ -452,9 +603,10 @@ struct FlashQuery { async fn admin_home( State(st): State>, + headers: axum::http::HeaderMap, Query(q): Query, ) -> Response { - if let Some(r) = admin_gate(&st) { + if let Some(r) = admin_gate(&st, cookie_from_headers(&headers)) { return r; } let url = st.db_url.as_deref().unwrap(); @@ -480,10 +632,11 @@ async fn admin_home( async fn admin_table( State(st): State>, + headers: axum::http::HeaderMap, Path(table): Path, Query(q): Query, ) -> Response { - if let Some(r) = admin_gate(&st) { + if let Some(r) = admin_gate(&st, cookie_from_headers(&headers)) { return r; } let url = st.db_url.as_deref().unwrap(); @@ -575,8 +728,12 @@ async fn admin_table( Html(admin_shell(&st, &table, Some(&table), &inner)).into_response() } -async fn admin_new_get(State(st): State>, Path(table): Path) -> Response { - if let Some(r) = admin_gate(&st) { +async fn admin_new_get( + State(st): State>, + headers: axum::http::HeaderMap, + Path(table): Path, +) -> Response { + if let Some(r) = admin_gate(&st, cookie_from_headers(&headers)) { return r; } let url = st.db_url.as_deref().unwrap(); @@ -603,10 +760,11 @@ async fn admin_new_get(State(st): State>, Path(table): Path>, + headers: axum::http::HeaderMap, Path(table): Path, Form(posted): Form>, ) -> Response { - if let Some(r) = admin_gate(&st) { + if let Some(r) = admin_gate(&st, cookie_from_headers(&headers)) { return r; } let url = st.db_url.as_deref().unwrap(); @@ -631,9 +789,10 @@ async fn admin_new_post( async fn admin_edit_get( State(st): State>, + headers: axum::http::HeaderMap, Path((table, id)): Path<(String, String)>, ) -> Response { - if let Some(r) = admin_gate(&st) { + if let Some(r) = admin_gate(&st, cookie_from_headers(&headers)) { return r; } let url = st.db_url.as_deref().unwrap(); @@ -681,10 +840,11 @@ async fn admin_edit_get( async fn admin_edit_post( State(st): State>, + headers: axum::http::HeaderMap, Path((table, id)): Path<(String, String)>, Form(posted): Form>, ) -> Response { - if let Some(r) = admin_gate(&st) { + if let Some(r) = admin_gate(&st, cookie_from_headers(&headers)) { return r; } let url = st.db_url.as_deref().unwrap(); @@ -709,9 +869,10 @@ async fn admin_edit_post( async fn admin_delete( State(st): State>, + headers: axum::http::HeaderMap, Path((table, id)): Path<(String, String)>, ) -> Response { - if let Some(r) = admin_gate(&st) { + if let Some(r) = admin_gate(&st, cookie_from_headers(&headers)) { return r; } let url = st.db_url.as_deref().unwrap(); @@ -732,3 +893,29 @@ fn esc(s: &str) -> String { .replace('<', "<") .replace('>', ">") } + +/// WebSocket server endpoint: `echo=true` replies with each received text frame; +/// otherwise it just drains frames (extension point for custom logic). +async fn ws_upgrade( + ws: axum::extract::WebSocketUpgrade, + echo: bool, +) -> Response { + ws.on_upgrade(move |socket| ws_echo_loop(socket, echo)) +} + +async fn ws_echo_loop(mut socket: axum::extract::ws::WebSocket, echo: bool) { + use axum::extract::ws::Message; + while let Some(Ok(msg)) = socket.recv().await { + match msg { + Message::Text(text) => { + if echo { + if socket.send(Message::Text(text)).await.is_err() { + break; + } + } + } + Message::Close(_) => break, + _ => {} + } + } +} diff --git a/plugins/web/src/lib.rs b/plugins/web/src/lib.rs index 278cc0a..df061f7 100644 --- a/plugins/web/src/lib.rs +++ b/plugins/web/src/lib.rs @@ -5,10 +5,13 @@ mod db; mod form; mod http; mod render; +mod session; mod table; +mod ws; use std::ffi::{CStr, CString}; use std::os::raw::{c_char, c_int, c_void}; +use std::path::PathBuf; use std::ptr; use serde_json::{json, Value}; @@ -138,6 +141,37 @@ fn call_lib(path: &str) -> Result { host_query_json("call_lib_path", &json!({ "path": path })) } +/// Absolute directory of the entry `.mq.md` (falls back to process cwd). +fn entry_dir() -> PathBuf { + host_query_json("entry_dir", &json!({})) + .ok() + .and_then(|v| v.as_str().map(|s| PathBuf::from(s))) + .unwrap_or_else(|| { + std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")) + }) +} + +/// `sqlite:` URL with a relative path → absolute against `entry_dir()`. +fn resolve_db_url(url: &str) -> String { + let stripped = url + .strip_prefix("sqlite:") + .or_else(|| url.strip_prefix("SQLITE:")) + .unwrap_or(url); + let abs = PathBuf::from(stripped); + let abs = if abs.is_absolute() { + abs + } else { + entry_dir().join(abs) + }; + if stripped.len() == url.len() { + abs.to_string_lossy().into_owned() + } else if url.starts_with("sqlite:") { + format!("sqlite:{}", abs.to_string_lossy()) + } else { + format!("SQLITE:{}", abs.to_string_lossy()) + } +} + fn arg_text(v: &Value, key: &str) -> Result { match v.get(key) { None | Some(Value::Null) => Err(format!("missing `{key}`")), @@ -159,15 +193,22 @@ fn arg_str_opt<'a>(v: &'a Value, key: &str) -> Option<&'a str> { } fn db_url_of(args: &Value) -> Result { - if let Some(s) = arg_str_opt(args, "url").or_else(|| arg_str_opt(args, "db_url")) { - return Ok(s.to_string()); - } - if let Some(db) = args.get("db") { + let raw = if let Some(s) = arg_str_opt(args, "url").or_else(|| arg_str_opt(args, "db_url")) + { + Some(s.to_string()) + } else if let Some(db) = args.get("db") { if let Some(s) = db.get("url").and_then(|v| v.as_str()) { - return Ok(s.to_string()); + Some(s.to_string()) + } else { + None } + } else { + None + }; + match raw { + Some(s) => Ok(resolve_db_url(&s)), + None => Err("missing db url".into()), } - Err("missing db url".into()) } fn reply(out_json: *mut *mut c_char, err_msg: *mut *mut c_char, r: Result) -> c_int { @@ -354,6 +395,7 @@ web_ffi!(web_app_new, |args: &Value| { "admin": admin, "forms": {}, "routes": {}, + "ws_routes": {}, "static_dir": null, "static_mount": "/static", })) @@ -414,6 +456,101 @@ web_ffi!(web_app_route, |args: &Value| { Ok(app) }); +web_ffi!(web_app_route_ws, |args: &Value| { + let mut app = args.get("app").cloned().unwrap_or(json!({})); + let path = normalize_route_path(arg_str(args, "path")?)?; + let echo = match args.get("echo") { + None | Some(Value::Null) => true, + Some(Value::Bool(b)) => *b, + Some(Value::String(s)) => matches!(s.as_str(), "true" | "True" | "1" | "yes"), + Some(Value::Number(n)) => n.as_i64().unwrap_or(0) != 0, + Some(_) => true, + }; + let obj = app + .as_object_mut() + .ok_or_else(|| "app must be a map".to_string())?; + let ws_routes = obj + .entry("ws_routes".to_string()) + .or_insert_with(|| json!({})); + let wmap = ws_routes + .as_object_mut() + .ok_or_else(|| "app.ws_routes must be a map".to_string())?; + wmap.insert(path, json!(echo)); + Ok(app) +}); + +web_ffi!(web_ws_connect, |args: &Value| { + let url = arg_str(args, "url")?.to_string(); + let message = arg_str_opt(args, "message").unwrap_or(""); + let headers = args.get("headers"); + let timeout_sec = args + .get("timeout_sec") + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) + .unwrap_or(30); + match crate::ws::connect(&url, message, headers, timeout_sec) { + Ok(v) => Ok(v), + Err(e) => Ok(json!({ "ok": false, "error": e })), + } +}); + +web_ffi!(web_session_new, |args: &Value| { + session::abi_session_new(args) +}); + +web_ffi!(web_session_set, |args: &Value| { + session::abi_session_set(args) +}); + +web_ffi!(web_session_get, |args: &Value| { + session::abi_session_get(args) +}); + +web_ffi!(web_session_del, |args: &Value| { + session::abi_session_del(args) +}); + +web_ffi!(web_session_destroy, |args: &Value| { + session::abi_session_destroy(args) +}); + +web_ffi!(web_auth_login, |args: &Value| { + session::abi_auth_login(args) +}); + +web_ffi!(web_auth_check, |args: &Value| { + session::abi_auth_check(args) +}); + +web_ffi!(web_auth_logout, |args: &Value| { + session::abi_auth_logout(args) +}); + +web_ffi!(web_auth_new, |args: &Value| { + let users = args.get("users").cloned().unwrap_or(Value::Null); + let session_ttl = args + .get("session_ttl") + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) + .unwrap_or(3600); + Ok(json!({ "users": users, "session_ttl": session_ttl })) +}); + +web_ffi!(web_app_auth, |args: &Value| { + let mut app = args.get("app").cloned().unwrap_or(json!({})); + let users = args.get("users").cloned().unwrap_or(Value::Null); + let session_ttl = args + .get("session_ttl") + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) + .unwrap_or(3600); + let obj = app + .as_object_mut() + .ok_or_else(|| "app must be a map".to_string())?; + obj.insert( + "auth".to_string(), + json!({ "users": users, "session_ttl": session_ttl }), + ); + Ok(app) +}); + web_ffi!(web_app_mount_form, |args: &Value| { let mut app = args.get("app").cloned().unwrap_or(json!({})); let id = arg_str(args, "id")?.to_string(); @@ -548,11 +685,20 @@ web_ffi!(web_db_table_info, |args: &Value| { web_ffi!(web_listen, |args: &Value| { use std::collections::HashMap; use std::path::PathBuf; - let (page, db_url, host, port, admin, forms, routes, static_dir, static_mount) = if args - .get("page") - .is_some() - || args.get("host").is_some() - { + let ( + page, + db_url, + host, + port, + admin, + forms, + routes, + static_dir, + static_mount, + auth_users, + session_ttl, + ws_routes, + ) = if args.get("page").is_some() || args.get("host").is_some() { let page = args.get("page").cloned().unwrap_or(json!({})); let db_url = db_url_of(args).ok(); let host = arg_str_opt(args, "host").unwrap_or("127.0.0.1"); @@ -568,6 +714,15 @@ web_ffi!(web_listen, |args: &Value| { let static_mount = arg_str_opt(args, "static_mount") .unwrap_or("/static") .to_string(); + let auth_users = args + .get("auth") + .or_else(|| args.get("users")) + .cloned() + .filter(|v| !matches!(v, Value::Null)); + let session_ttl = args + .get("session_ttl") + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) + .unwrap_or(3600); ( page, db_url, @@ -578,6 +733,9 @@ web_ffi!(web_listen, |args: &Value| { HashMap::new(), static_dir, static_mount, + auth_users, + session_ttl, + HashMap::new(), ) } else { let app = args.get("app").cloned().unwrap_or_else(|| args.clone()); @@ -587,7 +745,8 @@ web_ffi!(web_listen, |args: &Value| { .and_then(|d| d.get("url")) .and_then(|v| v.as_str()) .map(|s| s.to_string()) - .or_else(|| arg_str_opt(&app, "db_url").map(|s| s.to_string())); + .or_else(|| arg_str_opt(&app, "db_url").map(|s| s.to_string())) + .map(|s| resolve_db_url(&s)); let host = app .get("host") .and_then(|v| v.as_str()) @@ -613,6 +772,17 @@ web_ffi!(web_listen, |args: &Value| { routes.insert(k.clone(), v.clone()); } } + let mut ws_routes = HashMap::new(); + if let Some(obj) = app.get("ws_routes").and_then(|v| v.as_object()) { + for (k, v) in obj { + let echo = match v { + Value::Bool(b) => *b, + Value::String(s) => matches!(s.as_str(), "true" | "True" | "1" | "yes"), + _ => true, + }; + ws_routes.insert(k.clone(), echo); + } + } let static_dir = app .get("static_dir") .and_then(|v| v.as_str()) @@ -623,6 +793,28 @@ web_ffi!(web_listen, |args: &Value| { .and_then(|v| v.as_str()) .unwrap_or("/static") .to_string(); + // `auth` may hold the users table directly or `{users:…, ttl:…}`. + let (auth_users, session_ttl) = match app.get("auth") { + Some(Value::Object(m)) if m.contains_key("users") => ( + m.get("users").cloned(), + m.get("ttl") + .or_else(|| m.get("session_ttl")) + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) + .unwrap_or(3600), + ), + Some(other) if !matches!(other, Value::Null) => ( + Some(other.clone()), + app.get("session_ttl") + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) + .unwrap_or(3600), + ), + _ => ( + None, + app.get("session_ttl") + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) + .unwrap_or(3600), + ), + }; ( page, db_url, @@ -633,15 +825,16 @@ web_ffi!(web_listen, |args: &Value| { routes, static_dir, static_mount, + auth_users, + session_ttl, + ws_routes, ) }; let static_dir = static_dir.map(|p| { if p.is_absolute() { p } else { - std::env::current_dir() - .unwrap_or_else(|_| PathBuf::from(".")) - .join(p) + entry_dir().join(p) } }); http::listen( @@ -654,6 +847,9 @@ web_ffi!(web_listen, |args: &Value| { routes, static_dir, &static_mount, + auth_users, + session_ttl, + ws_routes, ) }); @@ -748,6 +944,30 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int web_form_from_schema as PluginFn, ), ("web_db_table_info", "url,table", web_db_table_info as PluginFn), + ("web_session_new", "ttl_sec", web_session_new as PluginFn), + ("web_session_set", "id,key,value", web_session_set as PluginFn), + ("web_session_get", "id,key", web_session_get as PluginFn), + ("web_session_del", "id,key", web_session_del as PluginFn), + ("web_session_destroy", "id", web_session_destroy as PluginFn), + ( + "web_auth_login", + "username,password,users,session_ttl", + web_auth_login as PluginFn, + ), + ("web_auth_check", "session_id", web_auth_check as PluginFn), + ("web_auth_logout", "session_id", web_auth_logout 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_route_ws", + "app,path,echo", + web_app_route_ws as PluginFn, + ), + ( + "web_ws_connect", + "url,message,headers,timeout_sec", + web_ws_connect as PluginFn, + ), ("web_listen", "app", web_listen as PluginFn), ]; for (name, params, f) in regs { diff --git a/plugins/web/src/session.rs b/plugins/web/src/session.rs new file mode 100644 index 0000000..0268ec1 --- /dev/null +++ b/plugins/web/src/session.rs @@ -0,0 +1,353 @@ +//! In-memory session store + cookie helpers (design ext-web-net §3.1 / §3.2). +//! +//! Sessions live for the lifetime of the plugin process (same as `web_listen`). +//! Session ids are random hex strings; cookies are `marqdo_sid=` with +//! `HttpOnly; Path=/; SameSite=Lax`. + +use std::collections::HashMap; +use std::sync::Mutex; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde_json::{json, Map, Value}; + +type SessionMap = HashMap; +struct Store { + ttl_sec: u64, + data: HashMap, +} + +static STORE: Mutex> = Mutex::new(None); + +fn now_secs() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|d| d.as_secs()) + .unwrap_or(0) +} + +fn with_store(f: impl FnOnce(&mut Store) -> T) -> T { + let mut guard = STORE.lock().unwrap_or_else(|e| e.into_inner()); + let store = guard.get_or_insert_with(|| Store { + ttl_sec: 3600, + data: HashMap::new(), + }); + f(store) +} + +fn prune(store: &mut Store) { + let now = now_secs(); + store.data.retain(|_, (exp, _)| *exp > now); +} + +fn random_id() -> String { + use std::time::{SystemTime, UNIX_EPOCH}; + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|d| d.subsec_nanos()) + .unwrap_or(0); + let pid = std::process::id(); + // Cheap but adequate unique-enough id for an in-memory session store. + format!("{pid:x}{nanos:x}{:x}", rand_word()) +} + +fn rand_word() -> u64 { + // xorshift64 from a counter; not cryptographically strong but fine for + // session cookies when combined with pid+time. (Plugin is single-process.) + static COUNTER: Mutex = Mutex::new(0x9e3779b97f4a7c15); + let mut c = COUNTER.lock().unwrap_or_else(|e| e.into_inner()); + let mut x = *c; + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + *c = x; + x +} + +/// Reset the store (used between listens / for tests). +pub fn reset(ttl_sec: u64) { + let mut guard = STORE.lock().unwrap_or_else(|e| e.into_inner()); + *guard = Some(Store { + ttl_sec, + data: HashMap::new(), + }); +} + +pub fn session_new(ttl_sec: Option) -> String { + with_store(|store| { + if let Some(ttl) = ttl_sec { + if ttl > 0 { + store.ttl_sec = ttl; + } + } + prune(store); + let id = random_id(); + let exp = now_secs() + store.ttl_sec; + store.data.insert(id.clone(), (exp, SessionMap::new())); + id + }) +} + +pub fn session_set(id: &str, key: &str, value: Value) -> bool { + with_store(|store| { + prune(store); + let Some((exp, m)) = store.data.get_mut(id) else { + return false; + }; + if *exp <= now_secs() { + return false; + } + m.insert(key.to_string(), value); + true + }) +} + +pub fn session_get(id: &str, key: &str) -> Option { + with_store(|store| { + prune(store); + let (exp, m) = store.data.get(id)?; + if *exp <= now_secs() { + return None; + } + m.get(key).cloned() + }) +} + +pub fn session_del(id: &str, key: &str) -> bool { + with_store(|store| { + prune(store); + let Some((exp, m)) = store.data.get_mut(id) else { + return false; + }; + if *exp <= now_secs() { + return false; + } + m.remove(key).is_some() + }) +} + +pub fn session_destroy(id: &str) -> bool { + with_store(|store| { + prune(store); + store.data.remove(id).is_some() + }) +} + +/// Parse the session id from a `Cookie` request header (looks for `marqdo_sid=…`). +pub fn session_id_from_cookie(cookie_header: Option<&str>) -> Option { + let header = cookie_header?; + for part in header.split(';') { + let part = part.trim(); + if let Some(v) = part.strip_prefix("marqdo_sid=") { + let v = v.trim_matches('"'); + if !v.is_empty() { + return Some(v.to_string()); + } + } + } + None +} + +/// `Set-Cookie: marqdo_sid=; HttpOnly; Path=/; SameSite=Lax; Max-Age=` +pub fn session_cookie(id: &str, ttl_sec: u64) -> String { + format!( + "marqdo_sid={id}; HttpOnly; Path=/; SameSite=Lax; Max-Age={ttl_sec}" + ) +} + +/// Validate `username`/`password` against an admin-users table (`|用户名|密码|` +/// with headers `username`/`密码` or `用户`/`密码`, or plain `username`/`password`). +/// Returns the matched username on success. +pub fn check_credentials(users: &Value, username: &str, password: &str) -> Option { + let rows = match users { + Value::Array(a) => a, + _ => return None, + }; + for row in rows { + let m = match row { + Value::Object(m) => m, + _ => continue, + }; + let u = pick_cell(m, &["用户名", "用户", "username", "user", "账号"]) + .map(cell_str) + .unwrap_or_default(); + let p = pick_cell(m, &["密码", "password", "pass", "口令"]) + .map(cell_str) + .unwrap_or_default(); + if u == username && p == password { + return Some(u); + } + } + None +} + +fn pick_cell<'a>(m: &'a Map, keys: &[&str]) -> Option<&'a Value> { + keys.iter().find_map(|k| m.get(*k)) +} + +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(), + } +} + +/// ABI helpers. +pub fn abi_session_new(args: &Value) -> Result { + let ttl = args + .get("ttl_sec") + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))); + let id = session_new(ttl); + Ok(json!({ "id": id })) +} + +pub fn abi_session_set(args: &Value) -> Result { + let id = arg_str(args, "id")?; + let key = arg_str(args, "key")?; + let value = args.get("value").cloned().unwrap_or(Value::Null); + Ok(json!({ "ok": session_set(id, key, value) })) +} + +pub fn abi_session_get(args: &Value) -> Result { + let id = arg_str(args, "id")?; + let key = arg_str(args, "key")?; + match session_get(id, key) { + Some(v) => Ok(json!({ "ok": true, "value": v })), + None => Ok(json!({ "ok": false })), + } +} + +pub fn abi_session_del(args: &Value) -> Result { + let id = arg_str(args, "id")?; + let key = arg_str(args, "key")?; + Ok(json!({ "ok": session_del(id, key) })) +} + +pub fn abi_session_destroy(args: &Value) -> Result { + let id = arg_str(args, "id")?; + Ok(json!({ "ok": session_destroy(id) })) +} + +pub fn abi_auth_login(args: &Value) -> Result { + let username = arg_str(args, "username")?; + let password = arg_str(args, "password")?; + let users = args.get("users").cloned().unwrap_or(Value::Null); + let ttl = args + .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(_) => { + let id = session_new(ttl); + session_set(&id, "username", json!(username)); + Ok(json!({ "ok": true, "session_id": id, "username": username })) + } + None => Ok(json!({ "ok": false })), + } +} + +pub fn abi_auth_check(args: &Value) -> Result { + let id = arg_str(args, "session_id")?; + match session_get(id, "username") { + Some(Value::String(u)) => Ok(json!({ "ok": true, "username": u })), + _ => Ok(json!({ "ok": false })), + } +} + +pub fn abi_auth_logout(args: &Value) -> Result { + let id = arg_str(args, "session_id")?; + Ok(json!({ "ok": session_destroy(id) })) +} + +fn arg_str<'a>(v: &'a Value, key: &str) -> Result<&'a str, String> { + v.get(key) + .and_then(|x| x.as_str()) + .ok_or_else(|| format!("missing `{key}`")) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn users() -> Value { + json!([ + { "username": "admin", "password": "secret" }, + { "用户": "站长", "密码": "pw123" } + ]) + } + + #[test] + fn session_set_get_del() { + reset(3600); + let id = session_new(None); + assert!(!id.is_empty()); + assert!(session_set(&id, "username", json!("admin"))); + assert_eq!( + session_get(&id, "username"), + Some(json!("admin")) + ); + assert!(session_del(&id, "username")); + assert_eq!(session_get(&id, "username"), None); + assert!(session_destroy(&id)); + } + + #[test] + fn auth_login_check_logout() { + reset(3600); + let r = abi_auth_login(&json!({ + "username": "admin", + "password": "secret", + "users": users(), + })) + .unwrap(); + assert_eq!(r["ok"].as_bool(), Some(true)); + let sid = r["session_id"].as_str().unwrap().to_string(); + let check = abi_auth_check(&json!({ "session_id": sid })).unwrap(); + assert_eq!(check["ok"].as_bool(), Some(true)); + assert_eq!(check["username"], "admin"); + + let bad = abi_auth_login(&json!({ + "username": "admin", + "password": "wrong", + "users": users(), + })) + .unwrap(); + assert_eq!(bad["ok"].as_bool(), Some(false)); + + // Chinese column headers also work. + let zh = abi_auth_login(&json!({ + "username": "站长", + "password": "pw123", + "users": users(), + })) + .unwrap(); + assert_eq!(zh["ok"].as_bool(), Some(true)); + + let out = abi_auth_logout(&json!({ "session_id": sid })).unwrap(); + assert_eq!(out["ok"].as_bool(), Some(true)); + let check2 = abi_auth_check(&json!({ "session_id": sid })).unwrap(); + assert_eq!(check2["ok"].as_bool(), Some(false)); + } + + #[test] + fn cookie_roundtrip() { + reset(3600); + let id = session_new(None); + let cookie = session_cookie(&id, 3600); + assert!(cookie.starts_with("marqdo_sid=")); + let got = session_id_from_cookie(Some(&format!( + "theme=light; {cookie}" + ))); + assert_eq!(got.as_deref(), Some(id.as_str())); + assert_eq!(session_id_from_cookie(Some("theme=light")), None); + assert_eq!(session_id_from_cookie(None), None); + } + + #[test] + fn check_credentials_missing() { + assert!(check_credentials(&json!(null), "a", "b").is_none()); + assert!(check_credentials(&json!("x"), "a", "b").is_none()); + assert!(check_credentials(&json!([]), "a", "b").is_none()); + } +} diff --git a/plugins/web/src/table.rs b/plugins/web/src/table.rs index 18a3ed5..bb975b2 100644 --- a/plugins/web/src/table.rs +++ b/plugins/web/src/table.rs @@ -253,19 +253,19 @@ pub fn as_compose(table: &Value) -> Value { fn infer_slot(src: &str) -> String { let base = src.rsplit('.').next().unwrap_or(src); match base { - "nav" | "links" => "nav".into(), - "side" | "sidebar" => "sidebar".into(), - "foot" | "footer" => "footer".into(), + "nav" | "links" | "导航" => "nav".into(), + "side" | "sidebar" | "侧栏" => "sidebar".into(), + "foot" | "footer" | "页脚" => "footer".into(), other => other.to_string(), } } pub fn normalize_slot(name: &str) -> String { match name { - "nav" | "links" => "nav".into(), - "side" | "sidebar" => "sidebar".into(), - "foot" | "footer" => "footer".into(), - "main" | "index" | "" => "main".into(), + "nav" | "links" | "导航" => "nav".into(), + "side" | "sidebar" | "侧栏" => "sidebar".into(), + "foot" | "footer" | "页脚" => "footer".into(), + "main" | "index" | "" | "主体" => "main".into(), other => other.to_string(), } } diff --git a/plugins/web/src/ws.rs b/plugins/web/src/ws.rs new file mode 100644 index 0000000..630eedc --- /dev/null +++ b/plugins/web/src/ws.rs @@ -0,0 +1,132 @@ +//! WebSocket client primitive (design ext-web-net §3.4). +//! +//! Single request–response style: connect, send one text `message`, collect all +//! server text replies until close, then return `{ok, messages}`. + +use futures_util::{SinkExt, StreamExt}; +use serde_json::{json, Value}; +use tokio_tungstenite::tungstenite::http::header::HeaderName; + +/// Convert `headers` (a JSON map) into tungstenite header pairs. +fn headers_from_value(headers: Option<&Value>) -> Vec<(HeaderName, String)> { + let Some(h) = headers else { + return Vec::new(); + }; + match h { + Value::Object(m) => m + .iter() + .filter_map(|(k, v)| { + HeaderName::from_bytes(k.as_bytes()) + .ok() + .map(|hn| (hn, v.as_str().unwrap_or("").to_string())) + }) + .collect(), + _ => Vec::new(), + } +} + +/// Connect to a WebSocket URL, send `message`, drain text replies until close. +pub fn connect( + url: &str, + message: &str, + headers: Option<&Value>, + timeout_sec: u64, +) -> Result { + if !(url.starts_with("ws://") || url.starts_with("wss://")) { + return Err("ws url must start with ws:// or wss://".into()); + } + + let hdrs = headers_from_value(headers); + let mut builder = tokio_tungstenite::tungstenite::client::IntoClientRequest::into_client_request( + url.to_string(), + ) + .map_err(|e| format!("bad ws url: {e}"))?; + for (hn, v) in &hdrs { + builder + .headers_mut() + .insert(hn.clone(), v.parse().map_err(|e| format!("bad header value: {e}"))?); + } + + let rt = tokio::runtime::Runtime::new().map_err(|e| e.to_string())?; + rt.block_on(async move { + let timeout = tokio::time::Duration::from_secs(timeout_sec.max(1)); + let fut = tokio_tungstenite::connect_async(builder); + let (mut socket, _resp) = tokio::time::timeout(timeout, fut) + .await + .map_err(|_| "ws connect timeout".to_string())? + .map_err(|e| format!("ws connect: {e}"))?; + + let send = tokio_tungstenite::tungstenite::Message::Text(message.to_string()); + socket + .send(send) + .await + .map_err(|e| format!("ws send: {e}"))?; + + let mut messages = Vec::new(); + loop { + match tokio::time::timeout(timeout, socket.next()).await { + Ok(Some(Ok(tokio_tungstenite::tungstenite::Message::Text(t)))) => { + messages.push(t.to_string()); + } + Ok(Some(Ok(tokio_tungstenite::tungstenite::Message::Close(_)))) + | Ok(Some(Err(_))) => break, + Ok(None) => break, + Ok(Some(Ok(_))) => {} + Err(_) => break, + } + } + Ok(json!({ "ok": true, "messages": messages })) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use tokio::net::TcpListener; + use tokio_tungstenite::tungstenite::Message; + + #[test] + fn echo_roundtrip() { + let rt = tokio::runtime::Runtime::new().expect("rt"); + let listener = rt.block_on(async { TcpListener::bind("127.0.0.1:0").await.expect("bind") }); + let addr = listener.local_addr().expect("addr"); + let server = std::thread::spawn(move || { + let rt = tokio::runtime::Runtime::new().expect("server rt"); + rt.block_on(async move { + let (stream, _) = listener.accept().await.expect("accept"); + let ws = tokio_tungstenite::accept_async(stream).await.expect("ws"); + let (mut sink, mut source) = ws.split(); + if let Some(Ok(Message::Text(t))) = source.next().await { + let _ = sink.send(Message::Text(t)).await; + } + }); + }); + + let url = format!("ws://{addr}/"); + let got = connect(&url, "ping", None, 5).expect("connect"); + assert_eq!(got["ok"], true); + assert_eq!(got["messages"], json!(["ping"])); + server.join().expect("server join"); + } + + #[test] + fn refuses_bad_url() { + let err = connect("http://127.0.0.1:1", "hi", None, 1).unwrap_err(); + assert!(err.contains("ws://") || err.contains("ws url")); + } + + #[test] + fn refused_connection() { + // Connect to a port that is closed; connect_async fails fast. + let rt = tokio::runtime::Runtime::new().expect("rt"); + let listener = rt.block_on(async { TcpListener::bind("127.0.0.1:0").await.expect("bind") }); + let addr = listener.local_addr().expect("addr"); + drop(listener); // port now closed + let url = format!("ws://{addr}/"); + let err = connect(&url, "hi", None, 2).unwrap_err(); + assert!( + err.starts_with("ws connect") || err.contains("timeout"), + "unexpected error: {err}" + ); + } +} diff --git a/src/host/dispatch.rs b/src/host/dispatch.rs index f0f63a8..a76c04a 100644 --- a/src/host/dispatch.rs +++ b/src/host/dispatch.rs @@ -121,6 +121,8 @@ pub enum HostFn { MapMerge = 119, MapSize = 120, CollectionClear = 121, + CookieParse = 122, + MultipartParse = 123, } @@ -239,6 +241,8 @@ impl HostFn { 119 => Self::MapMerge, 120 => Self::MapSize, 121 => Self::CollectionClear, + 122 => Self::CookieParse, + 123 => Self::MultipartParse, _ => return None, }) } @@ -360,6 +364,8 @@ impl HostFn { "host_map_merge" | "map_merge" => Self::MapMerge, "host_map_size" | "map_size" => Self::MapSize, "host_collection_clear" | "collection_clear" => Self::CollectionClear, + "host_cookie_parse" | "cookie_parse" => Self::CookieParse, + "host_multipart_parse" | "multipart_parse" => Self::MultipartParse, _ => return None, }) } @@ -468,6 +474,8 @@ impl HostFn { Self::MapMerge => "host_map_merge", Self::MapSize => "host_map_size", Self::CollectionClear => "host_collection_clear", + Self::CookieParse => "host_cookie_parse", + Self::MultipartParse => "host_multipart_parse", Self::WritebackRecord => "host_writeback_record", Self::WritebackGet => "host_writeback_get", Self::WritebackClear => "host_writeback_clear", @@ -550,6 +558,8 @@ impl HostFn { Self::MapKeys | Self::MapValues | Self::MapItems | Self::MapSize => &["map"], Self::MapMerge => &["a", "b"], Self::CollectionClear => &["value"], + Self::CookieParse => &["text", "is_response"], + Self::MultipartParse => &["body", "boundary"], Self::WritebackRecord => &["value"], Self::WritebackGet | Self::WritebackClear => &[], Self::WritebackList => &[], @@ -830,6 +840,8 @@ pub fn call_host( HostFn::MapMerge => super::collection::map_merge(require(bound, "a")?, require(bound, "b")?), HostFn::MapSize => super::collection::map_size(require(bound, "map")?), HostFn::CollectionClear => super::collection::collection_clear(require(bound, "value")?), + HostFn::CookieParse => net::cookie_parse(require(bound, "text")?, bound.get("is_response")), + HostFn::MultipartParse => net::multipart_parse(require(bound, "body")?, require(bound, "boundary")?), HostFn::OuterCallLine => Ok(Value::Int( ctx.call_site_lines .first() diff --git a/src/host/net.rs b/src/host/net.rs index 71430cf..bae3e28 100644 --- a/src/host/net.rs +++ b/src/host/net.rs @@ -181,6 +181,273 @@ fn truthy_flag(v: Option<&Value>) -> bool { } } +/// Split a Cookie / Set-Cookie header value into individual cookie attribute tokens. +/// `Set-Cookie` may contain multiple `name=value` cookies plus attributes per cookie; +/// a single request `Cookie` header is a flat `name=value; name=value` list. +fn split_cookie_tokens(header: &str, is_set_cookie: bool) -> Vec> { + // Set-Cookie: each cookie block is separated by ", " only when a next cookie begins. + // We split on ',' that is NOT preceded by an Expires/Max-Age value (contains '-' or ':') + // — heuristic good enough for common cases; value may contain ';' (e.g. quoted). + if !is_set_cookie { + let mut cookies = Vec::new(); + for seg in header.split(';') { + let seg = seg.trim(); + if seg.is_empty() { + continue; + } + let mut cur = Vec::new(); + if let Some((k, v)) = seg.split_once('=') { + cur.push((k.trim().to_string(), v.trim().trim_matches('"').to_string())); + } else { + cur.push((seg.to_string(), String::new())); + } + cookies.push(cur); + } + return cookies; + } + + let mut cookies: Vec> = Vec::new(); + let mut cur: Vec<(String, String)> = Vec::new(); + let mut i = 0; + let chars: Vec = header.chars().collect(); + while i < chars.len() { + let c = chars[i]; + if c == ',' { + // A comma is part of an `Expires` date value (e.g. "Wed, 21 Oct 2026") + // only when the previous attribute is `Expires`; otherwise it separates + // one Set-Cookie block from the next. + let in_expires_date = cur + .last() + .map(|(k, _)| k.eq_ignore_ascii_case("expires")) + .unwrap_or(false); + if in_expires_date { + if let Some((_, v)) = cur.last_mut() { + v.push(','); + } + i += 1; + continue; + } + // A real cookie separator: flush current block and start a new one. + cookies.push(std::mem::take(&mut cur)); + i += 1; + continue; + } + if c == ';' { + // Cookie request header: `;` separates independent cookies. + // Set-Cookie: `;` separates attributes *within* the same cookie block. + if !is_set_cookie || cur.is_empty() { + if !cur.is_empty() { + cookies.push(std::mem::take(&mut cur)); + } + } + i += 1; + continue; + } + // Accumulate a token: name=value until ; or , + let mut j = i; + while j < chars.len() && chars[j] != ';' && chars[j] != ',' { + j += 1; + } + let token: String = chars[i..j].iter().collect(); + let token = token.trim(); + if !token.is_empty() { + if let Some((k, v)) = token.split_once('=') { + cur.push(( + k.trim().to_string(), + v.trim().trim_matches('"').to_string(), + )); + } else { + cur.push((token.to_string(), String::new())); + } + } + i = j; + } + if !cur.is_empty() { + cookies.push(cur); + } + cookies +} + +fn normalized_key(s: &str) -> String { + s.chars() + .filter(|c| *c != '_' && *c != '-') + .collect::() + .to_ascii_lowercase() +} + +fn cookie_attr_value<'a>(pairs: &'a [(String, String)], key: &str) -> Option<&'a str> { + let want = normalized_key(key); + pairs + .iter() + .find(|(k, _)| normalized_key(k) == want) + .map(|(_, v)| v.as_str()) +} + +fn bool_attr(pairs: &[(String, String)], key: &str) -> Option { + let want = normalized_key(key); + pairs + .iter() + .find(|(k, _)| normalized_key(k) == want) + .map(|(_, v)| { + if v.is_empty() { + // Present-without-value boolean flag (e.g. `Secure` / `HttpOnly`). + true + } else { + !(v.eq_ignore_ascii_case("false") + || v.eq_ignore_ascii_case("0") + || v.eq_ignore_ascii_case("no")) + } + }) +} + +/// Parse a Cookie request header or Set-Cookie response header (RFC 6265 subset) +/// into a list of `{name, value, path, domain, expires, max_age, secure, http_only, same_site}`. +/// `is_response=true` treats the input as one or more `Set-Cookie` blocks. +pub fn cookie_parse(text: &Value, is_response: Option<&Value>) -> Result { + let header = as_text(text, "text")?; + let is_response = truthy_flag(is_response); + let blocks = split_cookie_tokens(header, is_response); + let mut out = Vec::new(); + for block in blocks { + let name = block + .first() + .map(|(k, _)| k.clone()) + .unwrap_or_default(); + if name.is_empty() { + continue; + } + let value = block.first().map(|(_, v)| v.clone()).unwrap_or_default(); + let mut entry: Vec<(String, Value)> = vec![ + ("name".into(), Value::Text(name)), + ("value".into(), Value::Text(value)), + ]; + // Attribute with optional value -> Text or None. + let text_attrs = [ + ("path", "path"), + ("domain", "domain"), + ("expires", "expires"), + ("max_age", "max_age"), + ("sameSite", "same_site"), + ]; + for (attr, key) in text_attrs { + let v = cookie_attr_value(&block, attr); + entry.push(( + key.to_string(), + match v { + Some(s) => Value::Text(s.to_string()), + None => Value::None, + }, + )); + } + // Boolean flags (Secure / HttpOnly are present-without-value). + for attr in ["secure", "http_only"] { + let present = bool_attr(&block, attr); + entry.push(( + attr.to_string(), + match present { + Some(b) => Value::Bool(b), + None => Value::None, + }, + )); + } + out.push(Value::Map(entry)); + } + Ok(Value::List(out)) +} + +/// Parse a `multipart/form-data` body (given its boundary) into +/// `[{name, filename?, content_type?, value}]`. Field values are decoded as text. +pub fn multipart_parse(body: &Value, boundary: &Value) -> Result { + let body = as_text(body, "body")?; + let boundary = as_text(boundary, "boundary")?; + let boundary = boundary.trim(); + if boundary.is_empty() { + return Err("multipart boundary is empty".into()); + } + let delim = format!("--{boundary}"); + let mut parts = Vec::new(); + let mut rest = body; + loop { + let Some(pos) = rest.find(&delim) else { + break; + }; + rest = &rest[pos + delim.len()..]; + // After boundary: either `--` (end) or CRLF + headers + if rest.strip_prefix("--").is_some() { + break; + } + let rest_trim = rest.strip_prefix("\r\n").or_else(|| rest.strip_prefix('\n')); + let Some(after) = rest_trim else { + break; + }; + rest = after; + // Headers until blank line + let Some(header_end) = rest.find("\r\n\r\n").or_else(|| rest.find("\n\n")) else { + break; + }; + let header_block = &rest[..header_end]; + let header_len = if rest[header_end..].starts_with("\r\n\r\n") { + 4 + } else { + 2 + }; + let body_start = header_end + header_len; + // Part body until next boundary + let body_end = rest[body_start..] + .find(&delim) + .map(|p| body_start + p) + .unwrap_or(rest.len()); + let part_body = &rest[body_start..body_end]; + // Trim trailing CRLF before boundary + let part_body = part_body + .strip_suffix("\r\n") + .or_else(|| part_body.strip_suffix('\n')) + .unwrap_or(part_body); + + let mut name = String::new(); + let mut filename = None; + let mut content_type = None; + for line in header_block.split('\n') { + let line = line.strip_suffix('\r').unwrap_or(line); + let lk = line.to_ascii_lowercase(); + if let Some(v) = lk.strip_prefix("content-disposition:") { + // form-data; name="x"; filename="y.txt" + for seg in v.split(';') { + let seg = seg.trim(); + if let Some(val) = seg.strip_prefix("name=") { + name = val.trim_matches('"').to_string(); + } else if let Some(val) = seg.strip_prefix("filename=") { + filename = Some(val.trim_matches('"').to_string()); + } + } + } else if let Some(v) = lk.strip_prefix("content-type:") { + content_type = Some(v.trim().to_string()); + } + } + let mut entry = vec![("name".to_string(), Value::Text(name))]; + match filename { + Some(f) if !f.is_empty() => { + entry.push(("filename".to_string(), Value::Text(f))); + entry.push(( + "content_type".to_string(), + Value::Text(content_type.unwrap_or_default()), + )); + entry.push(("value".to_string(), Value::Text(part_body.to_string()))); + } + _ => { + entry.push(("value".to_string(), Value::Text(part_body.to_string()))); + } + } + parts.push(Value::Map(entry)); + rest = &rest[body_end..]; + if rest.starts_with(&delim) { + continue; + } + break; + } + Ok(Value::List(parts)) +} + /// Extract `data:` payloads from an SSE body (ignores comments / event: / id:). pub fn sse_data_payloads(text: &str) -> Vec { let mut out = Vec::new(); @@ -619,4 +886,99 @@ data: [DONE]\n\ "expected done with result ok" ); } + + #[test] + fn cookie_request_header_parse() { + let v = cookie_parse( + &Value::Text("session=abc123; theme=dark; lang=zh-CN".into()), + Some(&Value::Bool(false)), + ) + .unwrap(); + let Value::List(items) = v else { + panic!("expected list"); + }; + assert_eq!(items.len(), 3); + let Value::Map(first) = &items[0] else { + panic!("expected map"); + }; + assert!(first + .iter() + .any(|(k, v)| k == "name" && matches!(v, Value::Text(t) if t == "session"))); + assert!(first + .iter() + .any(|(k, v)| k == "value" && matches!(v, Value::Text(t) if t == "abc123"))); + } + + #[test] + fn set_cookie_response_parse() { + let header = "id=42; Path=/; HttpOnly; Secure; SameSite=Lax, theme=light; Expires=Wed, 21 Oct 2026 07:28:00 GMT; Max-Age=3600"; + let blocks = split_cookie_tokens(header, true); + eprintln!("blocks = {blocks:?}"); + let v = cookie_parse( + &Value::Text(header.into()), + Some(&Value::Bool(true)), + ) + .unwrap(); + let Value::List(items) = v else { + panic!("expected list"); + }; + assert_eq!(items.len(), 2, "two Set-Cookie blocks"); + let Value::Map(first) = &items[0] else { + panic!("expected map"); + }; + assert!(first + .iter() + .any(|(k, v)| k == "path" && matches!(v, Value::Text(t) if t == "/"))); + assert!(first + .iter() + .any(|(k, v)| k == "http_only" && matches!(v, Value::Bool(true)))); + assert!(first + .iter() + .any(|(k, v)| k == "secure" && matches!(v, Value::Bool(true)))); + assert!(first + .iter() + .any(|(k, v)| k == "same_site" && matches!(v, Value::Text(t) if t == "Lax"))); + let Value::Map(second) = &items[1] else { + panic!("expected map"); + }; + assert!(second + .iter() + .any(|(k, v)| k == "name" && matches!(v, Value::Text(t) if t == "theme"))); + assert!(second.iter().any(|(k, v)| k == "max_age" + && matches!(v, Value::Text(t) if t == "3600"))); + } + + #[test] + fn multipart_form_parse() { + let boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; + let body = format!( + "--{boundary}\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\nHello\r\n--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"a.txt\"\r\nContent-Type: text/plain\r\n\r\nfile body\r\n--{boundary}--\r\n" + ); + let v = multipart_parse(&Value::Text(body.into()), &Value::Text(boundary.into())).unwrap(); + let Value::List(items) = v else { + panic!("expected list"); + }; + assert_eq!(items.len(), 2); + let Value::Map(f0) = &items[0] else { + panic!("expected map"); + }; + assert!(f0 + .iter() + .any(|(k, v)| k == "name" && matches!(v, Value::Text(t) if t == "title"))); + assert!(f0 + .iter() + .any(|(k, v)| k == "value" && matches!(v, Value::Text(t) if t == "Hello"))); + let Value::Map(f1) = &items[1] else { + panic!("expected map"); + }; + assert!(f1 + .iter() + .any(|(k, v)| k == "name" && matches!(v, Value::Text(t) if t == "file"))); + assert!(f1 + .iter() + .any(|(k, v)| k == "filename" && matches!(v, Value::Text(t) if t == "a.txt"))); + assert!(f1 + .iter() + .any(|(k, v)| k == "value" && matches!(v, Value::Text(t) if t == "file body"))); + } } diff --git a/src/host/plugin.rs b/src/host/plugin.rs index 49486f9..db6832a 100644 --- a/src/host/plugin.rs +++ b/src/host/plugin.rs @@ -273,6 +273,20 @@ unsafe extern "C" fn host_query( "cwd" => Ok(serde_json::Value::String( ctx.cwd.to_string_lossy().into_owned(), )), + "entry_dir" => Ok(serde_json::Value::String( + ctx.entry_path + .as_ref() + .map(|p| { + if p.is_dir() { + p.clone() + } else { + p.parent().unwrap_or(p).to_path_buf() + } + }) + .unwrap_or_else(|| ctx.cwd.clone()) + .to_string_lossy() + .into_owned(), + )), "call_lib_path" => (|| { let raw = args_owned.as_deref().unwrap_or("{}"); let args: serde_json::Value = serde_json::from_str(if raw.trim().is_empty() { diff --git a/src/parse/expr.rs b/src/parse/expr.rs index 120eaf4..e01a0fa 100644 --- a/src/parse/expr.rs +++ b/src/parse/expr.rs @@ -297,6 +297,26 @@ impl<'a> Parser<'a> { false } + /// Consume up to `max` hex digits; fewer (even zero) are allowed. + /// Returns the parsed value, or an error if a digit exceeds the limit + /// would overflow (`\xHH` requires exactly 2, `\u{...}` up to 6). + fn take_hex_digits(&mut self, max: usize) -> Result { + let mut value: u64 = 0; + let mut count = 0; + while count < max { + let Some(c) = self.peek_char() else { + break; + }; + let Some(d) = c.to_digit(16) else { + break; + }; + value = value * 16 + u64::from(d); + self.bump_char(); + count += 1; + } + Ok(value) + } + fn parse_or(&mut self) -> Result { let mut left = self.parse_and()?; loop { @@ -582,15 +602,51 @@ impl<'a> Parser<'a> { .peek_char() .ok_or_else(|| anyhow::anyhow!("dangling \\ in string"))?; self.bump_char(); - lit.push(match esc { - 'n' => '\n', - 't' => '\t', - 'r' => '\r', - '\\' => '\\', - '"' => '"', - '\'' => '\'', - other => bail!("unknown escape \\{other} in string"), - }); + match esc { + 'n' => lit.push('\n'), + 't' => lit.push('\t'), + 'r' => lit.push('\r'), + '\\' => lit.push('\\'), + '"' => lit.push('"'), + '\'' => lit.push('\''), + 'x' => { + let start = self.i; + let cp = self.take_hex_digits(2)?; + if self.i - start == 2 { + lit.push( + char::from_u32(cp as u32) + .ok_or_else(|| anyhow::anyhow!("invalid \\x escape"))?, + ); + } else { + // `\x` with fewer than two hex digits is not a valid + // escape: keep it verbatim. + self.i = start; + lit.push('\\'); + lit.push('x'); + } + } + 'u' => { + if self.eat("{") { + let digits = self.take_hex_digits(6)?; + if !self.eat("}") { + bail!("unterminated \\u{{...}} escape in string"); + } + lit.push( + char::from_u32(digits as u32) + .ok_or_else(|| anyhow::anyhow!("invalid \\u escape"))?, + ); + } else { + // `\u` without `{…}` is not a valid escape: keep it verbatim. + lit.push('\\'); + lit.push('u'); + } + } + // Unknown escapes are kept verbatim (`\q` → `\q`). + other => { + lit.push('\\'); + lit.push(other); + } + } continue; } if c == '`' { @@ -1010,6 +1066,59 @@ mod tests { assert!(matches!(e, Expr::Interp(_))); } + #[test] + fn quoted_string_hex_unicode_escapes() { + // `\x22` → `"` (hex escape) + let e = parse_expr("\"a\\x22b\"").unwrap(); + assert!(matches!( + e, + Expr::Literal(Literal::Text(ref s)) if s == "a\"b" + )); + // `\x1F` → control char + let e = parse_expr("\"\\x1F\"").unwrap(); + assert!(matches!( + e, + Expr::Literal(Literal::Text(ref s)) if s == "\u{1f}" + )); + // `\u{4e2d}` → 中 + let e = parse_expr("\"\\u{4e2d}\"").unwrap(); + assert!(matches!( + e, + Expr::Literal(Literal::Text(ref s)) if s == "中" + )); + // `\u{1F600}` → 😀 + let e = parse_expr("\"\\u{1F600}\"").unwrap(); + assert!(matches!( + e, + Expr::Literal(Literal::Text(ref s)) if s == "😀" + )); + // `\x22` inside a string that also interpolates + let e = parse_expr("\"x=\\x22`v`\\x22\"").unwrap(); + assert!(matches!(e, Expr::Interp(_))); + // `\x` without two hex digits is kept verbatim (`\x` → `\x`) + let e = parse_expr("\"a\\xb\"").unwrap(); + assert!(matches!( + e, + Expr::Literal(Literal::Text(ref s)) if s == "a\\xb" + )); + // `\u` without braces is kept verbatim (`\u` → `\u`) + let e = parse_expr("\"a\\ub\"").unwrap(); + assert!(matches!( + e, + Expr::Literal(Literal::Text(ref s)) if s == "a\\ub" + )); + } + + #[test] + fn quoted_string_unknown_escape_kept_verbatim() { + // Unknown escapes like `\q` are kept as-is rather than failing. + let e = parse_expr("\"a\\qb\"").unwrap(); + assert!(matches!( + e, + Expr::Literal(Literal::Text(ref s)) if s == "a\\qb" + )); + } + #[test] fn call_arg_quoted_path_keeps_slashes() { let e = parse_call_arg_value("\".marqdo/agent-runs\"").unwrap(); diff --git a/tests/ext/web-admin-smoke.mq.md b/tests/ext/web-admin-smoke.mq.md index 6984ed1..4af5b9d 100644 --- a/tests/ext/web-admin-smoke.mq.md +++ b/tests/ext/web-admin-smoke.mq.md @@ -84,3 +84,10 @@ import json:lib/json.mq.md > print text=required-should-fail 2. * > print text=required-ok + +*errs = > json.get value=`bad` key="errors"* +*nerr = > len value=`errs`* +1. `nerr` >= 1 + > print text=errors-echoed-ok +2. * + > print text=errors-echoed-fail diff --git a/tests/ext/web-entrydir-smoke.mq.md b/tests/ext/web-entrydir-smoke.mq.md new file mode 100644 index 0000000..3f656fb --- /dev/null +++ b/tests/ext/web-entrydir-smoke.mq.md @@ -0,0 +1,39 @@ +--- +title: web entry-dir relative path smoke +description: Relative db path resolves against the entry script dir, not cwd. +import web:ext/web/web.mq.md +--- + +# main + + + +*store = > web.db url="sqlite:entrydir-fixtures/data/entrydir.db"* + +`schema` = + +| 字段 | 类型 | +|------|------| +| title | text | + +> `store`.init name=items fields=`schema` + +`one` = + +| 行 | title | +|----|-------| +| 1 | hello | + +> `store`.insert table=items rows=`one` + +*rows = > `store`.select table="items" limit=10* +*n = > len value=`rows`* + +1. `n` == 1 + > print text=entrydir-db-ok +2. * + > print text=entrydir-db-fail diff --git a/tests/ext/web-net-smoke.mq.md b/tests/ext/web-net-smoke.mq.md new file mode 100644 index 0000000..8e90a79 --- /dev/null +++ b/tests/ext/web-net-smoke.mq.md @@ -0,0 +1,148 @@ +--- +title: web network-extension smoke +description: Session/auth/ws ABI primitives (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 + + + +`users` = + +| 行 | 用户 | 密码 | +|----|------|------| +| 1 | admin | secret | +| 2 | 站长 | pw123 | + + + +*sess = > web_session_new ttl_sec=120* +*sid = sess[^id]* +1. `sid` != "" + > print text=session-new-ok +2. * + > print text=session-new-fail + +*set1 = > web_session_set id=`sid` key="theme" value="dark"* +*ok1 = set1[^ok]* +1. `ok1` + > print text=session-set-ok +2. * + > print text=session-set-fail + +*get1 = > web_session_get id=`sid` key="theme"* +*v1 = get1[^value]* +1. `v1` == "dark" + > print text=session-get-ok +2. * + > print text=session-get-fail + +*del1 = > web_session_del id=`sid` key="theme"* +*ok2 = del1[^ok]* +1. `ok2` + > print text=session-del-ok +2. * + > print text=session-del-fail + +*get2 = > web_session_get id=`sid` key="theme"* +*ok3 = get2[^ok]* +1. `ok3` + > print text=session-del-confirm-fail +2. * + > print text=session-del-confirm + + + +*login = > web_auth_login username="admin" password="secret" users=`users` session_ttl=120* +*lok = login[^ok]* +1. `lok` + > print text=auth-login-ok +2. * + > print text=auth-login-fail + +*sid2 = login[^session_id]* +*check = > web_auth_check session_id=`sid2`* +*cok = check[^ok]* +1. `cok` + > print text=auth-check-ok +2. * + > print text=auth-check-fail + +*badlogin = > web_auth_login username="admin" password="wrong" users=`users` session_ttl=120* +*bok = badlogin[^ok]* +1. `bok` + > print text=auth-bad-should-fail +2. * + > print text=auth-bad-ok + +*zhlogin = > web_auth_login username="站长" password="pw123" users=`users` session_ttl=120* +*zhok = zhlogin[^ok]* +1. `zhok` + > print text=auth-zh-ok +2. * + > print text=auth-zh-fail + +*logout = > web_auth_logout session_id=`sid2`* +*dok = logout[^ok]* +1. `dok` + > print text=auth-logout-ok +2. * + > print text=auth-logout-fail + +*check2 = > web_auth_check session_id=`sid2`* +*cok2 = check2[^ok]* +1. `cok2` + > print text=auth-logout-confirm-fail +2. * + > print text=auth-logout-confirm + + + +*pg = > web.page title="ws-test"* +*app = > web.app page=`pg` admin=True* +*app = > `app`.route_ws path="/live" echo=True* +*routes = app[^ws_routes]* +*has_live = routes[^/live]* +1. `has_live` + > print text=ws-route-ok +2. * + > print text=ws-route-fail + +*app = > `app`.auth users=`users` session_ttl=120* +*authobj = app[^auth]* +*ausers = authobj[^users]* +*au1 = ausers[^1][^用户]* +*ou1 = users[^1][^用户]* +1. `au1` == `ou1` + > print text=app-auth-ok +2. * + > print text=app-auth-fail + + + +*authz = > web.auth users=`users` session_ttl=120* +*login2 = > `authz`.login username="admin" password="secret"* +*lok2 = login2[^ok]* +1. `lok2` + > print text=ext-auth-ok +2. * + > print text=ext-auth-fail + +*ws = > web.ws timeout_sec=1* +*badurl = > `ws`.connect url="ws://127.0.0.1:1" message="hi"* +*bwok = badurl[^ok]* +1. `bwok` + > print text=ws-connect-should-fail +2. * + > print text=ws-connect-error-ok \ No newline at end of file diff --git a/tests/gold.rs b/tests/gold.rs index 1689023..d73722b 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -652,7 +652,10 @@ fn keywords_input_escape() { #[test] fn keywords_quoted_string() { - assert_out("tests/keywords/quoted-string.mq.md", "a\nb"); + assert_out( + "tests/keywords/quoted-string.mq.md", + "a\nb\nhex \" quote' single\n中文\nkeep \\q verbatim\nABC", + ); } #[test] @@ -1331,6 +1334,52 @@ db-ok", ); } +#[test] +fn ext_web_entrydir_smoke() { + // Relative db paths must resolve against the *entry script* directory, + // not the process cwd (host_query("entry_dir")). We run from the repo + // root while the script lives in tests/ext, so the sqlite file must + // land under tests/ext/entrydir-fixtures/data/ and NOT repo-root/. + 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-entrydir-smoke.mq.md"; + let fixture_data = std::path::Path::new("tests/ext/entrydir-fixtures/data"); + let db_path = fixture_data.join("entrydir.db"); + let _ = std::fs::remove_dir_all(fixture_data); + let stale_root = std::path::Path::new("entrydir-fixtures"); + let _ = std::fs::remove_dir_all(stale_root); + + // cwd = repo root (CARGO_MANIFEST_DIR), script in tests/ext. + let output = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["run", script]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .output() + .expect("run web-entrydir-smoke"); + let code = output.status.code().unwrap_or(1); + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + assert_eq!(code, 0, "{script} stderr={stderr}"); + assert!( + stdout.contains("entrydir-db-ok"), + "{script} stdout={stdout}" + ); + assert!( + db_path.is_file(), + "expected db at {} (script dir), stdout={stdout} stderr={stderr}", + db_path.display() + ); + assert!( + !stale_root.exists(), + "db must not resolve against cwd (repo root): {} exists", + stale_root.display() + ); +} + #[test] fn ext_web_form_smoke() { let status = Command::new("cargo") @@ -1364,7 +1413,36 @@ insert-ok get-ok update-ok edit-ok -required-ok", +required-ok +errors-echoed-ok", + ); +} + +#[test] +fn ext_web_net_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-net-smoke.mq.md", + "session-new-ok +session-set-ok +session-get-ok +session-del-ok +session-del-confirm +auth-login-ok +auth-check-ok +auth-bad-ok +auth-zh-ok +auth-logout-ok +auth-logout-confirm +ws-route-ok +app-auth-ok +ext-auth-ok +ws-connect-error-ok", ); } @@ -1992,6 +2070,22 @@ fn lib_net_encode() { assert_out("tests/lib/net-encode.mq.md", "a+b"); } +#[test] +fn lib_net_cookie_parse() { + assert_out( + "tests/lib/net-cookie.mq.md", + "session\nabc123\nTrue\nTrue\nLax\n3600", + ); +} + +#[test] +fn lib_net_multipart_parse() { + assert_out( + "tests/lib/net-multipart.mq.md", + "title\nHello\nfile\na.txt\ntext/plain\nfile body", + ); +} + #[test] fn lib_net_openai_sse() { assert_out( diff --git a/tests/keywords/quoted-string.mq.md b/tests/keywords/quoted-string.mq.md index cff3b4f..94afbf8 100644 --- a/tests/keywords/quoted-string.mq.md +++ b/tests/keywords/quoted-string.mq.md @@ -5,5 +5,16 @@ title: quoted string literal # main *s = "a\nb"* - > print text=`s` + +*s2 = "hex \x22 quote\x27 single"* +> print text=`s2` + +*s3 = "\u{4e2d}\u{6587}"* +> print text=`s3` + +*s4 = "keep \q verbatim"* +> print text=`s4` + +*s5 = "\x41\x42\x43"* +> print text=`s5` \ No newline at end of file diff --git a/tests/lib/net-cookie.mq.md b/tests/lib/net-cookie.mq.md new file mode 100644 index 0000000..3fe2515 --- /dev/null +++ b/tests/lib/net-cookie.mq.md @@ -0,0 +1,17 @@ +--- +title: net cookie_parse +import net:lib/net.mq.md +--- + +# main + +*req = > net.cookie_parse text="session=abc123; theme=dark"* + +*resp = > net.cookie_parse text="id=42; Path=/; HttpOnly; Secure; SameSite=Lax, theme=light; Max-Age=3600" is_response=True* + +> print text=`req`[^1][^name] +> print text=`req`[^1][^value] +> print text=`resp`[^1][^http_only] +> print text=`resp`[^1][^secure] +> print text=`resp`[^1][^same_site] +> print text=`resp`[^2][^max_age] diff --git a/tests/lib/net-multipart.mq.md b/tests/lib/net-multipart.mq.md new file mode 100644 index 0000000..e1a085a --- /dev/null +++ b/tests/lib/net-multipart.mq.md @@ -0,0 +1,19 @@ +--- +title: net multipart_parse +import net:lib/net.mq.md +--- + +# main + +`boundary` = ----WebKitFormBoundary7MA4YWxkTrZu0gW + +`body` = "--`boundary`\nContent-Disposition: form-data; name=\"title\"\n\nHello\n--`boundary`\nContent-Disposition: form-data; name=\"file\"; filename=\"a.txt\"\nContent-Type: text/plain\n\nfile body\n--`boundary`--\n" + +*parts = > net.multipart_parse body=`body` boundary=`boundary`* + +> print text=`parts`[^1][^name] +> print text=`parts`[^1][^value] +> print text=`parts`[^2][^name] +> print text=`parts`[^2][^filename] +> print text=`parts`[^2][^content_type] +> print text=`parts`[^2][^value]