eggfetch-core 0.1.2

Async-first Rust HTTP client engine (tokio + hyper) with connection pooling, phase-aware timeouts, TLS, proxies, and compression
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Redirect handling engine.
//!
//! Implements configurable redirect following with method rewrite rules,
//! cross-origin header stripping, body replayability checks, and loop
//! detection. The redirect engine lives entirely in the Rust core; the
//! Python layer only configures policy and exposes response history.

use http::Method;
use url::Url;

use crate::body::RequestBody;
use crate::error::{Error, Result};
use crate::headers::Headers;
use crate::request::Request;

/// Configuration for HTTP redirect behavior.
#[derive(Debug, Clone)]
pub struct RedirectPolicy {
    /// Whether to follow redirects at all.
    pub follow: bool,
    /// Maximum number of redirects to follow before erroring.
    pub max_redirects: usize,
}

impl Default for RedirectPolicy {
    /// Default: do not follow redirects. This matches HTTPX's default.
    fn default() -> Self {
        Self {
            follow: false,
            max_redirects: 20,
        }
    }
}

impl RedirectPolicy {
    /// Create a new redirect policy with explicit settings.
    #[must_use]
    pub fn new(follow: bool, max_redirects: usize) -> Self {
        Self {
            follow,
            max_redirects,
        }
    }
}

/// Headers that should be removed when the body is dropped (e.g., on
/// POST→GET rewrite).
const BODY_HEADERS: &[&str] = &["content-length", "content-type", "transfer-encoding"];

/// Check if a status code indicates a redirect that should be followed.
#[must_use]
pub fn is_redirect_status(status: http::StatusCode) -> bool {
    matches!(status.as_u16(), 301 | 302 | 303 | 307 | 308)
}

/// Determine the method to use for a redirect request.
///
/// Rules:
/// - 303 (See Other): all non-HEAD methods become GET, body dropped
/// - 301/302: POST becomes GET (body dropped); other methods preserved
/// - 307/308: method and body preserved
#[must_use]
pub fn redirect_method(status: http::StatusCode, current_method: &Method) -> Method {
    match status.as_u16() {
        303 => {
            if current_method == Method::HEAD {
                Method::HEAD
            } else {
                Method::GET
            }
        }
        301 | 302 => {
            if current_method == Method::POST {
                Method::GET
            } else {
                current_method.clone()
            }
        }
        _ => current_method.clone(),
    }
}

/// Returns `true` if the redirect drops the request payload.
///
/// Rules (RFC 9110 §15.4.3/§15.4.4):
/// - 303: the method is rewritten to GET (except HEAD) and the payload
///   plus content-related headers are removed for **all** methods.
/// - 301/302: POST→GET rewrites drop the body; other methods preserved.
#[must_use]
pub fn drops_body_on_redirect(status: http::StatusCode, current_method: &Method) -> bool {
    if status.as_u16() == 303 {
        return current_method != Method::HEAD;
    }
    let new_method = redirect_method(status, current_method);
    current_method == Method::POST && new_method == Method::GET
}

/// Build a follow-up request from a redirect response and the original
/// request.
///
/// Applies method rewrite rules, strips sensitive headers on cross-origin
/// redirects, removes body-specific headers when the body is dropped,
/// and returns the new request with an empty body.
///
/// # Errors
///
/// Returns an error if:
/// - The redirect location is missing or invalid
/// - The redirect URL uses an unsupported scheme
/// - The body needs to be resent but is not replayable
pub fn build_redirect_request(
    original: &Request,
    status: http::StatusCode,
    location: &str,
) -> Result<Request> {
    let new_method = redirect_method(status, original.method());
    let drop_body = drops_body_on_redirect(status, original.method());
    build_redirect_request_with_policy(original, location, new_method, drop_body)
}

