跳转至

http.message

HTTP/1.1 报文构造与分框编解码(P4-A 协议半层) 形态(与 http/parse.ct 头注同源,皆发射面已证形态): ① 零 use 纯模块(不 import std.net —— 加载器菱形 use 误报 E5020 规避 + 解释器可测;不 import http.parse —— 同理:消费方双 use 即菱形。 故 CL 分框助手收标量槽,不收 HttpHead)。IO 粘合(经 net 门面写道) 归框架波次,net_write_str 原语一行即接。名字面前缀 msg_ / chunk_, 与 parse.ct 的 http_* 在 use 合并后的单命名空间并存不撞(E5030 守卫)。 ② 构造面 = Str 拼接头块(T2 已收账的 + 语义);分框面 = &I64[] 字节道 零拷贝进出;无闭包、无 Drop 局部、无方法调用、无 trait —— while 体 仅标量局部((f)/(g) 纪律)。 ③ chunked 解码 = 可重入状态机(RFC 9112 §7.1):chunk_step 以 (src, n, pos, st) 进、ChunkRet 出,状态显式随值返还 —— 分片到达 (一次读不全)即多次调用推进。重入契约(解码侧统一口径): - 调用方恒 pos += ret.consumed,并以 ret 的 phase/remain 为新状态; - rc = 0(需要更多输入):consumed = 未竟行的【行起点】距 pos 的 位移 —— 已落袋的 data/帧推进照计(consumed 相应含之),未竟行 整行回卷重扫(无半行状态;dst 重写同区间,幂等); - rc = 2(dst 满):consumed 记真实推进,排空 dst 后以新 pos/状态 重调; - rc = -1 协议错(err 槽记码),consumed/produced 记出错前推进量。 扩展(;后至 CR)跳过;trailer 段整段消费(字段不解析,行长受 cap); last-chunk(0) 后空行终止 phase = done(4)。 ④ 100-continue 钩子(RFC 9110 §10.1.1): 服务端 = http_is_expect_100(parse.ct)检出 + msg_continue() 常量头 (先发 100 再读体);客户端 = msg_should_expect 阈值判 + msg_expect_header() 附加;等待岔路(发体前读 1xx 或终态)需读道超时, 归框架波次。 ⑤ Content-Length 分框:体即 head_end 后 clen 字节;msg_body_remaining 给剩余量(调用方读道循环用;上限在 parse.ct 的 max_body 已裁)。

pub fn

Signature Returns Description
msg_hex(v: I64) Str 小写十六进制(chunk-size 线格式;v < 0 → "")
msg_request_line(method: Str, target: Str, ver: I64) Str 请求行:"METHOD target HTTP/1.x\r\n"(ver 0 = 1.0,余 = 1.1)
msg_status_line(ver: I64, status: I64, reason: Str) Str 状态行:"HTTP/1.x <3位码> <reason>\r\n"(reason 空 → 码后直 CRLF)
msg_header(name: Str, value: Str) Str 头行:"Name: value\r\n"(name 原样透传 —— token 校验归调用方/框架)
msg_head_end() Str 头块终止哨兵(空行 CRLF)
msg_cl_header(n: I64) Str Content-Length 头行
msg_request_head(method: Str, target: Str, host: Str, ver: I64, body_len: I64) Str 最小客户端请求头块(Host + Content-Length;体字节由调用方续写)
msg_response_head(ver: I64, status: I64, reason: Str, body_len: I64) Str 最小服务端响应头块(状态行 + Content-Length)
msg_chunk_header(size: I64) Str chunk 帧头(无扩展)
msg_chunk_data_end() Str chunk 数据后缀 CRLF
msg_last_chunk() Str 终止块(无 trailer 档;"0\r\n\r\n")
msg_last_chunk_trailer(name: Str, value: Str) Str 终止块带单条 trailer(trailer 逐条声明 —— 逐跳头禁入 trailer,校验归框架)
msg_continue() Str 服务端:检出 Expect: 100-continue 后先行发回的 1xx 头块
msg_expect_header() Str 客户端:Expect 头行(体长 ≥ 阈值才附 —— msg_should_expect 裁量)
msg_should_expect(body_len: I64, threshold: I64) I64 客户端阈值裁量:body_len ≥ threshold → 1(附 Expect,等 100/终态再发体)
msg_body_remaining(head_end: I64, clen: I64, fed: I64) I64 CL 体剩余量:头块 head_end、体长 clen、已喂 fed 字节(含头)→ 仍欠多少
(fed 过头 → 0;非 CL 分框(clen < 0)恒 0)
chunk_st_new() ChunkSt —
st_phase(st: ChunkSt) I64 公开 getter((h) 缺口绕行:成员访问留在本模块,消费方经 getter 取)
st_remain(st: ChunkSt) I64 —
st_err(st: ChunkSt) I64 —
cr_rc(r: ChunkRet) I64 —
cr_err(r: ChunkRet) I64 —
cr_consumed(r: ChunkRet) I64 —
cr_produced(r: ChunkRet) I64 —
cr_phase(r: ChunkRet) I64 —
cr_remain(r: ChunkRet) I64 —
chunk_step(src: &[I64], n: I64, pos: I64, st: ChunkSt, dst: &[I64], dfill: I64, dcap: I64) ChunkRet 解码一步:src[pos..n) → dst[dfill..dcap)(dfill = dst 已存量,跨调用由
调用方累进;写位 = dfill + 本次 produced)。size 行回卷重扫(无半行状态),
行界:size 行 2112 字节、trailer 行 4096 字节;chunk 计数 16 hex 上限、
乘前 2^59-1 界门(防 I64 溢出 panic/回绕——FFFFFFFFFFFFFFFE 远程 DoS
收口,2026-09-21 终审)+ CR 处 1e18 界门(负值防御冗余档)。
回卷提交界 = 未竟行起点(③ 契约)。