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
use std::{fmt, time::Duration};
use base64::{Engine, engine::general_purpose::STANDARD as base64};
use crate::http::header::{self, HeaderName, HeaderValue};
use crate::http::{HeaderMap, error::HttpError};
use crate::service::cfg::{CfgContext, Configuration};
use crate::time::{Millis, Seconds};
#[derive(Debug)]
/// Runtime configuration for an HTTP [`Client`](super::Client).
///
/// The configuration is stored in [`SharedCfg`](crate::SharedCfg) and can be
/// supplied before constructing a client with [`Client::with_config`](super::Client::with_config)
/// or [`ClientBuilder::build`](super::ClientBuilder::build).
pub struct ClientConfig {
pub(super) headers: HeaderMap,
pub(super) timeout: Millis,
pub(super) pl_limit: usize,
pub(super) pl_timeout: Millis,
pub(super) h1_lifetime: Duration,
pub(super) h1_keep_alive: Duration,
pub(super) h1_limit: usize,
pub(super) h2_lifetime: Duration,
pub(super) h2_keep_alive: Duration,
pub(super) h2_limit: usize,
pub(super) h2_max_streams: u32,
config: CfgContext,
}
impl Default for ClientConfig {
fn default() -> Self {
Self::new()
}
}
impl Configuration for ClientConfig {
const NAME: &str = "Http client configuration";
fn ctx(&self) -> &CfgContext {
&self.config
}
fn set_ctx(&mut self, ctx: CfgContext) {
self.config = ctx;
}
}
impl ClientConfig {
#[must_use]
/// Creates an HTTP client configuration with default values.
pub fn new() -> ClientConfig {
ClientConfig {
headers: HeaderMap::new(),
timeout: Millis(5_000),
pl_limit: 262_144,
pl_timeout: Millis(10_000),
h1_lifetime: Duration::from_secs(75),
h1_keep_alive: Duration::from_secs(15),
h1_limit: 8,
h2_lifetime: Duration::from_hours(1),
h2_keep_alive: Duration::from_mins(1),
h2_limit: 16,
h2_max_streams: 100,
config: CfgContext::default(),
}
}
/// Returns the headers added to every request.
pub fn headers(&self) -> &HeaderMap {
&self.headers
}
/// Returns the response-header timeout.
pub fn response_timeout(&self) -> Millis {
self.timeout
}
/// Returns the maximum response payload size.
///
/// A value of zero disables the limit.
pub fn response_payload_limit(&self) -> usize {
self.pl_limit
}
/// Returns the timeout for reading a complete response payload.
pub fn response_payload_timeout(&self) -> Millis {
self.pl_timeout
}
/// Returns the maximum number of simultaneous HTTP/1 connections per connection pool.
pub fn h1_connection_limit(&self) -> usize {
self.h1_limit
}
/// Returns the maximum number of HTTP/2 connections per host.
pub fn h2_connection_limit(&self) -> usize {
self.h2_limit
}
/// Returns the maximum number of concurrent requests per HTTP/2 connection.
pub fn h2_max_streams(&self) -> u32 {
self.h2_max_streams
}
/// Returns the keep-alive period for idle HTTP/2 connections.
pub fn h2_keepalive(&self) -> Seconds {
Seconds(self.h2_keep_alive.as_secs().try_into().unwrap_or(u16::MAX))
}
/// Returns the maximum lifetime of an HTTP/2 connection.
pub fn h2_lifetime(&self) -> Seconds {
Seconds(self.h2_lifetime.as_secs().try_into().unwrap_or(u16::MAX))
}
#[must_use]
/// Sets the maximum number of simultaneous connections per connection pool.
///
/// The limit is shared by all hosts. A client keeps separate pools for
/// plain and TLS connections, and each pool has its own limit. The limit
/// counts HTTP/1 connections in use and connections being opened;
/// HTTP/2 connections are limited by
/// [`set_h2_connection_limit`](Self::set_h2_connection_limit). A value of
/// zero disables the limit. The default is 8.
pub fn set_h1_connection_limit(mut self, limit: usize) -> Self {
self.h1_limit = limit;
self
}
#[must_use]
/// Sets the keep-alive period for idle pooled HTTP/1 connections.
///
/// HTTP/2 connections use [`set_h2_keepalive`](Self::set_h2_keepalive).
/// A pooled connection that has been idle longer than this period is not
/// reused. Expiration is checked lazily, when a connection for the same
/// host is next requested; the expired connection is closed at that point.
/// A zero duration disables the idle check; use
/// [`ClientRequest::force_close`](super::ClientRequest::force_close) to
/// avoid reusing a connection. The default is 15 seconds.
pub fn set_h1_keepalive<T: Into<Seconds>>(mut self, dur: T) -> Self {
self.h1_keep_alive = dur.into().into();
self
}
#[must_use]
/// Sets the maximum lifetime of a pooled HTTP/1 connection.
///
/// HTTP/2 connections use [`set_h2_lifetime`](Self::set_h2_lifetime).
/// A connection older than this period is not reused, regardless of how
/// recently it was used. Like the keep-alive period, this is checked when a
/// connection for the same host is next requested. A zero duration disables
/// the limit. The default is 75 seconds.
pub fn set_h1_lifetime<T: Into<Seconds>>(mut self, dur: T) -> Self {
self.h1_lifetime = dur.into().into();
self
}
#[must_use]
/// Sets the maximum number of HTTP/2 connections per host.
///
/// HTTP/2 connections are shared by concurrent requests. A new connection
/// to a host is opened only when every existing HTTP/2 connection to that
/// host has reached its stream limit, see
/// [`set_h2_max_streams`](Self::set_h2_max_streams). When the limit is
/// reached, requests wait for a free stream.
///
/// Requests on established HTTP/2 connections do not count against
/// [`set_h1_connection_limit`](Self::set_h1_connection_limit); opening a new
/// connection does, because the protocol is not known until the
/// connection is established. A value of zero disables the limit.
/// The default is 16.
pub fn set_h2_connection_limit(mut self, limit: usize) -> Self {
self.h2_limit = limit;
self
}
#[must_use]
/// Sets the maximum number of concurrent requests per HTTP/2 connection.
///
/// The peer's `SETTINGS_MAX_CONCURRENT_STREAMS` also applies; the lower of
/// the two is used. A value of zero uses only the peer's setting.
/// The default is 100.
pub fn set_h2_max_streams(mut self, limit: u32) -> Self {
self.h2_max_streams = limit;
self
}
#[must_use]
/// Sets the keep-alive period for idle HTTP/2 connections.
///
/// An HTTP/2 connection is idle when it has no in-flight requests; the
/// period is measured from the completion of its last request, including
/// the response payload. An idle connection older than this period is
/// closed when a connection for the same host is next requested.
/// A zero duration disables the idle check. The default is 60 seconds.
pub fn set_h2_keepalive<T: Into<Seconds>>(mut self, dur: T) -> Self {
self.h2_keep_alive = dur.into().into();
self
}
#[must_use]
/// Sets the maximum lifetime of an HTTP/2 connection.
///
/// An HTTP/2 connection older than this period is not used for new
/// requests and is closed gracefully, after its in-flight requests
/// complete. This is checked when a connection for the same host is next
/// requested. A zero duration disables the limit. The default is 1 hour.
pub fn set_h2_lifetime<T: Into<Seconds>>(mut self, dur: T) -> Self {
self.h2_lifetime = dur.into().into();
self
}
#[must_use]
/// Sets the response-header timeout.
///
/// The timeout covers receiving the response head after the request has
/// been sent. Connecting and sending the request are not included. A zero
/// duration disables the timeout. The default is 5 seconds.
pub fn set_response_timeout<T: Into<Millis>>(mut self, timeout: T) -> Self {
self.timeout = timeout.into();
self
}
#[must_use]
/// Disables the response-header timeout.
///
/// This is the same as `set_response_timeout(Millis::ZERO)`.
pub fn disable_timeout(mut self) -> Self {
self.timeout = Millis::ZERO;
self
}
#[must_use]
/// Sets the maximum size of a buffered response payload.
///
/// The default is 256 KiB. A value of zero disables the limit.
pub fn set_response_payload_limit(mut self, limit: usize) -> Self {
self.pl_limit = limit;
self
}
#[must_use]
/// Sets the timeout for reading a complete response payload.
///
/// The default is 10 seconds. A zero duration disables the timeout.
pub fn set_response_payload_timeout<T: Into<Millis>>(mut self, timeout: T) -> Self {
self.pl_timeout = timeout.into();
self
}
/// Adds a header to every request.
///
/// A request-specific header with the same name takes precedence.
pub fn set_header<K, V>(mut self, key: K, value: V) -> Result<Self, HttpError>
where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
{
let key = HeaderName::try_from(key).map_err(Into::into)?;
let value = HeaderValue::try_from(value).map_err(Into::into)?;
self.headers.append(key, value);
Ok(self)
}
fn insert_header(mut self, key: HeaderName, value: String) -> Result<Self, HttpError> {
let value = HeaderValue::try_from(value).map_err(HttpError::from)?;
self.headers.insert(key, value);
Ok(self)
}
/// Sets a client-wide HTTP Basic authentication header.
///
/// Replaces any previously configured `Authorization` header.
pub fn set_basic_auth<U>(self, username: U, password: Option<&str>) -> Result<Self, HttpError>
where
U: fmt::Display,
{
let auth = match password {
Some(password) => format!("{username}:{password}"),
None => format!("{username}:"),
};
self.insert_header(
header::AUTHORIZATION,
format!("Basic {}", base64.encode(auth)),
)
}
/// Sets a client-wide HTTP Bearer authentication header.
///
/// Replaces any previously configured `Authorization` header.
pub fn set_bearer_auth<T>(self, token: T) -> Result<Self, HttpError>
where
T: fmt::Display,
{
self.insert_header(header::AUTHORIZATION, format!("Bearer {token}"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basics() {
let cfg = ClientConfig::new().disable_timeout();
assert_eq!(cfg.timeout, Millis::ZERO);
}
#[test]
fn h2_settings() {
let cfg = ClientConfig::new();
assert_eq!(cfg.h2_connection_limit(), 16);
assert_eq!(cfg.h2_max_streams(), 100);
assert_eq!(cfg.h2_keepalive(), Seconds(60));
assert_eq!(cfg.h2_lifetime(), Seconds(3600));
let cfg = cfg
.set_h2_connection_limit(2)
.set_h2_max_streams(10)
.set_h2_keepalive(Seconds(5))
.set_h2_lifetime(Seconds(50));
assert_eq!(cfg.h2_connection_limit(), 2);
assert_eq!(cfg.h2_max_streams(), 10);
assert_eq!(cfg.h2_keepalive(), Seconds(5));
assert_eq!(cfg.h2_lifetime(), Seconds(50));
// http/1 settings are not affected
assert_eq!(cfg.h1_connection_limit(), 8);
assert_eq!(cfg.h1_keep_alive, Duration::from_secs(15));
assert_eq!(cfg.h1_lifetime, Duration::from_secs(75));
}
#[test]
fn response_payload_limit() {
let cfg = ClientConfig::new();
assert_eq!(cfg.pl_limit, 262_144);
let cfg = cfg.set_response_payload_limit(10);
assert_eq!(cfg.pl_limit, 10);
}
#[test]
fn response_payload_timeout() {
let cfg = ClientConfig::default();
assert_eq!(cfg.pl_timeout, Millis(10_000));
let cfg = cfg.set_response_payload_timeout(Millis(10));
assert_eq!(cfg.pl_timeout, Millis(10));
}
#[test]
fn valid_header_name() {
let cfg = ClientConfig::new().set_header("Content-Length", 1).unwrap();
assert!(cfg.headers.contains_key("Content-Length"));
}
#[test]
fn invalid_header_name() {
let res = ClientConfig::new().set_header("no valid header name", 1);
assert!(res.is_err());
}
#[test]
fn valid_header_value() {
let valid_header_value = HeaderValue::from(1234);
let cfg = ClientConfig::new()
.set_header("Content-Length", &valid_header_value)
.unwrap();
assert_eq!(cfg.headers.get("Content-Length"), Some(&valid_header_value));
}
#[test]
fn invalid_header_value() {
let res = ClientConfig::new()
.set_header("Content-Length", "\n")
.is_err();
assert!(res);
}
#[test]
fn client_basic_auth() {
let cfg = ClientConfig::new()
.set_basic_auth("username", Some("password"))
.unwrap();
assert_eq!(
cfg.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Basic dXNlcm5hbWU6cGFzc3dvcmQ="
);
let cfg = ClientConfig::new()
.set_basic_auth("username", None)
.unwrap();
assert_eq!(
cfg.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Basic dXNlcm5hbWU6"
);
}
#[test]
fn client_bearer_auth() {
let cfg = ClientConfig::new()
.set_bearer_auth("someS3cr3tAutht0k3n")
.unwrap();
assert_eq!(
cfg.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Bearer someS3cr3tAutht0k3n"
);
}
}