/// Build a follow-up request using a precomputed redirect method and
/// body-dropping decision.
///
/// Internal fast path for the pipeline, which already evaluated
/// [`redirect_method`] and [`drops_body_on_redirect`] for the hop.
///
/// # Errors
///
/// Same as [`build_redirect_request`].
pub(crate) fn build_redirect_request_with_policy(
    original: &Request,
    location: &str,
    new_method: Method,
    drop_body: bool,
) -> Result<Request> {
    let new_url = resolve_redirect_url(original.url(), location)?;

    validate_redirect_url(&new_url)?;

    // Build new headers.
    let mut new_headers = original.headers().clone();
    strip_headers_for_redirect(&mut new_headers, original.url(), &new_url, drop_body);

    // Handle body for the redirect.
    let new_body = if drop_body {
        RequestBody::Empty
    } else {
        original.body().try_clone_for_redirect()?
    };

    let mut redirect_request = Request::new(new_method, new_url);
    *redirect_request.headers_mut() = new_headers;
    redirect_request.set_body(new_body);
    // Deliberately do not carry the original request's wire version:
    // the redirect target may be a different host whose negotiated
    // protocol differs (matching httpx, which rebuilds the request).
    // ALPN governs TLS paths and the version policy lives on the
    // client config.

    Ok(redirect_request)
}

/// Resolve a redirect Location against the original URL.
///
/// Supports absolute URLs, relative paths, and scheme-relative URLs.
fn resolve_redirect_url(base: &Url, location: &str) -> Result<Url> {
    base.join(location)
        .map_err(|e| Error::InvalidRedirectLocation(format!("{e}")))
}

/// Validate that a redirect URL uses an allowed scheme.
fn validate_redirect_url(url: &Url) -> Result<()> {
    if !url.username().is_empty() || url.password().is_some() {
        return Err(Error::InvalidRedirectLocation(
            "redirect URL userinfo is not supported".into(),
        ));
    }
    match url.scheme() {
        "http" | "https" => Ok(()),
        other => Err(Error::Unsupported(format!(
            "redirect to unsupported scheme '{other}': only http and https are allowed"
        ))),
    }
}

