hypershunt 1.1.0

HTTP server and reverse proxy
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
// Per-location cache policy: the RFC 9111 cacheability decision (§3),
// freshness-lifetime resolution (§4.2), and the read-through /
// write-through orchestration the dispatch site calls.
//
// Stores fresh responses by a TTL that caps any origin freshness,
// honours `no-store`/`private`/`no-cache`/`Vary`/`Set-Cookie` and the
// `Authorization` rule, and answers client conditionals with a 304.  A
// stale entry that carries a validator is revalidated against the
// origin (§4.3) rather than refetched.  RFC 5861 stale-serving applies
// when the origin declared `stale-while-revalidate` / `stale-if-error`.
// Client request directives are honoured only when the location set
// `honor-client-cache-control` (otherwise the request `Cache-Control`
// is ignored).

use crate::cache::entry::StoredResponse;
use crate::cache::key::CacheKey;
use crate::cache::store::CacheStore;
use crate::config::CacheConfig;
use crate::error::{HttpResponse, bytes_body};
use crate::headers::RequestContext;
use crate::metrics::Metrics;
use bytes::Bytes;
use http_body_util::BodyExt;
use hyper::header::{
    AGE, AUTHORIZATION, CACHE_CONTROL, CONTENT_ENCODING, CONTENT_LENGTH, DATE,
    EXPIRES, HeaderMap, HeaderName, SET_COOKIE, VARY,
};
use hyper::{Method, Response, StatusCode};
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::{Duration, Instant, SystemTime};

pub struct CachePolicy {
    key: CacheKey,
    /// Upper bound on freshness; also the lifetime when the origin
    /// declared none.
    ttl: Duration,
    max_object_size: u64,
    /// Cacheable request methods, upper-case (validated GET/HEAD).
    methods: Vec<String>,
    /// Whether request `Cache-Control` directives are honoured.
    honor_client_cache_control: bool,
}

/// What `evaluate` yields for a cacheable response.
struct Decision {
    lifetime: Duration,
    initial_age: Duration,
    vary: Vec<(HeaderName, Option<String>)>,
    swr: Duration,
    sie: Duration,
}

/// Outcome of a read-through lookup.
pub enum Lookup {
    /// A usable entry: serve this response directly (full body, a
    /// client-driven 304, or -- under max-stale -- a stale body).
    Hit(HttpResponse),
    /// A stale entry within its `stale-while-revalidate` window: serve
    /// it now and refresh it in the background.
    StaleWhileRevalidate(Arc<StoredResponse>),
    /// Revalidate against the origin.  The dispatch site sends the
    /// stored validators and calls [`CachePolicy::serve_revalidated`]
    /// on a 304 or [`CachePolicy::maybe_store`] on a fresh 200.
    /// `serve_stale_on_error` is set when the entry is within its
    /// `stale-if-error` window, so an origin 5xx falls back to the
    /// stale body ([`CachePolicy::stale_response`]).
    Revalidate {
        entry: Arc<StoredResponse>,
        serve_stale_on_error: bool,
    },
    /// No usable entry (absent, a different variant, or stale with no
    /// way to revalidate): fetch and store normally.
    Miss,
}

impl CachePolicy {
    pub fn compile(cfg: &CacheConfig) -> CachePolicy {
        CachePolicy {
            key: CacheKey::compile(cfg.key.as_deref()),
            ttl: Duration::from_secs(cfg.ttl_secs),
            max_object_size: cfg.max_object_size,
            methods: cfg.methods.clone(),
            honor_client_cache_control: cfg.honor_client_cache_control,
        }
    }

    /// True when this request's method is eligible for caching.  The
    /// dispatch site skips the cache entirely otherwise.
    pub fn request_cacheable(&self, method: &Method) -> bool {
        (method == Method::GET || method == Method::HEAD)
            && self.methods.iter().any(|m| m == method.as_str())
    }

    /// Build the primary cache key for a request.
    pub fn build_key(&self, ctx: &RequestContext<'_>) -> String {
        self.key.build(ctx)
    }

