Pure-MoonBit RFC 9113 HTTP/2 and RFC 7541 HPACK Sans-I/O protocol engine, native async h2c server, SVG wire timeline visualizer, and browser frame studio
| External Oracle | Scope | Result |
|---|---|---|
| summerwind/h2spec v2.6.0 (-S strict mode) | All RFC 9113 framing, stream states, flow control, HPACK, HTTP semantics | 147 / 147 PASS (0 skipped, 0 failed) |
| http2jp/hpack-test-case (cross-impl decode) | 14 implementations (nghttp2, go-hpack, haskell-http2-*, node-http2, python-hpack, swift-nio) | 446 stories / 47,142 blocks / 548,382 headers — 100% exact |
| http2jp/hpack-test-case/raw-data (round-trip) | 32 stories / 3,384 blocks encoded with Huffman + dynamic table and decoded back | 1,162,372 B -> 361,250 B (-68.92%, 0 errors) |
| Node.js node:http2 (live TCP h2c) | 40 GET + 10 POST echo + 32 KB chunked response + PING | 51 / 51 concurrent streams PASS |
moon add Sam-Hui-dot/moon-http2@0.1.0let conn = @moon-http2.H2Connection::new_server()
let server_settings = conn.take_outbound()
let events = conn.feed(client_wire_bytes)
for ev in events {
match ev {
RequestComplete(stream_id~, ..) =>
conn.send_response(
stream_id,
200,
extra_headers=[@moon-http2.header_field("content-type", "text/plain")],
body=@moon-http2.ascii_to_bytes("hello from moon-http2"),
)
_ => ()
}
}
let reply_bytes = conn.take_outbound()
let svg = @moon-http2.render_timeline_svg(conn.frame_trace())# Run 4-target unit tests
moon test --target wasm --deny-warn .
moon test --target wasm-gc --deny-warn .
moon test --target js --deny-warn .
moon test --target native --deny-warn .
# Build native CLI and run live h2c server
moon build --target native --release --deny-warn cmd/main
./_build/native/release/build/cmd/main/main serve --port 8080
# Run all 3 external Oracles (http2jp/hpack-test-case + h2spec v2.6.0 + node:http2)
python demo/verify_h2.py
# Build zero-upload browser workbench
python demo/build_web.pypub(all) suberror H2Error {
ConnectionError(H2ErrorCode, String)
StreamError(UInt, H2ErrorCode, String)
CompressionError(String)
InvalidInput(String)
} derive(Eq, Debug)pub struct H2Connection {
preface_checked : Bool
recv_buf : Buffer
send_buf : Buffer
hpack_dec : HpackDecoder
hpack_enc : HpackEncoder
streams : Map[UInt, H2Stream]
last_remote_stream_id : UInt
local_max_frame_size : Int
remote_max_frame_size : Int
local_initial_window : Int
remote_initial_window : Int
local_max_concurrent_streams : Int
conn_send_window : Int
conn_recv_window : Int
conn_recv_consumed : Int
expect_continuation_stream : UInt?
continuation_end_stream : Bool
continuation_buf : Buffer
goaway_sent : Bool
closed : Bool
trace : Array[FrameTraceEntry]
}fn H2Connection::new_server(max_concurrent_streams? : Int, initial_window_size? : Int, max_frame_size? : Int) -> H2Connectionfn H2Connection::send_response(self : H2Connection, stream_id : UInt, status : Int, extra_headers? : Array[HeaderField], body? : Bytes) -> Unitpub(all) enum H2Event {
SettingsUpdated(Array[SettingParam])
RequestHeaders(stream_id~ : UInt, headers~ : Array[HeaderField], end_stream~ : Bool)
RequestData(stream_id~ : UInt, data~ : Bytes, end_stream~ : Bool)
RequestComplete(stream_id~ : UInt, headers~ : Array[HeaderField], body~ : Bytes)
StreamReset(stream_id~ : UInt, code~ : H2ErrorCode)
PingReceived(ack~ : Bool, opaque_data~ : Bytes)
GoAwayReceived(last_stream_id~ : UInt, code~ : H2ErrorCode, debug_data~ : Bytes)
WindowUpdated(stream_id~ : UInt, increment~ : UInt)
} derive(Eq, Debug)pub(all) enum H2Frame {
Data(stream_id~ : UInt, end_stream~ : Bool, data~ : Bytes, flow_len~ : Int)
Headers(stream_id~ : UInt, end_stream~ : Bool, end_headers~ : Bool, priority~ : PrioritySpec?, fragment~ : Bytes)
Priority(stream_id~ : UInt, spec~ : PrioritySpec)
RstStream(stream_id~ : UInt, code~ : H2ErrorCode)
Settings(ack~ : Bool, params~ : Array[SettingParam])
PushPromise(stream_id~ : UInt, promised_stream_id~ : UInt, end_headers~ : Bool, fragment~ : Bytes)
Ping(ack~ : Bool, opaque_data~ : Bytes)
GoAway(last_stream_id~ : UInt, code~ : H2ErrorCode, debug_data~ : Bytes)
WindowUpdate(stream_id~ : UInt, increment~ : UInt)
Continuation(stream_id~ : UInt, end_headers~ : Bool, fragment~ : Bytes)
Unknown(frame_type~ : Byte, flags~ : Byte, stream_id~ : UInt, payload~ : Bytes)
} derive(Eq, Debug)pub struct H2Stream {
id : UInt
state : StreamState
send_window : Int
recv_window : Int
recv_consumed : Int
expected_content_length : Int64?
received_data_bytes : Int64
headers_received : Bool
req_headers : Array[HeaderField]
req_body : Buffer
pending_body : Bytes
pending_offset : Int
pending_end_stream : Bool
}let CLIENT_PREFACE_STR : Stringlet SETTING_HEADER_TABLE_SIZE : UInt16fn bytes_to_hex(bs : Bytes) -> Stringfn error_message(error : Error) -> Stringfn huffman_encode(data : Bytes) -> Bytesfn huffman_encoded_len(data : Bytes) -> Intfn validate_request_headers(stream_id : UInt, headers : Array[HeaderField], is_trailer : Bool) -> Int64? raise H2ErrorInstall
Download zipPure-MoonBit RFC 9113 HTTP/2 and RFC 7541 HPACK Sans-I/O protocol engine, native async h2c server, SVG wire timeline visualizer, and browser frame studio