/// Strip sensitive and body-specific headers for a redirect request.
fn strip_headers_for_redirect(
    headers: &mut Headers,
    original_url: &Url,
    new_url: &Url,
    drop_body: bool,
) {
    // Always strip `authorization` and `proxy-authorization`. The pipeline
    // re-applies configured auth via `resolve_request_auth`, and leaving
    // the prior hop's header in place would trigger `ConflictingAuth` on
    // same-origin redirects that inherit the header. Stripping here is
    // safe because the pipeline decides whether to re-attach credentials
    // based on the `credentials_allowed` flag for the hop.
    for name in ["authorization", "proxy-authorization"] {
        headers.remove(name);
    }

    // Strip remaining sensitive headers on cross-origin redirects.
    // `authorization` and `proxy-authorization` were already stripped above,
    // so only the cross-origin-specific entries remain here.
    let cross_origin = original_url.origin() != new_url.origin();
    if cross_origin {
        for name in ["cookie", "host"] {
            headers.remove(name);
        }
    }

    // Remove body-specific headers when body is dropped.
    if drop_body {
        for name in BODY_HEADERS {
            headers.remove(name);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use http::StatusCode;
    use proptest::prelude::*;

    #[test]
    fn redirect_method_303_get() {
        assert_eq!(
            redirect_method(StatusCode::SEE_OTHER, &Method::GET),
            Method::GET
        );
    }

    #[test]
    fn redirect_method_303_post() {
        assert_eq!(
            redirect_method(StatusCode::SEE_OTHER, &Method::POST),
            Method::GET
        );
    }

    #[test]
    fn redirect_method_303_head() {
        assert_eq!(
            redirect_method(StatusCode::SEE_OTHER, &Method::HEAD),
            Method::HEAD
        );
    }

    #[test]
    fn redirect_method_301_post() {
        assert_eq!(
            redirect_method(StatusCode::MOVED_PERMANENTLY, &Method::POST),
            Method::GET
        );
    }

    #[test]
    fn redirect_method_302_post() {
        assert_eq!(
            redirect_method(StatusCode::FOUND, &Method::POST),
            Method::GET
        );
    }

    #[test]
    fn redirect_method_301_get() {
        assert_eq!(
            redirect_method(StatusCode::MOVED_PERMANENTLY, &Method::GET),
            Method::GET
        );
    }

    #[test]
    fn redirect_method_307_post() {
        assert_eq!(
            redirect_method(StatusCode::TEMPORARY_REDIRECT, &Method::POST),
            Method::POST
        );
    }

    #[test]
    fn redirect_method_308_post() {
        assert_eq!(
            redirect_method(StatusCode::PERMANENT_REDIRECT, &Method::POST),
            Method::POST
        );
    }

    #[test]
    fn redirect_method_307_preserves_put() {
        assert_eq!(
            redirect_method(StatusCode::TEMPORARY_REDIRECT, &Method::PUT),
            Method::PUT
        );
    }

    #[test]
    fn is_redirect_status_codes() {
        assert!(is_redirect_status(StatusCode::MOVED_PERMANENTLY));
        assert!(is_redirect_status(StatusCode::FOUND));
        assert!(is_redirect_status(StatusCode::SEE_OTHER));
        assert!(is_redirect_status(StatusCode::TEMPORARY_REDIRECT));
        assert!(is_redirect_status(StatusCode::PERMANENT_REDIRECT));
        assert!(!is_redirect_status(StatusCode::OK));
        assert!(!is_redirect_status(StatusCode::NOT_FOUND));
    }

    #[test]
    fn drops_body_on_301_post() {
        assert!(drops_body_on_redirect(
            StatusCode::MOVED_PERMANENTLY,
            &Method::POST
        ));
    }

    #[test]
    fn drops_body_on_303_post() {
        assert!(drops_body_on_redirect(StatusCode::SEE_OTHER, &Method::POST));
    }

    #[test]
    fn preserves_body_on_307_post() {
        assert!(!drops_body_on_redirect(
            StatusCode::TEMPORARY_REDIRECT,
            &Method::POST
        ));
    }

    #[test]
    fn drops_body_on_303_put() {
        // RFC 9110 §15.4.4: 303 removes the payload for all methods.
        assert!(drops_body_on_redirect(StatusCode::SEE_OTHER, &Method::PUT));
        assert!(drops_body_on_redirect(
            StatusCode::SEE_OTHER,
            &Method::DELETE
        ));
    }

    #[test]
    fn build_redirect_303_put_drops_payload_and_content_headers() {
        let mut req = Request::new(Method::PUT, Url::parse("https://example.com/a").unwrap());
        req.set_body(RequestBody::from(Bytes::from("payload")));
        req.headers_mut().insert("content-length", "7").unwrap();
        req.headers_mut()
            .insert("content-type", "text/plain")
            .unwrap();

        let redirect =
            build_redirect_request(&req, StatusCode::SEE_OTHER, "https://example.com/b").unwrap();

        assert_eq!(*redirect.method(), Method::GET);
        assert!(redirect.body().is_empty());
        assert!(redirect.headers().get("content-length").is_none());
        assert!(redirect.headers().get("content-type").is_none());
    }

    #[test]
    fn build_redirect_cross_origin_strips_host() {
        let mut req = Request::new(Method::GET, Url::parse("https://example.com/a").unwrap());
        req.headers_mut().insert("host", "internal.corp").unwrap();

        let same_origin =
            build_redirect_request(&req, StatusCode::FOUND, "https://example.com/b").unwrap();
        assert!(same_origin.headers().get("host").is_some());

        let cross_origin =
            build_redirect_request(&req, StatusCode::FOUND, "https://other.com/b").unwrap();
        assert!(cross_origin.headers().get("host").is_none());
    }

    #[test]
    fn preserves_body_on_302_get() {
        assert!(!drops_body_on_redirect(StatusCode::FOUND, &Method::GET));
    }

    // --- build_redirect_request tests ---

    #[test]
    fn build_redirect_same_origin_strips_authorization() {
        // The pipeline re-applies configured auth via `resolve_request_auth`;
        // leaving the prior hop's `Authorization` in place would trigger
        // `ConflictingAuth` on a same-origin redirect that inherits the
        // header. `cookie` and `host` survive same-origin redirects so the
        // server sees the same session state.
        let mut req = Request::new(Method::GET, Url::parse("https://example.com/a").unwrap());
        req.headers_mut()
            .insert("authorization", "Bearer tok")
            .unwrap();
        req.headers_mut().insert("cookie", "session=abc").unwrap();

        let redirect =
            build_redirect_request(&req, StatusCode::FOUND, "https://example.com/b").unwrap();

        assert_eq!(*redirect.method(), Method::GET);
        assert_eq!(redirect.url().as_str(), "https://example.com/b");
        assert!(redirect.headers().get("authorization").is_none());
        assert!(redirect.headers().get("cookie").is_some());
    }

    #[test]
    fn build_redirect_cross_origin_strips_auth() {
        let mut req = Request::new(Method::GET, Url::parse("https://example.com/a").unwrap());
        req.headers_mut()
            .insert("authorization", "Bearer tok")
            .unwrap();
        req.headers_mut().insert("cookie", "session=abc").unwrap();
        req.headers_mut()
            .insert("proxy-authorization", "Basic foo")
            .unwrap();
        req.headers_mut().insert("x-custom", "keep").unwrap();

        let redirect =
            build_redirect_request(&req, StatusCode::FOUND, "https://other.com/b").unwrap();

        assert!(redirect.headers().get("authorization").is_none());
        assert!(redirect.headers().get("cookie").is_none());
        assert!(redirect.headers().get("proxy-authorization").is_none());
        assert!(redirect.headers().get("x-custom").is_some());
    }

    #[test]
    fn build_redirect_301_post_drops_body_headers() {
        let mut req = Request::new(Method::POST, Url::parse("https://example.com/a").unwrap());
        req.set_body(RequestBody::from(Bytes::from("payload")));
        req.headers_mut().insert("content-length", "7").unwrap();
        req.headers_mut()
            .insert("content-type", "text/plain")
            .unwrap();
        req.headers_mut()
            .insert("transfer-encoding", "chunked")
            .unwrap();

        let redirect =
            build_redirect_request(&req, StatusCode::MOVED_PERMANENTLY, "https://example.com/b")
                .unwrap();

        assert_eq!(*redirect.method(), Method::GET);
        assert!(redirect.body().is_empty());
        assert!(redirect.headers().get("content-length").is_none());
        assert!(redirect.headers().get("content-type").is_none());
        assert!(redirect.headers().get("transfer-encoding").is_none());
    }

    #[test]
    fn build_redirect_307_post_preserves_body_headers() {
        let mut req = Request::new(Method::POST, Url::parse("https://example.com/a").unwrap());
        req.set_body(RequestBody::from(Bytes::from("payload")));
        req.headers_mut().insert("content-length", "7").unwrap();
        req.headers_mut()
            .insert("content-type", "text/plain")
            .unwrap();

        let redirect = build_redirect_request(
            &req,
            StatusCode::TEMPORARY_REDIRECT,
            "https://example.com/b",
        )
        .unwrap();

        assert_eq!(*redirect.method(), Method::POST);
        match redirect.body() {
            RequestBody::Bytes(b) => assert_eq!(b, "payload"),
            _ => panic!("expected bytes body"),
        }
        assert!(redirect.headers().get("content-length").is_some());
        assert!(redirect.headers().get("content-type").is_some());
    }

    #[test]
    fn build_redirect_relative_url() {
        let req = Request::new(Method::GET, Url::parse("https://example.com/a/b").unwrap());

        let redirect = build_redirect_request(&req, StatusCode::FOUND, "/c/d").unwrap();

        assert_eq!(redirect.url().as_str(), "https://example.com/c/d");
    }

    #[test]
    fn build_redirect_scheme_relative_url() {
        let req = Request::new(Method::GET, Url::parse("https://example.com/a").unwrap());

        let redirect = build_redirect_request(&req, StatusCode::FOUND, "//other.com/b").unwrap();

        assert_eq!(redirect.url().as_str(), "https://other.com/b");
    }

    #[test]
    fn build_redirect_rejects_userinfo() {
        let req = Request::new(Method::GET, Url::parse("https://example.com/a").unwrap());
        let error =
            build_redirect_request(&req, StatusCode::FOUND, "https://user:pass@example.com/b")
                .unwrap_err();
        assert!(matches!(error, Error::InvalidRedirectLocation(_)));
    }

    #[test]
    fn build_redirect_unsupported_scheme_errors() {
        let req = Request::new(Method::GET, Url::parse("https://example.com/a").unwrap());

        let err = build_redirect_request(&req, StatusCode::FOUND, "ftp://example.com/b");
        assert!(err.is_err());
    }

    #[test]
    fn build_redirect_does_not_preserve_original_version() {
        // The redirect target may negotiate a different protocol than the
        // original host; the rebuilt request must not inherit the old
        // wire version.
        let mut req = Request::new(Method::GET, Url::parse("https://example.com/a").unwrap());
        req.set_version(http::Version::HTTP_2);

        let redirect =
            build_redirect_request(&req, StatusCode::FOUND, "https://example.com/b").unwrap();

        assert_ne!(redirect.version(), http::Version::HTTP_2);
        assert_eq!(redirect.version(), http::Version::HTTP_11);
    }

    #[test]
    fn build_redirect_empty_body_preserved() {
        let req = Request::new(Method::POST, Url::parse("https://example.com/a").unwrap());

        let redirect = build_redirect_request(
            &req,
            StatusCode::TEMPORARY_REDIRECT,
            "https://example.com/b",
        )
        .unwrap();

        assert_eq!(*redirect.method(), Method::POST);
        assert!(redirect.body().is_empty());
    }

    // --- RedirectPolicy tests ---

    #[test]
    fn redirect_policy_default() {
        let policy = RedirectPolicy::default();
        assert!(!policy.follow);
        assert_eq!(policy.max_redirects, 20);
    }

    #[test]
    fn redirect_policy_new() {
        let policy = RedirectPolicy::new(true, 5);
        assert!(policy.follow);
        assert_eq!(policy.max_redirects, 5);
    }

    proptest::proptest! {
        #[test]
        fn redirect_method_preserves_non_post_on_301_302(method in "[A-Z]{3,7}") {
            use http::Method;
            let m = Method::from_bytes(method.as_bytes());
            if let Ok(m) = m {
                if m != Method::POST {
                    prop_assert_eq!(redirect_method(StatusCode::MOVED_PERMANENTLY, &m), m.clone());
                    prop_assert_eq!(redirect_method(StatusCode::FOUND, &m), m);
                }
            }
        }

        #[test]
        #[allow(clippy::if_not_else)]
        fn redirect_method_303_always_get(method in "[A-Z]{3,7}") {
            use http::Method;
            let m = Method::from_bytes(method.as_bytes());
            if let Ok(m) = m {
                if m != Method::HEAD {
                    prop_assert_eq!(redirect_method(StatusCode::SEE_OTHER, &m), Method::GET);
                } else {
                    prop_assert_eq!(redirect_method(StatusCode::SEE_OTHER, &m), Method::HEAD);
                }
            }
        }

        #[test]
        fn drops_body_consistent_with_redirect_method(status in "[3][012378][1234567890]", method in "[A-Z]{3,7}") {
            use http::Method;
            let s = StatusCode::from_bytes(status.as_bytes());
            let m = Method::from_bytes(method.as_bytes());
            if let (Ok(s), Ok(m)) = (s, m) {
                let new_method = redirect_method(s, &m);
                let drops = drops_body_on_redirect(s, &m);
                let expected = if s.as_u16() == 303 {
                    m != Method::HEAD
                } else {
                    m == Method::POST && new_method == Method::GET
                };
                prop_assert_eq!(drops, expected);
            }
        }

        #[test]
        fn is_redirect_status_only_matches_3xx(status in 100u16..600) {
            let s = StatusCode::from_u16(status);
            if let Ok(s) = s {
                let is_redirect = is_redirect_status(s);
                let expected = matches!(status, 301 | 302 | 303 | 307 | 308);
                prop_assert_eq!(is_redirect, expected);
            }
        }
    }
}