From c4b7c9e0d2425e0534b44aff8ad3c3a6bdc29d06 Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 9 Jul 2025 01:24:15 +0800 Subject: [PATCH] fix(auto_index): file path render error --- src/http/serve.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/http/serve.rs b/src/http/serve.rs index 80ac68d..431a04a 100644 --- a/src/http/serve.rs +++ b/src/http/serve.rs @@ -160,7 +160,7 @@ pub async fn serve( #[allow(clippy::unnecessary_to_owned)] let path = path.to_string(); if path.contains('.') { - (root.into(), vec![format!("{}{}", root, path)]) + (root.into(), vec![format!("{}/{}", root, path)]) } else { generate_default_index(&host_route, &format!("{root}/{path}")) } @@ -173,20 +173,23 @@ pub async fn serve( // - Return the first successfully streamed file. // - If all fail, return a `RouteNotFound` error. let mut path_exists = None; + // TODO: Check auto_index enable for path in path_arr { if fs::metadata(path.clone()).await.is_ok() { path_exists = Some(path); break; } } + debug!("path_exists: {:?}", path_exists); // 检查路径是否存在 // 不存时,检查是否开启自动生成目录索引 let path_exists = match path_exists { Some(path_exists) => path_exists, None => { let uri_path = uri.path(); - // 如果请求路径不以 / 结尾,则返回 301 Moved Permanently 状态码 - if !uri_path.ends_with('/') { + debug!("uri_path: {:?}", uri_path); + // 如果请求路径不是文件且不以 / 结尾,则返回 301 Moved Permanently 状态码 + if !uri_path.ends_with('/') && !uri_path.contains('.') { let mut response = Response::builder(); let stream = empty_stream().await?; let body = Body::from_stream(stream); @@ -575,7 +578,11 @@ async fn list_dir(host_root_str: &str, path: &PathBuf) -> anyhow::Result