    /// Whether this location honours request `Cache-Control` directives.
    pub fn honors_client_cc(&self) -> bool {
        self.honor_client_cache_control
    }

    /// Read-through.  `rcc` carries the client's request `Cache-Control`
    /// (default/empty when the location does not honour it, making every
    /// client directive a no-op).  Returns how the dispatch should
    /// proceed; counters are incremented by the caller, which knows the
    /// final outcome.
    pub fn lookup(
        &self,
        store: &CacheStore,
        key: &str,
        req_headers: &HeaderMap,
        now: Instant,
        rcc: &RequestCacheControl,
    ) -> Lookup {
        let Some(entry) = store.get(key) else {
            return Lookup::Miss;
        };
        if !entry.vary_matches(req_headers) {
            return Lookup::Miss;
        }

        let fresh = entry.is_fresh(now);
        // Client directives can make a still-fresh entry unusable.
        let too_old = rcc
            .max_age
            .is_some_and(|m| entry.current_age(now).as_secs() > m);
        let min_fresh_unmet = rcc
            .min_fresh
            .is_some_and(|mf| entry.remaining_freshness(now).as_secs() < mf);
        let usable_fresh =
            fresh && !rcc.no_cache && !too_old && !min_fresh_unmet;

        if usable_fresh {
            return if entry.client_not_modified(req_headers) {
                Lookup::Hit(entry.not_modified_response(now))
            } else {
                Lookup::Hit(entry.to_response(now))
            };
        }

        // Still fresh, but the client forced revalidation (no-cache /
        // max-age / min-fresh): revalidate if we can, else refetch.
        if fresh {
            return if entry.has_validators() {
                Lookup::Revalidate {
                    entry,
                    serve_stale_on_error: false,
                }
            } else {
                Lookup::Miss
            };
        }

        // The entry is genuinely stale from here on.
        let staleness = entry.staleness(now);

        // Client max-stale: serve the stale entry as-is (no origin trip)
        // when the client opted in and didn't also ask for no-cache.
        if !rcc.no_cache
            && let Some(max_stale) = rcc.max_stale
        {
            let within = match max_stale {
                None => true,
                Some(n) => staleness.as_secs() <= n,
            };
            if within {
                return Lookup::Hit(entry.to_response(now));
            }
        }

        // stale-while-revalidate: serve now, refresh in the background.
        if !rcc.no_cache
            && !entry.swr_window().is_zero()
            && staleness <= entry.swr_window()
        {
            return Lookup::StaleWhileRevalidate(entry);
        }

        // Revalidate when we have a validator; also keep the entry as a
        // stale-if-error fallback when within that window.
        let sie_ok =
            !entry.sie_window().is_zero() && staleness <= entry.sie_window();
        if entry.has_validators() || sie_ok {
            return Lookup::Revalidate {
                entry,
                serve_stale_on_error: sie_ok,
            };
        }

        // No validator and no stale window: drop it and refetch.
        store.remove(key);
        Lookup::Miss
    }

    /// Serve a stale entry as the stale-if-error fallback after the
    /// origin failed.  The body is served as-is with its current `Age`.
    pub fn stale_response(
        &self,
        entry: &StoredResponse,
        now: Instant,
    ) -> HttpResponse {
        entry.to_response(now)
    }

    /// After a stale entry was revalidated and the origin answered
    /// `304 Not Modified`: refresh the entry's freshness from the 304's
    /// metadata, re-store it, and serve it (honouring the client's own
    /// conditional request against the refreshed validators).
    pub fn serve_revalidated(
        &self,
        store: &CacheStore,
        key: String,
        entry: Arc<StoredResponse>,
        resp_304: HttpResponse,
        orig_req_headers: &HeaderMap,
        now: Instant,
    ) -> HttpResponse {
        let (parts, _body) = resp_304.into_parts();
        let cc = ResponseCacheControl::parse(&parts.headers);
        let lifetime = self.lifetime_from(&cc, &parts.headers);
        let initial_age = age_of(&parts.headers);
        let refreshed = Arc::new(entry.refreshed(
            &parts.headers,
            lifetime,
            initial_age,
            now,
        ));
        store.insert(key, refreshed.clone());
        if refreshed.client_not_modified(orig_req_headers) {
            refreshed.not_modified_response(now)
        } else {
            refreshed.to_response(now)
        }
    }

