hpx 2.4.7

High Performance HTTP Client
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::{num::NonZeroU32, time::Duration};

use crate::{
    Proxy,
    client::{
        conn::TcpConnectOptions,
        layer::{recovery::Recoveries, timeout::TimeoutOptions},
    },
    redirect, retry,
    tls::{CertStore, Identity, KeyLog, TlsVersion},
};

/// HTTP protocol preference for grouped client configuration.
#[derive(Clone, Copy, Default, Eq, PartialEq)]
pub enum HttpVersionPreference {
    /// Only negotiate HTTP/1.
    Http1,
    /// Only negotiate HTTP/2.
    Http2,
    /// Allow the client to negotiate the best supported protocol.
    #[default]
    All,
}

/// Reusable transport-layer settings for a [`crate::ClientBuilder`].
///
/// Applying a grouped config replaces the current transport group on the builder.
#[must_use]
#[derive(Clone)]
pub struct TransportConfigOptions {
    pub(crate) connect_timeout: Option<Duration>,
    pub(crate) connection_verbose: bool,
    pub(crate) tcp_nodelay: bool,
    pub(crate) tcp_reuse_address: bool,
    pub(crate) tcp_keepalive: Option<Duration>,
    pub(crate) tcp_keepalive_interval: Option<Duration>,
    pub(crate) tcp_keepalive_retries: Option<u32>,
    #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
    pub(crate) tcp_user_timeout: Option<Duration>,
    pub(crate) tcp_send_buffer_size: Option<usize>,
    pub(crate) tcp_recv_buffer_size: Option<usize>,
    pub(crate) tcp_happy_eyeballs_timeout: Option<Duration>,
    pub(crate) tcp_connect_options: TcpConnectOptions,
}

impl Default for TransportConfigOptions {
    fn default() -> Self {
        Self {
            connect_timeout: None,
            connection_verbose: false,
            tcp_nodelay: true,
            tcp_reuse_address: false,
            tcp_keepalive: Some(Duration::from_secs(15)),
            tcp_keepalive_interval: Some(Duration::from_secs(15)),
            tcp_keepalive_retries: Some(3),
            #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
            tcp_user_timeout: Some(Duration::from_secs(30)),
            tcp_send_buffer_size: None,
            tcp_recv_buffer_size: None,
            tcp_happy_eyeballs_timeout: Some(Duration::from_millis(300)),
            tcp_connect_options: TcpConnectOptions::default(),
        }
    }
}

impl TransportConfigOptions {
    /// Create a transport config with the client defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the connector timeout used for TCP and TLS establishment.
    pub fn connect_timeout<D>(mut self, timeout: D) -> Self
    where
        D: Into<Option<Duration>>,
    {
        self.connect_timeout = timeout.into();
        self
    }

    /// Enable or disable verbose transport logging.
    pub fn connection_verbose(mut self, verbose: bool) -> Self {
        self.connection_verbose = verbose;
        self
    }

    /// Set whether sockets use `TCP_NODELAY`.
    pub fn tcp_nodelay(mut self, enabled: bool) -> Self {
        self.tcp_nodelay = enabled;
        self
    }

    /// Set whether sockets use `SO_REUSEADDR`.
    pub fn tcp_reuse_address(mut self, enabled: bool) -> Self {
        self.tcp_reuse_address = enabled;
        self
    }

    /// Set the keepalive duration.
    pub fn tcp_keepalive<D>(mut self, value: D) -> Self
    where
        D: Into<Option<Duration>>,
    {
        self.tcp_keepalive = value.into();
        self
    }

    /// Set the keepalive interval.
    pub fn tcp_keepalive_interval<D>(mut self, value: D) -> Self
    where
        D: Into<Option<Duration>>,
    {
        self.tcp_keepalive_interval = value.into();
        self
    }

    /// Set the keepalive retry count.
    pub fn tcp_keepalive_retries<C>(mut self, retries: C) -> Self
    where
        C: Into<Option<u32>>,
    {
        self.tcp_keepalive_retries = retries.into();
        self
    }

