[go: up one dir, main page]

uv-client 0.0.7

This is an internal component crate of uv
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
use std::collections::HashSet;

use crate::rkyvutil::OwnedArchive;

/// Represents values for relevant cache-control directives.
///
/// This does include some directives that we don't use mostly because they are
/// trivial to support. (For example, `must-understand` at time of writing is
/// not used in our HTTP cache semantics. Neither is `proxy-revalidate` since
/// we are not a proxy.)
#[derive(
    Clone,
    Debug,
    Default,
    Eq,
    PartialEq,
    serde::Serialize,
    serde::Deserialize,
    rkyv::Archive,
    rkyv::Deserialize,
    rkyv::Serialize,
)]
#[rkyv(derive(Debug))]
pub struct CacheControl {
    // directives for requests and responses
    /// * <https://www.rfc-editor.org/rfc/rfc9111.html#name-max-age>
    /// * <https://www.rfc-editor.org/rfc/rfc9111.html#name-max-age-2>
    pub max_age_seconds: Option<u64>,
    /// * <https://www.rfc-editor.org/rfc/rfc9111.html#name-no-cache>
    /// * <https://www.rfc-editor.org/rfc/rfc9111.html#name-no-cache-2>
    pub no_cache: bool,
    /// * <https://www.rfc-editor.org/rfc/rfc9111.html#name-no-store>
    /// * <https://www.rfc-editor.org/rfc/rfc9111.html#name-no-store-2>
    pub no_store: bool,
    /// * <https://www.rfc-editor.org/rfc/rfc9111.html#name-no-transform>
    /// * <https://www.rfc-editor.org/rfc/rfc9111.html#name-no-transform-2>
    pub no_transform: bool,

    // request-only directives
    /// <https://www.rfc-editor.org/rfc/rfc9111.html#name-max-stale>
    pub max_stale_seconds: Option<u64>,
    /// <https://www.rfc-editor.org/rfc/rfc9111.html#name-min-fresh>
    pub min_fresh_seconds: Option<u64>,

    // response-only directives
    /// <https://www.rfc-editor.org/rfc/rfc9111.html#name-only-if-cached>
    pub only_if_cached: bool,
    /// <https://www.rfc-editor.org/rfc/rfc9111.html#name-must-revalidate>
    pub must_revalidate: bool,
    /// <https://www.rfc-editor.org/rfc/rfc9111.html#name-must-understand>
    pub must_understand: bool,
    /// <https://www.rfc-editor.org/rfc/rfc9111.html#name-private>
    pub private: bool,
    /// <https://www.rfc-editor.org/rfc/rfc9111.html#name-proxy-revalidate>
    pub proxy_revalidate: bool,
    /// <https://www.rfc-editor.org/rfc/rfc9111.html#name-public>
    pub public: bool,
    /// <https://www.rfc-editor.org/rfc/rfc9111.html#name-s-maxage>
    pub s_maxage_seconds: Option<u64>,
    /// <https://httpwg.org/specs/rfc8246.html>
    pub immutable: bool,
}

impl CacheControl {
    /// Convert this to an owned archive value.
    pub fn to_archived(&self) -> OwnedArchive<Self> {
        // There's no way (other than OOM) for serializing this type to fail.
        OwnedArchive::from_unarchived(self).expect("all possible values can be archived")
    }
}

impl<'b, B: 'b + ?Sized + AsRef<[u8]>> FromIterator<&'b B> for CacheControl {
    fn from_iter<T: IntoIterator<Item = &'b B>>(it: T) -> Self {
        CacheControlParser::new(it).collect()
    }
}

