OxL Reference

data Http2Handle

A copyable reference to an Http2 connection for other tasks (Http2.handle()): requests with the default settings.

Example:

oxl✓ checked in 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 h: Http2Handle = c.handle()
    let r = h.get("/hi")?
    print("{r.status} {h.origin == c.origin}")
    stop.shutdown()
    ()
  }?
}

fn main() io, net, time {
  match run() {
    NetError _ => print("net error")
    DecodeError _ => print("decode error")
    Panicked _ => print("panicked")
    _ => {}
  }
}
Output
200 true
@derive(Copy)
pub(api) data Http2Handle

Thread safety. copyable; it lets other tasks use the connection but must not outlive `Http2.close()`.

Since. 1.0.0-beta

Fields

pub(api) origin: str

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

Methods

send(method: str, path: str, headers: List<Header>, body: bytes) -> Response | NetError net
Sends a request on the shared HTTP/2 connection; path includes the query.
get(path: str, headers: List<Header>) -> Response | NetError net
GET path on the shared HTTP/2 connection.
post(path: str, body: bytes, headers: List<Header>) -> Response | NetError net
POST path with body on the shared HTTP/2 connection.

Defined in: std/http/http.oxl:5364

See also