    /// Set the Linux-family `TCP_USER_TIMEOUT`.
    #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
    #[cfg_attr(
        docsrs,
        doc(cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux")))
    )]
    pub fn tcp_user_timeout<D>(mut self, value: D) -> Self
    where
        D: Into<Option<Duration>>,
    {
        self.tcp_user_timeout = value.into();
        self
    }

    /// Set the TCP send buffer size.
    pub fn tcp_send_buffer_size<S>(mut self, size: S) -> Self
    where
        S: Into<Option<usize>>,
    {
        self.tcp_send_buffer_size = size.into();
        self
    }

    /// Set the TCP receive buffer size.
    pub fn tcp_recv_buffer_size<S>(mut self, size: S) -> Self
    where
        S: Into<Option<usize>>,
    {
        self.tcp_recv_buffer_size = size.into();
        self
    }

    /// Set the Happy Eyeballs fallback timeout.
    pub fn tcp_happy_eyeballs_timeout<D>(mut self, value: D) -> Self
    where
        D: Into<Option<Duration>>,
    {
        self.tcp_happy_eyeballs_timeout = value.into();
        self
    }

    /// Replace the low-level TCP connect options.
    pub fn tcp_connect_options(mut self, options: TcpConnectOptions) -> Self {
        self.tcp_connect_options = options;
        self
    }
}

/// Reusable connection-pool settings for a [`crate::ClientBuilder`].
#[must_use]
#[derive(Clone)]
pub struct PoolConfigOptions {
    pub(crate) idle_timeout: Option<Duration>,
    pub(crate) max_idle_per_host: usize,
    pub(crate) max_size: Option<NonZeroU32>,
}

impl Default for PoolConfigOptions {
    fn default() -> Self {
        Self {
            idle_timeout: Some(Duration::from_secs(90)),
            max_idle_per_host: usize::MAX,
            max_size: None,
        }
    }
}

impl PoolConfigOptions {
    /// Create a pool config with the client defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the idle timeout for pooled connections.
    pub fn idle_timeout<D>(mut self, value: D) -> Self
    where
        D: Into<Option<Duration>>,
    {
        self.idle_timeout = value.into();
        self
    }

    /// Set the maximum idle connections per host.
    pub fn max_idle_per_host(mut self, max: usize) -> Self {
        self.max_idle_per_host = max;
        self
    }

    /// Set the maximum pool size.
    pub fn max_size(mut self, max: Option<u32>) -> Self {
        self.max_size = max.and_then(NonZeroU32::new);
        self
    }
}

/// Reusable TLS settings for a [`crate::ClientBuilder`].
#[must_use]
#[derive(Clone)]
pub struct TlsConfigOptions {
    pub(crate) keylog: Option<KeyLog>,
    pub(crate) tls_info: bool,
    pub(crate) tls_sni: bool,
    pub(crate) verify_hostname: bool,
    pub(crate) identity: Option<Identity>,
    pub(crate) cert_store: CertStore,
    pub(crate) cert_verification: bool,
    pub(crate) min_version: Option<TlsVersion>,
    pub(crate) max_version: Option<TlsVersion>,
}

impl Default for TlsConfigOptions {
    fn default() -> Self {
        Self {
            keylog: None,
            tls_info: false,
            tls_sni: true,
            verify_hostname: true,
            identity: None,
            cert_store: CertStore::default(),
            cert_verification: true,
            min_version: None,
            max_version: None,
        }
    }
}

impl TlsConfigOptions {
    /// Create a TLS config with the client defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the client identity used for mutual TLS.
    pub fn identity(mut self, identity: Identity) -> Self {
        self.identity = Some(identity);
        self
    }

    /// Replace the certificate store.
    pub fn cert_store(mut self, store: CertStore) -> Self {
        self.cert_store = store;
        self
    }

    /// Enable or disable certificate verification.
    pub fn cert_verification(mut self, enabled: bool) -> Self {
        self.cert_verification = enabled;
        self
    }

    /// Enable or disable hostname verification.
    pub fn verify_hostname(mut self, enabled: bool) -> Self {
        self.verify_hostname = enabled;
        self
    }

    /// Enable or disable SNI.
    pub fn tls_sni(mut self, enabled: bool) -> Self {
        self.tls_sni = enabled;
        self
    }

