Grav v1.14.0

HTTP/2

import "std/net" · namespace std.net

An HTTP/2 client (RFC 9113): one connection carries many requests at once, with headers compressed by HPACK (RFC 7541).

import "std"
import "std/net"

fn main() {
    let h2 = Http2Client.open("https://example.com")        // TLS; "h2" agreed by ALPN
    let res = h2.get("/api/items")
    print("${res.status} ${res.header("content-type")}")
    print(res.text())

    // twenty requests in flight at once, on the one connection
    let reqs = new Array<Http2Request>()
    for (i in 0..20) { reqs.push(Http2Request.get("/api/items/${i}")) }
    for (r in h2.requestAll(reqs)) { print("${r.stream}: ${r.status}") }

    let saved = h2.post("/api/items", "{\"name\": \"pen\"}")
    h2.close()
}
  • Connecting. Http2Client.open("https://host[:port]") connects with TLS and offers h2 by ALPN (a NetError if the server will not speak it); "http://host[:port]" speaks HTTP/2 in the clear with prior knowledge (h2c). Pass a TlsContext to trust other certificates (TlsContext.insecureClient() for a test server).
  • Requests. get, post and request(Http2Request) send one and wait for its answer; requestAll sends many at once (as many as the server allows in flight, the rest as streams finish) and gives the answers in the requests' order. A Http2Response has the status, the headers (lower-case names), the body as Bytes, text() and the stream it came on.
  • The protocol. Flow control both ways (large uploads wait for the server's window; downloads open it as they are read), CONTINUATION frames, padding, 1xx answers skipped, PINGs answered, the server's settings honoured. A RST_STREAM fails its request, a GOAWAY the requests it did not take, with a NetError naming the server's reason.
  • Checked against nginx and Python's h2, with HPACK's tables generated from the RFC's (tools/gen-hpack.py). A client is for one thread.

API

class Http2Request

One request for an Http2Client.

Constructors

  • new Http2Request(method: string, path: string, headers: HashMap<string, string> = null, body: Bytes = null)

Static members

  • static fn get(path: string) -> Http2Request
  • static fn post(path: string, body: string, contentType: string = "application/json") -> Http2Request

Fields

  • method: string
  • path: string
  • headers: HashMap<string, string>
  • body: Bytes

class Http2Response

What the server answered.

Constructors

  • new Http2Response()

Fields

  • status: int = 0
  • headers: HashMap<string, string> = new HashMap<string, string>() — Header names in lower case (HTTP/2 sends them so); repeated ones joined by ", ".
  • body: Bytes = new Bytes()
  • stream: int = 0 — The stream it came on.

Methods

  • fn header(name: string) -> string
  • fn text() -> string
  • fn ok() -> bool

class Http2Stream

A request in flight.

Constructors

  • new Http2Stream(id: int, req: Http2Request, window: long)

Fields

  • id: int
  • req: Http2Request
  • res: Http2Response = new Http2Response()
  • done: bool = false
  • error: string = ""
  • sendWindow: long = 0
  • sent: int = 0
  • headerBlock: Bytes = new Bytes()
  • gotHeaders: bool = false

class Http2Client

Constructors

  • new Http2Client(conn: TcpStream, authority: string, scheme: string)

Static members

  • static fn open(url: string, ctx: TlsContext = null, timeoutMs: int = 30000) -> Http2Client — Opens a connection to "https://host[:port]" (TLS, ALPN "h2") or "http://host[:port]" (cleartext, prior knowledge). ctx null: the system's trusted certificates.
  • static fn connect(host: string, port: int, tls: bool = true, ctx: TlsContext = null, timeoutMs: int = 30000) -> Http2Client
  • static fn codeName(code: long) -> string

Fields

  • userAgent: string = "grav-http2" — A User-Agent sent with each request ("" for none).

Methods

  • fn request(req: Http2Request) -> Http2Response — Sends a request and waits for its answer.
  • fn get(path: string, headers: HashMap<string, string> = null) -> Http2Response
  • fn post(path: string, body: string, contentType: string = "application/json") -> Http2Response
  • fn requestAll(reqs: Array<Http2Request>) -> Array<Http2Response> — Sends every request at once (as many as the server allows in flight) on this one connection, and gives the answers in the requests' order.
  • fn close() — Ends the connection politely (GOAWAY), then closes it.

Edit this page on GitHub