konfig 0.4.7

serializable, schemable configuration for popular crates
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use std::{
    collections::{BTreeMap, HashSet},
    net::{IpAddr, SocketAddr},
};

use serde_with::TryFromInto;
use url::Url;

use crate::*;
use ::reqwest;

serde! {
    #[derive(Default)]
    pub struct Client {
        #[serde_as(as = "Option<HeaderMap>")]
        pub default_headers: Option<http::HeaderMap>,
        pub cookie_store: Option<bool>,
        pub gzip: Option<bool>,
        pub brotli: Option<bool>,
        pub zstd: Option<bool>,
        pub deflate: Option<bool>,
        pub redirect: Option<usize>,
        pub referer: Option<bool>,
        pub retry: Option<ClientRetry>,
        pub proxies: Option<FalseOr<Vec<Proxy>>>,
        #[serde_as(as = "Option<HumanTime>")]
        pub timeout: Option<Duration>,
        #[serde_as(as = "Option<HumanTime>")]
        pub read_timeout: Option<Duration>,
        #[serde_as(as = "Option<HumanTime>")]
        pub connect_timeout: Option<Duration>,
        pub connection_verbose: Option<bool>,
        #[serde_as(as = "Option<FalseOrWith<HumanTime, Duration>>")]
        pub pool_idle_timeout: Option<FalseOr<Duration>>,
        pub pool_max_idle_per_host: Option<usize>,
        pub http1_title_case_headers: Option<bool>,
        pub http1_allow_obsolete_multiline_headers_in_responses: Option<bool>,
        pub http1_ignore_invalid_headers_in_responses: Option<bool>,
        pub http1_allow_spaces_after_header_name_in_responses: Option<bool>,
        pub http1_only: Option<bool>,
        pub http09_responses: Option<bool>,
        pub http2_prior_knowledge: Option<bool>,
        pub http2_initial_stream_window_size: Option<FalseOr<u32>>,
        pub http2_initial_connection_window_size: Option<FalseOr<u32>>,
        pub http2_adaptive_window: Option<bool>,
        pub http2_max_frame_size: Option<FalseOr<u32>>,
        pub http2_max_list_size: Option<u32>,
        #[serde_as(as = "Option<FalseOrWith<HumanTime, Duration>>")]
        pub http2_keep_alive_interval: Option<FalseOr<Duration>>,
        #[serde_as(as = "Option<HumanTime>")]
        pub http2_keep_alive_timeout: Option<Duration>,
        pub http2_keep_alive_while_idle: Option<bool>,
        pub tcp_nodelay: Option<bool>,
        pub local_address: Option<FalseOr<IpAddr>>,
        pub interface: Option<String>,
        #[serde_as(as = "Option<FalseOrWith<HumanTime, Duration>>")]
        pub tcp_keepalive: Option<FalseOr<Duration>>,
        #[serde_as(as = "Option<FalseOrWith<HumanTime, Duration>>")]
        pub tcp_keepalive_internal: Option<FalseOr<Duration>>,
        pub tcp_keepalive_retries: Option<FalseOr<u32>>,
        #[serde_as(as = "Option<FalseOrWith<HumanTime, Duration>>")]
        pub tcp_user_timeout: Option<FalseOr<Duration>>,
        pub unix_socket: Option<String>,
        pub tls_certs: Option<TlsCerts>,
        pub tls_crls: Option<TlsCrls>,
        pub identity: Option<String>,
        pub tls_danger_accept_invalid_hostnames: Option<bool>,
        pub tls_danger_accept_invalid_certs: Option<bool>,
        pub tls_sni: Option<bool>,
        #[serde_as(as = "Option<TryFromInto<_Version>>")]
        pub tls_version_min: Option<reqwest::tls::Version>,
        #[serde_as(as = "Option<TryFromInto<_Version>>")]
        pub tls_version_max: Option<reqwest::tls::Version>,
        pub tls_info: Option<bool>,
        pub https_only: Option<bool>,
        pub hickory_dns: Option<bool>,
        pub resolve: Option<BTreeMap<String, Vec<SocketAddr>>>,
        pub tls_backend: Option<TlsBackend>
    }

    pub enum TlsCerts {
        Merge(Vec<String>),
        Only(Vec<String>),
    }

    pub enum TlsCrls {
        Only(Vec<String>),
    }

    pub enum TlsBackend {
        Native,
        Rustls
    }

    enum _Version {
        #[serde(rename = "1.0")]
        V1_0,
        #[serde(rename = "1.1")]
        V1_1,
        #[serde(rename = "1.2")]
        V1_2,
        #[serde(rename = "1.3")]
        V1_3,
    }
}

