ignore trailing slashes and query params
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -13,6 +13,22 @@ enum ResponseCode {
|
||||
NotFound,
|
||||
}
|
||||
|
||||
/// Remove query parameters and trailing slashes.
|
||||
/// Makes all paths lowercase.
|
||||
fn normalise_path(path: &str) -> String {
|
||||
let mut path = path
|
||||
.split('?')
|
||||
.next()
|
||||
.expect("split operation returns at least one substring");
|
||||
if path.len() > 1
|
||||
&& let Some('/') = path.chars().next_back()
|
||||
{
|
||||
path = &path[0..path.len() - 1];
|
||||
}
|
||||
|
||||
path.to_lowercase()
|
||||
}
|
||||
|
||||
async fn simple_response(mut socket: BufStream<TcpStream>, rc: ResponseCode) {
|
||||
let rc = match rc {
|
||||
ResponseCode::BadRequest => "400 Bad Request",
|
||||
@@ -46,7 +62,8 @@ async fn process_socket(mut socket: BufStream<TcpStream>, urlmap: Arc<HashMap<St
|
||||
simple_response(socket, ResponseCode::BadRequest).await;
|
||||
return;
|
||||
};
|
||||
let path = path.to_lowercase();
|
||||
|
||||
let path = normalise_path(path);
|
||||
|
||||
if http_method != "GET" {
|
||||
println!("process_request: forbidden method");
|
||||
@@ -103,7 +120,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if url_pair
|
||||
.chars()
|
||||
.next()
|
||||
.expect("String is longer than 1 character")
|
||||
.expect("String at least 1 character")
|
||||
!= '/'
|
||||
{
|
||||
continue;
|
||||
@@ -115,7 +132,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
Err(format!("Unable to parse line: '{}'", url_pair))?
|
||||
};
|
||||
|
||||
urlmap.insert(slug.to_lowercase(), redirect.to_string());
|
||||
urlmap.insert(normalise_path(slug), redirect.to_string());
|
||||
}
|
||||
|
||||
let urlmap = Arc::new(urlmap);
|
||||
|
||||
Reference in New Issue
Block a user