API Reference

Complete reference for all exported types, functions, and constants.

Nghttp2Wrapper.HTTP2_PREFACEConstant

Read one chunk from a TLS connection: either the initial client connection preface (24 bytes) and a following frame, or a single HTTP/2 frame (9-byte header + variable payload).

source
Nghttp2Wrapper.CallbacksType
Callbacks()

Create a new nghttp2 callbacks object. Resources are automatically freed when the object is garbage collected, or when close() is called.

source
Nghttp2Wrapper.ConnectionPoolType
ConnectionPool(; max_per_host=6, idle_timeout=60.0)

A pool of HTTP/2 client connections with automatic reuse and keep-alive.

Examples

pool = ConnectionPool()
response = pool_get(pool, "https://nghttp2.org/")
close(pool)
source
Nghttp2Wrapper.HTTP2ClientType
HTTP2Client(host; port=443, max_concurrent_streams=100,
            initial_window_size=65535, header_table_size=4096,
            verify_peer=true)

Create an HTTP/2 client connected to the given host over TLS. The connection is established immediately with ALPN negotiation for h2.

source
Nghttp2Wrapper.HTTP2ServerType
HTTP2Server(handler, port; host="0.0.0.0", certfile="", keyfile="")

Create an HTTP/2 server listening on the given port. If certfile and keyfile are provided, the server uses TLS with ALPN h2. Otherwise it uses cleartext HTTP/2 (h2c).

The handler function receives a ServerRequest and returns a ServerResponse.

Examples

Cleartext (h2c):

server = HTTP2Server(8080) do req
    ServerResponse(200, "Hello HTTP/2!")
end

TLS (h2):

server = HTTP2Server(8443; certfile="cert.pem", keyfile="key.pem") do req
    ServerResponse(200, "Hello HTTPS/2!")
end
source
Nghttp2Wrapper.NVPairType
NVPair(name, value)

An HTTP header name-value pair. Owns copies of the name and value as byte vectors, ensuring memory safety when passed to C functions.

Examples

nv = NVPair(":method", "GET")
nv = NVPair(":path", "/index.html")
source
Nghttp2Wrapper.Nghttp2DataProviderType

Layout-compatible representation of nghttp2's nghttp2_data_provider:

typedef union {
    int fd;
    void *ptr;
} nghttp2_data_source;

typedef struct {
    nghttp2_data_source source;
    nghttp2_data_source_read_callback read_callback;
} nghttp2_data_provider;

We only use the ptr variant of the union, so a single Ptr{Cvoid} for source matches the 8-byte union width on 64-bit platforms.

source
Nghttp2Wrapper.Nghttp2ErrorType
Nghttp2Error <: Exception

Exception thrown when an nghttp2 C function returns an error code.

Fields

  • code::Cint — the nghttp2 error code
  • msg::String — human-readable error description
  • fatal::Bool — true if the error is fatal (unrecoverable)
source
Nghttp2Wrapper.Nghttp2ErrorMethod
Nghttp2Error(code::Integer)

Construct an Nghttp2Error from an error code, automatically filling the message and fatal flag.

source
Nghttp2Wrapper.PrioritySpecType
PrioritySpec(; dep_stream=0, weight=16, exclusive=false)

HTTP/2 stream priority specification.

Fields

  • dep_stream::Int32 — dependent stream ID (0 = root)
  • weight::Int32 — priority weight (1-256, default 16)
  • exclusive::Bool — exclusive dependency flag
source
Nghttp2Wrapper.RequestType
Request(method, path; headers=NVPair[], body=UInt8[])

Represents an outgoing HTTP/2 request.

Fields

  • method::String — HTTP method (GET, POST, etc.)
  • path::String — Request path (e.g., "/index.html")
  • headers::Vector{NVPair} — Custom request headers
  • body::Vector{UInt8} — Request body
source
Nghttp2Wrapper.ResponseType
Response(status, headers, body)

Represents an HTTP/2 response.