impl FromIterator<CacheControlDirective> for CacheControl {
    fn from_iter<T: IntoIterator<Item = CacheControlDirective>>(it: T) -> Self {
        fn parse_int(value: &[u8]) -> Option<u64> {
            if !value.iter().all(u8::is_ascii_digit) {
                return None;
            }
            std::str::from_utf8(value).ok()?.parse().ok()
        }

        let mut cc = Self::default();
        for ccd in it {
            // Note that when we see invalid directive values, we follow [RFC
            // 9111 S4.2.1]. It says that invalid cache-control directives
            // should result in treating the response as stale. (Which we
            // accomplished by setting `must_revalidate` to `true`.)
            //
            // [RFC 9111 S4.2.1]: https://www.rfc-editor.org/rfc/rfc9111.html#section-4.2.1
            match &*ccd.name {
                // request + response directives
                "max-age" => match parse_int(&ccd.value) {
                    None => cc.must_revalidate = true,
                    Some(seconds) => cc.max_age_seconds = Some(seconds),
                },
                "no-cache" => cc.no_cache = true,
                "no-store" => cc.no_store = true,
                "no-transform" => cc.no_transform = true,
                // request-only directives
                "max-stale" => {
                    // As per [RFC 9111 S5.2.1.2], "If no value is assigned to
                    // max-stale, then the client will accept a stale response
                    // of any age." We implement that by just using the maximum
                    // number of seconds.
                    //
                    // [RFC 9111 S5.2.1.2]: https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.1.2
                    if ccd.value.is_empty() {
                        cc.max_stale_seconds = Some(u64::MAX);
                    } else {
                        match parse_int(&ccd.value) {
                            None => cc.must_revalidate = true,
                            Some(seconds) => cc.max_stale_seconds = Some(seconds),
                        }
                    }
                }
                "min-fresh" => match parse_int(&ccd.value) {
                    None => cc.must_revalidate = true,
                    Some(seconds) => cc.min_fresh_seconds = Some(seconds),
                },
                "only-if-cached" => cc.only_if_cached = true,
                "must-revalidate" => cc.must_revalidate = true,
                "must-understand" => cc.must_understand = true,
                "private" => cc.private = true,
                "proxy-revalidate" => cc.proxy_revalidate = true,
                "public" => cc.public = true,
                "s-maxage" => match parse_int(&ccd.value) {
                    None => cc.must_revalidate = true,
                    Some(seconds) => cc.s_maxage_seconds = Some(seconds),
                },
                "immutable" => cc.immutable = true,
                _ => {}
            }
        }
        cc
    }
}

/// A parser for the HTTP `Cache-Control` header.
///
/// The parser is mostly defined across multiple parts of multiple RFCs.
/// Namely, [RFC 9110 S5.6.2] says how to parse the names (or "keys") of each
/// directive (whose format is a "token"). [RFC 9110 S5.6.4] says how to parse
/// quoted values. And finally, [RFC 9111 Appendix A] gives the ABNF for the
/// overall header value.
///
/// This parser accepts an iterator of anything that can be cheaply converted
/// to a byte string (e.g., `http::header::HeaderValue`). Directives are parsed
/// from zero or more of these byte strings. Parsing cannot return an error,
/// but if something unexpected is found, the rest of that header value is
/// skipped.
///
/// Duplicate directives provoke an automatic insertion of `must-revalidate`,
/// as implied by [RFC 9111 S4.2.1], to ensure that the client will talk to the
/// server before using anything in case.
///
/// This parser handles a bit more than what we actually need in
/// `uv-client`. For example, we don't need to handle quoted values at all
/// since either don't use or care about values that require quoted. With that
/// said, the parser handles these because it wasn't that much extra work to do
/// so and just generally seemed like good sense. (If we didn't handle them and
/// parsed them incorrectly, that might mean parsing subsequent directives that
/// we do care about incorrectly.)
///
/// [RFC 9110 S5.6.2]: https://www.rfc-editor.org/rfc/rfc9110.html#name-tokens
/// [RFC 9110 S5.6.4]: https://www.rfc-editor.org/rfc/rfc9110.html#name-quoted-strings
/// [RFC 9111 Appendix A]: https://www.rfc-editor.org/rfc/rfc9111.html#name-collected-abnf
/// [RFC 9111 S4.2.1]: https://www.rfc-editor.org/rfc/rfc9111.html#calculating.freshness.lifetime
struct CacheControlParser<'b, I> {
    cur: &'b [u8],
    directives: I,
    seen: HashSet<String>,
}

impl<'b, B: 'b + ?Sized + AsRef<[u8]>, I: Iterator<Item = &'b B>> CacheControlParser<'b, I> {
    /// Create a new parser of zero or more `Cache-Control` header values. The
    /// given iterator should yield elements that satisfy `AsRef<[u8]>`.
    fn new<II: IntoIterator<IntoIter = I>>(headers: II) -> Self {
        let mut directives = headers.into_iter();
        let cur = directives.next().map(AsRef::as_ref).unwrap_or(b"");
        CacheControlParser {
            cur,
            directives,
            seen: HashSet::new(),
        }
    }