    /// Write-through: given the raw handler response, store it if
    /// eligible and return a response to forward.  Uncacheable or
    /// oversized responses are returned untouched (still streaming);
    /// cacheable ones are buffered (bounded by `Content-Length`),
    /// stored, and replayed with an `Age` header.
    pub async fn maybe_store(
        &self,
        store: &CacheStore,
        metrics: &Metrics,
        key: String,
        req_headers: &HeaderMap,
        resp: HttpResponse,
        now: Instant,
    ) -> HttpResponse {
        let (parts, body) = resp.into_parts();
        let Some(decision) =
            self.evaluate(parts.status, &parts.headers, req_headers)
        else {
            metrics.cache_bypass.fetch_add(1, Ordering::Relaxed);
            return Response::from_parts(parts, body);
        };
        // Only buffer when the body is bounded and fits the cap; an
        // absent or oversized Content-Length streams through uncached
        // so we never read an unbounded body into memory.
        let fits = parts
            .headers
            .get(CONTENT_LENGTH)
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse::<u64>().ok())
            .is_some_and(|n| n <= self.max_object_size);
        if !fits {
            metrics.cache_bypass.fetch_add(1, Ordering::Relaxed);
            return Response::from_parts(parts, body);
        }
        let bytes = match body.collect().await {
            Ok(c) => c.to_bytes(),
            Err(e) => {
                tracing::warn!("cache: body read failed: {e}");
                let mut r = Response::new(bytes_body(Bytes::from_static(
                    b"<h1>502 Bad Gateway</h1>",
                )));
                *r.status_mut() = StatusCode::BAD_GATEWAY;
                return r;
            }
        };
        let stored = Arc::new(
            StoredResponse::new(
                parts.status,
                &parts.headers,
                bytes,
                decision.lifetime,
                decision.initial_age,
                decision.vary,
                now,
            )
            .with_stale_windows(decision.swr, decision.sie),
        );
        store.insert(key, stored.clone());
        stored.to_response(now)
    }

    /// Apply the RFC 9111 §3 cacheability rules and resolve the
    /// freshness lifetime.  Returns `None` for an uncacheable
    /// response.  Split out (taking maps, not `Parts`) so it is
    /// directly unit-testable.
    fn evaluate(
        &self,
        status: StatusCode,
        resp_headers: &HeaderMap,
        req_headers: &HeaderMap,
    ) -> Option<Decision> {
        let cc = ResponseCacheControl::parse(resp_headers);
        if cc.no_store || cc.private || cc.no_cache {
            return None;
        }
        // A handler's own Set-Cookie is per-client; never cache it.
        if resp_headers.contains_key(SET_COOKIE) {
            return None;
        }
        // Don't cache an already-encoded body: our compression layer
        // runs after the cache and would have to special-case it.
        if resp_headers.contains_key(CONTENT_ENCODING) {
            return None;
        }
        let mut vary_names = Vec::new();
        for v in resp_headers.get_all(VARY) {
            let Ok(s) = v.to_str() else {
                return None;
            };
            for tok in s.split(',') {
                let t = tok.trim();
                if t == "*" {
                    return None;
                }
                if t.is_empty() {
                    continue;
                }
                if let Ok(name) =
                    HeaderName::from_bytes(t.to_ascii_lowercase().as_bytes())
                {
                    vary_names.push(name);
                }
            }
        }
        // RFC 9111 §3.5: a shared cache must not reuse a response to an
        // authorized request unless the origin explicitly allows it.
        if req_headers.contains_key(AUTHORIZATION)
            && !(cc.public || cc.s_maxage.is_some())
        {
            return None;
        }
        if !cacheable_status(status) {
            return None;
        }
        let lifetime = self.lifetime_from(&cc, resp_headers);
        if lifetime.is_zero() {
            return None;
        }
        let initial_age = age_of(resp_headers);
        let vary = vary_names
            .into_iter()
            .map(|name| {
                let val = req_headers
                    .get(&name)
                    .and_then(|v| v.to_str().ok())
                    .map(str::to_owned);
                (name, val)
            })
            .collect();
        Some(Decision {
            lifetime,
            initial_age,
            vary,
            swr: cc
                .stale_while_revalidate
                .map(Duration::from_secs)
                .unwrap_or_default(),
            sie: cc
                .stale_if_error
                .map(Duration::from_secs)
                .unwrap_or_default(),
        })
    }

    /// Resolve the freshness lifetime from already-parsed response
    /// `Cache-Control` plus `Expires`/`Date`: `s-maxage` > `max-age` >
    /// (`Expires` - `Date`), capped by the configured TTL; the TTL
    /// alone when the origin declared nothing.  Shared by the store
    /// path and the revalidation refresh path.
    fn lifetime_from(
        &self,
        cc: &ResponseCacheControl,
        headers: &HeaderMap,
    ) -> Duration {
        let origin = cc
            .s_maxage
            .or(cc.max_age)
            .map(Duration::from_secs)
            .or_else(|| expires_minus_date(headers));
        match origin {
            Some(o) => o.min(self.ttl),
            None => self.ttl,
        }
    }
}

