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
442
443
444
445
446
447
//! HTTP client - configuration.
use std::{collections::BTreeMap, num::NonZeroU32, path::Path, str::FromStr, time::Duration};
use reqwest::{
header::{HeaderName, HeaderValue},
ClientBuilder, Identity,
};
use reqwest_middleware::ClientWithMiddleware;
use serde::{Deserialize, Serialize};
use tokio::{fs::OpenOptions, io::AsyncReadExt};
use crate::{
http_client::{
cb::HttpClientCircuitBreakerConfig, errors::HttpClientError, middleware::wrap_client,
},
metrics::ClientMetricsState,
};
/// HTTP client configuration.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
pub struct HttpClientConfig {
/// Path to PEM-formatted file containing a private key and at least one client certificate.
#[serde(
default,
skip_serializing_if = "Option::is_none",
alias = "client_key",
alias = "identity"
)]
pub client_cert: Option<Box<Path>>,
/// Set a timeout for only the connect phase of a client.
///
/// Default is `None`.
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "humantime_serde"
)]
pub connect_timeout: Option<Duration>,
/// Enables a read timeout.
///
/// The timeout applies to each read operation, and resets after a successful read.
/// This is more appropriate for detecting stalled connections when the size isn’t known beforehand.
///
/// Default is no timeout.
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "humantime_serde"
)]
pub read_timeout: Option<Duration>,
/// Enables a total request timeout.
///
/// The timeout is applied from when the request starts connecting until the response body has
/// finished. Also considered a total deadline.
///
/// Default is no timeout.
#[serde(
default,
skip_serializing_if = "Option::is_none",
alias = "timeout",
with = "humantime_serde"
)]
pub request_timeout: Option<Duration>,
/// Set an optional timeout for idle sockets being kept-alive.
///
/// Set to `None` to disable timeout.
///
/// Default is 90 seconds.
#[serde(
default = "HttpClientConfig::default_pool_idle_timeout",
skip_serializing_if = "Option::is_none",
alias = "idle_timeout",
with = "humantime_serde"
)]
pub pool_idle_timeout: Option<Duration>,
/// Sets the maximum idle connection per host allowed in the pool.
#[serde(default = "HttpClientConfig::default_pool_max_idle_per_host")]
pub pool_max_idle_per_host: usize,
/// Set whether connections should emit verbose logs.
///
/// Enabling this option will emit [`log`] messages at the `TRACE` level for read and write
/// operations on connections.
///
/// [`log`]: https://crates.io/crates/log
#[serde(default)]
pub verbose: bool,
/// Sets the default headers for every request.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty", alias = "headers")]
pub extra_headers: BTreeMap<String, String>,
/// Set a redirect policy for this client.
///
/// Default will follow redirects up to a maximum of 10.
#[serde(default)]
pub redirect: HttpClientRedirectPolicy,
/// Enable or disable automatic setting of the `Referer` header.
///
/// Default is `true`.
#[serde(default = "crate::util::default_true", alias = "referrer")]
pub referer: bool,
/// TCP-level configuration.
#[serde(default)]
pub tcp: HttpClientTcpConfig,
/// HTTP/2 protocol configuration.
#[serde(default)]
pub http2: HttpClientHttp2Config,
/// Circuit breaker configuration.
#[serde(
default,
skip_serializing_if = "Option::is_none",
alias = "breaker",
alias = "circuit_breaker"
)]
pub cb: Option<HttpClientCircuitBreakerConfig>,
/// Short application name.
#[serde(skip)]
app_name: Option<String>,
/// Application version.
#[serde(skip)]
app_version: Option<String>,
}
impl Default for HttpClientConfig {
fn default() -> Self {
Self {
client_cert: None,
connect_timeout: None,
read_timeout: None,
request_timeout: None,
pool_idle_timeout: Self::default_pool_idle_timeout(),
pool_max_idle_per_host: Self::default_pool_max_idle_per_host(),
verbose: false,
extra_headers: BTreeMap::new(),
redirect: HttpClientRedirectPolicy::default(),
referer: true,
tcp: HttpClientTcpConfig::default(),
http2: HttpClientHttp2Config::default(),
cb: None,
app_name: None,
app_version: None,
}
}
}
impl HttpClientConfig {
/// Default value for [`Self::pool_idle_timeout`].
#[must_use]
#[inline]
fn default_pool_idle_timeout() -> Option<Duration> {
Some(Duration::from_secs(90))
}
/// Default value for [`Self::pool_max_idle_per_host`].
#[must_use]
#[inline]
fn default_pool_max_idle_per_host() -> usize {
usize::MAX
}
/// Build a value for `User-Agent` header.
#[must_use]
fn user_agent(&self) -> Option<HeaderValue> {
const UXUM_PRODUCT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
if let Some(app_name) = &self.app_name {
let val = if let Some(app_version) = &self.app_version {
let app_product = [app_name.as_str(), app_version.as_str()].join("/");
[&app_product, UXUM_PRODUCT].join(" ")
} else {
[app_name, UXUM_PRODUCT].join(" ")
};
HeaderValue::from_str(&val).ok()
} else {
HeaderValue::from_str(UXUM_PRODUCT).ok()
}
}
/// Set short name of an application.
///
/// Whitespace is not allowed, as this value is used in User-Agent: HTTP header, among other
/// things.
pub fn with_app_name(&mut self, app_name: impl ToString) -> &mut Self {
// TODO: maybe check for value correctness?
self.app_name = Some(app_name.to_string());
self
}
/// Set application version.
///
/// Preferably in semver format. Whitespace is not allowed, as this value is used in Server:
/// HTTP header, among other things.
pub fn with_app_version(&mut self, app_version: impl ToString) -> &mut Self {
// TODO: maybe check for value correctness?
self.app_version = Some(app_version.to_string());
self
}
/// Create [`reqwest::ClientBuilder`] from configuration.
///
/// # Errors
///
/// Returns `Err` if:
/// * Some client configuration is invalid.
/// * Unable to load TLS identity file(s) from filesystem.
pub async fn to_client_builder(&self) -> Result<ClientBuilder, HttpClientError> {
let mut builder = ClientBuilder::new()
.use_rustls_tls()
.min_tls_version(reqwest::tls::Version::TLS_1_2)
.connection_verbose(self.verbose)
.pool_idle_timeout(self.pool_idle_timeout)
.pool_max_idle_per_host(self.pool_max_idle_per_host)
.tls_sni(true)
.redirect(self.redirect.into())
.referer(self.referer)
.tcp_nodelay(self.tcp.nodelay)
.tcp_keepalive(self.tcp.keepalive)
.http2_adaptive_window(self.http2.adaptive_window)
.http2_initial_connection_window_size(
self.http2.initial_connection_window.map(NonZeroU32::get),
)
.http2_initial_stream_window_size(self.http2.initial_stream_window.map(NonZeroU32::get))
.http2_keep_alive_interval(self.http2.keepalive.interval)
.http2_keep_alive_while_idle(self.http2.keepalive.while_idle)
.http2_max_frame_size(self.http2.max_frame_size.map(NonZeroU32::get));
if let Some(client_cert) = &self.client_cert {
builder = builder.identity(load_identity(client_cert).await?);
}
if let Some(connect_timeout) = self.connect_timeout {
builder = builder.connect_timeout(connect_timeout);
}
if let Some(read_timeout) = self.read_timeout {
builder = builder.read_timeout(read_timeout);
}
if let Some(request_timeout) = self.request_timeout {
builder = builder.timeout(request_timeout);
}
if !self.extra_headers.is_empty() {
builder = builder.default_headers(
self.extra_headers
.iter()
.filter_map(|(k, v)| {
Some((
HeaderName::from_str(k).ok()?,
HeaderValue::from_str(v).ok()?,
))
})
.collect(),
);
}
if let Some(timeout) = self.http2.keepalive.timeout {
builder = builder.http2_keep_alive_timeout(timeout);
}
if let Some(user_agent) = self.user_agent() {
builder = builder.user_agent(user_agent);
}
Ok(builder)
}
/// Convert passed client builder into client with all necessary middlewares attached.
///
/// # Errors
///
/// Returns `Err` if:
/// * TLS subsystem cannot be initialized.
/// * DNS resolver fails to load its configuration.
pub fn build_client(
&self,
builder: ClientBuilder,
metrics: Option<ClientMetricsState>,
) -> Result<ClientWithMiddleware, HttpClientError> {
Ok(wrap_client(
builder.build()?,
metrics,
self.cb.as_ref().map(|cb| cb.make_circuit_breaker()),
))
}
/// Build and return configured [`reqwest`] HTTP client.
///
/// # Errors
///
/// Returns `Err` if:
/// * Some client configuration is invalid.
/// * Unable to load TLS identity file(s) from filesystem.
/// * TLS subsystem cannot be initialized.
/// * DNS resolver fails to load its configuration.
pub async fn to_client(
&self,
metrics: Option<ClientMetricsState>,
) -> Result<ClientWithMiddleware, HttpClientError> {
self.build_client(self.to_client_builder().await?, metrics)
}
}
/// TCP-level configuration.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
pub struct HttpClientTcpConfig {
/// Set whether sockets have `TCP_NODELAY` enabled.
///
/// Default is `true`.
#[serde(default = "crate::util::default_true")]
pub nodelay: bool,
/// Set `SO_KEEPALIVE` option for all sockets with the supplied duration.
///
/// If `None`, the option will not be set.
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "humantime_serde"
)]
pub keepalive: Option<Duration>,
}
impl Default for HttpClientTcpConfig {
fn default() -> Self {
Self {
nodelay: true,
keepalive: None,
}
}
}
/// HTTP/2 protocol configuration.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
pub struct HttpClientHttp2Config {
/// Sets whether to use an HTTP/2 adaptive flow control.
///
/// Enabling this will override the limits set in
/// [`Self::initial_stream_window`] and [`Self::initial_connection_window`].
///
/// Default is `false`.
#[serde(default)]
pub adaptive_window: bool,
/// Sets the max connection-level flow control for HTTP/2.
///
/// Default is currently 65535 but may change internally to optimize for common uses.
// TODO: bytesize
#[serde(default, skip_serializing_if = "Option::is_none")]
pub initial_connection_window: Option<NonZeroU32>,
/// Sets the `SETTINGS_INITIAL_WINDOW_SIZE` option for HTTP/2 stream-level flow control.
///
/// Default is currently 65535 but may change internally to optimize for common uses.
// TODO: bytesize
#[serde(default, skip_serializing_if = "Option::is_none")]
pub initial_stream_window: Option<NonZeroU32>,
/// HTTP/2 keep-alive configuration.
#[serde(default)]
pub keepalive: HttpClientHttp2KeepaliveConfig,
/// Sets the maximum frame size to use for HTTP/2.
///
/// Default is currently 16384 but may change internally to optimize for common uses.
// TODO: bytesize
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_frame_size: Option<NonZeroU32>,
}
/// HTTP/2 keepalive configuration.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[non_exhaustive]
pub struct HttpClientHttp2KeepaliveConfig {
/// Sets an interval for HTTP2 Ping frames should be sent to keep a connection alive.
///
/// Pass `None` to disable HTTP2 keep-alive.
/// Default is currently disabled.
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "humantime_serde"
)]
pub interval: Option<Duration>,
/// Sets a timeout for receiving an acknowledgement of the keep-alive ping.
///
/// If the ping is not acknowledged within the timeout, the connection will be closed.
/// Does nothing if [`Self::interval`] is disabled.
/// Default is currently disabled.
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "humantime_serde"
)]
pub timeout: Option<Duration>,
/// Sets whether HTTP2 keep-alive should apply while the connection is idle.
///
/// If disabled, keep-alive pings are only sent while there are open request/responses streams.
/// If enabled, pings are also sent when no streams are active.
/// Does nothing if [`Self::interval`] is disabled.
/// Default is `false`.
#[serde(default)]
pub while_idle: bool,
}
/// HTTP redirect policy.
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum HttpClientRedirectPolicy {
/// No redirects will be followed.
None,
/// Redirects will be followed up to a preconfigured limit.
Limited {
/// Max number of redirects to follow.
#[serde(flatten, default = "HttpClientRedirectPolicy::default_redirect_limit")]
redirect_limit: usize,
},
}
impl Default for HttpClientRedirectPolicy {
fn default() -> Self {
Self::Limited {
redirect_limit: Self::default_redirect_limit(),
}
}
}
impl From<HttpClientRedirectPolicy> for reqwest::redirect::Policy {
fn from(value: HttpClientRedirectPolicy) -> Self {
match value {
HttpClientRedirectPolicy::None => Self::none(),
HttpClientRedirectPolicy::Limited { redirect_limit } => Self::limited(redirect_limit),
}
}
}
impl HttpClientRedirectPolicy {
/// Default value for [`Self::Limited::redirect_limit`].
#[must_use]
#[inline]
fn default_redirect_limit() -> usize {
10
}
}
/// Load client X.509 identity from a local file.
async fn load_identity(pem_file: &Path) -> Result<Identity, HttpClientError> {
let mut pem_buf = Vec::new();
OpenOptions::new()
.read(true)
.open(pem_file)
.await
.map_err(HttpClientError::identity_load)?
.read_to_end(&mut pem_buf)
.await
.map_err(HttpClientError::identity_load)?;
Identity::from_pem(&pem_buf).map_err(Into::into)
}