front_line_router/http_version.rs
1/// Represents versions of the HTTP protocol.
2///
3/// Currently supports only HTTP/1.0 and HTTP/1.1.
4#[derive(Eq, PartialEq, Debug)]
5pub enum HttpVersion {
6 /// Represents the HTTP/1.0 version.
7 OneZero,
8
9 /// Represents the HTTP/1.1 version.
10 ///
11 /// This version includes features like persistent connections and chunked transfer-coding.
12 OneOne,
13}
14
15impl HttpVersion {
16 /// Parse an HTTP version from the given request line slice.
17 ///
18 /// This function will attempt to parse the provided slice and return the identified
19 /// HTTP version if recognized.
20 ///
21 /// # Arguments
22 ///
23 /// * `remaining_request_line` - A byte slice containing the part of the request
24 /// line representing the HTTP version.
25 ///
26 /// # Returns
27 ///
28 /// Returns `Some(HttpVersion)` if a valid HTTP version is identified. Otherwise,
29 /// returns `None`.
30 pub fn parse(remaining_request_line: &[u8]) -> Option<Self> {
31 if remaining_request_line == b"HTTP/1.1" {
32 return Some(HttpVersion::OneOne);
33 }
34 if remaining_request_line == b"HTTP/1.0" {
35 return Some(HttpVersion::OneZero);
36 }
37 None
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::HttpVersion;
44 use rstest::rstest;
45
46 #[rstest]
47 #[case(b"HTTP/1.1", Some(HttpVersion::OneOne))]
48 #[case(b"HTTP/1.0", Some(HttpVersion::OneZero))]
49 #[case(b"HTTP/0.9", None)]
50 #[case(b"HTTP/2.0", None)]
51 #[case(b"HTTPS/1.1", None)]
52 #[case(b"HTTP/1.10", None)]
53 #[case(b"HTTP/1.", None)]
54 fn test_http_version_parsing(#[case] input: &[u8], #[case] expected: Option<HttpVersion>) {
55 assert_eq!(HttpVersion::parse(input), expected);
56 }
57}