1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
//! Definition of all configurable client options.

use http;
use std::time::Duration;

/// Defines various protocol and connection options.
#[derive(Clone, Debug, Withers)]
pub struct Options {
    /// The policy for automatically following server redirects.
    ///
    /// The default is to not follow redirects.
    pub redirect_policy: RedirectPolicy,

    /// A preferred HTTP version the client should attempt to use to communicate to the server with.
    ///
    /// This is treated as a suggestion. A different version may be used if the server does not support it or negotiates
    /// a different version.
    ///
    /// The default value is `None` (any version).
    pub preferred_http_version: Option<http::Version>,

    /// A timeout for the maximum time allowed for a request-response cycle.
    ///
    /// The default value is `None` (unlimited).
    pub timeout: Option<Duration>,

    /// A timeout for the initial connection phase.
    ///
    /// The default value is 300 seconds.
    pub connect_timeout: Duration,

    /// Enable or disable TCP keepalive with a given probe interval.
    ///
    /// The default value is `None` (disabled).
    pub tcp_keepalive: Option<Duration>,

    /// Enable or disable the `TCP_NODELAY` option.
    ///
    /// The default value is `false`.
    pub tcp_nodelay: bool,

    /// Set the max buffer size in bytes to use for reading the response body.
    ///
    /// The default value is 8 KiB.
    pub buffer_size: usize,

    /// Indicates whether the `Referer` header should be automatically updated.
    pub auto_referer: bool,

    /// 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.
    pub proxy: Option<http::Uri>,

    /// A maximum upload speed for the request body, in bytes per second.
    ///
    /// The default is unlimited.
    pub max_upload_speed: Option<u64>,

    /// A maximum download speed for the response body, in bytes per second.
    ///
    /// The default is unlimited.
    pub max_download_speed: Option<u64>,
}

impl Default for Options {
    /// Create a new options with the default values.
    fn default() -> Self {
        Self {
            redirect_policy: RedirectPolicy::default(),
            preferred_http_version: None,
            timeout: None,
            connect_timeout: Duration::from_secs(300),
            tcp_keepalive: None,
            tcp_nodelay: false,
            buffer_size: 8192,
            auto_referer: false,
            proxy: None,
            max_upload_speed: None,
            max_download_speed: None,
        }
    }
}

/// Describes a policy for handling server redirects.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RedirectPolicy {
    /// Do not apply any special treatment to redirect responses. The response will be return as-is and redirects will
    /// not be followed.
    ///
    /// This is the default policy.
    None,
    /// Follow all redirects automatically.
    Follow,
    /// Follow redirects automatically up to a maximum number of redirects.
    Limit(u32),
}

impl Default for RedirectPolicy {
    fn default() -> Self {
        RedirectPolicy::None
    }
}