    /// Parses a token according to [RFC 9110 S5.6.2].
    ///
    /// If no token is found at the current position, then this returns `None`.
    /// Usually this indicates an invalid cache-control directive.
    ///
    /// This does not trim whitespace before or after the token.
    ///
    /// [RFC 9110 S5.6.2]: https://www.rfc-editor.org/rfc/rfc9110.html#name-tokens
    fn parse_token(&mut self) -> Option<String> {
        /// Returns true when the given byte can appear in a token, as
        /// defined by [RFC 9110 S5.6.2].
        ///
        /// [RFC 9110 S5.6.2]: https://www.rfc-editor.org/rfc/rfc9110.html#name-tokens
        fn is_token_byte(byte: u8) -> bool {
            matches!(
                byte,
                | b'!' | b'#' | b'$' | b'%' | b'&' | b'\'' | b'*' | b'+'
                | b'-' | b'.' | b'^' | b'_' | b'`' | b'|' | b'~'
                | b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z',
            )
        }
        let mut end = 0;
        while self.cur.get(end).copied().is_some_and(is_token_byte) {
            end += 1;
        }
        if end == 0 {
            None
        } else {
            let (token, rest) = self.cur.split_at(end);
            self.cur = rest;
            // This can't fail because `end` is only incremented when the
            // current byte is a valid token byte. And all valid token bytes
            // are ASCII and thus valid UTF-8.
            Some(String::from_utf8(token.to_vec()).expect("all valid token bytes are valid UTF-8"))
        }
    }

    /// Looks for an `=` as per [RFC 9111 Appendix A] which indicates that a
    /// cache directive has a value.
    ///
    /// This returns true if one was found. In which case, the `=` is consumed.
    ///
    /// This does not trim whitespace before or after the token.
    ///
    /// [RFC 9111 Appendix A]: https://www.rfc-editor.org/rfc/rfc9111.html#name-collected-abnf
    fn maybe_parse_equals(&mut self) -> bool {
        if self.cur.first().is_some_and(|&byte| byte == b'=') {
            self.cur = &self.cur[1..];
            true
        } else {
            false
        }
    }

    /// Parses a directive value as either an unquoted token or a quoted string
    /// as per [RFC 9111 Appendix A].
    ///
    /// If a valid value could not be found (for example, end-of-input or an
    /// opening quote without a closing quote), then `None` is returned. In
    /// this case, one should consider the cache-control header invalid.
    ///
    /// This does not trim whitespace before or after the token.
    ///
    /// Note that the returned value is *not* guaranteed to be valid UTF-8.
    /// Namely, it is possible for a quoted string to contain invalid UTF-8.
    ///
    /// [RFC 9111 Appendix A]: https://www.rfc-editor.org/rfc/rfc9111.html#name-collected-abnf
    fn parse_value(&mut self) -> Option<Vec<u8>> {
        if *self.cur.first()? == b'"' {
            self.cur = &self.cur[1..];
            self.parse_quoted_string()
        } else {
            self.parse_token().map(String::into_bytes)
        }
    }

    /// Parses a quoted string as per [RFC 9110 S5.6.4].
    ///
    /// This assumes the opening quote has already been consumed.
    ///
    /// If an invalid quoted string was found (e.g., no closing quote), then
    /// `None` is returned. An empty value may be returned.
    ///
    /// Note that the returned value is *not* guaranteed to be valid UTF-8.
    /// Namely, it is possible for a quoted string to contain invalid UTF-8.
    ///
    /// [RFC 9110 S5.6.4]: https://www.rfc-editor.org/rfc/rfc9110.html#name-quoted-strings
    fn parse_quoted_string(&mut self) -> Option<Vec<u8>> {
        fn is_qdtext_byte(byte: u8) -> bool {
            matches!(byte, b'\t' | b' ' | 0x21 | 0x23..=0x5B | 0x5D..=0x7E | 0x80..=0xFF)
        }
        fn is_quoted_pair_byte(byte: u8) -> bool {
            matches!(byte, b'\t' | b' ' | 0x21..=0x7E | 0x80..=0xFF)
        }
        let mut value = vec![];
        while !self.cur.is_empty() {
            let byte = self.cur[0];
            self.cur = &self.cur[1..];
            if byte == b'"' {
                return Some(value);
            } else if byte == b'\\' {
                let byte = *self.cur.first()?;
                self.cur = &self.cur[1..];
                // If we saw an escape but didn't see a valid
                // escaped byte, then we treat this value as
                // invalid.
                if !is_quoted_pair_byte(byte) {
                    return None;
                }
                value.push(byte);
            } else if is_qdtext_byte(byte) {
                value.push(byte);
            } else {
                break;
            }
        }
        // If we got here, it means we hit end-of-input before seeing a closing
        // quote. So we treat this as invalid and return `None`.
        None
    }