impl Client {
    /// Building the returned [`reqwest::ClientBuilder`] panics unless a
    /// [`rustls::crypto::CryptoProvider`](https://docs.rs/rustls/0.23.43/rustls/crypto/struct.CryptoProvider.html)
    ///
    /// Either install a provider, or add [`reqwest`] with an appropriate feature enabled.
    pub fn builder(self) -> Result<reqwest::ClientBuilder, BoxError> {
        let Self {
            default_headers,
            cookie_store,
            gzip,
            brotli,
            zstd,
            deflate,
            redirect,
            referer,
            retry,
            proxies,
            timeout,
            read_timeout,
            connect_timeout,
            connection_verbose,
            pool_idle_timeout,
            pool_max_idle_per_host,
            http1_title_case_headers,
            http1_allow_obsolete_multiline_headers_in_responses,
            http1_ignore_invalid_headers_in_responses,
            http1_allow_spaces_after_header_name_in_responses,
            http1_only,
            http09_responses,
            http2_prior_knowledge,
            http2_initial_stream_window_size,
            http2_initial_connection_window_size,
            http2_adaptive_window,
            http2_max_frame_size,
            http2_max_list_size,
            http2_keep_alive_interval,
            http2_keep_alive_timeout,
            http2_keep_alive_while_idle,
            tcp_nodelay,
            local_address,
            interface,
            tcp_keepalive,
            tcp_keepalive_internal,
            tcp_keepalive_retries,
            tcp_user_timeout,
            unix_socket,
            tls_certs,
            tls_crls,
            identity,
            tls_danger_accept_invalid_hostnames,
            tls_danger_accept_invalid_certs,
            tls_sni,
            tls_version_min,
            tls_version_max,
            tls_info,
            https_only,
            hickory_dns,
            resolve,
            tls_backend,
        } = self;
        reqwest::ClientBuilder::new()
            .tap_opt(default_headers, |b, v| b.default_headers(v))
            .tap_opt(cookie_store, |b, v| b.cookie_store(v))
            .tap_opt(gzip, |b, v| b.gzip(v))
            .tap_opt(brotli, |b, v| b.brotli(v))
            .try_tap_opt(zstd, |b, v| match v {
                #[cfg(feature = "reqwest-zstd")]
                true => Ok(b.zstd(true)),
                #[cfg(not(feature = "reqwest-zstd"))]
                true => Err(BoxError::from("enabling zstd requires building `konfig` with `reqwest-zstd`")),
                false => Ok::<_, BoxError>(b.no_zstd()),
            })?
            .tap_opt(deflate, |b, v| b.deflate(v))
            .tap_opt(redirect, |b, v| match v {
                0 => b.redirect(reqwest::redirect::Policy::none()),
                v => b.redirect(reqwest::redirect::Policy::limited(v)),
            })
            .tap_opt(referer, |b, v| b.referer(v))
            .try_tap_opt(retry, |b, v| v.builder().map(|v| b.retry(v)))?
            .try_tap_opt(proxies, |b, v| match v {
                FalseOr::False => Ok(b.no_proxy()),
                FalseOr::Or(proxs) => proxs
                    .into_iter()
                    .try_fold(b, |b, prox| prox.build().map(|v| b.proxy(v))),
            })?
            .tap_opt(timeout, |b, v| b.timeout(v))
            .tap_opt(read_timeout, |b, v| b.read_timeout(v))
            .tap_opt(connect_timeout, |b, v| b.connect_timeout(v))
            .tap_opt(connection_verbose, |b, v| b.connection_verbose(v))
            .tap_opt(pool_idle_timeout, |b, v| {
                b.pool_idle_timeout(v.into_option())
            })
            .tap_opt(pool_max_idle_per_host, |b, v| b.pool_max_idle_per_host(v))
            .tap_opt(http1_title_case_headers, |b, v| {
                if v { b.http1_title_case_headers() } else { b }
            })
            .tap_opt(
                http1_allow_obsolete_multiline_headers_in_responses,
                |b, v| b.http1_allow_obsolete_multiline_headers_in_responses(v),
            )
            .tap_opt(http1_ignore_invalid_headers_in_responses, |b, v| {
                b.http1_ignore_invalid_headers_in_responses(v)
            })
            .tap_opt(http1_allow_spaces_after_header_name_in_responses, |b, v| {
                b.http1_allow_spaces_after_header_name_in_responses(v)
            })
            .tap_opt(http1_only, |b, v| if v { b.http1_only() } else { b })
            .tap_opt(
                http09_responses,
                |b, v| {
                    if v { b.http09_responses() } else { b }
                },
            )
            .tap_opt(tcp_nodelay, |b, v| b.tcp_nodelay(v))
            .tap_opt(local_address, |b, v| b.local_address(v.into_option()))
            .tap_opt(interface, |b, v| b.interface(&v))
            .tap_opt(tcp_keepalive, |b, v| b.tcp_keepalive(v.into_option()))
            .tap_opt(tcp_keepalive_internal, |b, v| {
                b.tcp_keepalive_interval(v.into_option())
            })
            .tap_opt(tcp_keepalive_retries, |b, v| {
                b.tcp_keepalive_retries(v.into_option())
            })
            .try_tap_opt::<_, BoxError>(tcp_user_timeout, |b, v| cfg_select! {
                    any(target_os = "android", target_os = "fuchsia", target_os = "linux") => Ok(b.tcp_user_timeout(v.into_option())),
                    _ => match v {
                        FalseOr::False => Ok(b),
                        FalseOr::Or(_) => Err(BoxError::from("cannot enable tcp_user_timeout on this platform"))
                    },
                }
            )?
            .try_tap_opt::<_, BoxError>(unix_socket, |b, v| cfg_select! {
                unix => Ok(b.unix_socket(v)),
                _ => Err(BoxError::from("cannot enable unix_socket on this platform"))
            })?
            .try_tap_opt(tls_certs, |b, v| match v {
                TlsCerts::Merge(v) => v
                    .into_iter()
                    .map(|s| reqwest::tls::Certificate::from_pem(s.as_bytes()))
                    .collect::<Result<Vec<_>, _>>()
                    .map(|v| b.tls_certs_merge(v)),
                TlsCerts::Only(v) => v
                    .into_iter()
                    .map(|s| reqwest::tls::Certificate::from_pem(s.as_bytes()))
                    .collect::<Result<Vec<_>, _>>()
                    .map(|v| b.tls_certs_only(v)),
            })?
            .try_tap_opt(tls_crls, |b, v| match v {
                TlsCrls::Only(v) => v
                    .into_iter()
                    .map(|s| reqwest::tls::CertificateRevocationList::from_pem(s.as_bytes()))
                    .collect::<Result<Vec<_>, _>>()
                    .map(|v| b.tls_crls_only(v)),
            })?
            .try_tap_opt(identity, |b, v| {
                reqwest::tls::Identity::from_pem(v.as_bytes()).map(|v| b.identity(v))
            })?
            .tap_opt(tls_danger_accept_invalid_hostnames, |b, v| {
                b.tls_danger_accept_invalid_hostnames(v)
            })
            .tap_opt(tls_danger_accept_invalid_certs, |b, v| {
                b.tls_danger_accept_invalid_certs(v)
            })
            .tap_opt(tls_sni, |b, v| b.tls_sni(v))
            .tap_opt(tls_version_min, |b, v| b.tls_version_min(v))
            .tap_opt(tls_version_max, |b, v| b.tls_version_max(v))
            .tap_opt(tls_info, |b, v| b.tls_info(v))
            .tap_opt(https_only, |b, v| b.https_only(v))
            .tap_opt(http2_prior_knowledge, |b, v| {
                if v { b.http2_prior_knowledge() } else { b }
            })
            .tap_opt(http2_initial_stream_window_size, |b, v| {
                b.http2_initial_stream_window_size(v.into_option())
            })
            .tap_opt(http2_initial_connection_window_size, |b, v| {
                b.http2_initial_connection_window_size(v.into_option())
            })
            .tap_opt(http2_adaptive_window, |b, v| b.http2_adaptive_window(v))
            .tap_opt(http2_max_frame_size, |b, v| {
                b.http2_max_frame_size(v.into_option())
            })
            .tap_opt(http2_max_list_size, |b, v| b.http2_max_header_list_size(v))
            .tap_opt(http2_keep_alive_interval, |b, v| {
                b.http2_keep_alive_interval(v.into_option())
            })
            .tap_opt(http2_keep_alive_timeout, |b, v| {
                b.http2_keep_alive_timeout(v)
            })
            .tap_opt(http2_keep_alive_while_idle, |b, v| {
                b.http2_keep_alive_while_idle(v)
            })
            .tap_opt(hickory_dns, |b, v| b.hickory_dns(v))
            .tap_opt(resolve, |b, v| {
                v.into_iter()
                    .fold(b, |b, (domain, addrs)| b.resolve_to_addrs(&domain, &addrs))
            })
            .try_tap_opt(tls_backend, |b, v| match v {
                #[cfg(feature = "reqwest-native-tls")]
                TlsBackend::Native => Ok(b.tls_backend_native()),
                #[cfg(not(feature = "reqwest-native-tls"))]
                TlsBackend::Native => Err(BoxError::from(
                    "enabling the native backend requires building `konfig` with `reqwest-native-tls`",
                )),
                TlsBackend::Rustls => Ok::<_, BoxError>(b.tls_backend_rustls()),
            })
    }
}

