HttpClient

Struct HttpClient 

Source
pub struct HttpClient { /* private fields */ }
Expand description

A simple non-blocking HTTP 1.x client that does not attempt to reuse/keep-alive connections.

Calling connect(..) or request(..) will return a HttpClientSession, which encapsulates a FrameDuplex utilizing an HTTP [FramingStrategy]. The framing strategy utilizes Hyperium’s http lib for hyperium_http::Request and hyperium_http::Response structs.

The returned HttpClientSession will have pre-setup TLS for https URLs, and will pre-buffer the serialized request. Calls to drive(..) will perform the TLS handshake and flush the pending request. Calls to read(..) will buffer the response, and return a deserialized hyperium_http::Response.

For now, this only supports HTTP 1.x.

§Functions

HttpClient::request will open a HttpClientSession for the given hyperium_http::Request, buffering the request, which can be driven and read to completion. To open a connection without an immeidate pending hyperium_http::Request, use HttpClient::connect, which simply opens a persistent connection.

HttpClient::connect will open a persistent HttpClientSession to the given domain. This connection must call Session::drive() until Session::status returns [SessionStatus::Connected], at which point the session can send multiple hyperium_http::Request payloads and receive hyperium_http::Response payloads utilizing HTTP “keep-alive”.

§Example

use nbio::{Receive, ReceiveOutcome, Session};
use nbio::http::HttpClient;
use nbio::hyperium_http::Request;
use nbio::tcp_stream::OwnedTLSConfig;

// create the client and make the request
let mut client = HttpClient::new();
let mut conn = client
    .request(Request::get("http://icanhazip.com").body(()).unwrap())
    .unwrap();

// drive and read the conn until a full response is received
loop {
    conn.drive().unwrap();
    if let ReceiveOutcome::Payload(r) = conn.receive().unwrap() {
        // validate the response
        println!("Response Body: {}", String::from_utf8_lossy(r.body()));
        break;
    }
}

Implementations§

Source§

impl HttpClient

Source

pub fn new() -> Self

Create a new PersistentHttpClient

Source

pub fn with_connection_pool( self, max_connections_per_domain: usize, max_connections_total: usize, default_keep_alive_timeout: Duration, ) -> Self

Enable keep-alive connection pooling

  • max_connections_per_domain - max connections that can be pooled per scheme/host/port
  • max_connections_total - max connections that can be pooled in total
  • default_keep_alive_timeout - when the server does not response with the keep-alive header, use this as the default timeout
Source

pub fn with_addr_resolver(self, addr_resolver: Arc<AddrResolver>) -> Self

Set the AddrResolver to use to resolve DNS entries

Source

pub fn with_tls_connector(self, tls_connector: Arc<TlsConnector>) -> Self

Set the TlsConnector to use for a TLS handshakes

Source

pub fn with_tls_config(self, tls_config: OwnedTLSConfig) -> Result<Self, Error>

Create a native TLS connector with the given config

Source

pub fn connect( &self, host: &str, port: u16, scheme: Scheme, ) -> Result<HttpClientSession, Error>

Get from the pool or initiate a new HTTP connection that is ready for a new request.

This will return a HttpClientSession, which encapsulates a FrameDuplex utilizing an HTTP [FramingStrategy]. The framing strategy utilizes Hyperium’s http lib for hyperium_http::Request and hyperium_http::Response structs.

You may create a PersistentHttpConnection from the returned HttpClientSession by calling From<HttpClientSession> on it, is useful for keep-alive request/response coordination across multiple concurrent pending requests.

The returned HttpClientSession will have pre-setup TLS for https URLs, and will have pre-buffered the serialized request request. Before calling read/write, call Session::drive() to finish connecting and complete any pending TLS handshakes until Session::status returns [SessionStatus::Connected].

For now, this only supports HTTP 1.x.

Source

pub fn request<I: IntoBody>( &self, request: Request<I>, ) -> Result<HttpClientSession, Error>

Get from the pool or initiate a new HTTP connection that will send the given request.

This will return a HttpClientSession, which encapsulates a FrameDuplex utilizing an HTTP [FramingStrategy]. The framing strategy utilizes Hyperium’s http lib for hyperium_http::Request and hyperium_http::Response structs.

The returned HttpClientSession will have pre-setup TLS for https URLs, and will have pre-buffered the serialized request request. Calling drive(..) and read(..) will perform the TLS handshake, flush the pending request, buffer the response, and return a deserialized http::Response.

For now, this only supports HTTP 1.x.

Trait Implementations§

Source§

impl Default for HttpClient

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V