/// Parse the `Age` response header into a `Duration` (0 when absent).
fn age_of(headers: &HeaderMap) -> Duration {
    headers
        .get(AGE)
        .and_then(|v| v.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok())
        .map(Duration::from_secs)
        .unwrap_or_default()
}

/// Status codes Phase 1 will cache by default.  Conservative subset of
/// RFC 9110's cacheable-by-default list.
fn cacheable_status(status: StatusCode) -> bool {
    matches!(status.as_u16(), 200 | 203 | 204 | 300 | 301 | 404 | 410)
}

/// Freshness from `Expires` minus `Date`, when both parse.  A past
/// `Expires` yields a zero lifetime (already stale).
fn expires_minus_date(headers: &HeaderMap) -> Option<Duration> {
    let parse = |name: &HeaderName| -> Option<SystemTime> {
        headers
            .get(name)
            .and_then(|v| v.to_str().ok())
            .and_then(|s| httpdate::parse_http_date(s).ok())
    };
    let date = parse(&DATE)?;
    let expires = parse(&EXPIRES)?;
    Some(expires.duration_since(date).unwrap_or(Duration::ZERO))
}

/// The response `Cache-Control` directives the cache reads.
#[derive(Default)]
struct ResponseCacheControl {
    no_store: bool,
    private: bool,
    no_cache: bool,
    public: bool,
    max_age: Option<u64>,
    s_maxage: Option<u64>,
    /// RFC 5861 stale-serving windows.
    stale_while_revalidate: Option<u64>,
    stale_if_error: Option<u64>,
}

impl ResponseCacheControl {
    fn parse(headers: &HeaderMap) -> Self {
        let mut cc = ResponseCacheControl::default();
        for value in headers.get_all(CACHE_CONTROL) {
            let Ok(s) = value.to_str() else {
                continue;
            };
            for directive in s.split(',') {
                let directive = directive.trim();
                let (name, arg) = match directive.split_once('=') {
                    Some((n, a)) => (n.trim(), Some(a.trim())),
                    None => (directive, None),
                };
                match name.to_ascii_lowercase().as_str() {
                    "no-store" => cc.no_store = true,
                    "private" => cc.private = true,
                    "no-cache" => cc.no_cache = true,
                    "public" => cc.public = true,
                    "max-age" => cc.max_age = arg.and_then(parse_secs),
                    "s-maxage" => cc.s_maxage = arg.and_then(parse_secs),
                    "stale-while-revalidate" => {
                        cc.stale_while_revalidate = arg.and_then(parse_secs)
                    }
                    "stale-if-error" => {
                        cc.stale_if_error = arg.and_then(parse_secs)
                    }
                    _ => {}
                }
            }
        }
        cc
    }
}

