improv: #5 accept http 1.0 request version

This commit is contained in:
landrigun 2022-09-19 14:10:28 +01:00
parent 617c084782
commit 65e188bcf8

View File

@ -18,12 +18,13 @@ const HTTP_METHODS: [&'static str; 1] = ["POST"];
const HTTP_TARGETS: [&'static str; 3] = ["/validate/", "/get/", "/refresh/"]; const HTTP_TARGETS: [&'static str; 3] = ["/validate/", "/get/", "/refresh/"];
lazy_static! { 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)] #[derive(Debug)]
pub enum HTTPVersion { pub enum HTTPVersion {
Http1, Http1_0,
Http1_1,
Http2, Http2,
Unknown, Unknown,
} }
@ -31,7 +32,8 @@ pub enum HTTPVersion {
impl Into<String> for HTTPVersion { impl Into<String> for HTTPVersion {
fn into(self) -> String { fn into(self) -> String {
match self { 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::Http2 => "HTTP/2".to_string(),
Self::Unknown => "UNKNOWN".to_string(), Self::Unknown => "UNKNOWN".to_string(),
} }
@ -42,7 +44,8 @@ impl Into<String> for HTTPVersion {
impl From<&String> for HTTPVersion { impl From<&String> for HTTPVersion {
fn from(http_version: &String) -> Self { fn from(http_version: &String) -> Self {
match http_version.as_str() { 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, "HTTP/2" => Self::Http2,
_ => Self::Unknown, _ => Self::Unknown,
} }
@ -85,7 +88,6 @@ impl HTTPStartLine {
if !Self::check_target(&target) { if !Self::check_target(&target) {
return Err("target validation failed, unvalid target"); return Err("target validation failed, unvalid target");
} }
if !Self::check_version(&version) { if !Self::check_version(&version) {
return Err("http version validation failed, unknown version"); return Err("http version validation failed, unknown version");
} }
@ -272,7 +274,7 @@ fn test_handle_request() {
is_valid: bool, 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(), "POST /get/ HTTP/1.1\r\n\r\n".to_string(),
Expect { Expect {
@ -289,6 +291,14 @@ fn test_handle_request() {
is_valid: true, 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(), "GET / HTTP/1.1\r\n\r\n".to_string(),
Expect { Expect {