Don't unwrap in parse_uri anymore

This commit is contained in:
flyingscorpio@clevo 2023-01-08 23:08:29 +01:00
parent 56881725aa
commit d02a4a9350

View file

@ -86,8 +86,7 @@ impl Ear {
} }
} }
/// Receives and parses a request from `receive_request` to dermine the function to call. /// Receives and parses a request to dermine the function to call.
/// We are expecting the request to be like 'GET /api/titre/jean'
fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> { fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> {
let method = match full_request.split_whitespace().nth(0) { let method = match full_request.split_whitespace().nth(0) {
Some("GET") => HttpMethod::Get, Some("GET") => HttpMethod::Get,
@ -96,8 +95,10 @@ fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> {
None => return Err(ReqParseError("Malformed URI".to_string())), None => return Err(ReqParseError("Malformed URI".to_string())),
}; };
let uri = full_request.split_whitespace().nth(1).unwrap(); let uri = match full_request.split_whitespace().nth(1) {
let uri = decode(uri).expect("UTF-8").to_string(); Some(uri) => decode(uri).expect("UTF-8").to_string(),
None => return Err(ReqParseError("Malformed URI".to_string())),
};
let uri = uri let uri = uri
.split("/") .split("/")
.filter(|val| !val.is_empty()) .filter(|val| !val.is_empty())