Skip to main content

aube_util/http/
priority.rs

1//! RFC 9218 HTTP priority signal builder.
2//!
3//! Cold install issues two request classes against the same origin:
4//! tiny critical packuments (resolver-blocking) and large tarballs
5//! (fetch-phase, can stream lazily). Marking packuments urgent lets
6//! HTTP/2-aware origins schedule them ahead of pending tarball frames
7//! on the same connection.
8
9use std::fmt::Write;
10
11/// RFC 9218 §4.1 — request urgency, 0 = highest, 7 = lowest. Default
12/// 3 matches RFC §4.1 when no header is sent.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[repr(u8)]
15pub enum Urgency {
16    /// Resolver-blocking packument metadata.
17    Critical = 0,
18    /// Lockfile-driven tarball reads.
19    High = 1,
20    /// Default per RFC 9218 §4.1.
21    Default = 3,
22    /// Background prefetch / speculative fetches.
23    Background = 7,
24}
25
26/// Build the `Priority:` header value per RFC 9218 §4. `incremental`
27/// signals the server may deliver the response in chunks (true for
28/// tarballs, false for packuments where the consumer parses JSON whole).
29pub fn header_value(urgency: Urgency, incremental: bool) -> String {
30    let mut out = String::with_capacity(16);
31    let _ = write!(out, "u={}", urgency as u8);
32    if incremental {
33        out.push_str(", i");
34    }
35    out
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn header_default_is_u3() {
44        assert_eq!(header_value(Urgency::Default, false), "u=3");
45    }
46
47    #[test]
48    fn header_critical_packument() {
49        assert_eq!(header_value(Urgency::Critical, false), "u=0");
50    }
51
52    #[test]
53    fn header_tarball_incremental() {
54        assert_eq!(header_value(Urgency::High, true), "u=1, i");
55    }
56
57    #[test]
58    fn header_background_streaming() {
59        assert_eq!(header_value(Urgency::Background, true), "u=7, i");
60    }
61}