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 offersh2by ALPN (a NetError if the server will not speak it);"http://host[:port]"speaks HTTP/2 in the clear with prior knowledge (h2c). Pass aTlsContextto trust other certificates (TlsContext.insecureClient()for a test server). - Requests.
get,postandrequest(Http2Request)send one and wait for its answer;requestAllsends many at once (as many as the server allows in flight, the rest as streams finish) and gives the answers in the requests' order. AHttp2Responsehas thestatus, theheaders(lower-case names), thebodyasBytes,text()and thestreamit 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
NetErrornaming 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) -> Http2Requeststatic fn post(path: string, body: string, contentType: string = "application/json") -> Http2Request
Fields
method: stringpath: stringheaders: HashMap<string, string>body: Bytes
class Http2Response
What the server answered.
Constructors
new Http2Response()
Fields
status: int = 0headers: 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) -> stringfn text() -> stringfn ok() -> bool
class Http2Stream
A request in flight.
Constructors
new Http2Stream(id: int, req: Http2Request, window: long)
Fields
id: intreq: Http2Requestres: Http2Response = new Http2Response()done: bool = falseerror: string = ""sendWindow: long = 0sent: int = 0headerBlock: 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).ctxnull: the system's trusted certificates.static fn connect(host: string, port: int, tls: bool = true, ctx: TlsContext = null, timeoutMs: int = 30000) -> Http2Clientstatic 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) -> Http2Responsefn post(path: string, body: string, contentType: string = "application/json") -> Http2Responsefn 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.