/// The request `Cache-Control` directives honoured when a location
/// sets `honor-client-cache-control`.  All fields are "unset" by
/// default, so a `RequestCacheControl::default()` (used when the
/// location does not honour client directives) is a no-op.
#[derive(Default)]
pub struct RequestCacheControl {
    pub no_store: bool,
    pub no_cache: bool,
    pub only_if_cached: bool,
    pub max_age: Option<u64>,
    pub min_fresh: Option<u64>,
    /// `max-stale` with no argument is `Some(None)` (accept any stale);
    /// `max-stale=N` is `Some(Some(N))`.
    pub max_stale: Option<Option<u64>>,
}

impl RequestCacheControl {
    pub fn parse(headers: &HeaderMap) -> Self {
        let mut cc = RequestCacheControl::default();
        for value in headers.get_all(CACHE_CONTROL) {
            let Ok(s) = value.to_str() else {
                continue;
            };
            for directive in s.split(',') {
                let directive = directive.trim();
                let (name, arg) = match directive.split_once('=') {
                    Some((n, a)) => (n.trim(), Some(a.trim())),
                    None => (directive, None),
                };
                match name.to_ascii_lowercase().as_str() {
                    "no-store" => cc.no_store = true,
                    "no-cache" => cc.no_cache = true,
                    "only-if-cached" => cc.only_if_cached = true,
                    "max-age" => cc.max_age = arg.and_then(parse_secs),
                    "min-fresh" => cc.min_fresh = arg.and_then(parse_secs),
                    // Bare `max-stale` accepts any staleness.
                    "max-stale" => {
                        cc.max_stale = Some(arg.and_then(parse_secs))
                    }
                    _ => {}
                }
            }
        }
        cc
    }
}

/// Parse a delta-seconds argument, tolerating optional quotes.
fn parse_secs(arg: &str) -> Option<u64> {
    arg.trim_matches('"').parse::<u64>().ok()
}

#[cfg(test)]
mod tests {
    use super::*;
    use hyper::header::HeaderValue;

    fn policy(ttl: u64, max_obj: u64) -> CachePolicy {
        CachePolicy::compile(&CacheConfig {
            ttl_secs: ttl,
            max_object_size: max_obj,
            methods: vec!["GET".to_owned()],
            key: None,
            honor_client_cache_control: false,
        })
    }

    fn hmap(pairs: &[(&str, &str)]) -> HeaderMap {
        let mut h = HeaderMap::new();
        for (n, v) in pairs {
            h.append(
                HeaderName::from_bytes(n.as_bytes()).unwrap(),
                HeaderValue::from_str(v).unwrap(),
            );
        }
        h
    }

    #[test]
    fn request_cacheable_only_get_head_in_set() {
        let p = policy(60, 1024);
        assert!(p.request_cacheable(&Method::GET));
        assert!(!p.request_cacheable(&Method::HEAD)); // not in methods
        assert!(!p.request_cacheable(&Method::POST));
    }

    #[test]
    fn ttl_caps_origin_max_age() {
        let p = policy(30, 1024);
        let d = p
            .evaluate(
                StatusCode::OK,
                &hmap(&[("cache-control", "max-age=300")]),
                &HeaderMap::new(),
            )
            .expect("cacheable");
        assert_eq!(d.lifetime, Duration::from_secs(30));
    }

    #[test]
    fn origin_shorter_than_ttl_wins() {
        let p = policy(300, 1024);
        let d = p
            .evaluate(
                StatusCode::OK,
                &hmap(&[("cache-control", "max-age=10")]),
                &HeaderMap::new(),
            )
            .expect("cacheable");
        assert_eq!(d.lifetime, Duration::from_secs(10));
    }

    #[test]
    fn s_maxage_preferred_over_max_age() {
        let p = policy(300, 1024);
        let d = p
            .evaluate(
                StatusCode::OK,
                &hmap(&[("cache-control", "max-age=10, s-maxage=20")]),
                &HeaderMap::new(),
            )
            .expect("cacheable");
        assert_eq!(d.lifetime, Duration::from_secs(20));
    }

