moonicap

    Bounded ICAP/1.0 codec, OPTIONS negotiation and Preview client transaction engine

    icap
    content-adaptation
    protocol
    proxy
    sans-io
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    3 hours ago
    Downloads
    2

    No dependencies

    #MoonICAP

    CI

    #安装与最小使用

    moon add TangShiJi/moonicap@0.1.0

    在使用方 moon.pkg 中导入 "TangShiJi/moonicap" @icap:

    let request = @icap.message(
    Request(Reqmod,"icap://scanner.example/scan"),
    headers=[("Host","scanner.example")],
    body_kind=RequestBody,
    body=b"file-content",
    )
    let client = @icap.new_session(request,preview=Some(4)).unwrap()
    let first = client.start().unwrap()
    // 宿主写出 first,并将服务端读取的 Bytes 传入 receive。
    let actions = client.receive(b"ICAP/1.0 100 Continue\r\n\r\n").unwrap()
    // actions 中的 Send(bytes) 必须由宿主写出;Unchanged/Adapted/Rejected 为终态。

    也可使用 exchange(request, read, write, preview=Some(4)),由读写回调接入宿主连接并执行完整交互。超时/读写错误通过 Result 传播;不得默认为扫描通过。

    MoonBit 原生 ICAP/1.0 协议编解码、OPTIONS 协商与 Preview 客户端交互引擎。面向上传扫描、HTTP 请求过滤和响应内容适配;接入符合 RFC 3507 的适配服务。它不提供杀毒引擎,也不会将服务器返回 204 直接解释为“文件安全”。

    核心库与传输分离:调用方负责 TCP/TLS、超时与重连,库负责字节边界、封装偏移、chunked body、Preview/100/204 状态和错误。通过 bytes API 可接入 MoonBit 原生服务器或 Wasm 宿主。

    #范围

    • OPTIONS、REQMOD、RESPMOD 请求/响应,绝对 ICAP URI 与 IPv6 authority。
    • Encapsulated 偏移、HTTP 请求/响应头、null/req/res/opt body、二进制正文、chunk extensions 与 trailers。
    • Preview: 0、提前结束 ieof、100 Continue 后仅发送未发送后缀、早期 204、适配结果与服务错误。
    • OPTIONS Methods/ISTag/Preview/TTL/Transfer-*、能力到期与传输策略选择。
    • 大小和数量限制、分片输入与多报文解码、错误后状态失效;HTTP hop-by-hop 清理。

    标准基准:RFC 3507。第三方扩展 ICAP 206 不作为 RFC 3507 能力承诺。网络、TLS、身份认证策略、杀毒判定及流式磁盘存储由宿主处理。解码器为有上限的整报文缓冲,不宣称恒定内存或无限文件吞吐。完整兼容边界和已验证范围将随验收记录列明。

    #开发与复现

    moon info moon fmt --check moon check --target all --deny-warn moon test --target all --deny-warn moon build --target native --release cmd/main moon run --target native cmd/main -- options icap://localhost:1344/echo

    CLI 使用 Base64 输入输出保留二进制字节,decode 返回状态、正文 Base64 和消费字节数;输入不足退出 3,非法报文退出 2。Python 验证宿主仅用于本地网络互操作,不进入核心运行依赖。

    moon run --target native examples/upload_scan moon run --target native examples/proxy_response moon run --target native examples/service_options

    24 个测试在四目标分别执行;另有 512 个确定性畸形输入、有效报文单字节变异、全部拆分位置及 c-icap 真实互操作。独立服务安装、日志和九场景报告见 CI 产物。测试数量不代替行为验收。

    兼容与资源边界、九项验收、性能基线、发布证据。安装成功必须由公开包和不检出源码的消费者共同验证。

    #原创与许可

    原创 MoonBit 实现,依据 RFC 3507 独立设计,没有移植其他实现。代码 Apache-2.0。c-icap 仅作独立互操作测试服务,不复制或链接其源代码。生态查重见 docs/ecosystem-audit.json;2026-12-02 检索 4188 个模块的名称、描述、关键词,未发现 ICAP 同题命中,结论限定于公开元数据检索。

    0.1.x 接收兼容修复,修改必须保留协议反例与独立服务验证;性能优化先保留语义,再比较同一基准。见 CONTRIBUTING.md、SECURITY.md 和 NOTICE。此项目使用独立目录和仓库,与本人的分页、CDDL、NATS 或韧性库方向不同。

    Action

    pub(all) enum Action {
    NeedInput
    Send(Bytes)
    Unchanged
    Adapted(Message)
    Rejected(Int, String)
    } derive(Eq,
    Debug
    )

    Action::equal

    fn Action::equal(Action, Action) -> Bool
    automatically derived

    Action::not_equal

    fn Action::not_equal(x : Action, y : Action) -> Bool
    automatically derived

    Action::to_repr

    automatically derived

    AdaptationHeaders

    pub(all) struct AdaptationHeaders {
    http : Bytes
    forwarded : Array[(String, String)]
    }

    BodyKind

    pub(all) enum BodyKind {
    NoBody
    RequestBody
    ResponseBody
    OptionsBody
    } derive(Eq,
    Debug
    )

    BodyKind::equal

    fn BodyKind::equal(BodyKind, BodyKind) -> Bool
    automatically derived

    BodyKind::not_equal

    fn BodyKind::not_equal(x : BodyKind, y : BodyKind) -> Bool
    automatically derived

    BodyKind::to_repr

    automatically derived

    Capabilities

    pub(all) struct Capabilities {
    methods : Array[Method]
    istag : String
    preview : Int?
    max_connections : Int?
    ttl : Int?
    allow_204 : Bool
    transfer_preview : Array[String]
    transfer_ignore : Array[String]
    transfer_complete : Array[String]
    }

    Capabilities::expired

    fn Capabilities::expired(self : Capabilities, fetched_at : Int64, now : Int64) -> Bool

    Capabilities::transfer_mode

    fn Capabilities::transfer_mode(self : Capabilities, extension : String) -> String

    ClientSession

    pub struct ClientSession {
    phase : Phase
    verb : Method
    original : Message
    preview_size : Int?
    allow_204 : Bool
    remaining : Bytes
    preview_finished : Bool
    decoder : Decoder
    }

    ClientSession::finish

    fn ClientSession::finish(self : ClientSession) -> Result[Unit, ProtocolError]

    ClientSession::phase

    fn ClientSession::phase(self : ClientSession) -> Phase

    ClientSession::receive

    fn ClientSession::receive(self : ClientSession, bytes : Bytes) -> Result[Array[Action], ProtocolError]

    ClientSession::start

    fn ClientSession::start(self : ClientSession) -> Result[Bytes, ProtocolError]

    Decoded

    pub(all) struct Decoded {
    message : Message
    consumed : Int
    } derive(Eq,
    Debug
    )

    Decoded::equal

    fn Decoded::equal(Decoded, Decoded) -> Bool
    automatically derived

    Decoded::not_equal

    fn Decoded::not_equal(x : Decoded, y : Decoded) -> Bool
    automatically derived

    Decoded::to_repr

    automatically derived

    Decoder

    pub struct Decoder {
    buffer : Bytes
    bounds : Limits
    failed : Bool
    }

    Decoder::finish

    fn Decoder::finish(self : Decoder) -> Result[Unit, ProtocolError]

    Decoder::pending_bytes

    fn Decoder::pending_bytes(self : Decoder) -> Int

    Decoder::push

    fn Decoder::push(self : Decoder, input : Bytes) -> Result[Array[Message], ProtocolError]

    Limits

    pub(all) struct Limits {
    max_head : Int
    max_body : Int
    max_headers : Int
    max_chunk : Int
    } derive(Eq,
    Debug
    )

    Limits::equal

    fn Limits::equal(Limits, Limits) -> Bool
    automatically derived

    Limits::not_equal

    fn Limits::not_equal(x : Limits, y : Limits) -> Bool
    automatically derived

    Limits::to_repr

    automatically derived

    Message

    pub(all) struct Message {
    start : StartLine
    headers : Array[(String, String)]
    request_headers : Bytes?
    response_headers : Bytes?
    body_kind : BodyKind
    body : Bytes
    ieof : Bool
    trailers : Array[(String, String)]
    } derive(Eq,
    Debug
    )

    Message::equal

    fn Message::equal(Message, Message) -> Bool
    automatically derived

    Message::not_equal

    fn Message::not_equal(x : Message, y : Message) -> Bool
    automatically derived

    Message::to_repr

    automatically derived

    Method

    pub(all) enum Method {
    Options
    Reqmod
    Respmod
    } derive(Eq,
    Debug
    )

    Method::equal

    fn Method::equal(Method, Method) -> Bool
    automatically derived

    Method::not_equal

    fn Method::not_equal(x : Method, y : Method) -> Bool
    automatically derived

    Method::to_repr

    automatically derived

    Phase

    pub(all) enum Phase {
    Ready
    PreviewWait
    FinalWait
    Done
    Broken
    } derive(Eq,
    Debug
    )

    Phase::equal

    fn Phase::equal(Phase, Phase) -> Bool
    automatically derived

    Phase::not_equal

    fn Phase::not_equal(x : Phase, y : Phase) -> Bool
    automatically derived

    Phase::to_repr

    automatically derived

    ProtocolError

    pub(all) enum ProtocolError {
    Incomplete
    Malformed(String, Int)
    LimitExceeded(String)
    } derive(Eq,
    Debug
    )

    ProtocolError::equal

    automatically derived

    ProtocolError::not_equal

    fn ProtocolError::not_equal(x : ProtocolError, y : ProtocolError) -> Bool
    automatically derived

    ProtocolError::to_repr

    automatically derived

    ServiceUri

    pub(all) struct ServiceUri {
    authority : String
    host : String
    port : Int
    resource : String
    } derive(Eq,
    Debug
    )

    ServiceUri::equal

    fn ServiceUri::equal(ServiceUri, ServiceUri) -> Bool
    automatically derived

    ServiceUri::not_equal

    fn ServiceUri::not_equal(x : ServiceUri, y : ServiceUri) -> Bool
    automatically derived

    ServiceUri::to_repr

    automatically derived

    StartLine

    pub(all) enum StartLine {
    Request(Method, String)
    Response(Int, String)
    } derive(Eq,
    Debug
    )

    StartLine::equal

    fn StartLine::equal(StartLine, StartLine) -> Bool
    automatically derived

    StartLine::not_equal

    fn StartLine::not_equal(x : StartLine, y : StartLine) -> Bool
    automatically derived

    StartLine::to_repr

    automatically derived

    capabilities

    fn capabilities(response : Message) -> Result[Capabilities, ProtocolError]

    decode

    fn decode(input : Bytes, bounds? : Limits) -> Result[Decoded, ProtocolError]

    encode

    fn encode(value : Message, bounds? : Limits, chunk_size? : Int) -> Result[Bytes, ProtocolError]

    encode_chunks

    fn encode_chunks(body : Bytes, chunk_size? : Int, ieof? : Bool, trailers? : Array[(String, String)]) -> Result[Bytes, ProtocolError]

    exchange

    fn exchange(request : Message, read : () -> Result[Bytes, ProtocolError], write : (Bytes) -> Result[Unit, ProtocolError], preview? : Int?, allow_204? : Bool, bounds? : Limits, max_reads? : Int) -> Result[Action, ProtocolError]

    fn header(headers : Array[(String, String)], name : String) -> String?

    limits

    fn limits(max_head? : Int, max_body? : Int, max_headers? : Int, max_chunk? : Int) -> Limits

    message

    fn message(start : StartLine, body_kind? : BodyKind, body? : Bytes, headers? : Array[(String, String)], request_headers? : Bytes?, response_headers? : Bytes?, ieof? : Bool, trailers? : Array[(String, String)]) -> Message

    method_name

    fn method_name(verb : Method) -> String

    new_decoder

    fn new_decoder(bounds? : Limits) -> Decoder

    new_session

    fn new_session(request : Message, preview? : Int?, allow_204? : Bool, bounds? : Limits) -> Result[ClientSession, ProtocolError]

    parse_uri

    fn parse_uri(uri : String) -> Result[ServiceUri, ProtocolError]

    prepare_http

    fn prepare_http(start_line : String, fields : Array[(String, String)]) -> Result[AdaptationHeaders, ProtocolError]

    validate_message

    fn validate_message(value : Message) -> Result[Unit, ProtocolError]