Skip to content

Phase 1:typed headers(Via / NameAddr / CSeq / Contact / Route / simple ints) - #7

Draft
lixuanqun wants to merge 2 commits into
cursor/sip-uri-parser-3c36from
cursor/typed-headers-3c36
Draft

Phase 1:typed headers(Via / NameAddr / CSeq / Contact / Route / simple ints)#7
lixuanqun wants to merge 2 commits into
cursor/sip-uri-parser-3c36from
cursor/typed-headers-3c36

Conversation

@lixuanqun

Copy link
Copy Markdown
Owner

概述

把所有事务/对话层会用到的核心 header 都做成 typed model + parser。RFC 4475 badinv01 现已纳入 gate

🔗 stacked on PR #6

实装

Typed value types(在 sip-message

类型 说明
HostPort host + 可选 port,含 IPv6
ViaHeader protocol/transport/sentBy/params + 便利访问 branch / received / rport / ttl / maddr
NameAddr display-name + URI + params + tag()
ContactValue wildcard *NameAddr,含 expires() / qValue()
CSeqHeader sequence (long, ≤ 2^31-1) + method

Typed parsers(在 sip-codec.typed

解析器 处理
ViaParser 严格按 ABNF 切 sent-protocol、IPv6 sentBy、空 param 报错(让 badinv01 落网)
NameAddrParser 4 种 display-name 形式、quoted-string 转义、RFC 4475 lwsdisp 兼容
CSeqParser 序列号范围 + 方法 ABNF
IntegerHeaderParser Max-Forwards [0,255] / Expires / Content-Length
CallIdValidator RFC 3261 §25 word ABNF 字符集
RouteSetParser Route / Record-Route 列表保序
ContactParser wildcard 与列表互斥

合规闸门扩展

新增 everyTypedRejectFixtureIsRejectedByATypedHeaderParser:把 parser.phase = typed-header 的 fixture 喂给 Via/Contact 解析器,至少一个必抛对应 category 的 SipCodecException

rfc4475/3.1.2.1-badinv01 现已纳入 gate(typed Via parser 检测 ;;,;,, 失败)。

Bug 修复

  • Map.copyOf 不保留插入顺序 — 改用 Collections.unmodifiableMap(new LinkedHashMap<>(...))。修了 ViaHeaderNameAddrSipUri。修复前 wire roundtrip 在某些 JVM 状态下会失败。

验证

mvn -B verify → 9 modules SUCCESS
  sip-message:  10 tests
  sip-codec:    69 tests
  compliance:   36 tests
  total:       115 tests

40 个新增 typed parser tests,覆盖:单元、roundtrip、RFC 4475 引用。

Open in Web Open in Cursor 

cursoragent and others added 2 commits May 11, 2026 15:29
新模块结构:
- sip-message/com.sip.message.header.typed/
  · HostPort(record):host + 可选 port,支持 IPv6 bracketed
  · ViaHeader(record):protocolName/Version/transport + sentBy + params
    含 branch / received / rport / ttl / maddr 便利访问器
- sip-codec/com.sip.codec.typed/
  · ViaParser:手写 ABNF 解析
    · 按 RFC 3261 §25 严格切分 sent-protocol(必须 3 段)
    · sentBy 支持 IPv6 reference 与 hostname:port
    · params 按 ';' 切分,对空参数(如 ;;)抛 malformed-header
    · top-level ',' 切分让单 Via 头携带多个 via-parm 与多 Via 头等价
    · parseAll(Headers) 一次性扫所有 Via 头

测试覆盖(ViaParserTest,10 个):
- 单个 Via 解析、IPv6 sent-by
- 单头多值(逗号分隔)、多头汇总
- received / rport 参数
- 无值 rport
- RFC 4475 badinv01 的 Via 形式(;;,;,,)必拒,category=malformed-header
- 缺 transport / 缺 sent-by 拒绝
- asWire roundtrip

模块导出 sip-message 新增 header.typed 包,sip-codec 新增 typed 包。

Co-authored-by: li xuanqun <793005378@qq.com>
新增 typed 模型(在 sip-message):
- NameAddr:display-name + Uri + params,支持 tag() 等便利访问
- ContactValue:wildcard '*' 或 NameAddr,支持 expires() / qValue()
- CSeqHeader:sequence(long) + method,含 RFC §8.1.1.5 范围校验

新增 typed parser(在 sip-codec.typed):
- NameAddrParser:
  · 裸 addr-spec / 角括号 addr-spec / token display-name / quoted display-name
  · 兼容 RFC 4475 §3.1.1.6 lwsdisp(display-name 与 < 之间无 LWS)
  · 拒绝未闭合 quoted-string、未闭合角括号、badinv01 风格的空 params
- CSeqParser:number SP method,校验范围 [0, 2^31-1]
- IntegerHeaderParser:Max-Forwards [0,255]、Expires [0, MAX_INT]、Content-Length
- CallIdValidator:按 RFC 3261 §25 word ABNF 校验字符集
- RouteSetParser:Route / Record-Route 列表(保序)
- ContactParser:单 '*' 或 name-addr 列表,禁止 '*' 与其它值混存

ParserConformanceTest 增加 typed-header phase 维度
- everyTypedRejectFixtureIsRejectedByATypedHeaderParser:
  对 phase=typed-header 的 fixture 跑 Via/Contact 解析,至少一个必抛
- RFC 4475 badinv01 现已纳入 gate(typed Via parser 检测 ;;,;,, 失败)

bug 修复:ViaHeader/NameAddr/SipUri params 改用 Collections.unmodifiableMap +
LinkedHashMap,保证保序(之前用 Map.copyOf 可能丢失插入顺序导致 wire roundtrip 不稳定)。

mvn verify:9 模块 SUCCESS。Tests:
- sip-message: 10
- sip-codec:   69 (含 typed parsers: 10 Via + 12 NameAddr + 7 CSeq + 7 简单头 + 4 Contact)
- compliance:  21 self-test + 6 accept + 6 roundtrip + 2 structural + 1 typed = 36

Co-authored-by: li xuanqun <793005378@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants