use crate::guard::{is_hidden, may_be_served};
use crate::serve::INDEX_FILE;
use axum::extract::Request;
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use http::{Method, StatusCode, header};
use percent_encoding::{AsciiSet, NON_ALPHANUMERIC, percent_decode_str, utf8_percent_encode};
use std::fmt::Write;
use std::path::{Component, Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
const ASKS_FOR_THE_LIST: &str = "list";
const IN_A_LINK: &AsciiSet = &NON_ALPHANUMERIC
.remove(b'-')
.remove(b'.')
.remove(b'_')
.remove(b'~');
const STYLE: &str = "
:root { color-scheme: light dark; }
body { max-width: 64rem; margin: 2rem auto; padding: 0 1rem; font: 15px/1.5 system-ui, sans-serif; }
h1 { margin: 0 0 1rem; font-size: 1.25rem; font-weight: 600; overflow-wrap: anywhere; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-weight: 500; opacity: .65; border-bottom: 1px solid rgba(127, 127, 127, .3); }
th, td { padding: .3rem .6rem; }
td:first-child { overflow-wrap: anywhere; }
th + th, td + td { width: 1%; white-space: nowrap; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: rgba(127, 127, 127, .1); }
.size { text-align: right; }
.empty { opacity: .65; }
a { text-decoration: none; }
a:hover { text-decoration: underline; }
";
const LOCAL_TIME: &str = r#"{
const two = (number) => String(number).padStart(2, "0");
for (const time of document.querySelectorAll("time")) {
const at = new Date(time.dateTime);
if (Number.isNaN(at.getTime())) continue;
time.textContent = `${at.getFullYear()}-${two(at.getMonth() + 1)}-${two(at.getDate())} ${two(at.getHours())}:${two(at.getMinutes())}`;
}
}"#;
pub(crate) async fn serve_file_list(root: PathBuf, request: Request, next: Next) -> Response {
let address = request.uri().path().to_string();
let asked = request.uri().query() == Some(ASKS_FOR_THE_LIST);
let reads = matches!(*request.method(), Method::GET | Method::HEAD);
if !reads || !address.ends_with('/') {
return next.run(request).await;
}
if asked {
return match list(root, &address, asked).await {
Some(list) => list,
None => next.run(request).await,
};
}
let response = next.run(request).await;
if response.status() != StatusCode::NOT_FOUND {
return response;
}
list(root, &address, asked).await.unwrap_or(response)
}
async fn list(root: PathBuf, address: &str, asked: bool) -> Option<Response> {
let folder = folder_at(&root, address)?;
let at_top = folder == root;
let entries = tokio::task::spawn_blocking(move || {
let has_a_page = folder.join(INDEX_FILE).symlink_metadata().is_ok();
if has_a_page && !asked {
return None;
}
read(&root, &folder)
})
.await
.ok()
.flatten()?;
let shown = percent_decode_str(address).decode_utf8_lossy();
let html = page(&shown, &entries, at_top, asked);
Some(([(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response())
}
fn folder_at(root: &Path, address: &str) -> Option<PathBuf> {
let decoded = percent_decode_str(address.trim_start_matches('/'))
.decode_utf8()
.ok()?;
let mut folder = root.to_path_buf();
for part in Path::new(&*decoded).components() {
match part {
Component::Normal(name)
if Path::new(name)
.components()
.all(|inner| matches!(inner, Component::Normal(_))) =>
{
folder.push(name);
}
Component::CurDir => {}
_ => return None,
}
}
Some(folder)
}
struct Entry {
name: String,
is_folder: bool,
size: u64,
modified: Option<SystemTime>,
}
fn read(root: &Path, folder: &Path) -> Option<Vec<Entry>> {
if !folder.is_dir() || !may_be_served(root, folder) {
return None;
}
let mut entries = Vec::new();
for found in std::fs::read_dir(folder).ok()?.flatten() {
let Ok(name) = found.file_name().into_string() else {
continue;
};
let is_link = found.file_type().is_ok_and(|kind| kind.is_symlink());
if is_hidden(&name) || (is_link && !may_be_served(root, &found.path())) {
continue;
}
let Ok(details) = std::fs::metadata(found.path()) else {
continue;
};
if !details.is_dir() && !details.is_file() {
continue;
}
entries.push(Entry {
name,
is_folder: details.is_dir(),
size: details.len(),
modified: details.modified().ok(),
});
}
entries.sort_by_cached_key(|entry| {
(
!entry.is_folder,
entry.name.to_lowercase(),
entry.name.clone(),
)
});
Some(entries)
}
fn page(shown: &str, entries: &[Entry], at_top: bool, asked: bool) -> String {
let title = escape(shown);
let asking = if asked { "?list" } else { "" };
let mut html = format!(
r#"<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Index of {title}</title>
<style>{STYLE}</style>
</head>
<body>
<h1>Index of {title}</h1>
<table>
<thead><tr><th>Name</th><th class="size">Size</th><th>Modified</th></tr></thead>
<tbody>
"#
);
if !at_top {
let _ = writeln!(
html,
r#"<tr><td><a href="../{asking}">../</a></td><td></td><td></td></tr>"#
);
}
if entries.is_empty() {
html.push_str(r#"<tr><td class="empty" colspan="3">This folder is empty</td></tr>"#);
html.push('\n');
}
for entry in entries {
let name = escape(&entry.name);
let link = utf8_percent_encode(&entry.name, IN_A_LINK);
let written = entry.modified.map(written_at).unwrap_or_default();
if entry.is_folder {
let _ = writeln!(
html,
r#"<tr><td><a href="{link}/{asking}">{name}/</a></td><td class="size"></td><td>{written}</td></tr>"#
);
} else {
let size = readable_size(entry.size);
let _ = writeln!(
html,
r#"<tr><td><a href="{link}">{name}</a></td><td class="size">{size}</td><td>{written}</td></tr>"#
);
}
}
let _ = writeln!(
html,
"</tbody>\n</table>\n<script>{LOCAL_TIME}</script>\n</body>\n</html>"
);
html
}
fn escape(text: &str) -> String {
let mut escaped = String::with_capacity(text.len());
for character in text.chars() {
match character {
'&' => escaped.push_str("&"),
'<' => escaped.push_str("<"),
'>' => escaped.push_str(">"),
'"' => escaped.push_str("""),
'\'' => escaped.push_str("'"),
_ => escaped.push(character),
}
}
escaped
}
fn readable_size(bytes: u64) -> String {
const UNITS: [&str; 4] = ["KB", "MB", "GB", "TB"];
if bytes < 1024 {
return format!("{bytes} B");
}
let mut size = bytes as f64 / 1024.0;
let mut unit = 0;
while size >= 1023.95 && unit < UNITS.len() - 1 {
size /= 1024.0;
unit += 1;
}
format!("{size:.1} {}", UNITS[unit])
}
fn written_at(time: SystemTime) -> String {
let (date, minute) = utc_minute(seconds_since_1970(time));
format!(r#"<time datetime="{date}T{minute}Z">{date} {minute} UTC</time>"#)
}
fn seconds_since_1970(time: SystemTime) -> i64 {
match time.duration_since(UNIX_EPOCH) {
Ok(after) => i64::try_from(after.as_secs()).unwrap_or(i64::MAX),
Err(before) => {
let gap = before.duration();
let whole = i64::try_from(gap.as_secs()).unwrap_or(i64::MAX);
-whole - i64::from(gap.subsec_nanos() > 0)
}
}
}
fn utc_minute(seconds: i64) -> (String, String) {
let days = seconds.div_euclid(86_400);
let minutes = seconds.rem_euclid(86_400) / 60;
let from_march = days + 719_468;
let era = from_march.div_euclid(146_097);
let day_of_era = from_march.rem_euclid(146_097);
let year_of_era =
(day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
let month_from_march = (5 * day_of_year + 2) / 153;
let day = day_of_year - (153 * month_from_march + 2) / 5 + 1;
let month = if month_from_march < 10 {
month_from_march + 3
} else {
month_from_march - 9
};
let year = year_of_era + era * 400 + i64::from(month <= 2);
(
format!("{year:04}-{month:02}-{day:02}"),
format!("{:02}:{:02}", minutes / 60, minutes % 60),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_address_names_a_folder_only_inside_the_served_directory() {
let root = Path::new("/site");
assert_eq!(folder_at(root, "/"), Some(root.to_path_buf()));
assert_eq!(
folder_at(root, "/docs/api/"),
Some(root.join("docs").join("api"))
);
assert_eq!(folder_at(root, "/./docs/"), Some(root.join("docs")));
assert_eq!(folder_at(root, "/caf%C3%A9/"), Some(root.join("café")));
for address in ["/../", "/docs/../../", "/%2e%2e/", "/%2Fetc/", "/%FF/"] {
assert_eq!(folder_at(root, address), None, "{address}");
}
}
#[test]
fn a_name_in_a_link_stays_one_name() {
let link = |name: &str| utf8_percent_encode(name, IN_A_LINK).to_string();
assert_eq!(link("a b#c?d:e%f"), "a%20b%23c%3Fd%3Ae%25f");
assert_eq!(link("café"), "caf%C3%A9");
assert_eq!(link("A-z_0.9~"), "A-z_0.9~");
}
#[test]
fn a_name_on_the_page_stays_text() {
assert_eq!(
escape(r#"<a href="x">'&'</a>"#),
"<a href="x">'&'</a>"
);
}
#[test]
fn sizes_read_as_a_person_would_say_them() {
assert_eq!(readable_size(0), "0 B");
assert_eq!(readable_size(1023), "1023 B");
assert_eq!(readable_size(1024), "1.0 KB");
assert_eq!(readable_size(1536), "1.5 KB");
assert_eq!(readable_size(1024 * 1024 - 1), "1.0 MB");
assert_eq!(readable_size(5 * 1024 * 1024 * 1024), "5.0 GB");
}
#[test]
fn a_time_before_1970_falls_in_the_second_before_it() {
use std::time::Duration;
let before = |millis| UNIX_EPOCH - Duration::from_millis(millis);
assert_eq!(seconds_since_1970(before(500)), -1);
assert_eq!(seconds_since_1970(before(1_000)), -1);
assert_eq!(seconds_since_1970(before(60_500)), -61);
assert_eq!(
seconds_since_1970(UNIX_EPOCH + Duration::from_millis(1_500)),
1
);
assert_eq!(utc_minute(seconds_since_1970(before(500))).1, "23:59");
}
#[test]
fn times_are_written_in_utc_to_the_minute() {
for (seconds, date, minute) in [
(0, "1970-01-01", "00:00"),
(951_868_740, "2000-02-29", "23:59"),
(1_789_397_460, "2026-09-14", "14:51"),
(-60, "1969-12-31", "23:59"),
] {
assert_eq!(
utc_minute(seconds),
(date.to_string(), minute.to_string()),
"{seconds}"
);
}
}
}