OxL Справочник OxL
РусскийEnglish

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().

oxl
let c = Http2.connect("https://example.org")?
defer c.close()
let r = c.get("/index.html")?

Example:

oxl✓ проверяется в CI
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")
    _ => {}
  }
}
Вывод
200 v2 hi over HTTP/2
@must_close
pub(api) data Http2

Потокобезопасность. many tasks may have requests in flight on the connection (use `handle()` to pass it to them).

С версии. 1.0.0-beta

Поля

pub(api) origin: str

The origin (https://host) of the connection.

Методы

connect(origin: str, cfg: HttpConfig) -> Http2 | NetError net
Connects, performs the TLS handshake (https) and the HTTP/2 preface exchange.
protocol() -> str
The protocol negotiated with ALPN (h2), empty for h2c.
usable() -> bool
The connection can start new requests (not closed, no GOAWAY received).
active() -> i64
Requests in flight.
ping(timeout: Duration?) -> Duration | NetError net
Round trip time of a PING frame.
openStream(method: str, path: str, headers: List<Header>, body: bytes, timeout: Duration?) -> ResponseStream | NetError net
As 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 net
Sends a request; path is the path with the query (/a?b=1).
handle() -> Http2Handle
A copyable token to use the connection from other tasks (spawn); it must not outlive close().
get(path: str, headers: List<Header>) -> Response | NetError net
GET path on the connection.
head(path: str, headers: List<Header>) -> Response | NetError net
HEAD path on the connection.
delete(path: str, headers: List<Header>) -> Response | NetError net
DELETE path on the connection.
post(path: str, body: bytes, headers: List<Header>) -> Response | NetError net
POST path with body on the connection.
put(path: str, body: bytes, headers: List<Header>) -> Response | NetError net
PUT path with body on the connection.
postText(path: str, text: str, headers: List<Header>) -> Response | NetError net
POST path with a UTF-8 text body on the connection.
close(grace: Duration)
GOAWAY, waits at most grace for the requests in flight, closes.

Определено в: std/http/http.oxl:5534

См. также