From 1f218b074c98cb6b8e48e34f92b6bf2623a72eca Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 20 Nov 2023 13:23:52 +0100 Subject: fix index.html not being sent if index=false --- src/files.rs | 67 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 31 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index cf53942..733d045 100644 --- a/src/files.rs +++ b/src/files.rs @@ -53,27 +53,29 @@ pub async fn serve_files( let metadata = path.metadata()?; if metadata.file_type().is_dir() { - if !config.index { - return Err(ServiceError::NotFound); - } debug!("sending index for {path:?}"); - - if !rpath.ends_with("/") { - let mut r = Response::new(String::new()); - *r.status_mut() = StatusCode::FOUND; - r.headers_mut().insert( - LOCATION, - HeaderValue::from_str(&format!("{}/", rpath)).map_err(|_| ServiceError::Other)?, - ); - return Ok(r.map(|b| b.map_err(|e| match e {}).boxed())); + if let Ok(indexhtml) = read_to_string(path.join("index.html")).await { + return Ok(html_string_response(indexhtml)); } - return index(&path, rpath.to_string()).await.map(|s| { - let mut r = Response::new(s); - r.headers_mut() - .insert(CONTENT_TYPE, HeaderValue::from_static("text/html")); - r.map(|b| b.map_err(|e| match e {}).boxed()) - }); + if config.index { + if !rpath.ends_with("/") { + let mut r = Response::new(String::new()); + *r.status_mut() = StatusCode::FOUND; + r.headers_mut().insert( + LOCATION, + HeaderValue::from_str(&format!("{}/", rpath)) + .map_err(|_| ServiceError::Other)?, + ); + return Ok(r.map(|b| b.map_err(|e| match e {}).boxed())); + } + + return index(&path, rpath.to_string()) + .await + .map(html_string_response); + } else { + return Err(ServiceError::NotFound); + } } let range = req.headers().typed_get::(); @@ -216,20 +218,23 @@ async fn index(path: &Path, rpath: String) -> Result { .map(|e| e.and_then(|e| Ok((e.file_name().into_string().unwrap(), e.metadata()?)))) .filter(|e| e.as_ref().map(|(e, _)| !e.starts_with(".")).unwrap_or(true)) .collect::, _>>()?; - if let Ok(indexhtml) = read_to_string(path.join("index.html")).await { - Ok(indexhtml) - } else { - let banner = read_to_string(path.join("index.banner.html")).await.ok(); - let mut s = String::new(); - IndexTemplate { - files, - banner, - path: rpath, - } - .render(&mut s) - .unwrap(); - Ok(s) + let banner = read_to_string(path.join("index.banner.html")).await.ok(); + let mut s = String::new(); + IndexTemplate { + files, + banner, + path: rpath, } + .render(&mut s) + .unwrap(); + Ok(s) +} + +fn html_string_response(s: String) -> hyper::Response> { + let mut r = Response::new(s); + r.headers_mut() + .insert(CONTENT_TYPE, HeaderValue::from_static("text/html")); + r.map(|b| b.map_err(|e| match e {}).boxed()) } markup::define! { -- cgit v1.2.3-70-g09d2