浏览代码

fix: 修正路径处理逻辑和URL拼接问题

1. 在autoindex函数中添加路径格式验证,解决 '/pub' 路径404的bug
2. 修改render_list中的URL拼接逻辑,去除base_path和url中的多余斜杠

Signed-off-by: longjin <longjin@DragonOS.org>
longjin 1 周之前
父节点
当前提交
138fa75626
共有 2 个文件被更改,包括 6 次插入2 次删除
  1. 4 1
      src/main.rs
  2. 2 1
      src/render/mod.rs

+ 4 - 1
src/main.rs

@@ -123,12 +123,15 @@ fn validate_path(full_path: &PathBuf) -> Result<&str, HttpError> {
     })
 }
 
-#[get("/pub/{path:.*}")]
+#[get("/pub{path:.*}")]
 async fn autoindex(req: HttpRequest, path: web::Path<String>) -> HttpResponse {
     let base_path = BASE_PATH.to_string();
     log::debug!("Base path: {:?}", base_path);
     log::debug!("Request path: {:?}", path);
     let mut req_path = path.into_inner();
+    if !req_path.is_empty() && !req_path.starts_with('/') {
+        return HttpError::not_found("路径不存在", "请求的资源不存在").to_http_response();
+    }
 
     if req_path.is_empty() {
         req_path = "/".to_string();

+ 2 - 1
src/render/mod.rs

@@ -75,11 +75,12 @@ pub fn render_list(
     req_path: &str,
     src_entries: Vec<StorageEntry>,
 ) -> anyhow::Result<String> {
+    let base_path = base_path.trim_end_matches('/');
     let mut entries = Vec::new();
     entries.push(IndexDirEntry::parent_entry());
     src_entries.into_iter().for_each(|e| {
         let mut et: IndexDirEntry = e.into();
-        et.url = format!("{}/{}", base_path, et.url);
+        et.url = format!("{}/{}", base_path, et.url.trim_start_matches('/'));
         entries.push(et);
     });