Extend network stack: session/auth, WebSocket, cookie/multipart primitives

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 <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-25 14:58:46 +08:00
1 parent 7dfa161422
commit 7645dd4c1f
47 files changed
+3380 -75

No files matched your search

+19
View File
@@ -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 回显端点(首页小部件或 `网页.实时` 的 `连接` 接入)
@@ -0,0 +1,10 @@
## 页脚
`页脚` =
| 属性 | 值 | 样式 |
|------|-----|------|
| Marqdo 网络扩展示例 | https://github.com | |
| 文档 | /tools | |
**`页脚`**
@@ -0,0 +1,15 @@
## 导航
站点顶部导航。`后台` 指向需要登录的 `/admin`。
`导航` =
| 属性 | 值 | 样式 |
|------|-----|------|
| 首页 | / | |
| 关于 | /about | |
| 发布 | /new | |
| 工具 | /tools | |
| 后台 | /admin | |
**`导航`**
@@ -0,0 +1,15 @@
## 侧栏
本示例展示了哪些特性。
`侧栏` =
| 属性 | 值 | 样式 |
|------|-----|------|
| 文章 | / | |
| WebSocket | /#live | |
| 登录鉴权 | /admin | |
| 表单 | /new | |
| lib/net | /tools | |
**`侧栏`**
@@ -0,0 +1,23 @@
## 结构
`字段` =
| 字段 | 类型 | 可空 |
|------|------|------|
| id | integer | false |
| title | text | false |
| body | text | true |
**`字段`**
## 种子
`行` =
| id | title | body |
|----|-------|------|
| 1 | 欢迎来到网络扩展站 | 首页列表来自 SQLite。 |
| 2 | 登录门禁后台 | /admin 页面需要 session 会话。 |
| 3 | WebSocket 实时 | /live 端点会把你的消息实时回显。 |
**`行`**
+19
View File
@@ -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**
+111
View File
@@ -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 = > 网页.页面 标题="网络扩展站" 引言="<h1>网络扩展站</h1><p>SQLite 文章列表、WebSocket 实时小部件、登录门禁后台、以及标准库 lib/net 的解析工具。</p><h2 id=\"live\">实时 WebSocket</h2><p id=\"live-status\">connecting…</p><input id=\"live-msg\" placeholder=\"输入一条消息…\"><button id=\"live-send\" disabled>发送</button><div id=\"live-out\"></div><script src=\"/static/live.js\"></script>"*
*page = > page.组件装配 组件=`首页`*
*page = > page.主体装配 主体=`列表`*
*about = > 网页.页面 标题="关于" 引言="<h1>关于</h1><p>演示网络扩展后的 web 库:会话、登录鉴权、WebSocket、multipart/cookie 解析。</p>"*
*about = > about.组件装配 组件=`首页`*
<!-- 工具页:用 lib/net 解析展示 -->
*解析请求 = > 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]*
`工具段` =
| 段 |
|----|
| <h1>网络工具</h1><p>由 lib/net 提供的解析演示:解析Cookie 与 解析多部分。</p><section class="content cards"> |
| <article><h2>Cookie 请求头</h2><p> |
| `请求块`[^name]=`请求块`[^value] |
| </p></article><article><h2>Set-Cookie 响应头</h2><p> |
| `响应块`[^name]=`响应块`[^value] · HttpOnly=`响应块`[^http_only] |
| </p></article><article><h2>多部分字段</h2><p> |
| `字段块`[^name]=`字段块`[^value] |
| </p></article><article><h2>多部分文件</h2><p> |
| `文件块`[^name] · `文件块`[^filename] · `文件块`[^content_type] |
| </p></article></section> |
*工具引言 = > text.str_join xs=`工具段` sep=""*
*tools = > 网页.页面 标题="工具" 引言=`工具引言`*
*tools = > tools.组件装配 组件=`首页`*
*article_form = > 网页.表单 表="articles" 动作="插入"*
*article_form = > article_form.字段 字段=`文章字段`*
*article_form = > article_form.规则 规则=`文章规则`*
*new = > 网页.页面 标题="发布" 引言="<h1>发布</h1><p>表单插入到 articles 表。</p>"*
*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`.监听
+40
View File
@@ -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();
});
})();
@@ -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 |
**`卡片正文`**
+19
View File
@@ -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.
@@ -0,0 +1,10 @@
## foot
`foot` =
| 属性 | 值 | 样式 |
|------|-----|------|
| Marqdo web-net example | https://github.com | |
| Docs | /tools | |
**`foot`**
@@ -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`**
@@ -0,0 +1,15 @@
## side
Sidebar index of what this example demonstrates.
`side` =
| 属性 | 值 | 样式 |
|------|-----|------|
| Articles | / | |
| WebSocket | /#live | |
| Auth | /admin | |
| Forms | /new | |
| net lib | /tools | |
**`side`**
+23
View File
@@ -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`**
+19
View File
@@ -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**
+129
View File
@@ -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="<h1>Net Site</h1><p>Articles from SQLite, a live WebSocket widget, login-gated admin, and net-library parsing tools.</p><h2 id=\"live\">Live WebSocket</h2><p id=\"live-status\">connecting…</p><input id=\"live-msg\" placeholder=\"type a message…\"><button id=\"live-send\" disabled>send</button><div id=\"live-out\"></div><script src=\"/static/live.js\"></script>"*
*page = > page.compose_components components=home*
*page = > page.compose_main main=index*
About page: same shell.
*about = > web.page title="About" intro="<h1>About</h1><p>Demonstrates the network-extended web library: sessions, auth, WebSocket, multipart/cookie parsing.</p>"*
*about = > about.compose_components components=home*
<!-- Tools page (lib/net parsing) -->
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<bytes>\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` =
| 段 |
|----|
| <h1>Net tools</h1><p>Parsing demos powered by lib/net: cookie_parse and multipart_parse.</p><section class="content cards"> |
| <article><h2>Cookie request</h2><p> |
| `req_cookie`[^name]=`req_cookie`[^value] |
| </p></article><article><h2>Set-Cookie response</h2><p> |
| `resp_cookie`[^name]=`resp_cookie`[^value] · HttpOnly=`resp_cookie`[^http_only] |
| </p></article><article><h2>Multipart field</h2><p> |
| `mp_title`[^name]=`mp_title`[^value] |
| </p></article><article><h2>Multipart file</h2><p> |
| `mp_cover`[^name] · `mp_cover`[^filename] · `mp_cover`[^content_type] |
| </p></article></section> |
*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="<h1>Publish</h1><p>Form inserts into articles.</p>"*
*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
+40
View File
@@ -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();
});
})();
+42
View File
@@ -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`**