Nyx by Example

HTTP/2 Frames

HTTP/2 is a binary protocol. Every message is a typed frame with a 9-byte header. DATA carries bodies, HEADERS carries HPACK-compressed metadata, SETTINGS negotiates connection parameters, WINDOW_UPDATE controls flow.

Code

// nyx-http2 frames — HEADERS, DATA, SETTINGS frame types

import "std/http2"

fn main() -> int {
    // HTTP/2 is a binary protocol with typed frames.
    // Each frame has a 9-byte header + payload.

    print("HTTP/2 frame types:")
    print("  H2_DATA      = " + int_to_string(H2_DATA) + "  — request/response bodies")
    print("  H2_HEADERS   = " + int_to_string(H2_HEADERS) + "  — HPACK-compressed headers")
    print("  H2_SETTINGS  = " + int_to_string(H2_SETTINGS) + "  — connection parameters")
    print("  H2_PING      = " + int_to_string(H2_PING) + "  — keep-alive")
    print("  H2_GOAWAY    = " + int_to_string(H2_GOAWAY) + "  — graceful shutdown")
    print("  H2_WINDOW_UPDATE = " + int_to_string(H2_WINDOW_UPDATE) + "  — flow control")

    print("")
    print("Connection preface: client sends 24-byte magic string")
    print("\"PRI * HTTP/2.0\\r\\n\\r\\nSM\\r\\n\\r\\n\" followed by SETTINGS frame.")
    print("")
    print("Each request opens a new stream_id (odd for client-initiated).")
    print("HPACK compresses repeated headers: Host, User-Agent, etc.")
    return 0
}

Output

HTTP/2 frame types:
  H2_DATA      = 0  — request/response bodies
  H2_HEADERS   = 1  — HPACK-compressed headers
  H2_SETTINGS  = 4  — connection parameters
  H2_PING      = 6  — keep-alive
  H2_GOAWAY    = 7  — graceful shutdown
  H2_WINDOW_UPDATE = 8  — flow control

Connection preface: client sends 24-byte magic string
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" followed by SETTINGS frame.

Each request opens a new stream_id (odd for client-initiated).
HPACK compresses repeated headers: Host, User-Agent, etc.

Explanation

Where HTTP/1.1 was a text protocol you could debug with telnet, HTTP/2 is binary and framed. The 9-byte header encodes length (24 bits), type (8 bits), flags (8 bits), and a 31-bit stream identifier. Understanding frame types is the key to reading HTTP/2 traces: SETTINGS is sent at startup to negotiate limits, HEADERS opens a stream, DATA carries the body, WINDOW_UPDATE prevents fast senders from overwhelming slow receivers, and GOAWAY politely tells the peer to stop opening new streams.

← Previous Next →

Source: examples/by-example/95-http2-frames.nx