openwire 0.1.1

OkHttp-inspired async HTTP client for Rust built on hyper and tower
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
use std::sync::Arc;

use http::StatusCode;
use openwire_core::{
    EstablishmentStage, FailurePhase, RedirectContext, RedirectDecision, RedirectPolicy,
    ResponseRetryContext, RetryAfter, RetryContext, RetryPolicy, WireError, WireErrorKind,
};

#[derive(Clone, Default)]
pub(crate) struct RetryPolicyConfig {
    default: DefaultRetryPolicy,
    custom: Option<Arc<dyn RetryPolicy>>,
}

impl RetryPolicyConfig {
    pub(crate) fn default_config(&self) -> &DefaultRetryPolicy {
        &self.default
    }

    pub(crate) fn has_custom_policy(&self) -> bool {
        self.custom.is_some()
    }

    pub(crate) fn policy(&self) -> &dyn RetryPolicy {
        self.custom
            .as_deref()
            .map(|policy| policy as &dyn RetryPolicy)
            .unwrap_or(&self.default)
    }

    pub(crate) fn default_mut(&mut self) -> &mut DefaultRetryPolicy {
        self.custom = None;
        &mut self.default
    }

    pub(crate) fn set_custom<P>(&mut self, policy: P)
    where
        P: RetryPolicy,
    {
        self.custom = Some(Arc::new(policy));
    }
}

#[derive(Clone, Debug)]
pub struct DefaultRetryPolicy {
    retry_on_connection_failure: bool,
    max_retries: usize,
    retry_canceled_requests: bool,
}

impl DefaultRetryPolicy {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn retry_on_connection_failure(&self) -> bool {
        self.retry_on_connection_failure
    }

    pub fn set_retry_on_connection_failure(&mut self, enabled: bool) {
        self.retry_on_connection_failure = enabled;
    }

    pub fn max_retries(&self) -> usize {
        self.max_retries
    }

    pub fn set_max_retries(&mut self, max_retries: usize) {
        self.max_retries = max_retries;
    }

    pub fn retry_canceled_requests(&self) -> bool {
        self.retry_canceled_requests
    }

    pub fn set_retry_canceled_requests(&mut self, enabled: bool) {
        self.retry_canceled_requests = enabled;
    }
}

impl Default for DefaultRetryPolicy {
    fn default() -> Self {
        Self {
            retry_on_connection_failure: true,
            max_retries: 1,
            retry_canceled_requests: false,
        }
    }
}

