[][src]Struct isahc::HttpClientBuilder

pub struct HttpClientBuilder { /* fields omitted */ }

An HTTP client builder, capable of creating custom HttpClient instances with customized behavior.

Examples

use isahc::config::{RedirectPolicy, VersionNegotiation};
use isahc::prelude::*;
use std::time::Duration;

let client = HttpClient::builder()
    .timeout(Duration::from_secs(60))
    .redirect_policy(RedirectPolicy::Limit(10))
    .version_negotiation(VersionNegotiation::http2())
    .build()?;

Methods

impl HttpClientBuilder[src]

pub fn new() -> Self[src]

Create a new builder for building a custom client. All configuration will start out with the default values.

This is equivalent to the Default implementation.

pub fn cookies(self) -> Self[src]

Enable persistent cookie handling using a cookie jar.

This method requires the cookies feature to be enabled.

pub fn max_connections(self, max: usize) -> Self[src]

Set a maximum number of simultaneous connections that this client is allowed to keep open at one time.

If set to a value greater than zero, no more than max connections will be opened at one time. If executing a new request would require opening a new connection, then the request will stay in a "pending" state until an existing connection can be used or an active request completes and can be closed, making room for a new connection.

Setting this value to 0 disables the limit entirely.

This is an effective way of limiting the number of sockets or file descriptors that this client will open, though note that the client may use file descriptors for purposes other than just HTTP connections.

By default this value is 0 and no limit is enforced.

To apply a limit per-host, see HttpClientBuilder::max_connections_per_host.

pub fn max_connections_per_host(self, max: usize) -> Self[src]

Set a maximum number of simultaneous connections that this client is allowed to keep open to individual hosts at one time.

If set to a value greater than zero, no more than max connections will be opened to a single host at one time. If executing a new request would require opening a new connection, then the request will stay in a "pending" state until an existing connection can be used or an active request completes and can be closed, making room for a new connection.

Setting this value to 0 disables the limit entirely. By default this value is 0 and no limit is enforced.

To set a global limit across all hosts, see HttpClientBuilder::max_connections.

pub fn connection_cache_size(self, size: usize) -> Self[src]

Set the size of the connection cache.

After requests are completed, if the underlying connection is reusable, it is added to the connection cache to be reused to reduce latency for future requests.

Setting the size to 0 disables connection caching for all requests using this client.

By default this value is unspecified. A reasonable default size will be chosen.

pub fn timeout(self, timeout: Duration) -> Self[src]

Set a timeout for the maximum time allowed for a request-response cycle.

If not set, no timeout will be enforced.

pub fn connect_timeout(self, timeout: Duration) -> Self[src]

Set a timeout for the initial connection phase.

If not set, a connect timeout of 300 seconds will be used.

pub fn version_negotiation(self, negotiation: VersionNegotiation) -> Self[src]

Configure how the use of HTTP versions should be negotiated with the server.

The default is [HttpVersionNegotiation::latest_compatible].

pub fn redirect_policy(self, policy: RedirectPolicy) -> Self[src]

Set a policy for automatically following server redirects.

The default is to not follow redirects.

pub fn auto_referer(self) -> Self[src]

Update the Referer header automatically when following redirects.

pub fn authentication(self, authentication: Authentication) -> Self[src]

Set one or more default HTTP authentication methods to attempt to use when authenticating with the server.

Depending on the authentication schemes enabled, you will also need to set credentials to use for authentication using HttpClientBuilder::credentials.

Examples

let client = HttpClient::builder()
    .authentication(Authentication::basic() | Authentication::digest())
    .credentials(Credentials::new("clark", "qwerty"))
    .build()?;

pub fn credentials(self, credentials: Credentials) -> Self[src]

Set the default credentials to use for HTTP authentication on all requests.

This setting will do nothing unless you also set one or more authentication methods using HttpClientBuilder::authentication.

pub fn tcp_keepalive(self, interval: Duration) -> Self[src]

Enable TCP keepalive with a given probe interval.

pub fn tcp_nodelay(self) -> Self[src]

Enables the TCP_NODELAY option on connect.

pub fn proxy(self, proxy: impl Into<Option<Uri>>) -> Self[src]

Set a proxy to use for requests.

The proxy protocol is specified by the URI scheme.

  • http: Proxy. Default when no scheme is specified.
  • https: HTTPS Proxy. (Added in 7.52.0 for OpenSSL, GnuTLS and NSS)
  • socks4: SOCKS4 Proxy.
  • socks4a: SOCKS4a Proxy. Proxy resolves URL hostname.
  • socks5: SOCKS5 Proxy.
  • socks5h: SOCKS5 Proxy. Proxy resolves URL hostname.

By default the system proxy will be used, for example if one specified in either the http_proxy or https_proxy environment variables on *nix platforms.

Setting to None explicitly disable the use of a proxy.

Examples

Using http://proxy:80 as a proxy:

let client = HttpClient::builder()
    .proxy(Some("http://proxy:80".parse()?))
    .build()?;

Explicitly disable the use of a proxy:

let client = HttpClient::builder()
    .proxy(None)
    .build()?;

pub fn proxy_blacklist(self, hosts: impl IntoIterator<Item = String>) -> Self[src]

