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

+7
View File
@@ -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
+39
View File
@@ -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
<!--
Run from a different cwd than the script dir (e.g. repo root):
the sqlite file must land under <script dir>/entrydir-fixtures/data,
NOT under the process cwd.
-->
*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
+148
View File
@@ -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
<!-- load web plugin so ABI primitives are registered -->
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
<!-- admin users table (row-oriented records) -->
`users` =
| 行 | 用户 | 密码 |
|----|------|------|
| 1 | admin | secret |
| 2 | 站长 | pw123 |
<!-- session CRUD -->
*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
<!-- auth login/check/logout -->
*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
<!-- route ws + app auth (offline) -->
*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
<!-- via ext/web author surface -->
*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
+96 -2
View File
@@ -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(
+12 -1
View File
@@ -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`
+17
View File
@@ -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]
+19
View File
@@ -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]