impl RetryPolicy for DefaultRetryPolicy {
    fn should_retry(&self, ctx: &RetryContext<'_>) -> Option<&'static str> {
        if !ctx.is_body_replayable() || ctx.attempt() as usize >= self.max_retries {
            return None;
        }

        let error = ctx.error();
        if error.request_committed() && !ctx.request_method().is_idempotent() {
            return None;
        }

        match (error.kind(), error.phase()) {
            (WireErrorKind::Dns, FailurePhase::Dns) if self.retry_on_connection_failure => {
                return Some("dns");
            }
            (WireErrorKind::Connect, FailurePhase::Tcp)
                if self.retry_on_connection_failure && !error.is_non_retryable_connect() =>
            {
                return Some("connect");
            }
            (WireErrorKind::Connect, FailurePhase::ProxyTunnel)
                if self.retry_on_connection_failure && error.is_retryable_establishment() =>
            {
                return Some("connect");
            }
            (WireErrorKind::Tls, FailurePhase::Tls)
                if self.retry_on_connection_failure && error.is_retryable_establishment() =>
            {
                return Some("tls");
            }
            (WireErrorKind::Protocol, FailurePhase::ProtocolBinding)
                if self.retry_on_connection_failure && error.is_retryable_establishment() =>
            {
                return Some("connect");
            }
            (WireErrorKind::Timeout, FailurePhase::Tcp | FailurePhase::ProxyTunnel)
                if self.retry_on_connection_failure =>
            {
                return Some("connect_timeout");
            }
            _ => {}
        }

        match error.establishment_stage() {
            Some(EstablishmentStage::Dns) if error.is_retryable_establishment() => {
                return self.retry_on_connection_failure.then_some("dns");
            }
            Some(EstablishmentStage::Tcp) if error.is_connect_timeout() => {
                return self
                    .retry_on_connection_failure
                    .then_some("connect_timeout");
            }
            Some(EstablishmentStage::Tcp | EstablishmentStage::ProtocolBinding)
                if error.is_retryable_establishment() =>
            {
                return self.retry_on_connection_failure.then_some("connect");
            }
            Some(EstablishmentStage::Tls) if error.is_retryable_establishment() => {
                return self.retry_on_connection_failure.then_some("tls");
            }
            Some(EstablishmentStage::RouteExhausted | EstablishmentStage::ProxyTunnel)
                if error.is_retryable_establishment() =>
            {
                return self.retry_on_connection_failure.then_some("connect");
            }
            Some(_) => return None,
            None => {}
        }

        match error.kind() {
            WireErrorKind::Canceled if self.retry_canceled_requests => Some("canceled"),
            WireErrorKind::Dns if self.retry_on_connection_failure => Some("dns"),
            WireErrorKind::Connect
                if self.retry_on_connection_failure && !error.is_non_retryable_connect() =>
            {
                Some("connect")
            }
            WireErrorKind::Tls if self.retry_on_connection_failure => Some("tls"),
            WireErrorKind::Timeout
                if self.retry_on_connection_failure && error.is_connect_timeout() =>
            {
                Some("connect_timeout")
            }
            _ => None,
        }
    }

    fn should_retry_response(&self, ctx: &ResponseRetryContext<'_>) -> Option<&'static str> {
        if !ctx.is_body_replayable() || ctx.retry_count() as usize >= self.max_retries {
            return None;
        }

        match ctx.response_status() {
            StatusCode::REQUEST_TIMEOUT
                if self.retry_on_connection_failure
                    && !matches!(
                        ctx.retry_after(),
                        Some(RetryAfter::Delayed | RetryAfter::Invalid)
                    ) =>
            {
                Some("http_408")
            }
            StatusCode::SERVICE_UNAVAILABLE
                if matches!(ctx.retry_after(), Some(RetryAfter::Immediate)) =>
            {
                Some("http_503")
            }
            _ => None,
        }
    }
}

#[derive(Clone, Default)]
pub(crate) struct RedirectPolicyConfig {
    default: DefaultRedirectPolicy,
    custom: Option<Arc<dyn RedirectPolicy>>,
}

impl RedirectPolicyConfig {
    pub(crate) fn default_config(&self) -> &DefaultRedirectPolicy {
        &self.default
    }

    pub(crate) fn has_custom_policy(&self) -> bool {
        self.custom.is_some()
    }

    pub(crate) fn policy(&self) -> &dyn RedirectPolicy {
        self.custom
            .as_deref()
            .map(|policy| policy as &dyn RedirectPolicy)
            .unwrap_or(&self.default)
    }

    pub(crate) fn default_policy(&self) -> Option<&DefaultRedirectPolicy> {
        self.custom.is_none().then_some(&self.default)
    }

    pub(crate) fn default_mut(&mut self) -> &mut DefaultRedirectPolicy {
        self.custom = None;
        &mut self.default
    }

    pub(crate) fn set_custom<P>(&mut self, policy: P)
    where
        P: RedirectPolicy,
    {
        self.custom = Some(Arc::new(policy));
    }
}

#[derive(Clone, Debug)]
pub struct DefaultRedirectPolicy {
    follow_redirects: bool,
    max_redirects: usize,
    allow_insecure_redirects: bool,
}

impl DefaultRedirectPolicy {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn follow_redirects(&self) -> bool {
        self.follow_redirects
    }

    pub fn set_follow_redirects(&mut self, enabled: bool) {
        self.follow_redirects = enabled;
    }

    pub fn max_redirects(&self) -> usize {
        self.max_redirects
    }

    pub fn set_max_redirects(&mut self, max_redirects: usize) {
        self.max_redirects = max_redirects;
    }

    pub fn allow_insecure_redirects(&self) -> bool {
        self.allow_insecure_redirects
    }

    pub fn set_allow_insecure_redirects(&mut self, enabled: bool) {
        self.allow_insecure_redirects = enabled;
    }
}

impl Default for DefaultRedirectPolicy {
    fn default() -> Self {
        Self {
            follow_redirects: true,
            max_redirects: 10,
            allow_insecure_redirects: false,
        }
    }
}

impl RedirectPolicy for DefaultRedirectPolicy {
    fn should_redirect(&self, ctx: &RedirectContext<'_>) -> RedirectDecision {
        if !self.follow_redirects || !is_redirect_status(ctx.response_status()) {
            return RedirectDecision::Stop;
        }

        if ctx.redirect_count() as usize >= self.max_redirects {
            return RedirectDecision::Error(
                WireError::redirect(format!("too many redirects (max {})", self.max_redirects))
                    .with_response_status(ctx.response_status()),
            );
        }

        if !self.allow_insecure_redirects
            && ctx.request_uri().scheme_str() == Some("https")
            && ctx.location().scheme_str() == Some("http")
        {
            return RedirectDecision::Error(
                WireError::redirect(format!(
                    "refusing insecure redirect from {} to {}",
                    ctx.request_uri(),
                    ctx.location()
                ))
                .with_response_status(ctx.response_status()),
            );
        }

        RedirectDecision::Follow
    }
}

fn is_redirect_status(status: StatusCode) -> bool {
    matches!(
        status,
        StatusCode::MULTIPLE_CHOICES
            | StatusCode::MOVED_PERMANENTLY
            | StatusCode::FOUND
            | StatusCode::SEE_OTHER
            | StatusCode::TEMPORARY_REDIRECT
            | StatusCode::PERMANENT_REDIRECT
    )
}

#[cfg(test)]
mod tests {
    use http::Method;

    use super::{DefaultRedirectPolicy, DefaultRetryPolicy};
    use openwire_core::{
        RedirectContext, RedirectDecision, RedirectPolicy, ResponseRetryContext, RetryAfter,
        RetryContext, RetryPolicy, WireError, WireErrorKind,
    };

    #[test]
    fn default_retry_policy_respects_max_retries_and_cancel_toggle() {
        let mut policy = DefaultRetryPolicy::default();
        policy.set_retry_canceled_requests(true);

        let canceled = WireError::new(WireErrorKind::Canceled, "canceled");
        let method = Method::GET;
        let first = RetryContext::new(&canceled, 0, true, &method);
        let second = RetryContext::new(&canceled, 1, true, &method);

        assert_eq!(policy.should_retry(&first), Some("canceled"));
        assert_eq!(policy.should_retry(&second), None);
    }

    #[test]
    fn default_retry_policy_handles_response_status_retries() {
        let policy = DefaultRetryPolicy::default();
        let method = Method::POST;

        let request_timeout =
            ResponseRetryContext::new(http::StatusCode::REQUEST_TIMEOUT, 0, true, &method, None);
        assert_eq!(
            policy.should_retry_response(&request_timeout),
            Some("http_408")
        );

        let request_timeout_with_delay = ResponseRetryContext::new(
            http::StatusCode::REQUEST_TIMEOUT,
            0,
            true,
            &method,
            Some(RetryAfter::Delayed),
        );
        assert_eq!(
            policy.should_retry_response(&request_timeout_with_delay),
            None
        );

        let service_unavailable = ResponseRetryContext::new(
            http::StatusCode::SERVICE_UNAVAILABLE,
            0,
            true,
            &method,
            Some(RetryAfter::Immediate),
        );
        assert_eq!(
            policy.should_retry_response(&service_unavailable),
            Some("http_503")
        );

        let service_unavailable_without_header = ResponseRetryContext::new(
            http::StatusCode::SERVICE_UNAVAILABLE,
            0,
            true,
            &method,
            None,
        );
        assert_eq!(
            policy.should_retry_response(&service_unavailable_without_header),
            None
        );
    }

    #[test]
    fn default_redirect_policy_rejects_https_to_http_downgrade() {
        let policy = DefaultRedirectPolicy::default();
        let method = Method::GET;
        let current = "https://secure.test/start".parse().expect("current uri");
        let location = "http://secure.test/next".parse().expect("location");
        let ctx = RedirectContext::new(
            &method,
            &current,
            http::StatusCode::FOUND,
            &location,
            0,
            true,
        );

        match policy.should_redirect(&ctx) {
            RedirectDecision::Error(error) => {
                assert_eq!(error.kind(), WireErrorKind::Redirect);
                assert!(error.to_string().contains(
                    "refusing insecure redirect from https://secure.test/start to http://secure.test/next"
                ));
            }
            other => panic!(
                "expected redirect error, got {:?}",
                describe_decision(other)
            ),
        }
    }

    #[test]
    fn default_redirect_policy_follows_multiple_choices_with_location() {
        let policy = DefaultRedirectPolicy::default();
        let method = Method::GET;
        let current = "http://example.test/start".parse().expect("current uri");
        let location = "http://example.test/choice".parse().expect("location");
        let ctx = RedirectContext::new(
            &method,
            &current,
            http::StatusCode::MULTIPLE_CHOICES,
            &location,
            0,
            true,
        );

        assert!(matches!(
            policy.should_redirect(&ctx),
            RedirectDecision::Follow
        ));
    }

    fn describe_decision(decision: RedirectDecision) -> &'static str {
        match decision {
            RedirectDecision::Follow => "follow",
            RedirectDecision::Stop => "stop",
            RedirectDecision::Error(_) => "error",
        }
    }
}