client
import std::net::http2::client; · source
Types
Http2Client
type struct Http2Client {
static new() -> Http2Client;
async get(&this, addr: SocketAddr, authority: Str, path: Str) -> Result<Response, IoError>;
async post(&this, addr: SocketAddr, authority: Str, path: Str, content_type: Str, body: Array<u8>) -> Result<Response, IoError>;
}
Http2Client makes one-shot h2c requests over plain TCP: get and post each dial addr, upgrade to HTTP/2 with the prior-knowledge preface, run one request/response, and close. authority becomes the :authority pseudo-header — HTTP/2's spelling of Host.
import std::net::http2::client;
mut c: Http2Client = Http2Client::new();
const resp: Response = await c.get(addr, Str::new("localhost:8080"), Str::new("/"))?;
one_shot is the same with a request you built yourself. connect is the building block underneath both: it takes ownership of an already-connected BufStream<S>, performs the client handshake, and hands back an Http2Connection to issue several requests over. Over a TlsStream whose ALPN negotiated h2, that is how you speak HTTPS/2.
Constructors
static new() -> Http2Client;
Methods
async get(&this, addr: SocketAddr, authority: Str, path: Str) -> Result<Response, IoError>;
async get(&this, addr: SocketAddr, authority: Str, path: Str) -> Result<Response, IoError>;GET over h2c. authority is the :authority pseudo-header (host[:port]); path is the request target.
async post(&this, addr: SocketAddr, authority: Str, path: Str, content_type: Str, body: Array<u8>) -> Result<Response, IoError>;
async post(&this, addr: SocketAddr, authority: Str, path: Str, content_type: Str, body: Array<u8>) -> Result<Response, IoError>;POST over h2c with a body and Content-Type.
Free Functions
async function connect<S>(conn: BufStream<S>) -> Result<Http2Connection<S>, IoError> where S: AsyncTransport;
async function connect<S>(conn: BufStream<S>) -> Result<Http2Connection<S>, IoError> where S: AsyncTransport;Upgrade conn to an HTTP/2 client connection by performing the preface + SETTINGS handshake. Takes ownership; the returned connection closes the transport when dropped. A failed handshake releases it.
Performs the client handshake over an already-connected BufStream<S> and returns an Http2Connection to issue several requests over. Over a TlsStream whose ALPN negotiated h2, this is how you speak HTTPS/2.
async function one_shot(addr: SocketAddr, authority: Str, req: Request) -> Result<Response, IoError>;
async function one_shot(addr: SocketAddr, authority: Str, req: Request) -> Result<Response, IoError>;Dial addr, upgrade to h2c, run one request/response, close.
Dials addr, sends one request you built yourself, and closes. Http2Client::get and post are conveniences over this.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Http2Client {}One-shot h2c (cleartext) requests over plain TCP.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Http2Client::new() -> Http2Clientasync function name(param: Type) -> T { ... }Compiles the function into a stackless state machine. Calling it builds a future whose output is the declared return type; nothing runs until that future is polled. Allowed on free functions, methods, and trait methods.
language referenceasync Http2Client::get(&this, addr: SocketAddr, authority: Str, path: Str) -> Result<Response, IoError>GET over h2c. authority is the :authority pseudo-header (host[:port]); path is the request target.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) addr: SocketAddr(parameter) authority: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::str(parameter) path: Strtype struct Response {
status: StatusCode;
version: String;
headers: Headers;
body: Array<u8>;
}HTTP response.
std::net::http::responsetype struct IoError {
kind: IoErrorKind;
os_code: i32;
}async Http2Client::post(&this, addr: SocketAddr, authority: Str, path: Str, content_type: Str, body: Array<u8>) -> Result<Response, IoError>POST over h2c with a body and Content-Type.
(parameter) content_type: Str(parameter) body: Array<u8>type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arrayimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::net::http2;net::http2: HTTP/2 (RFC 7540) with HPACK header compression (RFC 7541).
module pagemut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut c: Http2Clientstatic Http2Client::new() -> Http2Clientconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const resp: Responseawait futureSuspends the enclosing async body until the operand - which must implement Future - completes, and evaluates to its output. A prefix operator allowed anywhere an expression is (conditions, arms, guards), but only inside async code.
mut c: Http2Clientasync Http2Client::get(&this, addr: SocketAddr, authority: Str, path: Str) -> Result<Response, IoError>GET over h2c. authority is the :authority pseudo-header (host[:port]); path is the request target.
static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
value?Error propagation. On a Result<T, E> it yields the T of an Ok and otherwise returns the Err(e) from the enclosing function unchanged; on an Option<T> it yields the T of a Some or returns None. The enclosing function must return a matching Result / Option.
async Http2Client::post(&this, addr: SocketAddr, authority: Str, path: Str, content_type: Str, body: Array<u8>) -> Result<Response, IoError>POST over h2c with a body and Content-Type.
async function one_shot(addr: SocketAddr, authority: Str, req: Request) -> Result<Response, IoError>Dial addr, upgrade to h2c, run one request/response, close.
async function connect<S>(conn: BufStream<S>) -> Result<Http2Connection<S>, IoError>
where S: AsyncTransportUpgrade conn to an HTTP/2 client connection by performing the preface + SETTINGS handshake. Takes ownership; the returned connection closes the transport when dropped. A failed handshake releases it.
type struct BufStream<S> {
inner: S;
rbuf: Array<u8>;
rpos: u64;
scratch: Array<u8>;
wbuf: Array<u8>;
}type struct TlsStream {
ssl: void*;
inner: TcpStream;
}function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
async function connect<S>(conn: BufStream<S>) -> Result<Http2Connection<S>, IoError>
where S: AsyncTransportUpgrade conn to an HTTP/2 client connection by performing the preface + SETTINGS handshake. Takes ownership; the returned connection closes the transport when dropped. A failed handshake releases it.
type parameter Swhere S: AsyncTransport
(parameter) conn: BufStream<S>type struct Http2Connection<S> {
conn: BufStream<S>;
encoder: HpackEncoder;
decoder: HpackDecoder;
is_client: boolean;
next_stream_id: u32;
goaway_received: boolean;
remote_max_frame: u64;
remote_initial_window: i64;
conn_send_window: i64;
stream_send_window: i64;
}where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait AsyncTransport {
async read_into(mut &this, buf: Array<u8>) -> Transfer;
async write_from(mut &this, buf: Array<u8>) -> Transfer;
}One raw, unbuffered async byte channel: what BufStream<S> needs of a transport, and nothing more.
async function one_shot(addr: SocketAddr, authority: Str, req: Request) -> Result<Response, IoError>Dial addr, upgrade to h2c, run one request/response, close.
(parameter) req: Requesttype struct Request {
method: Method;
path: String;
version: String;
headers: Headers;
body: Array<u8>;
params: HashMap<String, String>;
}HTTP request.
std::net::http::request