rama_http/matcher/
version.rs

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
use crate::{Request, Version};
use rama_core::{context::Extensions, Context};
use std::fmt::{self, Debug, Formatter};

/// A matcher that matches one or more HTTP methods.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct VersionMatcher(u16);

impl VersionMatcher {
    /// A matcher that matches HTTP/0.9 requests.
    pub const HTTP_09: Self = Self::from_bits(0b0_0000_0010);

    /// A matcher that matches HTTP/1.0 requests.
    pub const HTTP_10: Self = Self::from_bits(0b0_0000_0100);

    /// A matcher that matches HTTP/1.1 requests.
    pub const HTTP_11: Self = Self::from_bits(0b0_0000_1000);

    /// A matcher that matches HTTP/2.0 (h2) requests.
    pub const HTTP_2: Self = Self::from_bits(0b0_0001_0000);

    /// A matcher that matches HTTP/3.0 (h3) requests.
    pub const HTTP_3: Self = Self::from_bits(0b0_0010_0000);

    const fn bits(&self) -> u16 {
        let bits = self;
        bits.0
    }

    const fn from_bits(bits: u16) -> Self {
        Self(bits)
    }

    pub(crate) const fn contains(&self, other: Self) -> bool {
        self.bits() & other.bits() == other.bits()
    }

    /// Performs the OR operation between the [`VersionMatcher`] in `self` with `other`.
    pub const fn or(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }
}

impl<State, Body> rama_core::matcher::Matcher<State, Request<Body>> for VersionMatcher {
    /// returns true on a match, false otherwise
    fn matches(
        &self,
        _ext: Option<&mut Extensions>,
        _ctx: &Context<State>,
        req: &Request<Body>,
    ) -> bool {
        VersionMatcher::try_from(req.version())
            .ok()
            .map(|version| self.contains(version))
            .unwrap_or_default()
    }
}

/// Error type used when converting a [`Version`] to a [`VersionMatcher`] fails.
#[derive(Debug)]
pub struct NoMatchingVersionMatcher {
    version: Version,
}

impl NoMatchingVersionMatcher {
    /// Get the [`Version`] that couldn't be converted to a [`VersionMatcher`].
    pub fn version(&self) -> &Version {
        &self.version
    }
}

impl fmt::Display for NoMatchingVersionMatcher {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "no `VersionMatcher` for `{:?}`", self.version)
    }
}

impl std::error::Error for NoMatchingVersionMatcher {}

impl TryFrom<Version> for VersionMatcher {
    type Error = NoMatchingVersionMatcher;

    fn try_from(m: Version) -> Result<Self, Self::Error> {
        match m {
            Version::HTTP_09 => Ok(VersionMatcher::HTTP_09),
            Version::HTTP_10 => Ok(VersionMatcher::HTTP_10),
            Version::HTTP_11 => Ok(VersionMatcher::HTTP_11),
            Version::HTTP_2 => Ok(VersionMatcher::HTTP_2),
            Version::HTTP_3 => Ok(VersionMatcher::HTTP_3),
            other => Err(Self::Error { version: other }),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use rama_core::matcher::Matcher;

    #[test]
    fn test_version_matcher() {
        let matcher = VersionMatcher::HTTP_11;
        let req = Request::builder()
            .version(Version::HTTP_11)
            .body(())
            .unwrap();
        assert!(matcher.matches(None, &Context::default(), &req));
    }

    #[test]
    fn test_version_matcher_any() {
        let matcher = VersionMatcher::HTTP_11
            .or(VersionMatcher::HTTP_10)
            .or(VersionMatcher::HTTP_11);

        let req = Request::builder()
            .version(Version::HTTP_10)
            .body(())
            .unwrap();
        assert!(matcher.matches(None, &Context::default(), &req));

        let req = Request::builder()
            .version(Version::HTTP_11)
            .body(())
            .unwrap();
        assert!(matcher.matches(None, &Context::default(), &req));

        let req = Request::builder()
            .version(Version::HTTP_2)
            .body(())
            .unwrap();
        assert!(!matcher.matches(None, &Context::default(), &req));
    }

    #[test]
    fn test_version_matcher_fail() {
        let matcher = VersionMatcher::HTTP_11;
        let req = Request::builder()
            .version(Version::HTTP_10)
            .body(())
            .unwrap();
        assert!(!matcher.matches(None, &Context::default(), &req));
    }
}