impl From<_Version> for reqwest::tls::Version {
    fn from(value: _Version) -> Self {
        match value {
            _Version::V1_0 => Self::TLS_1_0,
            _Version::V1_1 => Self::TLS_1_1,
            _Version::V1_2 => Self::TLS_1_2,
            _Version::V1_3 => Self::TLS_1_3,
        }
    }
}

impl TryFrom<reqwest::tls::Version> for _Version {
    type Error = BoxError;
    fn try_from(value: reqwest::tls::Version) -> Result<Self, Self::Error> {
        Ok(match value {
            reqwest::tls::Version::TLS_1_0 => _Version::V1_0,
            reqwest::tls::Version::TLS_1_1 => _Version::V1_1,
            reqwest::tls::Version::TLS_1_2 => _Version::V1_2,
            reqwest::tls::Version::TLS_1_3 => _Version::V1_3,
            other => return Err(BoxError::from(format!("uncovered version {other:?}"))),
        })
    }
}

serde! {
    pub struct ClientRetry {
        pub host: RetryHost,
        pub unlimited: Option<bool>,
        #[serde_as(as = "Option<BoundedFloat<0, 1000>>")]
        pub max_extra_load: Option<f32>,
        pub max_retries_per_request: Option<u32>,
    }

    #[serde(untagged)]
    pub enum RetryHost {
        Host(String),
        Regex { regex: regex::Regex },
        #[serde(rename_all = "kebab-case")]
        RegexSet { regex_set: regex::RegexSet },
        Hosts(HashSet<String>),
    }
}

