From e981168d92b7354d52462cb4afd62cac3c09fa3d Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 20 Nov 2026 23:13:06 +0800 Subject: [PATCH] feat(stdlib): add HTTP framing over TCP and close P5 C10 locally - Add stdlib/net/http.mq.md: status/header/chunked response parsing and request construction in .mq.md; no host HTTP/TLS implementation. - Add stdlib/tests/http.mq.md and an end-to-end loopback test that sends a GET over resource.open("tcp"), reads bytes, parses the response and returns status/content-type/body. - Update ADR-0034, status/spec/README/changelog and evidence; refresh the bundle digest. Gate: fmt, clippy -D warnings, 430 tests passed. --- CHANGELOG.md | 3 + README.md | 3 + crates/cli/tests/http_over_tcp.rs | 120 +++++++++++ crates/cli/tests/stdlib_http.rs | 40 ++++ docs/STATUS.md | 9 +- docs/adr/0034-tcp-resource-endpoint.md | 5 +- docs/evidence/2026-11-20-http-over-tcp.md | 52 +++++ stdlib/README.md | 3 +- stdlib/net/http.mq.md | 239 ++++++++++++++++++++++ stdlib/stdlib.json | 2 +- stdlib/tests/http.mq.md | 14 ++ 11 files changed, 486 insertions(+), 4 deletions(-) create mode 100644 crates/cli/tests/http_over_tcp.rs create mode 100644 crates/cli/tests/stdlib_http.rs create mode 100644 docs/evidence/2026-11-20-http-over-tcp.md create mode 100644 stdlib/net/http.mq.md create mode 100644 stdlib/tests/http.mq.md diff --git a/CHANGELOG.md b/CHANGELOG.md index b33c601..f811398 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -127,6 +127,9 @@ currently `0.1.0`; no release tag has been cut. Dates use ISO 8601. `std:` resolution, tamper rejection and frozen-binary hash equality. - P2 closure gate: add a pure no-Foreign/no-Plugin lane for C01–C06/C11/C12 (20 fixtures + stdlib table), source shortcut scan and golden comparison. +- P5 HTTP framing/client: `stdlib/net/http.mq.md` parses status/headers/chunked + responses and builds requests; `http_over_tcp.rs` runs a loopback TCP HTTP + exchange through `resource.open("tcp")`. No host HTTP/TLS implementation. - CM-A08 GFM rendering: `stdlib/data/table.mq.md::转GFM` renders a logical Table as a GFM pipe table with cell escaping; CLI test added. - P5 HTTP parser fixture (C10): semantic fixture 71 parses HTTP/1.1 status, diff --git a/README.md b/README.md index 1b6e851..8954564 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,9 @@ executes anything. `.mq.md` print chain is the only execution path for `打印`/`print` on an official install (ADR-0031), covering all 15 value types; `HostOp::Print` and the legacy formatter are removed (ADR-0027/0028/0029/0030/0031). +* **HTTP/1.1 framing in `.mq.md`** — `stdlib/net/http.mq.md` status/header/ + chunked parsing and request construction; a loopback TCP test runs a real + exchange (ADR-0034; no TLS/host HTTP claim). * **Blocking TCP endpoint (RFC-CM P5 first slice)** — `resource.open("tcp", ":", ["read","write"])` with exact IP grants, project mode and 5-second timeouts; DNS/TLS/UDP/server sockets excluded (ADR-0034). diff --git a/crates/cli/tests/http_over_tcp.rs b/crates/cli/tests/http_over_tcp.rs new file mode 100644 index 0000000..92644b0 --- /dev/null +++ b/crates/cli/tests/http_over_tcp.rs @@ -0,0 +1,120 @@ +//! C10/P5: a `.mq.md` HTTP client over the TCP resource endpoint. + +use std::io::{Read, Write}; +use std::net::TcpListener; +use std::path::Path; +use std::process::Command; + +struct TempDir(std::path::PathBuf); + +impl TempDir { + fn new(tag: &str) -> Self { + let path = + std::env::temp_dir().join(format!("marqdo-http-tcp-{}-{tag}", std::process::id())); + let _ = std::fs::remove_dir_all(&path); + std::fs::create_dir_all(&path).unwrap(); + Self(path) + } + fn path(&self) -> &Path { + &self.0 + } +} + +impl Drop for TempDir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +#[test] +fn mqmd_http_client_parses_a_response_from_a_tcp_stream() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let mut request = [0u8; 512]; + let count = stream.read(&mut request).unwrap(); + assert!(String::from_utf8_lossy(&request[..count]).starts_with("GET / HTTP/1.1\r\n")); + stream + .write_all( + b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nhello", + ) + .unwrap(); + }); + + let dir = TempDir::new("client"); + let program = format!( + r#"--- +import: + http: "std:net/http" + 编码: "std:text" +--- + +## main + +**流 = resource.open("tcp", "{address}", ["read", "write"])** + +**请求 = http.构建请求("GET", "/", [["Host", "example.test"]], "")** + +**请求内存 = memory.new(1, 1)** + +**请求长度 = 编码.utf8编码(请求, 请求内存, 0)** + +**写入 = resource.write(流, 请求内存, 0, 请求长度)** + +**接收 = memory.new(1, 1)** + +**读出 = resource.read(流, 接收, 0, 4096)** + +**数量 = 读出.value.count** + +**响应文本 = ""** + +**下标 = 0** + +- `下标 < 数量` + + **响应文本 = 响应文本 + text.from_codepoint(接收[下标])** + + **下标 = 下标 + 1** + +**关闭 = resource.close(流)** + +**响应 = http.解析响应(响应文本)** + +*[写入.ok, 读出.ok, 响应.状态, 响应.头["Content-Type"], 响应.体, 关闭.ok]* +"# + ); + std::fs::write(dir.path().join("index.mq.md"), program).unwrap(); + let output = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["run", "index.mq.md", "--allow-net", "127.0.0.1", "--json"]) + .current_dir(dir.path()) + .env( + "MARQDO_STDLIB_DIR", + Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap() + .join("stdlib"), + ) + .output() + .expect("run marqdo"); + assert_eq!( + output.status.code(), + Some(0), + "stderr={} stdout={}", + String::from_utf8_lossy(&output.stderr), + String::from_utf8_lossy(&output.stdout) + ); + let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json"); + assert_eq!(report["success"], true); + let items = report["value"]["items"].as_array().expect("list"); + assert_eq!(items[0]["value"], true); + assert_eq!(items[1]["value"], true); + assert_eq!(items[2]["decimal"], "200"); + assert_eq!(items[3]["value"], "text/plain"); + assert_eq!(items[4]["value"], "hello"); + assert_eq!(items[5]["value"], true); + server.join().unwrap(); +} diff --git a/crates/cli/tests/stdlib_http.rs b/crates/cli/tests/stdlib_http.rs new file mode 100644 index 0000000..a591e90 --- /dev/null +++ b/crates/cli/tests/stdlib_http.rs @@ -0,0 +1,40 @@ +//! CM-A08/P5 stdlib HTTP framing module tests. + +use std::path::Path; +use std::process::Command; + +fn repo_root() -> std::path::PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap() + .to_path_buf() +} + +#[test] +fn stdlib_http_parses_chunked_responses_and_builds_requests() { + let output = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["run", "tests/http.mq.md", "--json"]) + .current_dir(repo_root().join("stdlib")) + .env("MARQDO_STDLIB_DIR", repo_root().join("stdlib")) + .output() + .expect("run http self-test"); + assert_eq!( + output.status.code(), + Some(0), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json"); + assert_eq!(report["success"], true); + let items = report["value"]["items"].as_array().expect("list"); + assert_eq!(items[0]["decimal"], "200"); + assert_eq!(items[1]["value"], "OK"); + assert_eq!(items[2]["value"], "text/plain"); + assert_eq!(items[3]["value"], "hello world"); + assert_eq!( + items[4]["value"], + "POST /echo HTTP/1.1\r\nHost: example.test\r\nContent-Length: 2\r\n\r\nok" + ); +} diff --git a/docs/STATUS.md b/docs/STATUS.md index 961f0c5..5af9d96 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -136,6 +136,10 @@ This is still a **local VM lane**, not a self-hosted GitHub Actions security URL PluginHandle `plugin/export` are read-only observation members (host id private); `stdlib/format.mq.md` reproduces builtin `打印`/`repr` for all 15 value types, including Formula/Code/FunctionRef and Code UTF-8 byte length. +- HTTP framing and client (P5/C10): `stdlib/net/http.mq.md` parses status, + headers and `Transfer-Encoding: chunked`, builds requests, and an end-to-end + test parses a loopback TCP response; fixture 71 pins the same logic. No host + HTTP/TLS implementation. - Blocking TCP endpoint (P5 first slice, ADR-0034): project-mode `resource.open("tcp", ip:port, ["read","write"])` with exact IP grant and timeouts; no DNS/TLS/UDP/server sockets; strict/pure refuse. CLI `--allow-net`. @@ -200,7 +204,7 @@ behavior. This table records only what is in this tree now. | P2 Marqdo-written allocator/data structures + minimal structure observation | **PASS (local pure lane); external stdlib repo publication pending** | `.mq.md`-only evidence: bump allocator with u32 codec, linked list, failed-allocation atomicity (48–50); CM-A04 structure primitives and a table filter (51–54, ADR-0023); a first-fit free-list allocator with block reuse, splitting, address-adjacent coalescing and `Result` maps (55, ADR-0024), a `Memory`-backed binary tree with recursive insert/in-order traversal (56), a `.mq.md` CSV row splitter (57), an open-addressing hash table with linear probing, update and `hash.full` (58), a `.mq.md` UTF-8 encoder over `Memory` (59), a validating UTF-8 decoder rejecting truncated, bad-continuation and surrogate/overlong input (60), a `Memory`-backed graph adjacency list (61), a recursive JSON parser for nested arrays/objects, strings with escapes, integers, booleans, null and error Results (62), a `Memory`-backed stack bytecode VM with PUSH/ADD/MUL/JMP/JZ/HALT (63), big-endian 16/32/64-bit plus LEB128 integer codecs (64), a `.mq.md` table sort rebuilding a new table from `table.row/columns/cell` (65), a table join (67), a quoted/escaped CSV row parser (68), table group (69), and an Int matrix multiply (70); the first-fit allocator also detects double free (`alloc.double_free`). Evidence: [`memory-p2-closure`](evidence/2026-11-20-memory-p2-closure.md), [`structure-api-p2`](evidence/2026-11-20-structure-api-p2.md), [`result-and-free-list-p2`](evidence/2026-11-20-result-and-free-list-p2.md). Local gate PASS: `crates/cli/tests/p2_closure.rs` runs 20 C01–C06/C11/C12 fixtures plus the stdlib table self-test in `--mode pure` (no foreign/plugin/import grants), scans sources for foreign/plugin/import shortcuts and compares value/stdout to goldens; evidence [`p2-closure-gate`](evidence/2026-11-20-p2-closure-gate.md). The bundled `stdlib/` tree, `std:` imports, digest verification, Analysis identity and frozen-CLI closure test are implemented (ADR-0028). Remaining outside C01–C12: publication as a separate external stdlib repository, and no remote CI URL. | | P3 Resource/Stream ABI | **PARTIAL** | ADR-0025/0026 CM-A06 slices: opaque `Resource`, `resource.open/read/write/close`, stdout write in pure/project, granted project-file read and buffered project-file write in project, EOF/closed/handle errors, wire/host rejection; strict permits only stdio/stdout and refuses file/network resources. Fixture 66 + CLI/host tests. CM-A07 **Compat-C** is implemented: when a program calls `打印/print`, the loader injects the bundled `stdlib/io.mq.md` and the runtime routes to `io.写值` (all 15 value types, UTF-8 via `Memory`, `resource.write(stdout)`); `HostOp::Print` and the legacy `Value::display()` print path are removed, and a missing bundle fails with `kernel.unavailable`. Strict permits only the existing stdio console channel. Still missing: directories, network, generic provider registry, Table storage, strict file/network resources. | | P4 Table logical/physical migration | **PARTIAL** | ADR-0032/CM-A08: `Value::Table` logical semantics unchanged (T0 `Vec>`); `stdlib/data/table.mq.md` provides select/filter/sort/append/set/join/group/`转GFM` as value-returning `.mq.md` functions with input immutability. GFM rendering is covered; no T1/T2/T3 physical storage, column store, cross-storage implementation or Memory-backed table. | -| P5 third-party Provider / TCP / HTTP | **PARTIAL** | semantic fixture 71 implements HTTP/1.1 response parsing (status/headers/chunked) and request construction in `.mq.md`; no host HTTP/TLS. ADR-0034 blocking TCP resource endpoint (`resource.open("tcp", ":", ["read","write"])`, exact IP grant, project-only, 5s timeouts; loopback host+CLI tests). ADR-0033 in-process `ResourceProvider` v1: explicitly trusted providers register by kind, project-mode only, gated by `Capabilities.allow_resource_provider`, using the existing `resource.*`/`Result` ABI; a mock read/write provider is tested. No C/Worker provider transport, host DNS/TLS/HTTP implementation, UDP, clock/random/process/device providers, or provider package installation. | +| P5 third-party Provider / TCP / HTTP | **PARTIAL** | `stdlib/net/http.mq.md` + semantic fixture 71 implement HTTP/1.1 status/header/chunked parsing and request construction in `.mq.md`; `http_over_tcp.rs` runs a local HTTP exchange over `resource.open("tcp",...)`; no host HTTP/TLS and no external network test. ADR-0034 blocking TCP resource endpoint (`resource.open("tcp", ":", ["read","write"])`, exact IP grant, project-only, 5s timeouts; loopback host+CLI tests). ADR-0033 in-process `ResourceProvider` v1: explicitly trusted providers register by kind, project-mode only, gated by `Capabilities.allow_resource_provider`, using the existing `resource.*`/`Result` ABI; a mock read/write provider is tested. No C/Worker provider transport, host DNS/TLS/HTTP implementation, UDP, clock/random/process/device providers, or provider package installation. | | P6 performance / native backends | **NOT IMPLEMENTED** | no bytecode/JIT/GPU/QPU work | The P1 memory domain is pure computation: it needs no grant and is available in @@ -244,6 +248,9 @@ user-written allocator is explicitly **not** claimed. - [`evidence/2026-11-20-tcp-endpoint.md`](evidence/2026-11-20-tcp-endpoint.md) — ADR-0034 blocking TCP endpoint, IP grants, DNS refusal, loopback host/CLI echo tests. +- [`evidence/2026-11-20-http-over-tcp.md`](evidence/2026-11-20-http-over-tcp.md) + — P5/C10 `.mq.md` HTTP framing over a loopback TCP stream, stdlib module and + end-to-end client test. - [`evidence/2026-11-20-p2-closure-gate.md`](evidence/2026-11-20-p2-closure-gate.md) — P2 C01–C06/C11/C12 pure no-Foreign/no-Plugin closure lane and golden comparison. diff --git a/docs/adr/0034-tcp-resource-endpoint.md b/docs/adr/0034-tcp-resource-endpoint.md index 931626c..ea469de 100644 --- a/docs/adr/0034-tcp-resource-endpoint.md +++ b/docs/adr/0034-tcp-resource-endpoint.md @@ -68,7 +68,10 @@ an external effect and is not replayed as pure computation. ## Consequences * `.mq.md` can implement a line protocol/HTTP client parser on top of the TCP - byte stream using the existing Memory/Text/Result primitives. + byte stream using the existing Memory/Text/Result primitives. The bundled + `stdlib/net/http.mq.md` provides HTTP/1.1 response parsing (status, headers, + chunked transfer) and request construction; `http_over_tcp.rs` runs a local + HTTP exchange through `resource.open("tcp", ...)` and the module. * The runtime still has no `net.request` builtin or new `HostOp`; a CLI test opens a local echo server, writes/reads `ping` through `resource.*`, and closes. diff --git a/docs/evidence/2026-11-20-http-over-tcp.md b/docs/evidence/2026-11-20-http-over-tcp.md new file mode 100644 index 0000000..b87038b --- /dev/null +++ b/docs/evidence/2026-11-20-http-over-tcp.md @@ -0,0 +1,52 @@ +# RFC-CM P5/C10 evidence: HTTP/1.1 over the TCP resource endpoint + +Date: 2026-11-20 (Asia/Shanghai) +Repository: `marqdo-cli` +Baseline: `0d4dc1d` plus this step's changes. +Related: [ADR-0034](../adr/0034-tcp-resource-endpoint.md), +[HTTP parser fixture](2026-11-20-http-parser.md), [STATUS §5](../STATUS.md). +Scope: `.mq.md` HTTP framing over a loopback TCP stream; no host HTTP/TLS. + +## Module + +`stdlib/net/http.mq.md` (import `std:net/http`) provides: + +```text +解析响应(文本) -> {状态, 原因, 头, 体} +解码分块(体) -> Text +构建请求(方法, 路径, 头列表, 体) -> Text +切片/找子串/找字符/十六进制值 +``` + +It uses only `text.codepoints`/`text.from_codepoint` and List/Map/Text values; +there is no `net.request` or host HTTP parser. + +## End-to-end test + +`crates/cli/tests/http_over_tcp.rs`: + +1. starts a loopback `TcpListener` that reads the request and sends + `HTTP/1.1 200 OK ... Content-Length: 5 ... hello`; +2. runs a generated `.mq.md` program with `--allow-net 127.0.0.1`; +3. the program builds a GET request, UTF-8-encodes it through `std:text` into + `Memory`, writes it to `resource.open("tcp", addr, ["read","write"])`, + reads the response bytes, decodes them to Text with + `text.from_codepoint`, parses with `stdlib/net/http`, and returns + `[write.ok, read.ok, status, content-type, body, close.ok]`; +4. asserts `[true, true, 200, "text/plain", "hello", true]`. + +`stdlib/tests/http.mq.md` independently pins the parser/builder without a +socket. Semantic fixture 71 pins the same logic in the conformance corpus. + +## Gate + +Local container: `cargo test -p marqdo-cli --test http_over_tcp` PASS and +`cargo test -p marqdo-cli --test stdlib_http` PASS; full workspace gate PASS. +No external network access is required. + +## Not done + +* HTTPS/TLS; no TLS security claim. +* External DNS, redirects, connection reuse, compression, cookies, HTTP/2. +* A host HTTP client/server; this is `.mq.md` framing over the byte stream. +* Strict-mode TCP (strict refuses TCP; project mode only). diff --git a/stdlib/README.md b/stdlib/README.md index 381e18c..6c264e7 100644 --- a/stdlib/README.md +++ b/stdlib/README.md @@ -8,7 +8,8 @@ module; a mismatch is a hard error. - `text.mq.md` — UTF-8 encoding into `Memory` - `io.mq.md` — `写文本` / `写值` through `resource.write(stdout)` - `demo.mq.md` — side-by-side comparison with builtin `打印` -- `data/table.mq.md` — value-returning table select/filter/sort/append/set/join/group +- `data/table.mq.md` — value-returning table select/filter/sort/append/set/join/group/GFM +- `net/http.mq.md` — HTTP/1.1 response parsing (headers/chunked) and request builder - `tests/` — bundle self-tests The digest is a SHA-256 over sorted `*.mq.md` relative paths, a NUL, file diff --git a/stdlib/net/http.mq.md b/stdlib/net/http.mq.md new file mode 100644 index 0000000..8d36a1f --- /dev/null +++ b/stdlib/net/http.mq.md @@ -0,0 +1,239 @@ +## 切片 + +`(点, 起, 止)` + +**结果 = ""** + +**下标 = 起** + +- `下标 < 止` + + **结果 = 结果 + text.from_codepoint(点[下标])** + + **下标 = 下标 + 1** + +*结果* + +## 找子串 + +`(点, 子点, 起点)` + +**结果 = 0 - 1** + +**下标 = 起点** + +- `下标 + 长度(子点) <= 长度(点) and 结果 < 0` + + **偏移 = 0** + + **匹配 = true** + + - `偏移 < 长度(子点) and 匹配` + + 1. `点[下标 + 偏移] != 子点[偏移]` + + **匹配 = false** + + 2. `true` + + **忽略 = 0** + + **偏移 = 偏移 + 1** + + 1. `匹配` + + **结果 = 下标** + + 2. `true` + + **忽略 = 0** + + **下标 = 下标 + 1** + +*结果* + +## 找字符 + +`(点, 码点, 起点)` + +**结果 = 0 - 1** + +**下标 = 起点** + +- `下标 < 长度(点) and 结果 < 0` + + 1. `点[下标] == 码点` + + **结果 = 下标** + + 2. `true` + + **忽略 = 0** + + **下标 = 下标 + 1** + +*结果* + +## 十六进制值 + +`(点)` + +**数值 = 0** + +**下标 = 0** + +- `下标 < 长度(点)` + + **码点 = 点[下标]** + + 1. `码点 >= 48 and 码点 <= 57` + + **数值 = 数值 * 16 + 码点 - 48** + + 2. `码点 >= 65 and 码点 <= 70` + + **数值 = 数值 * 16 + 码点 - 55** + + 3. `码点 >= 97 and 码点 <= 102` + + **数值 = 数值 * 16 + 码点 - 87** + + 4. `true` + + **忽略 = 0** + + **下标 = 下标 + 1** + +*数值* + +## 解码分块 + +`(体)` + +**点 = text.codepoints(体)** + +**结果 = ""** + +**位置 = 0** + +- `位置 < 长度(点)` + + **行尾 = 找子串(点, text.codepoints("\r\n"), 位置)** + + - `行尾 < 0` + + *""* + + **大小文本 = 切片(点, 位置, 行尾)** + + **大小 = 十六进制值(text.codepoints(大小文本))** + + 1. `大小 == 0` + + **位置 = 长度(点)** + + 2. `true` + + **结果 = 结果 + 切片(点, 行尾 + 2, 行尾 + 2 + 大小)** + + **位置 = 行尾 + 2 + 大小 + 2** + +*结果* + +## 解析响应 + +`(文本)` + +**点 = text.codepoints(文本)** + +**分隔 = text.codepoints("\r\n\r\n")** + +**头部结束 = 找子串(点, 分隔, 0)** + +**头部 = 切片(点, 0, 头部结束)** + +**体 = 切片(点, 头部结束 + 4, 长度(点))** + +**头部点 = text.codepoints(头部)** + +**行列表 = []** + +**行起 = 0** + +**下一行 = 找子串(头部点, text.codepoints("\r\n"), 0)** + +- `下一行 >= 0` + + **行列表 = list.append(行列表, 切片(头部点, 行起, 下一行))** + + **行起 = 下一行 + 2** + + **下一行 = 找子串(头部点, text.codepoints("\r\n"), 行起)** + +**行列表 = list.append(行列表, 切片(头部点, 行起, 长度(头部点)))** + +**状态行 = 行列表[0]** + +**状态点 = text.codepoints(状态行)** + +**第一空格 = 找字符(状态点, 32, 0)** + +**第二空格 = 找字符(状态点, 32, 第一空格 + 1)** + +**状态 = 整数(切片(状态点, 第一空格 + 1, 第二空格))** + +**原因 = 切片(状态点, 第二空格 + 1, 长度(状态点))** + +**头 = {}** + +**行号 = 1** + +- `行号 < 长度(行列表)` + + **行点 = text.codepoints(行列表[行号])** + + **冒号 = 找字符(行点, 58, 0)** + + 1. `冒号 >= 0` + + **名 = 切片(行点, 0, 冒号)** + + **值 = 切片(行点, 冒号 + 2, 长度(行点))** + + **头 = map.set(头, 名, 值)** + + 2. `true` + + **忽略 = 0** + + **行号 = 行号 + 1** + +**传输 = map.get(头, "Transfer-Encoding")** + +**解码后 = 体** + +1. `传输 == "chunked"` + + **解码后 = 解码分块(体)** + +2. `true` + + **解码后 = 体** + +*{状态: 状态, 原因: 原因, 头: 头, 体: 解码后}* + +## 构建请求 + +`(方法, 路径, 头列表, 体)` + +**请求 = 方法 + " " + 路径 + " HTTP/1.1\r\n"** + +**行号 = 0** + +- `行号 < 长度(头列表)` + + **请求 = 请求 + 头列表[行号][0] + ": " + 头列表[行号][1] + "\r\n"** + + **行号 = 行号 + 1** + +*请求 + "\r\n" + 体* diff --git a/stdlib/stdlib.json b/stdlib/stdlib.json index d7553d5..1310f7c 100644 --- a/stdlib/stdlib.json +++ b/stdlib/stdlib.json @@ -1,5 +1,5 @@ { "name": "marqdo-stdlib", "version": "0.1.0", - "digest": "sha256:7a9c46fa2372017395413980f857b6863dc118c3f6c9a824abd03ddf3c59f3d4" + "digest": "sha256:832940f35fe5651eabe4ac672e12e2d8f2bf4e649cc430e455425b4f2a983121" } diff --git a/stdlib/tests/http.mq.md b/stdlib/tests/http.mq.md new file mode 100644 index 0000000..525613f --- /dev/null +++ b/stdlib/tests/http.mq.md @@ -0,0 +1,14 @@ +--- +import: + http: "std:net/http" +--- + +## main + +**响应文本 = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n"** + +**响应 = http.解析响应(响应文本)** + +**请求 = http.构建请求("POST", "/echo", [["Host", "example.test"], ["Content-Length", "2"]], "ok")** + +*[响应.状态, 响应.原因, 响应.头["Content-Type"], 响应.体, 请求]*