Skip to main content

trillium_http/
version.rs

1// originally from https://github.com/http-rs/http-types/blob/main/src/version.rs
2
3use crate::Error;
4use std::{
5    fmt::Display,
6    str::{self, FromStr},
7};
8
9/// The version of the HTTP protocol in use.
10#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
11#[non_exhaustive]
12pub enum Version {
13    /// HTTP/0.9
14    Http0_9,
15
16    /// HTTP/1.0
17    Http1_0,
18
19    /// HTTP/1.1
20    Http1_1,
21
22    /// HTTP/2
23    Http2,
24
25    /// HTTP/3
26    Http3,
27}
28
29#[cfg(feature = "serde")]
30impl serde::Serialize for Version {
31    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
32    where
33        S: serde::Serializer,
34    {
35        serializer.collect_str(self)
36    }
37}
38
39#[cfg(feature = "serde")]
40impl<'de> serde::Deserialize<'de> for Version {
41    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
42    where
43        D: serde::Deserializer<'de>,
44    {
45        String::deserialize(deserializer)?
46            .parse()
47            .map_err(serde::de::Error::custom)
48    }
49}
50
51impl PartialEq<&Version> for Version {
52    #[allow(
53        clippy::unconditional_recursion,
54        reason = "*other deref'd to &Version dispatches to the derived PartialEq, not back to \
55                  this impl"
56    )]
57    fn eq(&self, other: &&Version) -> bool {
58        self == *other
59    }
60}
61
62impl PartialEq<Version> for &Version {
63    #[allow(
64        clippy::unconditional_recursion,
65        reason = "*self deref'd to Version dispatches to the derived PartialEq, not back to this \
66                  impl"
67    )]
68    fn eq(&self, other: &Version) -> bool {
69        *self == other
70    }
71}
72
73impl Version {
74    /// returns the http version as a static str, such as "HTTP/1.1"
75    pub const fn as_str(&self) -> &'static str {
76        match self {
77            Version::Http0_9 => "HTTP/0.9",
78            Version::Http1_0 => "HTTP/1.0",
79            Version::Http1_1 => "HTTP/1.1",
80            Version::Http2 => "HTTP/2",
81            Version::Http3 => "HTTP/3",
82        }
83    }
84
85    pub(crate) fn parse(buf: &[u8]) -> crate::Result<Self> {
86        // The request-line HTTP-version is case-sensitive (`HTTP-name` is the literal uppercase
87        // bytes), unlike the lenient `FromStr` used elsewhere. Only HTTP/1.x can appear in an h1
88        // request-line — h2/h3 use binary framing — so a higher minor of our major is processed as
89        // 1.1 and everything else (`HTTP/2.0`, lowercase, garbage) is a malformed
90        // request-line (`InvalidVersion` → 400), not an unsupported major.
91        match buf {
92            b"HTTP/1.0" => Ok(Self::Http1_0),
93            [b'H', b'T', b'T', b'P', b'/', b'1', b'.', b'1'..=b'9'] => Ok(Self::Http1_1),
94            _ => Err(Error::InvalidVersion),
95        }
96    }
97}
98
99impl FromStr for Version {
100    type Err = Error;
101
102    fn from_str(s: &str) -> Result<Self, Self::Err> {
103        match s {
104            "HTTP/0.9" | "http/0.9" | "0.9" => Ok(Self::Http0_9),
105            "HTTP/1.0" | "http/1.0" | "1.0" => Ok(Self::Http1_0),
106            "HTTP/1.1" | "http/1.1" | "1.1" => Ok(Self::Http1_1),
107            "HTTP/2" | "http/2" | "2" => Ok(Self::Http2),
108            "HTTP/3" | "http/3" | "3" => Ok(Self::Http3),
109            _ => Err(Error::InvalidVersion),
110        }
111    }
112}
113
114impl AsRef<str> for Version {
115    fn as_ref(&self) -> &str {
116        self.as_str()
117    }
118}
119
120impl AsRef<[u8]> for Version {
121    fn as_ref(&self) -> &[u8] {
122        self.as_str().as_bytes()
123    }
124}
125
126impl Display for Version {
127    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
128        f.write_str(self.as_ref())
129    }
130}
131
132#[cfg(test)]
133mod test {
134    use super::*;
135    #[test]
136    fn from_str() {
137        let versions = [
138            Version::Http0_9,
139            Version::Http1_0,
140            Version::Http1_1,
141            Version::Http2,
142            Version::Http3,
143        ];
144
145        for version in versions {
146            assert_eq!(version.as_str().parse::<Version>().unwrap(), version);
147            assert_eq!(version.to_string().parse::<Version>().unwrap(), version);
148        }
149
150        assert_eq!(
151            "not a version".parse::<Version>().unwrap_err().to_string(),
152            "Invalid or missing version"
153        );
154    }
155
156    #[test]
157    fn eq() {
158        assert_eq!(Version::Http1_1, Version::Http1_1);
159        assert_eq!(Version::Http1_1, &Version::Http1_1);
160        assert_eq!(&Version::Http1_1, Version::Http1_1);
161    }
162
163    #[test]
164    fn to_string() {
165        let output = format!(
166            "{} {} {} {} {}",
167            Version::Http0_9,
168            Version::Http1_0,
169            Version::Http1_1,
170            Version::Http2,
171            Version::Http3
172        );
173        assert_eq!("HTTP/0.9 HTTP/1.0 HTTP/1.1 HTTP/2 HTTP/3", output);
174    }
175
176    #[test]
177    fn ord() {
178        use Version::{Http0_9, Http1_0, Http1_1, Http2, Http3};
179        assert!(Http3 > Http2);
180        assert!(Http2 > Http1_1);
181        assert!(Http1_1 > Http1_0);
182        assert!(Http1_0 > Http0_9);
183    }
184}