    /// Looks for a `,` as per [RFC 9111 Appendix A]. If one is found, then it
    /// is consumed and this returns true.
    ///
    /// This does not trim whitespace before or after the token.
    ///
    /// [RFC 9111 Appendix A]: https://www.rfc-editor.org/rfc/rfc9111.html#name-collected-abnf
    fn maybe_parse_directive_delimiter(&mut self) -> bool {
        if self.cur.first().is_some_and(|&byte| byte == b',') {
            self.cur = &self.cur[1..];
            true
        } else {
            false
        }
    }

    /// [RFC 9111 Appendix A] says that optional whitespace may appear between
    /// cache directives. We actually also allow whitespace to appear before
    /// the first directive and after the last directive.
    ///
    /// [RFC 9111 Appendix A]: https://www.rfc-editor.org/rfc/rfc9111.html#name-collected-abnf
    fn skip_whitespace(&mut self) {
        while self.cur.first().is_some_and(u8::is_ascii_whitespace) {
            self.cur = &self.cur[1..];
        }
    }

    fn emit_directive(
        &mut self,
        directive: CacheControlDirective,
    ) -> Option<CacheControlDirective> {
        let duplicate = !self.seen.insert(directive.name.clone());
        if duplicate {
            self.emit_revalidation()
        } else {
            Some(directive)
        }
    }

    fn emit_revalidation(&mut self) -> Option<CacheControlDirective> {
        if self.seen.insert("must-revalidate".to_string()) {
            Some(CacheControlDirective::must_revalidate())
        } else {
            // If we've already emitted a must-revalidate
            // directive, then don't do it again.
            None
        }
    }
}

impl<'b, B: 'b + ?Sized + AsRef<[u8]>, I: Iterator<Item = &'b B>> Iterator
    for CacheControlParser<'b, I>
{
    type Item = CacheControlDirective;

    fn next(&mut self) -> Option<CacheControlDirective> {
        loop {
            if self.cur.is_empty() {
                self.cur = self.directives.next().map(AsRef::as_ref)?;
            }
            while !self.cur.is_empty() {
                self.skip_whitespace();
                let Some(mut name) = self.parse_token() else {
                    // If we fail to parse a token, then this header value is
                    // either corrupt or empty. So skip the rest of it.
                    let invalid = !self.cur.is_empty();
                    self.cur = b"";
                    // But if it was invalid, force revalidation.
                    if invalid {
                        if let Some(d) = self.emit_revalidation() {
                            return Some(d);
                        }
                    }
                    break;
                };
                name.make_ascii_lowercase();
                if !self.maybe_parse_equals() {
                    // Eat up whitespace and the next delimiter. We don't care
                    // if we find a terminator.
                    self.skip_whitespace();
                    self.maybe_parse_directive_delimiter();
                    let directive = CacheControlDirective {
                        name,
                        value: vec![],
                    };
                    match self.emit_directive(directive) {
                        None => continue,
                        Some(d) => return Some(d),
                    }
                }
                let Some(value) = self.parse_value() else {
                    // If we expected a value (we saw an =) but couldn't find a
                    // valid value, then this header value is probably corrupt.
                    // So skip the rest of it.
                    self.cur = b"";
                    match self.emit_revalidation() {
                        None => break,
                        Some(d) => return Some(d),
                    }
                };
                // Eat up whitespace and the next delimiter. We don't care if
                // we find a terminator.
                self.skip_whitespace();
                self.maybe_parse_directive_delimiter();
                let directive = CacheControlDirective { name, value };
                if let Some(d) = self.emit_directive(directive) {
                    return Some(d);
                }
            }
        }
    }
}

/// A single directive from the `Cache-Control` header.
#[derive(Debug, Eq, PartialEq)]
struct CacheControlDirective {
    /// The name of the directive.
    name: String,
    /// A possibly empty value.
    ///
    /// Note that directive values may contain invalid UTF-8. (Although they
    /// cannot actually contain arbitrary bytes. For example, NUL bytes, among
    /// others, are not allowed.)
    value: Vec<u8>,
}

