From 65e188bcf83b080e7a0cbba99d9d350e89d62e37 Mon Sep 17 00:00:00 2001 From: landrigun Date: Mon, 19 Sep 2022 14:10:28 +0100 Subject: [PATCH] improv: #5 accept http 1.0 request version --- src/handlers/request.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/handlers/request.rs b/src/handlers/request.rs index 323ba07..77bc465 100644 --- a/src/handlers/request.rs +++ b/src/handlers/request.rs @@ -18,12 +18,13 @@ const HTTP_METHODS: [&'static str; 1] = ["POST"]; const HTTP_TARGETS: [&'static str; 3] = ["/validate/", "/get/", "/refresh/"]; lazy_static! { - static ref HTTP_VERSION_REGEX: Regex = Regex::new("^HTTP/(1.1|2)$").unwrap(); + static ref HTTP_VERSION_REGEX: Regex = Regex::new("^HTTP/(1.1|1.0|2)$").unwrap(); } #[derive(Debug)] pub enum HTTPVersion { - Http1, + Http1_0, + Http1_1, Http2, Unknown, } @@ -31,7 +32,8 @@ pub enum HTTPVersion { impl Into for HTTPVersion { fn into(self) -> String { match self { - Self::Http1 => "HTTP/1.1".to_string(), + Self::Http1_0 => "HTTP/1.0".to_string(), + Self::Http1_1 => "HTTP/1.1".to_string(), Self::Http2 => "HTTP/2".to_string(), Self::Unknown => "UNKNOWN".to_string(), } @@ -42,7 +44,8 @@ impl Into for HTTPVersion { impl From<&String> for HTTPVersion { fn from(http_version: &String) -> Self { match http_version.as_str() { - "HTTP/1.1" => Self::Http1, + "HTTP/1.0" => Self::Http1_0, + "HTTP/1.1" => Self::Http1_1, "HTTP/2" => Self::Http2, _ => Self::Unknown, } @@ -85,7 +88,6 @@ impl HTTPStartLine { if !Self::check_target(&target) { return Err("target validation failed, unvalid target"); } - if !Self::check_version(&version) { return Err("http version validation failed, unknown version"); } @@ -272,7 +274,7 @@ fn test_handle_request() { is_valid: bool, } - let test_cases: [(String, Expect); 10] = [ + let test_cases: [(String, Expect); 11] = [ ( "POST /get/ HTTP/1.1\r\n\r\n".to_string(), Expect { @@ -289,6 +291,14 @@ fn test_handle_request() { is_valid: true, }, ), + ( + "POST /validate/ HTTP/1.0\r\n\r\n".to_string(), + Expect { + start_line: "POST /validate/ HTTP/1.0".to_string(), + body: None, + is_valid: true, + }, + ), ( "GET / HTTP/1.1\r\n\r\n".to_string(), Expect {