veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

net/http2

lib/net/http2.vk

lib/net/http2.vk Pure-Veko RFC 9113 HTTP/2 multiplexed client & server connection layer over io/Stream and codec/http2.

Message struct
signature
Message = struct { stream_id i64, status i64 = ..., method str = ..., path str = ..., headers map[str] = ..., body bytes, has_stream bool = ..., stream any = ... }

HTTP/2 request/response message exchanged over an H2Conn stream.

signature
H2ServerOpts = struct { max_requests i64 = ..., max_concurrent_streams i64 = ..., max_frame_size i64 = ..., max_header_bytes i64 = ..., max_continuation_cnt i64 = ..., max_hpack_table_size i64 = ..., max_body_bytes i64 = ..., max_rst_budget i64 = ..., max_ctrl_budget i64 = ... }

Server limits & DoS/flood mitigations for serve-conn-opts.

H2Conn struct
signature
H2Conn = struct { stream io/Stream, next_stream_id i64 = ..., is_server bool = ... }

Active HTTP/2 connection wrapping an io/Stream (plain TCP, TLS 1.3 ALPN 'h2', or io/mem-stream).

signature
write-frame = fn [mut conn H2Conn, f h2/Frame] i64

Writes a single codec/http2 Frame onto conn.

signature
read-frame-max = fn [mut conn H2Conn, max_frame_size i64] h2/Frame

Reads the next codec/http2 Frame from conn enforcing max_frame_size.

signature
read-frame = fn [mut conn H2Conn] h2/Frame

Reads the next codec/http2 Frame from conn (default 16 MiB wire max).

signature
encode-rst-stream = fn [err_code i64] bytes

Encodes a 4-byte RST_STREAM payload (error_code u32 BE).

signature
encode-goaway = fn [last_sid i64, err_code i64] bytes

Encodes an 8-byte GOAWAY payload (last_stream_id u31 BE || error_code u32 BE).

signature
send-goaway = fn [mut conn H2Conn, last_sid i64, err_code i64] i64

Sends a GOAWAY frame on stream 0.

signature
send-rst-stream = fn [mut conn H2Conn, stream_id i64, err_code i64] i64

Sends a RST_STREAM frame on stream_id.

signature
client-open = fn [mut s io/Stream] H2Conn

Initializes an HTTP/2 client connection over s: sends the RFC 9113 preface and initial SETTINGS frame.

signature
server-accept = fn [mut s io/Stream] H2Conn

Initializes an HTTP/2 server connection over s: verifies the client preface and sends server SETTINGS + ACK.

signature
request = fn [mut conn H2Conn, method str, path str, authority str, headers map[str], body bytes] Message

Sends an HTTP/2 request (HEADERS + optional DATA) and reads the server's Message response.

Executable Example
veko
use [io, h2 = net/http2, ch2 = codec/http2]
mut wire = io/mem-stream (bytes/with-cap 0)
mut cc = h2/client-open mut wire
hblk = ch2/hpack-encode [(ch2/Header { name = ':method', value = 'GET' }) (ch2/Header { name = ':path', value = '/ping' })]
_ = h2/write-frame mut cc (ch2/Frame { ftype = 1, flags = 5, stream_id = 1, payload = hblk })
mut rx = io/mem-stream (io/read-all mut cc.stream)
mut sc = h2/server-accept mut rx
req = h2/read-request mut sc
if req.method != 'GET' or req.path != '/ping' or req.stream_id != 1 [ err 'net/http2' ] else [ .ok ]
signature
read-request = fn [mut conn H2Conn] Message

Reads the next complete HTTP/2 request Message on a server H2Conn.

signature
write-response = fn [mut conn H2Conn, stream_id i64, status i64, headers map[str], body bytes] i64

Writes an HTTP/2 server response (HEADERS + optional DATA with END_STREAM) on stream_id.

signature
encode-window-update = fn [inc i64] bytes
signature
serve-conn-opts = fn [mut s io/Stream, handler any, opts H2ServerOpts] i64

Serves an RFC 9113 HTTP/2 connection over s with explicit H2ServerOpts DoS & flood limits (concurrent stream cap, CONTINUATION flood guard, HPACK table cap, and CVE-2023-44487 Rapid Reset GOAWAY).

signature
serve-conn = fn [mut s io/Stream, handler any, max_requests i64] i64

Serves an RFC 9113 HTTP/2 connection over s using default H2ServerOpts. handler is fn [req Message] Message. Pass max_requests <= 0 for unlimited streams on the connection.