impl ClientRetry {
    pub fn builder(self) -> Result<reqwest::retry::Builder, BoxError> {
        let Self {
            host,
            unlimited,
            max_extra_load,
            max_retries_per_request,
        } = self;
        let b = match host {
            RetryHost::Host(s) => reqwest::retry::for_host(s),
            RetryHost::Regex { regex } => {
                struct RegexIsMatch(::regex::Regex);
                impl PartialEq<&str> for RegexIsMatch {
                    fn eq(&self, other: &&str) -> bool {
                        self.0.is_match(other)
                    }
                }
                reqwest::retry::for_host(RegexIsMatch(regex.builder().build()?))
            }
            RetryHost::RegexSet { regex_set } => {
                struct RegexSetIsMatch(::regex::RegexSet);
                impl PartialEq<&str> for RegexSetIsMatch {
                    fn eq(&self, other: &&str) -> bool {
                        self.0.is_match(other)
                    }
                }
                reqwest::retry::for_host(RegexSetIsMatch(regex_set.builder().build()?))
            }
            RetryHost::Hosts(items) => {
                struct SetStringIsMatch(HashSet<String>);
                impl PartialEq<&str> for SetStringIsMatch {
                    fn eq(&self, other: &&str) -> bool {
                        self.0.contains(*other)
                    }
                }
                reqwest::retry::for_host(SetStringIsMatch(items))
            }
        };
        Ok(b.tap_opt(max_extra_load, |b, v| b.max_extra_load(v))
            .tap_opt(max_retries_per_request, |b, v| b.max_retries_per_request(v))
            .tap_opt(unlimited, |b, v| match v {
                true => b.no_budget(),
                false => b,
            }))
    }
}

