HTTP/2 Server
HTTP/2 multiplexes many streams over a single TCP connection. The server handler receives a stream_id so it can interleave responses. HPACK compresses repeated headers across streams.
Code
// nyx-http2 server — HTTP/2 with HPACK and multiplexing
import "std/http2"
fn on_request(fd: int, stream_id: int, method: String, path: String, body: String) -> int {
// HTTP/2 handler receives stream_id so one connection handles many streams
let hdrs: Array = ["content-type", "application/json"]
if path == "/" {
h2_send_response(fd, stream_id, 200, hdrs, "{\"message\": \"hello over HTTP/2\"}")
} else {
h2_send_response(fd, stream_id, 404, hdrs, "{\"error\": \"not found\"}")
}
return 0
}
fn main() -> int {
let port: int = 3004
let workers: int = 4
print("HTTP/2 server starting on :" + int_to_string(port))
print(" mode: h2c (HTTP/2 cleartext, no TLS)")
print(" multiplexing: multiple streams per connection")
print(" compression: HPACK for headers")
// h2_serve blocks accepting connections, handling HTTP/2 frames,
// dispatching streams to the handler.
// h2_serve(port, workers, on_request)
print("")
print("test with: curl --http2-prior-knowledge http://localhost:3004/")
return 0
}
Output
HTTP/2 server starting on :3004 mode: h2c (HTTP/2 cleartext, no TLS) multiplexing: multiple streams per connection compression: HPACK for headers test with: curl --http2-prior-knowledge http://localhost:3004/
Explanation
HTTP/1.1's one-request-per-connection model is a performance cliff when a page needs 80 assets. HTTP/2 solves it by multiplexing: every request opens a new stream inside the same TCP connection, and responses can return out of order. The stream_id threads through your handler so the runtime can interleave frames. HPACK compresses the boilerplate headers (Host, User-Agent, Accept) that would otherwise dominate small requests. nyx-http2 implements it all in pure Nyx on top of the raw TCP runtime.