    /// Enable TLS info extensions on responses.
    pub fn tls_info(mut self, enabled: bool) -> Self {
        self.tls_info = enabled;
        self
    }

    /// Configure TLS key logging.
    pub fn keylog(mut self, keylog: KeyLog) -> Self {
        self.keylog = Some(keylog);
        self
    }

    /// Set the minimum TLS version.
    pub fn min_tls_version(mut self, version: TlsVersion) -> Self {
        self.min_version = Some(version);
        self
    }

    /// Set the maximum TLS version.
    pub fn max_tls_version(mut self, version: TlsVersion) -> Self {
        self.max_version = Some(version);
        self
    }
}

/// Reusable HTTP protocol settings for a [`crate::ClientBuilder`].
#[must_use]
#[derive(Clone)]
pub struct ProtocolConfigOptions {
    pub(crate) http_version_preference: HttpVersionPreference,
    pub(crate) https_only: bool,
    pub(crate) retry_policy: retry::Policy,
    pub(crate) redirect_policy: redirect::Policy,
    pub(crate) referer: bool,
    pub(crate) timeout_options: TimeoutOptions,
    pub(crate) recoveries: Recoveries,
}

impl Default for ProtocolConfigOptions {
    fn default() -> Self {
        Self {
            http_version_preference: HttpVersionPreference::All,
            https_only: false,
            retry_policy: retry::Policy::default(),
            redirect_policy: redirect::Policy::none(),
            referer: true,
            timeout_options: TimeoutOptions::default(),
            recoveries: Recoveries::new(),
        }
    }
}

impl ProtocolConfigOptions {
    /// Create a protocol config with the client defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the preferred HTTP version.
    pub fn http_version_preference(mut self, preference: HttpVersionPreference) -> Self {
        self.http_version_preference = preference;
        self
    }

    /// Restrict the client to HTTPS requests only.
    pub fn https_only(mut self, enabled: bool) -> Self {
        self.https_only = enabled;
        self
    }

    /// Set the retry policy.
    pub fn retry_policy(mut self, policy: retry::Policy) -> Self {
        self.retry_policy = policy;
        self
    }

    /// Set the redirect policy.
    pub fn redirect_policy(mut self, policy: redirect::Policy) -> Self {
        self.redirect_policy = policy;
        self
    }

    /// Enable or disable automatic referer propagation.
    pub fn referer(mut self, enabled: bool) -> Self {
        self.referer = enabled;
        self
    }

    /// Replace the grouped timeout settings.
    pub fn timeout_options(mut self, options: TimeoutOptions) -> Self {
        self.timeout_options = options;
        self
    }

    /// Replace the grouped recovery hooks.
    pub fn recoveries(mut self, recoveries: Recoveries) -> Self {
        self.recoveries = recoveries;
        self
    }
}

/// Reusable proxy settings for a [`crate::ClientBuilder`].
#[must_use]
#[derive(Clone)]
pub struct ProxyConfigOptions {
    pub(crate) proxies: Vec<Proxy>,
    pub(crate) auto_system_proxy: bool,
}

impl Default for ProxyConfigOptions {
    fn default() -> Self {
        Self::new()
    }
}

impl ProxyConfigOptions {
    /// Create a proxy config with the client defaults.
    pub fn new() -> Self {
        Self {
            proxies: Vec::new(),
            auto_system_proxy: true,
        }
    }

    /// Enable or disable automatic system proxy detection.
    pub fn system_proxy(mut self, enabled: bool) -> Self {
        self.auto_system_proxy = enabled;
        self
    }

    /// Add a proxy rule.
    pub fn proxy(mut self, proxy: Proxy) -> Self {
        self.proxies.push(proxy);
        self.auto_system_proxy = false;
        self
    }

    /// Replace the proxy list.
    pub fn proxies<I>(mut self, proxies: I) -> Self
    where
        I: IntoIterator<Item = Proxy>,
    {
        self.proxies = proxies.into_iter().collect();
        self.auto_system_proxy = false;
        self
    }

    /// Clear all proxy rules and disable system proxy detection.
    pub fn no_proxy(mut self) -> Self {
        self.proxies.clear();
        self.auto_system_proxy = false;
        self
    }
}