Major rewrite, automatic path, smaller binary

This commit is contained in:
Caileb 2025-06-19 10:38:25 -05:00
parent af221bedf7
commit 1e3bd82ae7
4 changed files with 414 additions and 586 deletions

View file

@ -1,414 +1,547 @@
// To hide console on Windows for GUI (but makes CLI output complex):
// #![cfg_attr(all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows")]
#![forbid(unsafe_code)]
#![forbid(clippy::all)]
use std::{
convert::Infallible,
env,
fs,
io::{self, Write},
net::{IpAddr, SocketAddr},
path::{Component, Path, PathBuf},
str::FromStr,
sync::Arc,
};
use anyhow::{bail, Context, Result};
use clap::Parser;
use http_body_util::Full;
use hyper::{
body::{Bytes, Incoming},
body::Bytes,
header::{CONTENT_TYPE, LOCATION},
server::conn::http1,
service::service_fn,
Method, Request, Response, StatusCode,
};
use hyper_util::rt::TokioIo;
use log::{debug, error, info, warn};
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use std::{
convert::Infallible,
env,
fs as std_fs,
io::{Read, Write},
net::{IpAddr, SocketAddr},
path::{Component, Path, PathBuf},
str::FromStr,
sync::Arc,
};
use tokio::net::TcpListener;
const APP_NAME: &str = clap::crate_name!();
const VERSION: &str = clap::crate_version!();
const APP_NAME: &str = "quickstart";
const VERSION: &str = env!("CARGO_PKG_VERSION");
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
const PATH_SEGMENT: &AsciiSet = &FRAGMENT.add(b'%').add(b'/').add(b'?').add(b'#');
// --- CLI Argument Parsing Setup ---
#[derive(Parser, Debug)]
#[clap(name = APP_NAME, version = VERSION, about = "A quick, simple HTTP file server. Auto-installs/updates on first run or version change.")]
struct Cli {
#[clap(subcommand)]
command: Option<Commands>,
type Body = http_body_util::Full<Bytes>;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
#[clap(short, long, default_value = ".", global = true)]
#[derive(Debug)]
struct Config {
dir: PathBuf,
#[clap(short, long, default_value_t = 8080, global = true)]
port: u16,
#[clap(short, long, value_parser = parse_ip_addr, default_value_t = IpAddr::from_str("0.0.0.0").unwrap(), global = true)]
bind: IpAddr,
}
#[derive(clap::Subcommand, Debug)]
enum Commands {
/// (Re)Install this application to a local directory for easy PATH access
Install,
impl Default for Config {
fn default() -> Self {
Self {
dir: PathBuf::from("."),
port: 8080,
bind: IpAddr::from_str("127.0.0.1").unwrap(),
}
}
}
fn parse_ip_addr(s: &str) -> Result<IpAddr, String> {
IpAddr::from_str(s).map_err(|e| e.to_string())
#[derive(Debug)]
enum ServeError {
NotFound,
Forbidden,
Io(io::Error),
}
impl From<io::Error> for ServeError {
fn from(err: io::Error) -> Self {
match err.kind() {
io::ErrorKind::NotFound => Self::NotFound,
io::ErrorKind::PermissionDenied => Self::Forbidden,
_ => Self::Io(err),
}
}
}
impl ServeError {
fn status_code(&self) -> StatusCode {
match self {
Self::NotFound => StatusCode::NOT_FOUND,
Self::Forbidden => StatusCode::FORBIDDEN,
Self::Io(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn message(&self) -> String {
match self {
Self::NotFound => "Not Found".to_string(),
Self::Forbidden => "Forbidden".to_string(),
Self::Io(e) => format!("Internal Server Error: {}", e),
}
}
}
// --- Installation Helper Functions ---
fn get_install_paths() -> Result<(PathBuf, PathBuf, PathBuf)> {
let home_dir = dirs::home_dir().context("Failed to determine home directory.")?;
let install_dir = home_dir.join(format!(".{}", APP_NAME.to_lowercase())).join("bin");
let exe_name = if cfg!(windows) { format!("{}.exe", APP_NAME) } else { APP_NAME.to_string() };
let home_dir = if let Some(home) = env::var_os("HOME") {
PathBuf::from(home)
} else if let Some(userprofile) = env::var_os("USERPROFILE") {
PathBuf::from(userprofile)
} else {
return Err("Could not determine home directory".into());
};
let install_dir = home_dir.join(format!(".{}", APP_NAME)).join("bin");
let exe_name = if cfg!(windows) {
format!("{}.exe", APP_NAME)
} else {
APP_NAME.to_string()
};
let exe_path = install_dir.join(&exe_name);
let version_path = install_dir.join(format!("{}.version", APP_NAME.to_lowercase()));
let version_path = install_dir.join(format!("{}.version", APP_NAME));
Ok((install_dir, exe_path, version_path))
}
fn read_version(path: &Path) -> Result<Option<String>> {
if !path.exists() { return Ok(None); }
let mut file = std_fs::File::open(path)?;
let mut version = String::new();
file.read_to_string(&mut version)?;
Ok(Some(version.trim().to_string()))
fn read_version(path: &Path) -> Option<String> {
fs::read_to_string(path).ok().map(|s| s.trim().to_string())
}
fn write_version(path: &Path, version: &str) -> Result<()> {
std_fs::create_dir_all(path.parent().unwrap())?;
std_fs::File::create(path)?.write_all(version.as_bytes())?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(path, version)?;
Ok(())
}
fn install_app(current_exe: &Path, target_dir: &Path, target_exe: &Path, version_path: &Path) -> Result<()> {
if current_exe == target_exe {
let installed_version = read_version(version_path)?;
let installed_version = read_version(version_path);
if installed_version.as_deref() != Some(VERSION) {
write_version(version_path, VERSION)?;
info!("Updated version file for executable at target location.");
println!("[{}] Updated version file to: {}", APP_NAME, VERSION);
}
return Ok(());
}
std_fs::create_dir_all(target_dir)?;
std_fs::copy(current_exe, target_exe)?;
fs::create_dir_all(target_dir)?;
fs::copy(current_exe, target_exe)?;
write_version(version_path, VERSION)?;
info!("Copied executable to {} and wrote version: {}", target_exe.display(), VERSION);
println!("[{}] Installed to: {}", APP_NAME, target_exe.display());
Ok(())
}
fn print_path_instructions(target_dir: &Path, target_exe: &Path) {
println!("\n--- {} Installation/Update ---", APP_NAME.to_uppercase());
println!("{} has been copied/updated to: {}", APP_NAME, target_exe.display());
println!("\nIMPORTANT: For easy access, add this directory to your PATH:");
println!(" {}", target_dir.display());
fn add_to_path_instructions(target_dir: &Path) {
println!("\n=== Manual PATH Setup ===");
if cfg!(windows) {
println!("\nFor example:");
println!(" - PowerShell (current session): $env:Path += \";{}\"", target_dir.display());
println!(" - Command Prompt (current session): set PATH=%PATH%;{}", target_dir.display());
println!(" - To make it permanent, search for 'environment variables' in Windows settings.");
println!("To add to PATH permanently on Windows:");
println!("1. Search 'environment variables' in Windows settings");
println!("2. Edit your user PATH variable");
println!("3. Add: {}", target_dir.display());
println!("4. RESTART your terminal/IDE");
println!("\nOr for current session only (PowerShell):");
println!(" $env:Path += \";{}\"", target_dir.display());
} else {
println!("\nFor example (add to ~/.bashrc or ~/.profile):");
println!("Add this line to your ~/.bashrc or ~/.profile:");
println!(" export PATH=\"{}:$PATH\"", target_dir.display());
println!("Then RESTART your terminal/IDE or run: source ~/.bashrc");
}
println!("\nOnce PATH is updated, you can run '{}' from any terminal.", APP_NAME);
println!("\nOnce PATH is updated, run '{}' from anywhere!", APP_NAME);
println!("==========================");
}
// --- Server Error Type ---
#[derive(Debug)]
enum ServeError {
NotFound,
Forbidden,
Io(std::io::Error),
}
impl From<std::io::Error> for ServeError {
fn from(err: std::io::Error) -> ServeError {
match err.kind() {
std::io::ErrorKind::NotFound => ServeError::NotFound,
std::io::ErrorKind::PermissionDenied => ServeError::Forbidden,
_ => ServeError::Io(err),
fn try_add_to_path_auto(target_dir: &Path) -> bool {
if cfg!(windows) {
// SAFE Windows PATH addition using PowerShell to read current PATH first
let target_str = target_dir.to_string_lossy();
// Use PowerShell to safely read and update PATH
let script = format!(
"$current = [Environment]::GetEnvironmentVariable('PATH', 'User'); \
if ($current -and $current -like '*{}*') {{ \
Write-Host 'Already in PATH' \
}} else {{ \
$new = if ($current) {{ \"$current;{}\" }} else {{ \"{}\" }}; \
[Environment]::SetEnvironmentVariable('PATH', $new, 'User'); \
Write-Host 'Added to PATH' \
}}",
target_str, target_str, target_str
);
let output = std::process::Command::new("powershell")
.args(&["-Command", &script])
.output();
if let Ok(output) = output {
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
if stdout.contains("Added to PATH") {
println!("[{}] ✓ Added to Windows PATH!", APP_NAME);
println!(" RESTART your terminal/IDE for PATH changes to take effect");
return true;
} else if stdout.contains("Already in PATH") {
println!("[{}] ✓ Already in Windows PATH", APP_NAME);
return true;
}
}
}
} else {
// Safe Linux/macOS PATH addition
if let Some(home) = env::var_os("HOME") {
let bashrc = PathBuf::from(home).join(".bashrc");
if let Ok(content) = fs::read_to_string(&bashrc) {
let target_str = target_dir.to_string_lossy();
if content.contains(&*target_str) {
println!("[{}] ✓ Already in ~/.bashrc PATH", APP_NAME);
return true;
}
if let Ok(mut file) = fs::OpenOptions::new().append(true).open(&bashrc) {
let path_line = format!("export PATH=\"{}:$PATH\"", target_str);
if writeln!(file, "\n# Added by {}", APP_NAME).is_ok() &&
writeln!(file, "{}", path_line).is_ok() {
println!("[{}] ✓ Added to ~/.bashrc PATH", APP_NAME);
println!(" RESTART your terminal/IDE or run: source ~/.bashrc");
return true;
}
}
}
}
}
false
}
impl std::fmt::Display for ServeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ServeError::NotFound => write!(f, "Not Found"),
ServeError::Forbidden => write!(f, "Forbidden"),
ServeError::Io(e) => write!(f, "IO Error: {}", e),
fn parse_args() -> Config {
let args: Vec<String> = env::args().collect();
let mut config = Config::default();
let mut i = 1;
while i < args.len() {
match args[i].as_str() {
"install" => {
// Handle explicit install command in main()
return config;
}
"-d" | "--dir" => {
if i + 1 < args.len() {
config.dir = PathBuf::from(&args[i + 1]);
i += 2;
} else {
eprintln!("Error: --dir requires a value");
std::process::exit(1);
}
}
"-p" | "--port" => {
if i + 1 < args.len() {
match args[i + 1].parse() {
Ok(port) => config.port = port,
Err(_) => {
eprintln!("Error: Invalid port number");
std::process::exit(1);
}
}
i += 2;
} else {
eprintln!("Error: --port requires a value");
std::process::exit(1);
}
}
"-b" | "--bind" => {
if i + 1 < args.len() {
match IpAddr::from_str(&args[i + 1]) {
Ok(ip) => config.bind = ip,
Err(_) => {
eprintln!("Error: Invalid IP address");
std::process::exit(1);
}
}
i += 2;
} else {
eprintln!("Error: --bind requires a value");
std::process::exit(1);
}
}
"-h" | "--help" => {
println!("Usage: {} [COMMAND] [OPTIONS]", args[0]);
println!("\nCommands:");
println!(" install Install to user directory and setup PATH");
println!("\nOptions:");
println!(" -d, --dir <DIR> Directory to serve [default: .]");
println!(" -p, --port <PORT> Port to listen on [default: 8080]");
println!(" -b, --bind <IP> IP address to bind to [default: 127.0.0.1]");
println!(" -h, --help Print this help message");
std::process::exit(0);
}
arg => {
eprintln!("Error: Unknown argument '{}'", arg);
eprintln!("Use --help for usage information");
std::process::exit(1);
}
}
}
}
impl std::error::Error for ServeError {}
// --- Main Entry Point ---
fn main() -> Result<()> {
let current_exe = env::current_exe().context("Failed to get current exe path")?;
config
}
#[tokio::main]
async fn main() -> Result<()> {
let current_exe = env::current_exe()?;
let (install_dir, install_path, version_path) = get_install_paths()?;
let mut explicit_install = false;
// Check for explicit install command
let args: Vec<String> = env::args().collect();
if args.len() > 1 && args[1] == "install" {
explicit_install = true;
}
if explicit_install {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
info!("Explicit install command detected.");
match install_app(&current_exe, &install_dir, &install_path, &version_path) {
Ok(_) => print_path_instructions(&install_dir, &install_path),
Err(e) => error!("Installation failed: {}", e),
println!("[{}] Installing...", APP_NAME);
install_app(&current_exe, &install_dir, &install_path, &version_path)?;
if !try_add_to_path_auto(&install_dir) {
add_to_path_instructions(&install_dir);
}
return Ok(());
}
let needs_update = if current_exe != install_path {
println!("[{}] Current executable is not at the standard install location.", APP_NAME);
// Auto-install/update logic
let needs_install = if current_exe != install_path {
println!("[{}] First run - installing to: {}", APP_NAME, install_path.display());
true
} else {
match read_version(&version_path) {
Ok(Some(ver)) if ver != VERSION => {
println!("[{}] Version mismatch: {} vs {}. Will update.", APP_NAME, ver, VERSION);
Some(ver) if ver != VERSION => {
println!("[{}] Updating from {} to {}", APP_NAME, ver, VERSION);
true
}
Ok(None) => {
println!("[{}] No version file found. Will write version info.", APP_NAME);
None => {
println!("[{}] Writing version info", APP_NAME);
true
}
Err(e) => {
eprintln!("[WARNING] Could not read version file: {}. Consider running `{} install`.", e, APP_NAME);
false
}
_ => false
}
};
if needs_update {
println!("[{}] Updating to: {}", APP_NAME, install_path.display());
match install_app(&current_exe, &install_dir, &install_path, &version_path) {
Ok(_) => {
print_path_instructions(&install_dir, &install_path);
println!("\n[{}] Proceeding to run from: {}", APP_NAME, current_exe.display());
}
Err(e) => {
eprintln!("[WARNING] Auto-update failed: {}. Will run from current location.", e);
eprintln!(" Try running `{} install` manually.", APP_NAME);
if needs_install {
if let Err(e) = install_app(&current_exe, &install_dir, &install_path, &version_path) {
eprintln!("[WARNING] Auto-install failed: {}. Running from current location.", e);
} else {
if !try_add_to_path_auto(&install_dir) {
add_to_path_instructions(&install_dir);
}
}
println!("---");
println!(); // Add spacing
}
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let config = parse_args();
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
rt.block_on(async {
let cli = Cli::parse();
if cli.command.is_some() {
warn!("Unhandled subcommand after initial checks.");
return Ok(());
}
run_server(cli.dir, cli.port, cli.bind).await
})
}
// --- Server Logic ---
async fn run_server(serve_dir_arg: PathBuf, port: u16, bind_ip: IpAddr) -> Result<()> {
let root_dir = tokio::fs::canonicalize(&serve_dir_arg).await
.with_context(|| format!("Failed to resolve path: {}", serve_dir_arg.display()))?;
let root_dir = fs::canonicalize(&config.dir)
.map_err(|e| format!("Failed to resolve path '{}': {}", config.dir.display(), e))?;
if !root_dir.is_dir() {
bail!("The specified path '{}' is not a directory.", root_dir.display());
return Err(format!("Path '{}' is not a directory", root_dir.display()).into());
}
let app_state = Arc::new(root_dir.clone());
let addr = SocketAddr::new(bind_ip, port);
info!("Serving directory: {}", root_dir.display());
info!("Listening on http://{}", addr);
info!("Press Ctrl+C to shut down.");
let addr = SocketAddr::new(config.bind, config.port);
println!("Serving {} at http://{}", root_dir.display(), addr);
println!("Press Ctrl+C to stop");
let listener = TcpListener::bind(addr).await?;
let root_dir = Arc::new(root_dir);
loop {
let (stream, remote_addr) = tokio::select! {
let (stream, _) = tokio::select! {
biased;
_ = shutdown_signal() => break,
res = listener.accept() => match res {
Ok(s_ra) => s_ra,
Err(e) => {
if e.kind() == std::io::ErrorKind::InvalidInput {
info!("Listener closed, shutting down.");
break;
}
error!("Failed to accept connection: {}", e);
continue;
}
}
_ = tokio::signal::ctrl_c() => break,
result = listener.accept() => result?,
};
debug!("Accepted connection from: {}", remote_addr);
let io = TokioIo::new(stream);
let app_state_clone = Arc::clone(&app_state);
tokio::task::spawn(async move {
let service = service_fn(move |req| handle_request(req, Arc::clone(&app_state_clone)));
if let Err(err) = http1::Builder::new().serve_connection(io, service).with_upgrades().await {
let root_dir = Arc::clone(&root_dir);
tokio::spawn(async move {
let io = TokioIo::new(stream);
let service = service_fn(move |req| handle_request(req, Arc::clone(&root_dir)));
if let Err(err) = http1::Builder::new()
.serve_connection(io, service)
.await
{
let err_str = err.to_string();
if !err_str.contains("connection closed") && !err_str.contains("reset by peer")
&& !err_str.contains("broken pipe") && !err_str.contains("unexpected end of file") {
warn!("Error serving connection from {}: {}", remote_addr, err);
if !err_str.contains("connection closed")
&& !err_str.contains("broken pipe")
&& !err_str.contains("reset by peer") {
eprintln!("Connection error: {}", err);
}
}
});
}
info!("Server shut down successfully.");
println!("Shutting down");
Ok(())
}
async fn handle_request(req: Request<Incoming>, root_dir: Arc<PathBuf>) -> Result<Response<Full<Bytes>>, Infallible> {
let response = match serve_path(&root_dir, req.uri().path(), &req).await {
async fn handle_request(
req: Request<hyper::body::Incoming>,
root_dir: Arc<PathBuf>
) -> std::result::Result<Response<Body>, Infallible> {
let response = match serve_request(&root_dir, &req).await {
Ok(resp) => resp,
Err(e) => {
warn!("Error serving path {}: {}", req.uri().path(), e);
let status = match e {
ServeError::NotFound => StatusCode::NOT_FOUND,
ServeError::Forbidden => StatusCode::FORBIDDEN,
ServeError::Io(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
Err(err) => {
Response::builder()
.status(status)
.status(err.status_code())
.header(CONTENT_TYPE, "text/plain")
.body(Full::from(status.canonical_reason().unwrap_or("Error").to_string()))
.body(Body::from(err.message()))
.unwrap()
}
};
Ok(response)
}
async fn serve_path(root_dir: &Path, req_path: &str, req: &Request<Incoming>) -> Result<Response<Full<Bytes>>, ServeError> {
async fn serve_request(
root_dir: &Path,
req: &Request<hyper::body::Incoming>
) -> std::result::Result<Response<Body>, ServeError> {
if req.method() != Method::GET && req.method() != Method::HEAD {
return Ok(Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
.header(CONTENT_TYPE, "text/plain")
.body(Full::from("Method Not Allowed"))
.body(Body::from("Method Not Allowed"))
.unwrap());
}
let req_path = req.uri().path();
let decoded_path = percent_encoding::percent_decode_str(req_path)
.decode_utf8()
.map_err(|_| ServeError::Forbidden)?;
let mut path_to_check = root_dir.to_path_buf();
let mut path = root_dir.to_path_buf();
for component in Path::new(decoded_path.as_ref()).components() {
match component {
Component::Normal(comp) => path_to_check.push(comp),
Component::Normal(comp) => path.push(comp),
Component::ParentDir => {
if path_to_check == *root_dir { return Err(ServeError::Forbidden); }
path_to_check.pop();
if path == root_dir {
return Err(ServeError::Forbidden);
}
path.pop();
}
_ => {}
_ => {}
}
}
let full_path = tokio::fs::canonicalize(&path_to_check).await?;
if !full_path.starts_with(root_dir) {
warn!("Path traversal attempt: '{}' resolved to '{}'", req_path, full_path.display());
let canonical_path = tokio::fs::canonicalize(&path).await?;
if !canonical_path.starts_with(root_dir) {
return Err(ServeError::Forbidden);
}
let metadata = tokio::fs::metadata(&full_path).await?;
let metadata = tokio::fs::metadata(&canonical_path).await?;
if metadata.is_dir() {
let index_path = full_path.join("index.html");
match tokio::fs::metadata(&index_path).await {
Ok(index_md) if index_md.is_file() => {
serve_file(&index_path, req.method() == Method::HEAD).await
}
_ => {
if !req_path.ends_with('/') {
return Ok(Response::builder()
.status(StatusCode::FOUND)
.header(LOCATION, format!("{}/", req_path))
.body(Full::from(Bytes::new()))
.unwrap());
}
list_directory(&full_path, root_dir, req_path).await
let index_path = canonical_path.join("index.html");
if tokio::fs::metadata(&index_path).await.map_or(false, |m| m.is_file()) {
serve_file(&index_path, req.method() == Method::HEAD).await
} else {
if !req_path.ends_with('/') {
return Ok(Response::builder()
.status(StatusCode::MOVED_PERMANENTLY)
.header(LOCATION, format!("{}/", req_path))
.body(Body::new(Bytes::new()))
.unwrap());
}
serve_directory(&canonical_path, root_dir, req_path).await
}
} else if metadata.is_file() {
serve_file(&full_path, req.method() == Method::HEAD).await
serve_file(&canonical_path, req.method() == Method::HEAD).await
} else {
Err(ServeError::NotFound)
}
}
async fn serve_file(file_path: &Path, is_head: bool) -> Result<Response<Full<Bytes>>, ServeError> {
let mime_type = mime_guess::from_path(file_path).first_or_octet_stream();
let body = if is_head {
Full::new(Bytes::new())
async fn serve_file(
file_path: &Path,
head_only: bool
) -> std::result::Result<Response<Body>, ServeError> {
let mime_type = mime_guess::from_path(file_path)
.first_or_octet_stream()
.to_string();
let body = if head_only {
Body::new(Bytes::new())
} else {
Full::from(tokio::fs::read(file_path).await?)
Body::from(tokio::fs::read(file_path).await?)
};
Ok(Response::builder()
.status(StatusCode::OK)
.header(CONTENT_TYPE, mime_type.as_ref())
.header(CONTENT_TYPE, mime_type)
.body(body)
.unwrap())
}
async fn list_directory(dir_path: &Path, root_dir: &Path, req_path: &str) -> Result<Response<Full<Bytes>>, ServeError> {
let mut body = format!(
"<html><head><meta charset=\"UTF-8\"><title>Index of {}</title></head><body><h1>Index of {}</h1><hr><pre>",
req_path, req_path
async fn serve_directory(
dir_path: &Path,
root_dir: &Path,
req_path: &str,
) -> std::result::Result<Response<Body>, ServeError> {
let title = format!("Index of {}", req_path);
let mut html = format!(
"<!DOCTYPE html>\
<html><head><meta charset=\"utf-8\"><title>{}</title>\
<style>body{{font-family:monospace;margin:40px}}\
a{{text-decoration:none;color:#0066cc}}\
a:hover{{text-decoration:underline}}</style>\
</head><body><h1>{}</h1><hr><pre>",
title, title
);
if dir_path != root_dir && req_path != "/" && !req_path.is_empty() {
let mut parent_uri_path = PathBuf::from(req_path.trim_end_matches('/'));
parent_uri_path.pop();
let parent_link = if parent_uri_path.as_os_str().is_empty() {
"/".to_string()
} else {
format!("{}/", parent_uri_path.to_string_lossy())
if dir_path != root_dir {
let parent_path = req_path.trim_end_matches('/');
let parent_link = if let Some(pos) = parent_path.rfind('/') {
if pos == 0 { "/" } else { &parent_path[..pos + 1] }
} else {
"/"
};
body.push_str(&format!("<a href=\"{}\">..</a>\n",
utf8_percent_encode(&parent_link, PATH_SEGMENT)));
html.push_str(&format!(
"<a href=\"{}\">..</a>\n",
utf8_percent_encode(parent_link, PATH_SEGMENT)
));
}
let mut entries = Vec::new();
let mut dir_iter = tokio::fs::read_dir(dir_path).await?;
while let Some(entry) = dir_iter.next_entry().await? {
entries.push(entry);
let mut dir_reader = tokio::fs::read_dir(dir_path).await?;
while let Some(entry) = dir_reader.next_entry().await? {
entries.push(entry);
}
entries.sort_by_key(|e| (!e.path().is_dir(), e.file_name().to_ascii_lowercase()));
entries.sort_by(|a, b| {
let a_is_dir = a.path().is_dir();
let b_is_dir = b.path().is_dir();
match (a_is_dir, b_is_dir) {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => a.file_name().to_ascii_lowercase().cmp(&b.file_name().to_ascii_lowercase()),
}
});
for entry in entries {
let name = entry.file_name().to_string_lossy().into_owned();
let mut link = name.clone();
if entry.file_type().await?.is_dir() { link.push('/'); }
let name = entry.file_name().to_string_lossy().to_string();
let is_dir = entry.file_type().await?.is_dir();
let display_name = if is_dir {
format!("{}/", name)
} else {
name
};
body.push_str(&format!("<a href=\"{}\">{}</a>\n",
utf8_percent_encode(&link, PATH_SEGMENT), name));
html.push_str(&format!(
"<a href=\"{}\">{}</a>\n",
utf8_percent_encode(&display_name, PATH_SEGMENT),
display_name
));
}
body.push_str("</pre><hr></body></html>");
html.push_str("</pre><hr></body></html>");
Ok(Response::builder()
.status(StatusCode::OK)
.header(CONTENT_TYPE, "text/html; charset=utf-8")
.body(Full::from(body))
.body(Body::from(html))
.unwrap())
}
async fn shutdown_signal() {
tokio::signal::ctrl_c().await.expect("Failed to install CTRL+C signal handler");
info!("Received Ctrl+C, shutting down gracefully...");
}