Clean url with suffix

This commit is contained in:
LittleSheep 2024-01-14 14:52:19 +08:00
parent 91ecf9d7bb
commit 905b70349b
5 changed files with 44 additions and 7 deletions

View File

@ -6,4 +6,4 @@ hosts = ["localhost"]
paths = ["/"]
[[locations.destinations]]
id = "static"
uri = "files://regions/index.html"
uri = "files://regions?index=index.html"

View File

@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Hello, World!</title>
</head>
<body>
<p>Hello, there!</p>
<p>
Here's the roadsign benchmarking test data! And you are in the subfolder
now!
</p>
</body>
</html>

View File

@ -98,20 +98,23 @@ impl Destination {
.get("utf8")
.and_then(|val| val.as_bool())
.unwrap_or(false),
with_slash: queries
.get("slash")
.and_then(|val| val.as_bool())
.unwrap_or(false),
browse: queries
.get("browse")
.and_then(|val| val.as_bool())
.unwrap_or(false),
with_slash: queries
.get("slash")
.and_then(|val| val.as_bool())
.unwrap_or(false),
index: queries
.get("index")
.and_then(|val| val.as_str().map(str::to_string)),
fallback: queries
.get("fallback")
.and_then(|val| val.as_str().map(str::to_string)),
suffix: queries
.get("suffix")
.and_then(|val| val.as_str().map(str::to_string)),
})
}
_ => Err(()),

View File

@ -20,7 +20,9 @@ pub fn scan_regions(basepath: String) -> io::Result<(Vec<config::Region>, u32)>
}
pub fn load_region(file: DirEntry) -> Result<config::Region, String> {
if file.path().extension().and_then(OsStr::to_str).unwrap() != "toml" {
if file.metadata().map(|val| val.is_dir()).unwrap()
|| file.path().extension().and_then(OsStr::to_str).unwrap() != "toml"
{
return Err("File entry wasn't toml file".to_string());
}

View File

@ -97,10 +97,11 @@ pub async fn respond_hypertext(
pub struct StaticResponderConfig {
pub uri: String,
pub utf8: bool,
pub with_slash: bool,
pub browse: bool,
pub with_slash: bool,
pub index: Option<String>,
pub fallback: Option<String>,
pub suffix: Option<String>,
}
pub async fn respond_static(
@ -142,6 +143,22 @@ pub async fn respond_static(
}
if !file_path.exists() {
if let Some(suffix) = cfg.suffix {
let file_name = file_path
.file_name()
.and_then(OsStr::to_str)
.unwrap()
.to_string();
file_path.pop();
file_path.push((file_name + &suffix).as_str());
if file_path.is_file() {
return Ok(StaticFileRequest::from_request_without_body(req)
.await?
.create_response(&file_path, cfg.utf8)?
.into_response());
}
}
if let Some(file) = cfg.fallback {
let fallback_path = base_path.join(file);
if fallback_path.is_file() {