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
impl HttpClient
Sourcepub fn with_connection_pool(
self,
max_connections_per_domain: usize,
max_connections_total: usize,
default_keep_alive_timeout: Duration,
) -> Self
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/portmax_connections_total- max connections that can be pooled in totaldefault_keep_alive_timeout- when the server does not response with the keep-alive header, use this as the default timeout
Sourcepub fn with_addr_resolver(self, addr_resolver: Arc<AddrResolver>) -> Self
pub fn with_addr_resolver(self, addr_resolver: Arc<AddrResolver>) -> Self
Set the AddrResolver to use to resolve DNS entries
Sourcepub fn with_tls_connector(self, tls_connector: Arc<TlsConnector>) -> Self
pub fn with_tls_connector(self, tls_connector: Arc<TlsConnector>) -> Self
Set the TlsConnector to use for a TLS handshakes
Sourcepub fn with_tls_config(self, tls_config: OwnedTLSConfig) -> Result<Self, Error>
pub fn with_tls_config(self, tls_config: OwnedTLSConfig) -> Result<Self, Error>
Create a native TLS connector with the given config
Sourcepub fn connect(
&self,
host: &str,
port: u16,
scheme: Scheme,
) -> Result<HttpClientSession, Error>
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.
Sourcepub fn request<I: IntoBody>(
&self,
request: Request<I>,
) -> Result<HttpClientSession, Error>
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.