Files
marqdo-ext-http/index.mq.md
cflmy d0d654179e
ci/woodpecker/push/ci Pipeline was successful
ci/woodpecker/pr/ci Pipeline was successful
fix: allocate HTTP byte buffers by actual size and build headers once
Fix marqdo-ext-http#13: `http.文本字节` allocated a fixed 8 MiB Memory region on
every call, and `http.构建请求字节` called it once for the request line and once
per header, so a 6-header request spent ~56 MiB of the default 64 MiB run
budget and a second request failed with `resource.exhausted`.

- `文本字节`: request the minimal page count for the actual text (4-byte
  UTF-8 worst case per codepoint, 128-page cap, 1 page minimum).
- `构建请求字节`: concatenate the request line and all headers into one text,
  encode once, then concatenate the body; per-header fixed allocations are gone.
- Public signatures and produced request bytes are unchanged (`构建请求` text
  API stays compatible).
- Regression case: under the default budget, build the same 6-header request 8
  times and assert the final byte length/request text; the old code failed on
  the second build.
- docs/evidence records the real CLI/Bundle run.

No CLI/Host change, no new Host request, no release; real loopback multi-request
exchange (#3) and the optional streaming response view remain open/UNVERIFIED.
2026-11-30 12:22:44 +08:00

7.3 KiB

import
import
utf8
std:text/utf8

@exec http

切片字节

params(字节, 起, 止)

结果 = []

下标 = 起

while 下标 < 止 and 下标 < 长度(字节)

结果 = list.append(结果, 字节[下标])

下标 = 下标 + 1

end return 结果

找子串

params(字节, 子, 起点)

结果 = 0 - 1

下标 = 起点

while 下标 + 长度(子) <= 长度(字节) and 结果 < 0

偏移 = 0

匹配 = true

while 偏移 < 长度(子) and 匹配

if 字节[下标 + 偏移] != 子[偏移]

匹配 = false

else if true

忽略 = 0

end end 偏移 = 偏移 + 1

end if 匹配

结果 = 下标

else if true

忽略 = 0

end end 下标 = 下标 + 1

end return 结果

找字符

params(字节, 值, 起点)

结果 = 0 - 1

下标 = 起点

while 下标 < 长度(字节) and 结果 < 0

if 字节[下标] == 值

结果 = 下标

else if true

忽略 = 0

end end 下标 = 下标 + 1

end return 结果

十六进制值

params(字节)

数值 = 0

下标 = 0

while 下标 < 长度(字节)

值 = 字节[下标]

if 值 >= 48 and 值 <= 57

数值 = 数值 * 16 + 值 - 48

else if 值 >= 65 and 值 <= 70

数值 = 数值 * 16 + 值 - 55

else if 值 >= 97 and 值 <= 102

数值 = 数值 * 16 + 值 - 87

else if true

忽略 = 0

end end end end 下标 = 下标 + 1

end return 数值

文本字节

params(文本)

把 UTF-8 文本编码为字节列表:按实际文本长度申请最小 Memory 页数(UTF-8 最多 4 字节/码点),上限 128 页(8 MiB)。不再每次固定申请 8 MiB,使同一 程序内的多次报文构造共享默认 64 MiB 预算。

点 = text.codepoints(文本)

上界 = 长度(点) * 4

页 = (上界 + 65535) // 65536

if 页 < 1 页 = 1 end if 页 > 128 页 = 128 end

内存 = memory.new(页, 页)

字节数 = utf8.编码(文本, 内存, 0)

结果 = []

下标 = 0

while 下标 < 字节数

结果 = list.append(结果, 内存[下标])

下标 = 下标 + 1

end return 结果

字节文本

params(字节)

把字节列表按 UTF-8 解码为文本,上限 8 MiB。

长度值 = 长度(字节)

if 长度值 == 0

return {ok: true, value: ""}

end 页 = (长度值 + 65535) // 65536

内存 = memory.new(页, 页)

下标 = 0

while 下标 < 长度值

内存[下标] = 字节[下标]

下标 = 下标 + 1

end return utf8.解码(内存, 0, 长度值)

解码分块字节

params(体)

结果 = []

位置 = 0

错误 = ""

while 位置 < 长度(体) and 错误 == ""

行尾 = 找子串(体, [13, 10], 位置)

if 行尾 < 0

错误 = "http.chunked"

else if true

大小 = 十六进制值(切片字节(体, 位置, 行尾))

if 大小 == 0

位置 = 长度(体)

else if 位置 + 大小 + 4 > 长度(体)

错误 = "http.chunked"

else if true

结果 = list.concat(结果, 切片字节(体, 行尾 + 2, 行尾 + 2 + 大小))

位置 = 行尾 + 2 + 大小 + 2

end end end end end end if 错误 != ""

return {ok: false, code: 错误, message: "分块长度无效"}

end return {ok: true, value: 结果}

解析响应字节

params(字节)

解析 HTTP/1.1 响应字节。头部按 ASCII 解析,正文保持原始字节;chunked 长度以字节计。成功返回 {ok:true, value:{status, reason, headers, body}}。

头部结束 = 找子串(字节, [13, 10, 13, 10], 0)

if 头部结束 < 0

return {ok: false, code: "http.invalid", message: "缺少头部结束"}

end 头部 = 切片字节(字节, 0, 头部结束)

体 = 切片字节(字节, 头部结束 + 4, 长度(字节))

行列表 = []

行起 = 0

下一行 = 找子串(头部, [13, 10], 0)

while 下一行 >= 0

行列表 = list.append(行列表, 切片字节(头部, 行起, 下一行))

行起 = 下一行 + 2

下一行 = 找子串(头部, [13, 10], 行起)

end 行列表 = list.append(行列表, 切片字节(头部, 行起, 长度(头部)))

if 长度(行列表) == 0

return {ok: false, code: "http.invalid", message: "缺少状态行"}

end 状态行 = 字节文本(行列表[0])

if not 状态行.ok

return {ok: false, code: "http.invalid", message: "状态行不是 ASCII"}

end 状态字节 = 行列表[0]

第一空格 = 找字符(状态字节, 32, 0)

第二空格 = 找字符(状态字节, 32, 第一空格 + 1)

if 第一空格 < 0 or 第二空格 < 0

return {ok: false, code: "http.invalid", message: "状态行格式无效"}

end 状态文本 = 字节文本(切片字节(状态字节, 第一空格 + 1, 第二空格))

if not 状态文本.ok

return {ok: false, code: "http.invalid", message: "状态码不是 ASCII"}

end 状态 = 整数(状态文本.value)

原因 = 字节文本(切片字节(状态字节, 第二空格 + 1, 长度(状态字节)))

if not 原因.ok

return {ok: false, code: "http.invalid", message: "原因短语不是 ASCII"}

end 头 = {}

行号 = 1

while 行号 < 长度(行列表)

行 = 行列表[行号]

冒号 = 找字符(行, 58, 0)

if 冒号 >= 0

名 = 字节文本(切片字节(行, 0, 冒号))

值 = 字节文本(切片字节(行, 冒号 + 2, 长度(行)))

if 名.ok and 值.ok

头 = map.set(头, 名.value, 值.value)

else if true

忽略 = 0

end end else if true

忽略 = 0

end end 行号 = 行号 + 1

end 传输 = map.get(头, "Transfer-Encoding")

解码后 = 体

if 传输 == "chunked"

分块 = 解码分块字节(体)

if not 分块.ok

return 分块

end 解码后 = 分块.value

else if true

忽略 = 0

end end return {ok: true, value: {status: 状态, reason: 原因.value, headers: 头, body: 解码后}}

解析响应

params(文本)

兼容文本 API:按字节解析后,把正文按 UTF-8 解码。

响应 = 解析响应字节(文本字节(文本))

if not 响应.ok

return {状态: 0, 原因: "", 头: {}, 体: ""}

end 正文 = 字节文本(响应.value.body)

if not 正文.ok

return {状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: ""}

end return {状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: 正文.value}

构建请求字节

params(方法, 路径, 头列表, 体字节)

构建 HTTP/1.1 请求字节。方法、路径、头名/值按 UTF-8 编码;正文使用 传入的字节列表。头部先拼成一个文本再编码一次,避免每个头一次固定内存分配。

头部 = 方法 + " " + 路径 + " HTTP/1.1" + "\r\n"

下标 = 0

while 下标 < 长度(头列表)

头 = 头列表[下标]

头部 = 头部 + 文本(头[0]) + ": " + 文本(头[1]) + "\r\n"

下标 = 下标 + 1

end

头部字节 = 文本字节(头部 + "\r\n")

return list.concat(头部字节, 体字节)

构建请求

params(方法, 路径, 头列表, 体文本)

兼容文本 API,正文按 UTF-8 编码。

请求 = 构建请求字节(方法, 路径, 头列表, 文本字节(体文本))

文本结果 = 字节文本(请求)

if not 文本结果.ok

return ""

end return 文本结果.value