impl CacheControlDirective {
    /// Returns a `must-revalidate` directive. This is useful for forcing a
    /// cache decision that the response is stale, and thus the server should
    /// be consulted for whether the cached response ought to be used or not.
    fn must_revalidate() -> Self {
        Self {
            name: "must-revalidate".to_string(),
            value: vec![],
        }
    }
}

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

    #[test]
    fn cache_control_token() {
        let cc: CacheControl = CacheControlParser::new(["no-cache"]).collect();
        assert!(cc.no_cache);
        assert!(!cc.must_revalidate);
    }

    #[test]
    fn cache_control_max_age() {
        let cc: CacheControl = CacheControlParser::new(["max-age=60"]).collect();
        assert_eq!(Some(60), cc.max_age_seconds);
        assert!(!cc.must_revalidate);
    }

    // [RFC 9111 S5.2.1.1] says that client MUST NOT quote max-age, but we
    // support parsing it that way anyway.
    //
    // [RFC 9111 S5.2.1.1]: https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.1.1
    #[test]
    fn cache_control_max_age_quoted() {
        let cc: CacheControl = CacheControlParser::new([r#"max-age="60""#]).collect();
        assert_eq!(Some(60), cc.max_age_seconds);
        assert!(!cc.must_revalidate);
    }

    #[test]
    fn cache_control_max_age_invalid() {
        let cc: CacheControl = CacheControlParser::new(["max-age=6a0"]).collect();
        assert_eq!(None, cc.max_age_seconds);
        assert!(cc.must_revalidate);
    }

    #[test]
    fn cache_control_immutable() {
        let cc: CacheControl = CacheControlParser::new(["max-age=31536000, immutable"]).collect();
        assert_eq!(Some(31_536_000), cc.max_age_seconds);
        assert!(cc.immutable);
        assert!(!cc.must_revalidate);
    }

    #[test]
    fn cache_control_unrecognized() {
        let cc: CacheControl = CacheControlParser::new(["lion,max-age=60,zebra"]).collect();
        assert_eq!(Some(60), cc.max_age_seconds);
    }

    #[test]
    fn cache_control_invalid_squashes_remainder() {
        let cc: CacheControl = CacheControlParser::new(["no-cache,\x00,max-age=60"]).collect();
        // The invalid data doesn't impact things before it.
        assert!(cc.no_cache);
        // The invalid data precludes parsing anything after.
        assert_eq!(None, cc.max_age_seconds);
        // The invalid contents should force revalidation.
        assert!(cc.must_revalidate);
    }

    #[test]
    fn cache_control_invalid_squashes_remainder_but_not_other_header_values() {
        let cc: CacheControl =
            CacheControlParser::new(["no-cache,\x00,max-age=60", "max-stale=30"]).collect();
        // The invalid data doesn't impact things before it.
        assert!(cc.no_cache);
        // The invalid data precludes parsing anything after
        // in the same header value, but not in other
        // header values.
        assert_eq!(Some(30), cc.max_stale_seconds);
        // The invalid contents should force revalidation.
        assert!(cc.must_revalidate);
    }

    #[test]
    fn cache_control_parse_token() {
        let directives = CacheControlParser::new(["no-cache"]).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![CacheControlDirective {
                name: "no-cache".to_string(),
                value: vec![]
            }]
        );
    }

    #[test]
    fn cache_control_parse_token_to_token_value() {
        let directives = CacheControlParser::new(["max-age=60"]).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![CacheControlDirective {
                name: "max-age".to_string(),
                value: b"60".to_vec(),
            }]
        );
    }

    #[test]
    fn cache_control_parse_token_to_quoted_string() {
        let directives =
            CacheControlParser::new([r#"private="cookie,x-something-else""#]).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![CacheControlDirective {
                name: "private".to_string(),
                value: b"cookie,x-something-else".to_vec(),
            }]
        );
    }

    #[test]
    fn cache_control_parse_token_to_quoted_string_with_escape() {
        let directives =
            CacheControlParser::new([r#"private="something\"crazy""#]).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![CacheControlDirective {
                name: "private".to_string(),
                value: br#"something"crazy"#.to_vec(),
            }]
        );
    }

    #[test]
    fn cache_control_parse_multiple_directives() {
        let header = r#"max-age=60, no-cache, private="cookie", no-transform"#;
        let directives = CacheControlParser::new([header]).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![
                CacheControlDirective {
                    name: "max-age".to_string(),
                    value: b"60".to_vec(),
                },
                CacheControlDirective {
                    name: "no-cache".to_string(),
                    value: vec![]
                },
                CacheControlDirective {
                    name: "private".to_string(),
                    value: b"cookie".to_vec(),
                },
                CacheControlDirective {
                    name: "no-transform".to_string(),
                    value: vec![]
                },
            ]
        );
    }

    #[test]
    fn cache_control_parse_multiple_directives_across_multiple_header_values() {
        let headers = [
            r"max-age=60, no-cache",
            r#"private="cookie""#,
            r"no-transform",
        ];
        let directives = CacheControlParser::new(headers).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![
                CacheControlDirective {
                    name: "max-age".to_string(),
                    value: b"60".to_vec(),
                },
                CacheControlDirective {
                    name: "no-cache".to_string(),
                    value: vec![]
                },
                CacheControlDirective {
                    name: "private".to_string(),
                    value: b"cookie".to_vec(),
                },
                CacheControlDirective {
                    name: "no-transform".to_string(),
                    value: vec![]
                },
            ]
        );
    }

    #[test]
    fn cache_control_parse_one_header_invalid() {
        let headers = [
            r"max-age=60, no-cache",
            r#", private="cookie""#,
            r"no-transform",
        ];
        let directives = CacheControlParser::new(headers).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![
                CacheControlDirective {
                    name: "max-age".to_string(),
                    value: b"60".to_vec(),
                },
                CacheControlDirective {
                    name: "no-cache".to_string(),
                    value: vec![]
                },
                CacheControlDirective {
                    name: "must-revalidate".to_string(),
                    value: vec![]
                },
                CacheControlDirective {
                    name: "no-transform".to_string(),
                    value: vec![]
                },
            ]
        );
    }

    #[test]
    fn cache_control_parse_invalid_directive_drops_remainder() {
        let header = r#"max-age=60, no-cache, ="cookie", no-transform"#;
        let directives = CacheControlParser::new([header]).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![
                CacheControlDirective {
                    name: "max-age".to_string(),
                    value: b"60".to_vec(),
                },
                CacheControlDirective {
                    name: "no-cache".to_string(),
                    value: vec![]
                },
                CacheControlDirective {
                    name: "must-revalidate".to_string(),
                    value: vec![]
                },
            ]
        );
    }

    #[test]
    fn cache_control_parse_name_normalized() {
        let header = r"MAX-AGE=60";
        let directives = CacheControlParser::new([header]).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![CacheControlDirective {
                name: "max-age".to_string(),
                value: b"60".to_vec(),
            }]
        );
    }

    // When a duplicate directive is found, we keep the first one
    // and add in a `must-revalidate` directive to indicate that
    // things are stale and the client should do a re-check.
    #[test]
    fn cache_control_parse_duplicate_directives() {
        let header = r"max-age=60, no-cache, max-age=30";
        let directives = CacheControlParser::new([header]).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![
                CacheControlDirective {
                    name: "max-age".to_string(),
                    value: b"60".to_vec(),
                },
                CacheControlDirective {
                    name: "no-cache".to_string(),
                    value: vec![]
                },
                CacheControlDirective {
                    name: "must-revalidate".to_string(),
                    value: vec![]
                },
            ]
        );
    }

    #[test]
    fn cache_control_parse_duplicate_directives_across_headers() {
        let headers = [r"max-age=60, no-cache", r"max-age=30"];
        let directives = CacheControlParser::new(headers).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![
                CacheControlDirective {
                    name: "max-age".to_string(),
                    value: b"60".to_vec(),
                },
                CacheControlDirective {
                    name: "no-cache".to_string(),
                    value: vec![]
                },
                CacheControlDirective {
                    name: "must-revalidate".to_string(),
                    value: vec![]
                },
            ]
        );
    }

    // Tests that we don't emit must-revalidate multiple times
    // even when something is duplicated multiple times.
    #[test]
    fn cache_control_parse_duplicate_redux() {
        let header = r"max-age=60, no-cache, no-cache, max-age=30";
        let directives = CacheControlParser::new([header]).collect::<Vec<_>>();
        assert_eq!(
            directives,
            vec![
                CacheControlDirective {
                    name: "max-age".to_string(),
                    value: b"60".to_vec(),
                },
                CacheControlDirective {
                    name: "no-cache".to_string(),
                    value: vec![]
                },
                CacheControlDirective {
                    name: "must-revalidate".to_string(),
                    value: vec![]
                },
            ]
        );
    }
}