serde! {
    pub struct Proxy {
        pub url: Url,
        pub protocol: ProxyProtocol,
        #[serde_as(as = "Option<HeaderMap>")]
        pub headers: Option<http::HeaderMap>,
        pub no_proxy: Option<NoProxy>,
    }

    pub enum ProxyProtocol {
        Http,
        Https,
        Either,
    }

    #[serde(from = "Untagged<String, FromEnv>", into = "Untagged<String, FromEnv>")]
    #[schemars(with = "Untagged<String, FromEnv>")]
    pub enum NoProxy {
        String(String),
        FromEnv,
    }

    struct FromEnv { from_env: True }
}

convert_enum! {
    NoProxy = Untagged<String, FromEnv> {
        [NoProxy::String(s)] = [Untagged::Left(s)]
        [NoProxy::FromEnv]   = [Untagged::Right(FromEnv { from_env: True })]
    }
}

impl Proxy {
    pub fn build(self) -> reqwest::Result<reqwest::Proxy> {
        let Self {
            url,
            protocol,
            headers,
            no_proxy,
        } = self;
        Ok(match protocol {
            ProxyProtocol::Http => reqwest::Proxy::http(url),
            ProxyProtocol::Https => reqwest::Proxy::https(url),
            ProxyProtocol::Either => reqwest::Proxy::all(url),
        }?
        .tap_opt(headers, |b, v| b.headers(v))
        .tap_opt(no_proxy, |b, v| match v {
            NoProxy::FromEnv => b.no_proxy(reqwest::NoProxy::from_env()),
            NoProxy::String(s) => b.no_proxy(reqwest::NoProxy::from_string(&s)),
        }))
    }
}

#[test]
fn reqwest_client_no_proxy() {
    assert_round_trip::<NoProxy>(
        json!("hello"),
        expect![[r#"
        String(
            "hello",
        )
    "#]],
    );
    assert_round_trip::<NoProxy>(
        json!({"from-env": true}),
        expect![[r#"
        FromEnv
    "#]],
    );
}