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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! Definition of all configurable client options.
use std::net::SocketAddr;
use std::path::PathBuf;
use std::time::Duration;
/// Defines various protocol and connection options.
#[derive(Clone, Debug, withers_derive::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 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](https://c-ares.haxx.se), otherwise this option has no
/// effect.
pub dns_servers: Option<Vec<SocketAddr>>,
/// 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>,
/// 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 ssl_ciphers: Option<Vec<String>>,
/// 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
///
/// ```
/// # use chttp::options::*;
/// let cert = ClientCertificate::PEM {
/// path: "client.pem".into(),
/// private_key: Some(PrivateKey::PEM {
/// path: "key.pem".into(),
/// password: Some("secret".into()),
/// }),
/// };
/// let options = Options::default()
/// .with_ssl_client_certificate(Some(cert));
/// ```
pub ssl_client_certificate: Option<ClientCertificate>,
}
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,
dns_servers: None,
max_upload_speed: None,
max_download_speed: None,
ssl_ciphers: None,
ssl_client_certificate: 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 returned 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
}
}
/// A public key certificate file.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ClientCertificate {
/// A PEM-encoded certificate file.
PEM {
/// Path to the certificate file.
path: PathBuf,
/// Private key corresponding to the SSL/TLS certificate.
private_key: Option<PrivateKey>,
},
/// A DER-encoded certificate file.
DER {
/// Path to the certificate file.
path: PathBuf,
/// Private key corresponding to the SSL/TLS certificate.
private_key: Option<PrivateKey>,
},
/// A PKCS#12-encoded certificate file.
P12 {
/// Path to the certificate file.
path: PathBuf,
/// Password to decrypt the certificate file.
password: Option<String>,
},
}
/// A private key file.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PrivateKey {
PEM {
/// Path to the key file.
path: PathBuf,
/// Password to decrypt the key file.
password: Option<String>,
},
DER {
/// Path to the key file.
path: PathBuf,
/// Password to decrypt the key file.
password: Option<String>,
},
}