Fields

  • status::Int — HTTP status code (e.g., 200, 404)
  • headers::Vector{NVPair} — Response headers
  • body::Vector{UInt8} — Response body
source
Nghttp2Wrapper.ResponseBodySourceType

Response body source passed to nghttp2 via nghttp2_data_provider.

data holds the full response body bytes; offset is the next byte to read. The data source read callback copies up to len bytes into nghttp2's buffer on each invocation, advances offset, and signals NGHTTP2_DATA_FLAG_EOF when offset reaches length(data).

The struct is kept alive across the C boundary by storing it in the owning ServerContext's response_bodies dict (so GC does not reclaim it while nghttp2 still holds a pointer). The dict entry is removed when the callback signals EOF.

source
Nghttp2Wrapper.ServerResponseType
ServerResponse(status; headers=NVPair[], body=UInt8[])
ServerResponse(status, body::AbstractString)
ServerResponse(status, headers, body)

Represents an HTTP/2 response to be sent by a server handler.

source
Nghttp2Wrapper.SessionType
Session(callbacks::Callbacks; server::Bool=false)

Create a new HTTP/2 session (client by default, server if server=true). The session holds a reference to the callbacks to prevent premature GC.

source
Base.getMethod
get(client, path; headers=NVPair[]) → Response

Send an HTTP/2 GET request.

source
Nghttp2Wrapper._server_data_source_read_cbMethod

nghttp2 data source read callback.

Copies up to len bytes from the ResponseBodySource (whose Julia pointer is carried in the source union) into the output buffer that nghttp2 provides. Advances the source's offset, and sets NGHTTP2_DATA_FLAG_EOF in data_flags once the whole body has been consumed.

Matches the C signature:

ssize_t (*nghttp2_data_source_read_callback)(
    nghttp2_session *session, int32_t stream_id, uint8_t *buf,
    size_t length, uint32_t *data_flags, nghttp2_data_source *source,
    void *user_data);
source
Nghttp2Wrapper._server_on_frame_recv_cbMethod

onframerecv callback: dispatches handler when ENDSTREAM is received. The nghttp2frame struct layout: nghttp2framehd (length:sizet, streamid:int32, type:uint8, flags:uint8, ...)

source
Nghttp2Wrapper.acquireMethod
acquire(pool, host; port=443, verify_peer=true) → HTTP2Client

Get a client from the pool. Reuses an idle connection if available, otherwise creates a new one.

source
Nghttp2Wrapper.deflateMethod
deflate(d::HpackDeflater, headers::Vector{NVPair}) → Vector{UInt8}

Compress headers using HPACK. Returns the compressed bytes.

source
Nghttp2Wrapper.forward_requestMethod
forward_request(client::HTTP2Client, req::ServerRequest) → ServerResponse

Forward a server request to an upstream HTTP/2 server via the client, and return the upstream response as a ServerResponse. Useful for building HTTP/2 proxies.

source
Nghttp2Wrapper.inflateMethod
inflate(i::HpackInflater, data::Vector{UInt8}) → Vector{NVPair}

Decompress HPACK-encoded headers. Returns a vector of NVPairs.

source
Nghttp2Wrapper.inspect_dynamic_tableMethod
inspect_dynamic_table(d::HpackDeflater) → Vector{NVPair}

Return the entries currently in the HPACK dynamic table. Note: This is an approximation — nghttp2 doesn't expose direct dynamic table iteration, so we return the max_size as context information.

source
Nghttp2Wrapper.listener_portMethod
listener_port(server::HTTP2Server) -> Int

Return the TCP port the server's listening socket is bound to. Works for both plaintext (h2c) and TLS (h2) listeners.

source
Nghttp2Wrapper.nghttp2_hd_inflate_hd2Method
nghttp2_hd_inflate_hd2(inflater, nv_out, inflate_flags, data, datalen, in_final) → Cssize_t

Decompress headers from data. On each call, check inflate_flags for NGHTTP2_HD_INFLATE_EMIT (header available in nv_out) and NGHTTP2_HD_INFLATE_FINAL (decompression complete).

