data Http2
An HTTP/2 connection to one origin (RFC 9113). https:// origins negotiate h2 with ALPN; http:// origins use cleartext
HTTP/2 with prior knowledge (h2c). Many requests may be in flight on the connection at the same time, from different tasks
(multiplexing, with flow control); responses are complete (Response, version is 2, maxBody applies). Redirects are
not followed. @must_close: end it with close().
let c = Http2.connect("https://example.org")?
defer c.close()
let r = c.get("/index.html")?Example:
use std.http { Server, ServerConfig, ServerHandle, Router, Request, Reply, Client, Header, HttpConfig, Response, WebSocket, WsConfig, WsKind, WsMessage, SseWriter, SseStream, SseConfig, SseEvent, BodyWriter, Http2, Http2Handle, ResponseStream, chunks }
fn run() -> () | NetError | DecodeError | Panicked net, io, time {
let srv = Server.bind(ServerConfig{ port: 0, followNode: false })?
defer srv.close()
let stop = srv.handle()
let base = "http://127.0.0.1:{srv.port()}"
let router = Router.new().get("/hi", (req: Request) => Reply.text("hi over {req.version}")).post("/echo", (req: Request) => Reply.bytes(req.body, "text/plain"))
scope {
spawn stop.serve(router)
let c = Http2.connect(base)?
defer c.close()
let r = c.get("/hi")?
print("{r.status} v{r.version} {r.text()!}")
stop.shutdown()
()
}?
}
fn main() io, net, time {
match run() {
NetError _ => print("net error")
DecodeError _ => print("decode error")
Panicked _ => print("panicked")
_ => {}
}
}Output
200 v2 hi over HTTP/2
@must_close
pub(api) data Http2Thread safety. many tasks may have requests in flight on the connection (use `handle()` to pass it to them).
Since. 1.0.0-beta
Fields
pub(api) origin: str | The origin ( |
Methods
connect | (origin: str, cfg: HttpConfig) -> Http2 | NetError netConnects, performs the TLS handshake (https) and the HTTP/2 preface exchange. |
protocol | () -> strThe protocol negotiated with ALPN (h2), empty for h2c. |
usable | () -> boolThe connection can start new requests (not closed, no GOAWAY received). |
active | () -> i64Requests in flight. |
ping | (timeout: Duration?) -> Duration | NetError netRound trip time of a PING frame. |
openStream | (method: str, path: str, headers: List<Header>, body: bytes, timeout: Duration?) -> ResponseStream | NetError netAs send, but returns as soon as the response head is there; the body is read from the ResponseStream (D-3970). |
send | (method: str, path: str, headers: List<Header>, body: bytes, timeout: Duration?) -> Response | NetError netSends a request; path is the path with the query (/a?b=1). |
handle | () -> Http2HandleA copyable token to use the connection from other tasks (spawn); it must not outlive close(). |
get | (path: str, headers: List<Header>) -> Response | NetError netGET path on the connection. |
head | (path: str, headers: List<Header>) -> Response | NetError netHEAD path on the connection. |
delete | (path: str, headers: List<Header>) -> Response | NetError netDELETE path on the connection. |
post | (path: str, body: bytes, headers: List<Header>) -> Response | NetError netPOST path with body on the connection. |
put | (path: str, body: bytes, headers: List<Header>) -> Response | NetError netPUT path with body on the connection. |
postText | (path: str, text: str, headers: List<Header>) -> Response | NetError netPOST path with a UTF-8 text body on the connection. |
close | (grace: Duration)GOAWAY, waits at most grace for the requests in flight, closes. |
Defined in: std/http/http.oxl:5534