diff --git a/Cargo.toml b/Cargo.toml index 82cd378..8a96b54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "http-server" -version = "0.5.0" +version = "0.6.0" authors = ["Damjan Georgievski "] license = "MIT" readme = "README.md" diff --git a/src/style.css b/src/style.css new file mode 100644 index 0000000..640ffb5 --- /dev/null +++ b/src/style.css @@ -0,0 +1,12 @@ +body {background-color: white} +h1 {margin-bottom: 0} +table td:nth-child(2) {text-align:right} +table { + width:100%; + margin: 1em auto; + padding: 0.5em 0; + border-top: 1px; + border-bottom: 1px; + border-color: #aaa; + border-style: solid none; +} diff --git a/src/web.rs b/src/web.rs index 72a6ecc..5ffb4bb 100644 --- a/src/web.rs +++ b/src/web.rs @@ -6,6 +6,7 @@ use htmlescape::encode_minimal as escape_html_entity; use channel; +use std::fmt::Write; use std::path::PathBuf; use std; use bytes; @@ -31,36 +32,43 @@ fn handle_directory<'a, 'b>( .filter_map(|entry| if dir.is_visible(&entry) { entry.ok() } else {None}) .collect(); paths.sort_by_key(|r| (!r.metadata().unwrap().file_type().is_dir(), r.file_name())); + + + let dir_tar_path = String::from(req.path().trim_right_matches('/')) + ".tar"; + let tar_url = utf8_percent_encode(&dir_tar_path, DEFAULT_ENCODE_SET).to_string(); + let mut body = String::new(); - body.push_str(&format!("

Index of {index}

[.tar of whole directory]
- - \n", index=req.path())); + writeln!(body, "

Index of {}

", req.path()).unwrap(); + writeln!(body, r#"[.tar of whole directory]"#, tar_url).unwrap(); + writeln!(body, "
📁 ../Size
").unwrap(); + writeln!(body, "").unwrap(); + for entry in paths { let meta = entry.metadata()?; let file_url = utf8_percent_encode(&entry.file_name().to_string_lossy(), DEFAULT_ENCODE_SET).to_string(); let file_name = escape_html_entity(&entry.file_name().to_string_lossy()); let size = meta.len(); - body.push_str(""); + write!(body, "").unwrap(); if meta.file_type().is_dir() { - body.push_str(&format!(r#""#, file_name=file_name, file_url=file_url)); - body.push_str(&format!(r#""#, file_url=file_url)); + writeln!(body, r#""#, file_url, file_name).unwrap(); + write!(body, r#" "#, file_url).unwrap(); } else { - body.push_str(&format!(r#""#, file_name=file_name, file_url=file_url)); - body.push_str(&format!("", size=size)); + writeln!(body, r#""#, file_url, file_name).unwrap(); + write!(body, " ", size).unwrap(); } - body.push_str("\n"); + writeln!(body, "").unwrap(); } - body.push_str("
📁 ../Size
📂 {file_name}/[.tar]📂 {}/[.tar]🗎 {file_name}{size}🗎 {}{}

\n"); + writeln!(body, "").unwrap(); - let mut html = String::from(format!(" - - Index of {index} - - - \n", index=req.path())); - html.push_str(body.as_str()); - html.push_str("\n"); + let mut html = String::new(); + writeln!(html, "").unwrap(); + writeln!(html, "").unwrap(); + writeln!(html, "Index of {}", req.path()).unwrap(); + writeln!(html, "", include_str!("style.css")).unwrap(); + writeln!(html, "").unwrap(); + writeln!(html, "\n{}", body).unwrap(); + writeln!(html, "").unwrap(); Ok(HttpResponse::Ok().content_type("text/html; charset=utf-8").body(html)) }