    #[test]
    fn no_store_private_no_cache_are_uncacheable() {
        let p = policy(60, 1024);
        for d in ["no-store", "private", "no-cache"] {
            assert!(
                p.evaluate(
                    StatusCode::OK,
                    &hmap(&[("cache-control", d)]),
                    &HeaderMap::new()
                )
                .is_none(),
                "{d} should be uncacheable"
            );
        }
    }

    #[test]
    fn max_age_zero_is_uncacheable() {
        let p = policy(60, 1024);
        assert!(
            p.evaluate(
                StatusCode::OK,
                &hmap(&[("cache-control", "max-age=0")]),
                &HeaderMap::new()
            )
            .is_none()
        );
    }

    #[test]
    fn set_cookie_and_content_encoding_block_caching() {
        let p = policy(60, 1024);
        assert!(
            p.evaluate(
                StatusCode::OK,
                &hmap(&[("set-cookie", "a=b")]),
                &HeaderMap::new()
            )
            .is_none()
        );
        assert!(
            p.evaluate(
                StatusCode::OK,
                &hmap(&[("content-encoding", "gzip")]),
                &HeaderMap::new()
            )
            .is_none()
        );
    }

    #[test]
    fn vary_star_is_uncacheable_named_vary_captured() {
        let p = policy(60, 1024);
        assert!(
            p.evaluate(
                StatusCode::OK,
                &hmap(&[("vary", "*")]),
                &HeaderMap::new()
            )
            .is_none()
        );
        let d = p
            .evaluate(
                StatusCode::OK,
                &hmap(&[("vary", "Accept-Language")]),
                &hmap(&[("accept-language", "en")]),
            )
            .expect("cacheable");
        assert_eq!(d.vary.len(), 1);
        assert_eq!(d.vary[0].1.as_deref(), Some("en"));
    }

    #[test]
    fn authorized_request_needs_public_or_s_maxage() {
        let p = policy(60, 1024);
        let auth = hmap(&[("authorization", "Bearer x")]);
        // Plain max-age is not enough for a shared cache.
        assert!(
            p.evaluate(
                StatusCode::OK,
                &hmap(&[("cache-control", "max-age=60")]),
                &auth
            )
            .is_none()
        );
        // public makes it cacheable.
        assert!(
            p.evaluate(
                StatusCode::OK,
                &hmap(&[("cache-control", "public, max-age=60")]),
                &auth
            )
            .is_some()
        );
    }

    #[test]
    fn uncacheable_status_rejected() {
        let p = policy(60, 1024);
        assert!(
            p.evaluate(
                StatusCode::INTERNAL_SERVER_ERROR,
                &HeaderMap::new(),
                &HeaderMap::new()
            )
            .is_none()
        );
        // 200 with no directives uses the TTL.
        let d = p
            .evaluate(StatusCode::OK, &HeaderMap::new(), &HeaderMap::new())
            .expect("200 cacheable by TTL");
        assert_eq!(d.lifetime, Duration::from_secs(60));
    }

    // -- Revalidation (phase 2) ------------------------------------

    fn store() -> Arc<CacheStore> {
        CacheStore::new(1 << 20, Arc::new(Metrics::new()))
    }

    fn put(
        store: &CacheStore,
        key: &str,
        headers: &[(&str, &str)],
        lifetime: Duration,
        at: Instant,
    ) {
        let mut h = HeaderMap::new();
        for (n, v) in headers {
            h.insert(
                HeaderName::from_bytes(n.as_bytes()).unwrap(),
                HeaderValue::from_str(v).unwrap(),
            );
        }
        store.insert(
            key.to_owned(),
            Arc::new(StoredResponse::new(
                StatusCode::OK,
                &h,
                Bytes::from_static(b"body"),
                lifetime,
                Duration::ZERO,
                vec![],
                at,
            )),
        );
    }

    fn rcc() -> RequestCacheControl {
        RequestCacheControl::default()
    }

