Skip to content

Commit 0d31083

Browse files
committed
feat: 增加静态文件路径URL解码及未找到MIME类型处理
1 parent 8b04d5f commit 0d31083

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

silent/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ upgrade = ["dep:tokio-tungstenite"]
2323
multipart = ["tokio/fs", "dep:multer", "dep:multimap", "dep:tempfile", "dep:textnonce"]
2424
sse = ["dep:pin-project", "dep:tokio-stream"]
2525
security = ["dep:argon2", "dep:pbkdf2", "dep:aes-gcm", "dep:aes", "dep:rsa"]
26-
static = ["tokio/fs"]
26+
static = ["tokio/fs", "dep:urlencoding"]
2727
session = ["cookie", "dep:async-session"]
2828
cookie = ["dep:cookie"]
2929
template = ["dep:tera"]
@@ -61,6 +61,7 @@ http-body = "1.0.1"
6161
futures = "0.3.31"
6262
tokio-util = "0.7.13"
6363
anyhow = "1.0.95"
64+
urlencoding = { version = "2.1.3", optional = true }
6465

6566
# Scheduler
6667
cron = { version = "0.15.0", optional = true }

silent/src/handler/handler_wrapper_static.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ impl HandlerWrapperStatic {
3535
impl Handler for HandlerWrapperStatic {
3636
async fn call(&self, req: Request) -> Result<Response, SilentError> {
3737
if let Ok(file_path) = req.get_path_params::<String>("path") {
38+
// 文件路径使用url解码
39+
let file_path =
40+
urlencoding::decode(&file_path).map_err(|_| SilentError::BusinessError {
41+
code: StatusCode::NOT_FOUND,
42+
msg: "Not Found".to_string(),
43+
})?;
3844
let mut path = format!("{}/{}", self.path, file_path);
3945
if path.ends_with('/') {
4046
path.push_str("index.html");
@@ -43,6 +49,8 @@ impl Handler for HandlerWrapperStatic {
4349
let mut res = Response::empty();
4450
if let Some(content_type) = mime_guess::from_path(path).first() {
4551
res.set_typed_header(headers::ContentType::from(content_type));
52+
} else {
53+
res.set_typed_header(headers::ContentType::octet_stream());
4654
}
4755
let reader_stream = ReaderStream::new(file);
4856
let stream = reader_stream.boxed();

0 commit comments

Comments
 (0)