source
Nghttp2Wrapper.nghttp2_session_mem_send2Method
nghttp2_session_mem_send2(session) → (Cssize_t, Ptr{UInt8})

Serialize outgoing data from the session into memory. Returns (nbytes, data_ptr) where nbytes is the number of bytes serialized and data_ptr points to the serialized data.

source
Nghttp2Wrapper.nghttp2_submit_request2Method
nghttp2_submit_request2(session, pri_spec, nva, nvlen, data_prd, stream_user_data) → Int32

Submit a request. Returns the stream ID on success, or a negative error code.

source
Nghttp2Wrapper.nghttp2_submit_settingsMethod
nghttp2_submit_settings(session, flags, iv, niv) → Cint

Submit a SETTINGS frame. iv is a pointer to an array of Nghttp2SettingsEntry and niv is the number of entries.

source
Nghttp2Wrapper.nghttp2_versionFunction
nghttp2_version(least_version=0) → Ptr{Nghttp2Info}

Return a pointer to the nghttp2 library version information. If least_version is not 0, returns C_NULL if the library version is less than the specified version.

source
Nghttp2Wrapper.pool_postMethod
pool_post(pool, url; headers=NVPair[], body=UInt8[]) → Response

Make a POST request using a pooled connection.

source
Nghttp2Wrapper.pool_requestMethod
pool_request(pool, method, url; headers=NVPair[], body=UInt8[]) → Response

Make a request using a pooled connection. The URL is parsed to extract host, port, and path. The connection is automatically acquired and released.

source
Nghttp2Wrapper.postMethod
post(client, path; headers=NVPair[], body=UInt8[]) → Response

Send an HTTP/2 POST request.

source
Nghttp2Wrapper.recv!Method
recv!(session::Session, data::Vector{UInt8}) → Int

Process incoming data. Returns the number of bytes consumed.

source
Nghttp2Wrapper.requestMethod
request(client, method, path; headers=NVPair[], body=UInt8[]) → Response

Send an HTTP/2 request and wait for the response. Pseudo-headers (:method, :path, :scheme, :authority) are automatically constructed.

source
Nghttp2Wrapper.request_streamMethod
request_stream(client, method, path; headers=NVPair[], body=UInt8[]) → (Channel{Response}, Channel{Vector{UInt8}})

Send an HTTP/2 request with streaming response body. Returns a done channel and a body chunks channel.

source
Nghttp2Wrapper.send!Method
send!(session::Session) → Vector{UInt8}

Serialize all pending outgoing data from the session into a byte vector.

source
Nghttp2Wrapper.shutdown!Method
shutdown!(client::HTTP2Client)

Initiate graceful shutdown by sending a GOAWAY frame and waiting for all pending streams to complete.

source
Nghttp2Wrapper.shutdown!Method
shutdown!(server::HTTP2Server)

Gracefully shut down the server: stop accepting new connections, wait for in-flight requests to complete.

source
Nghttp2Wrapper.to_nghttp2_nvMethod
to_nghttp2_nv(nv::NVPair) → Nghttp2Nv

Convert an NVPair to the C-compatible Nghttp2Nv struct. The caller MUST ensure the NVPair remains alive (GC.@preserve) for the duration of any C call using the returned struct.

source
Nghttp2Wrapper.websocket_connectMethod
websocket_connect(client, path; headers=NVPair[]) → Int32

Initiate a WebSocket connection over HTTP/2 (RFC 8441) by sending a CONNECT request with the :protocol pseudo-header set to websocket.

Returns the stream ID for the WebSocket tunnel.

Note: Requires server support for RFC 8441 (Extended CONNECT).

source
Nghttp2Wrapper.with_nvaMethod
with_nva(f, pairs::Vector{NVPair})

Convert a vector of NVPairs to a C-compatible array of Nghttp2Nv structs, call f(nva_ptr, nvlen), and return the result. The NVPair data is GC-preserved for the duration of the call.

source