Disable proxy usage to use for the provided list of hosts.

Examples

let client = HttpClient::builder()
    // Disable proxy for specified hosts.
    .proxy_blacklist(vec!["a.com".to_string(), "b.org".to_string()])
    .build()?;

pub fn proxy_authentication(self, authentication: Authentication) -> Self[src]

Set one or more default HTTP authentication methods to attempt to use when authenticating with a proxy.

Depending on the authentication schemes enabled, you will also need to set credentials to use for authentication using HttpClientBuilder::proxy_credentials.

Examples

let client = HttpClient::builder()
    .proxy("http://proxy:80".parse::<http::Uri>()?)
    .proxy_authentication(Authentication::basic())
    .proxy_credentials(Credentials::new("clark", "qwerty"))
    .build()?;

pub fn proxy_credentials(self, credentials: Credentials) -> Self[src]

Set the default credentials to use for proxy authentication on all requests.

This setting will do nothing unless you also set one or more proxy authentication methods using HttpClientBuilder::proxy_authentication.

pub fn max_upload_speed(self, max: u64) -> Self[src]

Set a maximum upload speed for the request body, in bytes per second.

The default is unlimited.

pub fn max_download_speed(self, max: u64) -> Self[src]

Set a maximum download speed for the response body, in bytes per second.

The default is unlimited.

pub fn dns_cache(self, cache: impl Into<DnsCache>) -> Self[src]

Configure DNS caching.

By default, DNS entries are cached by the client executing the request and are used until the entry expires. Calling this method allows you to change the entry timeout duration or disable caching completely.

Note that DNS entry TTLs are not respected, regardless of this setting.

By default caching is enabled with a 60 second timeout.

Examples

let client = HttpClient::builder()
    // Cache entries for 10 seconds.
    .dns_cache(Duration::from_secs(10))
    // Cache entries forever.
    .dns_cache(DnsCache::Forever)
    // Don't cache anything.
    .dns_cache(DnsCache::Disable)
    .build()?;

pub fn dns_servers(self, servers: impl IntoIterator<Item = SocketAddr>) -> Self[src]

Set a list of specific DNS servers to be used for DNS resolution.

By default this option is not set and the system's built-in DNS resolver is used. This option can only be used if libcurl is compiled with c-ares, otherwise this option has no effect.

pub fn ssl_client_certificate(self, certificate: ClientCertificate) -> Self[src]

Set a custom SSL/TLS client certificate to use for all client connections.

If a format is not supported by the underlying SSL/TLS engine, an error will be returned when attempting to send a request using the offending certificate.

The default value is none.

Examples

let client = HttpClient::builder()
    .ssl_client_certificate(ClientCertificate::pem_file(
        "client.pem",
        PrivateKey::pem_file("key.pem", String::from("secret")),
    ))
    .build()?;

pub fn ssl_ca_certificate(self, certificate: CaCertificate) -> Self[src]

Set a custom SSL/TLS CA certificate bundle to use for all client connections.

The default value is none.

Notes

On Windows it may be necessary to combine this with SslOption::DANGER_ACCEPT_REVOKED_CERTS in order to work depending on the contents of your CA bundle.

Examples

let client = HttpClient::builder()
    .ssl_ca_certificate(CaCertificate::file("ca.pem"))
    .build()?;

pub fn ssl_ciphers(self, servers: impl IntoIterator<Item = String>) -> Self[src]

Set a list of ciphers to use for SSL/TLS connections.

The list of valid cipher names is dependent on the underlying SSL/TLS engine in use. You can find an up-to-date list of potential cipher names at https://curl.haxx.se/docs/ssl-ciphers.html.

The default is unset and will result in the system defaults being used.

pub fn ssl_options(self, options: SslOption) -> Self[src]

Set various options that control SSL/TLS behavior.

Most options are for disabling security checks that introduce security risks, but may be required as a last resort. Note that the most secure options are already the default and do not need to be specified.

The default value is SslOption::NONE.

Warning

You should think very carefully before using this method. Using any options that alter how certificates are validated can introduce significant security vulnerabilities.

Examples

let client = HttpClient::builder()
    .ssl_options(SslOption::DANGER_ACCEPT_INVALID_CERTS | SslOption::DANGER_ACCEPT_REVOKED_CERTS)
    .build()?;

pub fn metrics(self, enable: bool) -> Self[src]

Enable comprehensive per-request metrics collection.

When enabled, detailed timing metrics will be tracked while a request is in progress, such as bytes sent and received, estimated size, DNS lookup time, etc. For a complete list of the available metrics that can be inspected, see the Metrics documentation.

When enabled, to access a view of the current metrics values you can use ResponseExt::metrics.

While effort is taken to optimize hot code in metrics collection, it is likely that enabling it will have a small effect on overall throughput. Disabling metrics may be necessary for absolute peak performance.

By default metrics are disabled.

pub fn build(self) -> Result<HttpClient, Error>[src]

Build an HttpClient using the configured options.

If the client fails to initialize, an error will be returned.

Trait Implementations

impl Default for HttpClientBuilder[src]

impl Debug for HttpClientBuilder[src]

Auto Trait Implementations

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]