    #[test]
    fn lookup_fresh_is_hit() {
        let p = policy(60, 1024);
        let s = store();
        let t0 = Instant::now();
        put(&s, "k", &[], Duration::from_secs(60), t0);
        assert!(matches!(
            p.lookup(&s, "k", &HeaderMap::new(), t0, &rcc()),
            Lookup::Hit(_)
        ));
    }

    #[test]
    fn lookup_stale_with_validator_is_revalidate() {
        let p = policy(60, 1024);
        let s = store();
        let t0 = Instant::now();
        put(&s, "k", &[("etag", "\"v1\"")], Duration::from_secs(5), t0);
        assert!(matches!(
            p.lookup(
                &s,
                "k",
                &HeaderMap::new(),
                t0 + Duration::from_secs(6),
                &rcc()
            ),
            Lookup::Revalidate { .. }
        ));
    }

    #[test]
    fn lookup_stale_without_validator_is_miss_and_removed() {
        let p = policy(60, 1024);
        let s = store();
        let t0 = Instant::now();
        put(&s, "k", &[], Duration::from_secs(5), t0);
        assert!(matches!(
            p.lookup(
                &s,
                "k",
                &HeaderMap::new(),
                t0 + Duration::from_secs(6),
                &rcc()
            ),
            Lookup::Miss
        ));
        // The dead entry was dropped on the way out.
        assert!(s.get("k").is_none());
    }

    #[test]
    fn serve_revalidated_refreshes_freshness() {
        let p = policy(60, 1024);
        let s = store();
        let t0 = Instant::now();
        put(&s, "k", &[("etag", "\"v1\"")], Duration::from_secs(5), t0);
        let stale_at = t0 + Duration::from_secs(6);
        let Lookup::Revalidate { entry, .. } =
            p.lookup(&s, "k", &HeaderMap::new(), stale_at, &rcc())
        else {
            panic!("expected revalidate");
        };
        // Origin says "still valid, max-age=100".
        let mut r304 = Response::new(bytes_body(Bytes::new()));
        *r304.status_mut() = StatusCode::NOT_MODIFIED;
        r304.headers_mut()
            .insert(CACHE_CONTROL, HeaderValue::from_static("max-age=100"));
        let resp = p.serve_revalidated(
            &s,
            "k".to_owned(),
            entry,
            r304,
            &HeaderMap::new(),
            stale_at,
        );
        // Full body served (client sent no conditional).
        assert_eq!(resp.status(), StatusCode::OK);
        // And the entry is fresh again -- next lookup is a plain hit.
        assert!(matches!(
            p.lookup(&s, "k", &HeaderMap::new(), stale_at, &rcc()),
            Lookup::Hit(_)
        ));
    }

    // -- Phase 3: client directives + RFC 5861 ---------------------

    // Store an entry with explicit stale-while-revalidate /
    // stale-if-error windows.
    fn put_windows(
        store: &CacheStore,
        key: &str,
        validator: bool,
        lifetime: Duration,
        swr: Duration,
        sie: Duration,
        at: Instant,
    ) {
        let mut h = HeaderMap::new();
        if validator {
            h.insert(
                HeaderName::from_static("etag"),
                HeaderValue::from_static("\"v1\""),
            );
        }
        store.insert(
            key.to_owned(),
            Arc::new(
                StoredResponse::new(
                    StatusCode::OK,
                    &h,
                    Bytes::from_static(b"body"),
                    lifetime,
                    Duration::ZERO,
                    vec![],
                    at,
                )
                .with_stale_windows(swr, sie),
            ),
        );
    }

    #[test]
    fn client_no_cache_forces_revalidation_of_fresh_entry() {
        let p = policy(60, 1024);
        let s = store();
        let t0 = Instant::now();
        put(&s, "k", &[("etag", "\"v1\"")], Duration::from_secs(60), t0);
        let mut cc = rcc();
        cc.no_cache = true;
        assert!(matches!(
            p.lookup(&s, "k", &HeaderMap::new(), t0, &cc),
            Lookup::Revalidate { .. }
        ));
    }

