Trait isahc::config::Configurable[][src]

pub trait Configurable: WithRequestConfig {
Show 28 methods fn cookie_jar(self, cookie_jar: CookieJar) -> Self; fn timeout(self, timeout: Duration) -> Self { ... }
fn connect_timeout(self, timeout: Duration) -> Self { ... }
fn low_speed_timeout(self, low_speed: u32, timeout: Duration) -> Self { ... }
fn version_negotiation(self, negotiation: VersionNegotiation) -> Self { ... }
fn redirect_policy(self, policy: RedirectPolicy) -> Self { ... }
fn auto_referer(self) -> Self { ... }
fn automatic_decompression(self, decompress: bool) -> Self { ... }
fn expect_continue<T>(self, expect: T) -> Self
    where
        T: Into<ExpectContinue>
, { ... }
fn authentication(self, authentication: Authentication) -> Self { ... }
fn credentials(self, credentials: Credentials) -> Self { ... }
fn tcp_keepalive(self, interval: Duration) -> Self { ... }
fn tcp_nodelay(self) -> Self { ... }
fn interface<I>(self, interface: I) -> Self
    where
        I: Into<NetworkInterface>
, { ... }
fn ip_version(self, version: IpVersion) -> Self { ... }
fn dial<D>(self, dialer: D) -> Self
    where
        D: Into<Dialer>
, { ... }
fn proxy(self, proxy: impl Into<Option<Uri>>) -> Self { ... }
fn proxy_blacklist<I, T>(self, hosts: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<String>
, { ... }
fn proxy_authentication(self, authentication: Authentication) -> Self { ... }
fn proxy_credentials(self, credentials: Credentials) -> Self { ... }
fn max_upload_speed(self, max: u64) -> Self { ... }
fn max_download_speed(self, max: u64) -> Self { ... }
fn ssl_client_certificate(self, certificate: ClientCertificate) -> Self { ... }
fn ssl_ca_certificate(self, certificate: CaCertificate) -> Self { ... }
fn ssl_ciphers<I, T>(self, ciphers: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<String>
, { ... }
fn ssl_options(self, options: SslOption) -> Self { ... }
fn title_case_headers(self, enable: bool) -> Self { ... }
fn metrics(self, enable: bool) -> Self { ... }
}
Expand description

Provides additional methods when building a request for configuring various execution-related options on how the request should be sent.

This trait can be used to either configure requests individually by invoking them on an http::request::Builder, or to configure the default settings for an HttpClient by invoking them on an HttpClientBuilder.

This trait is sealed and cannot be implemented for types outside of Isahc.

Required methods

Set a cookie jar to use to accept, store, and supply cookies for incoming responses and outgoing requests.

A cookie jar can be shared across multiple requests or with an entire client, allowing cookies to be persisted across multiple requests.

Availability

This method is only available when the cookies feature is enabled.

Provided methods

Specify a maximum amount of time that a complete request/response cycle is allowed to take before being aborted. This includes DNS resolution, connecting to the server, writing the request, and reading the response.

Since response bodies are streamed, you will likely receive a Response before the response body stream has been fully consumed. This means that the configured timeout will still be active for that request, and if it expires, further attempts to read from the stream will return a TimedOut I/O error.

This also means that if you receive a response with a body but do not immediately start reading from it, then the timeout timer will still be active and may expire before you even attempt to read the body. Keep this in mind when consuming responses and consider handling the response body right after you receive it if you are using this option.

If not set, no timeout will be enforced.

Examples

use isahc::{prelude::*, Request};
use std::time::Duration;

// This page is too slow and won't respond in time.
let response = Request::get("https://httpbin.org/delay/10")
    .timeout(Duration::from_secs(5))
    .body(())?
    .send()
    .expect_err("page should time out");

Set a timeout for establishing connections to a host.

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

Specify a maximum amount of time where transfer rate can go below a minimum speed limit. low_speed is that limit in bytes/s.

If not set, no low speed limits are imposed.

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

The default is VersionNegotiation::latest_compatible.

Examples

use isahc::{
    config::VersionNegotiation,
    prelude::*,
    HttpClient,
};

// Never use anything newer than HTTP/1.x for this client.
let http11_client = HttpClient::builder()
    .version_negotiation(VersionNegotiation::http11())
    .build()?;

// HTTP/2 with prior knowledge.
let http2_client = HttpClient::builder()
    .version_negotiation(VersionNegotiation::http2())
    .build()?;

Set a policy for automatically following server redirects.

The default is to not follow redirects.

Examples

use isahc::{config::RedirectPolicy, prelude::*, Request};

// This URL redirects us to where we want to go.
let response = Request::get("https://httpbin.org/redirect/1")
    .redirect_policy(RedirectPolicy::Follow)
    .body(())?
    .send()?;

// This URL redirects too much!
let error = Request::get("https://httpbin.org/redirect/10")
    .redirect_policy(RedirectPolicy::Limit(5))
    .body(())?
    .send()
    .expect_err("too many redirects");

Update the Referer header automatically when following redirects.

Enable or disable automatic decompression of the response body for various compression algorithms as returned by the server in the Content-Encoding response header.

If set to true (the default), Isahc will automatically and transparently decode the HTTP response body for known and available compression algorithms. If the server returns a response with an unknown or unavailable encoding, Isahc will return an InvalidContentEncoding error.

If you do not specify a specific value for the Accept-Encoding header, Isahc will set one for you automatically based on this option.

Configure the use of the Expect request header when sending request bodies with HTTP/1.1.

By default, when sending requests containing a body of large or unknown length over HTTP/1.1, Isahc will send the request headers first without the body and wait for the server to respond with a 100 (Continue) status code, as defined by RFC 7231, Section 5.1.1. This gives the opportunity for the server to reject the response without needing to first transmit the request body over the network, if the body contents are not necessary for the server to determine an appropriate response.

For servers that do not support this behavior and instead simply wait for the request body without responding with a 100 (Continue), there is a limited timeout before the response body is sent anyway without confirmation. The default timeout is 1 second, but this can be configured.

The Expect behavior can also be disabled entirely.

This configuration only takes effect when using HTTP/1.1.

Examples

use std::time::Duration;
use isahc::{
    config::ExpectContinue,
    prelude::*,
    HttpClient,
};

// Use the default behavior (enabled).
let client = HttpClient::builder()
    .expect_continue(ExpectContinue::enabled())
    // or equivalently...
    .expect_continue(ExpectContinue::default())
    // or equivalently...
    .expect_continue(true)
    .build()?;

// Customize the timeout if the server doesn't respond with a 100
// (Continue) status.
let client = HttpClient::builder()
    .expect_continue(ExpectContinue::timeout(Duration::from_millis(200)))
    // or equivalently...
    .expect_continue(Duration::from_millis(200))
    .build()?;

// Disable the Expect header entirely.
let client = HttpClient::builder()
    .expect_continue(ExpectContinue::disabled())
    // or equivalently...
    .expect_continue(false)
    .build()?;

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 Configurable::credentials.

Examples

use isahc::{
    auth::{Authentication, Credentials},
    prelude::*,
    HttpClient,
};

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

Set the credentials to use for HTTP authentication.

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

Enable TCP keepalive with a given probe interval.

Enables the TCP_NODELAY option on connect.

Bind local socket connections to a particular network interface.

Examples

Bind to an IP address.

use isahc::{
    prelude::*,
    config::NetworkInterface,
    HttpClient,
    Request,
};
use std::net::IpAddr;

// Bind to an IP address.
let client = HttpClient::builder()
    .interface(IpAddr::from([192, 168, 1, 2]))
    .build()?;

// Bind to an interface by name (not supported on Windows).
let client = HttpClient::builder()
    .interface(NetworkInterface::name("eth0"))
    .build()?;

// Reset to using whatever interface the TCP stack finds suitable (the
// default).
let request = Request::get("https://example.org")
    .interface(NetworkInterface::any())
    .body(())?;

Select a specific IP version when resolving hostnames. If a given hostname does not resolve to an IP address of the desired version, then the request will fail with a connection error.

This does not affect requests with an explicit IP address as the host.

The default is IpVersion::Any.

Specify a socket to connect to instead of the using the host and port defined in the request URI.

Examples

Connecting to a Unix socket:

use isahc::{
    config::Dialer,
    prelude::*,
    Request,
};

let request = Request::get("http://localhost/containers")
    .dial(Dialer::unix_socket("/path/to/my.sock"))
    .body(())?;

Connecting to a specific Internet socket address:

use isahc::{
    config::Dialer,
    prelude::*,
    Request,
};
use std::net::Ipv4Addr;

let request = Request::get("http://exmaple.org")
    // Actually issue the request to localhost on port 8080. The host
    // header will remain unchanged.
    .dial(Dialer::ip_socket((Ipv4Addr::LOCALHOST, 8080)))
    .body(())?;

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 no proxy will be used, unless one is specified in either the http_proxy or https_proxy environment variables.

Setting to None explicitly disables the use of a proxy.

Examples

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

use isahc::{prelude::*, HttpClient};

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

Explicitly disable the use of a proxy:

use isahc::{prelude::*, HttpClient};

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

Disable proxy usage for the provided list of hosts.

Examples

use isahc::{prelude::*, HttpClient};

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

Set one or more 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 Configurable::proxy_credentials.

Examples

use isahc::{
    auth::{Authentication, Credentials},
    prelude::*,
    HttpClient,
};

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

Set the credentials to use for proxy authentication.

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

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

The default is unlimited.

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

The default is unlimited.

Set a custom SSL/TLS client certificate to use for 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

use isahc::{
    config::{ClientCertificate, PrivateKey},
    prelude::*,
    Request,
};

let response = Request::get("localhost:3999")
    .ssl_client_certificate(ClientCertificate::pem_file(
        "client.pem",
        PrivateKey::pem_file("key.pem", String::from("secret")),
    ))
    .body(())?
    .send()?;
use isahc::{
    config::{ClientCertificate, PrivateKey},
    prelude::*,
    HttpClient,
};

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

Set a custom SSL/TLS CA certificate bundle to use for 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

use isahc::{config::CaCertificate, prelude::*, HttpClient};

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

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.

Set various options for this request 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

use isahc::{config::SslOption, prelude::*, Request};

let response = Request::get("https://badssl.com")
    .ssl_options(SslOption::DANGER_ACCEPT_INVALID_CERTS | SslOption::DANGER_ACCEPT_REVOKED_CERTS)
    .body(())?
    .send()?;
use isahc::{config::SslOption, prelude::*, HttpClient};

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

Enable or disable sending HTTP header names in Title-Case instead of lowercase form.

This option only affects user-supplied headers and does not affect low-level headers that are automatically supplied for HTTP protocol details, such as Connection and Host (unless you override such a header yourself).

This option has no effect when using HTTP/2 or newer where headers are required to be lowercase.

Enable or disable 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.

Implementations on Foreign Types

Implementors