fix handling of special characters in file names
This commit is contained in:
@@ -3,6 +3,8 @@ use crate::webserver::database::ParsedSqlFile;
|
||||
use crate::{file_cache::FileCache, AppState};
|
||||
use awc::http::uri::PathAndQuery;
|
||||
use log::debug;
|
||||
use percent_encoding;
|
||||
use std::ffi::OsString;
|
||||
use std::path::{Path, PathBuf};
|
||||
use RoutingAction::{CustomNotFound, Execute, NotFound, Redirect, Serve};
|
||||
|
||||
@@ -88,7 +90,19 @@ where
|
||||
{
|
||||
match path_and_query.path().strip_prefix(config.prefix()) {
|
||||
None => Err(Redirect(config.prefix().to_string())),
|
||||
Some(path) => Ok(PathBuf::from(path)),
|
||||
Some(path) => {
|
||||
let decoded = percent_encoding::percent_decode_str(path);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::ffi::OsStringExt;
|
||||
let decoded = decoded.collect::<Vec<u8>>();
|
||||
Ok(PathBuf::from(OsString::from_vec(decoded)))
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
Ok(PathBuf::from(decoded.decode_utf8_lossy().as_ref()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -569,6 +569,17 @@ async fn test_static_files() {
|
||||
assert_eq!(&body, &b"It works !"[..]);
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_spaces_in_file_names() {
|
||||
let resp = req_path("/tests/spaces%20in%20file%20name.sql")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), http::StatusCode::OK);
|
||||
let body = test::read_body(resp).await;
|
||||
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||
assert!(body_str.contains("It works !"), "{body_str}");
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_with_site_prefix() {
|
||||
let mut config = test_config();
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
select 'text' as component, 'It works !' AS contents;
|
||||
Reference in New Issue
Block a user