    #[test]
    fn client_max_age_treats_old_entry_as_stale() {
        let p = policy(600, 1024);
        let s = store();
        let t0 = Instant::now();
        put(&s, "k", &[("etag", "\"v1\"")], Duration::from_secs(600), t0);
        let mut cc = rcc();
        cc.max_age = Some(5); // client accepts at most 5s old
        // Entry is fresh by the origin but 10s old: client rejects it.
        assert!(matches!(
            p.lookup(
                &s,
                "k",
                &HeaderMap::new(),
                t0 + Duration::from_secs(10),
                &cc
            ),
            Lookup::Revalidate { .. }
        ));
    }

    #[test]
    fn client_max_stale_serves_stale_entry() {
        let p = policy(60, 1024);
        let s = store();
        let t0 = Instant::now();
        put(&s, "k", &[], Duration::from_secs(5), t0);
        let mut cc = rcc();
        cc.max_stale = Some(Some(100)); // accept up to 100s stale
        // 6s stored, 1s stale -> served from cache.
        assert!(matches!(
            p.lookup(
                &s,
                "k",
                &HeaderMap::new(),
                t0 + Duration::from_secs(6),
                &cc
            ),
            Lookup::Hit(_)
        ));
    }

    #[test]
    fn stale_while_revalidate_window_serves_stale() {
        let p = policy(60, 1024);
        let s = store();
        let t0 = Instant::now();
        put_windows(
            &s,
            "k",
            true,
            Duration::from_secs(5),
            Duration::from_secs(60), // swr
            Duration::ZERO,
            t0,
        );
        // 2s past fresh, within the 60s swr window.
        assert!(matches!(
            p.lookup(
                &s,
                "k",
                &HeaderMap::new(),
                t0 + Duration::from_secs(7),
                &rcc()
            ),
            Lookup::StaleWhileRevalidate(_)
        ));
    }

    #[test]
    fn stale_if_error_keeps_validatorless_entry_for_fallback() {
        let p = policy(60, 1024);
        let s = store();
        let t0 = Instant::now();
        // No validator, but a stale-if-error window.
        put_windows(
            &s,
            "k",
            false,
            Duration::from_secs(5),
            Duration::ZERO,
            Duration::from_secs(60), // sie
            t0,
        );
        let l = p.lookup(
            &s,
            "k",
            &HeaderMap::new(),
            t0 + Duration::from_secs(7),
            &rcc(),
        );
        match l {
            Lookup::Revalidate {
                serve_stale_on_error,
                ..
            } => assert!(serve_stale_on_error),
            _ => panic!("expected revalidate with stale-on-error"),
        }
        // The entry is retained (not dropped) for the fallback.
        assert!(s.get("k").is_some());
    }

    #[test]
    fn evaluate_parses_stale_windows() {
        let p = policy(60, 1024);
        let d = p
            .evaluate(
                StatusCode::OK,
                &hmap(&[(
                    "cache-control",
                    "max-age=10, stale-while-revalidate=30, \
                     stale-if-error=120",
                )]),
                &HeaderMap::new(),
            )
            .expect("cacheable");
        assert_eq!(d.swr, Duration::from_secs(30));
        assert_eq!(d.sie, Duration::from_secs(120));
    }

    #[test]
    fn request_cache_control_parses_directives() {
        let cc = RequestCacheControl::parse(&hmap(&[(
            "cache-control",
            "no-store, max-age=5, min-fresh=10, only-if-cached",
        )]));
        assert!(cc.no_store);
        assert!(cc.only_if_cached);
        assert!(!cc.no_cache);
        assert_eq!(cc.max_age, Some(5));
        assert_eq!(cc.min_fresh, Some(10));
        // Bare `max-stale` accepts any staleness; valued is bounded.
        assert_eq!(
            RequestCacheControl::parse(&hmap(&[(
                "cache-control",
                "max-stale"
            )]))
            .max_stale,
            Some(None)
        );
        assert_eq!(
            RequestCacheControl::parse(&hmap(&[(
                "cache-control",
                "max-stale=60"
            )]))
            .max_stale,
            Some(Some(60))
        );
    }
}