many improvements to the html generated

- css is now in src/style.css - included in the executable at compile time
- use writeln!/write! macros instead of push_str + format!
- properly generate the link to the .tar for the current directory

v0.6
This commit is contained in:
Damjan Georgievski 2018-07-22 05:49:19 +02:00
parent 1a12b3aa30
commit 592ea53524
3 changed files with 39 additions and 19 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "http-server" name = "http-server"
version = "0.5.0" version = "0.6.0"
authors = ["Damjan Georgievski <gdamjan@gmail.com>"] authors = ["Damjan Georgievski <gdamjan@gmail.com>"]
license = "MIT" license = "MIT"
readme = "README.md" readme = "README.md"

12
src/style.css Normal file
View file

@ -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;
}

View file

@ -6,6 +6,7 @@ use htmlescape::encode_minimal as escape_html_entity;
use channel; use channel;
use std::fmt::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std; use std;
use bytes; use bytes;
@ -31,36 +32,43 @@ fn handle_directory<'a, 'b>(
.filter_map(|entry| if dir.is_visible(&entry) { entry.ok() } else {None}) .filter_map(|entry| if dir.is_visible(&entry) { entry.ok() } else {None})
.collect(); .collect();
paths.sort_by_key(|r| (!r.metadata().unwrap().file_type().is_dir(), r.file_name())); 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(); let mut body = String::new();
body.push_str(&format!("<h1>Index of {index}</h1><small>[<a href={index}.tar>.tar</a> of whole directory]</small><hr> writeln!(body, "<h1>Index of {}</h1>", req.path()).unwrap();
<table> writeln!(body, r#"<small>[<a href="{}">.tar</a> of whole directory]</small>"#, tar_url).unwrap();
<tr><td>📁 <a href='../'>../</a></td><td>Size</td></tr>\n", index=req.path())); writeln!(body, "<table>").unwrap();
writeln!(body, "<tr><td>📁 <a href='../'>../</a></td><td>Size</td></tr>").unwrap();
for entry in paths { for entry in paths {
let meta = entry.metadata()?; let meta = entry.metadata()?;
let file_url = utf8_percent_encode(&entry.file_name().to_string_lossy(), DEFAULT_ENCODE_SET).to_string(); 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 file_name = escape_html_entity(&entry.file_name().to_string_lossy());
let size = meta.len(); let size = meta.len();
body.push_str("<tr>"); write!(body, "<tr>").unwrap();
if meta.file_type().is_dir() { if meta.file_type().is_dir() {
body.push_str(&format!(r#"<td>📂 <a href="{file_url}/">{file_name}/</a></td>"#, file_name=file_name, file_url=file_url)); writeln!(body, r#"<td>📂 <a href="{}/">{}/</a></td>"#, file_url, file_name).unwrap();
body.push_str(&format!(r#"<td><small>[<a href="{file_url}.tar">.tar</a>]</small></td>"#, file_url=file_url)); write!(body, r#" <td><small>[<a href="{}.tar">.tar</a>]</small></td>"#, file_url).unwrap();
} else { } else {
body.push_str(&format!(r#"<td>🗎 <a href="{file_url}">{file_name}</a></td>"#, file_name=file_name, file_url=file_url)); writeln!(body, r#"<td>🗎 <a href="{}">{}</a></td>"#, file_url, file_name).unwrap();
body.push_str(&format!("<td>{size}</td>", size=size)); write!(body, " <td>{}</td>", size).unwrap();
} }
body.push_str("</tr>\n"); writeln!(body, "</tr>").unwrap();
} }
body.push_str("</table><hr>\n"); writeln!(body, "</table>").unwrap();
let mut html = String::from(format!("<html> let mut html = String::new();
<head> writeln!(html, "<!DOCTYPE html>").unwrap();
<title>Index of {index}</title> writeln!(html, "<html><head>").unwrap();
<style>h1 {{margin-bottom: 0}} table {{width:100%}} table td:nth-child(2) {{text-align:right}}</style> writeln!(html, "<title>Index of {}</title>", req.path()).unwrap();
</head> writeln!(html, "<style>\n{}</style>", include_str!("style.css")).unwrap();
<body bgcolor='white'>\n", index=req.path())); writeln!(html, "</head>").unwrap();
html.push_str(body.as_str()); writeln!(html, "<body>\n{}</body>", body).unwrap();
html.push_str("</body></html>\n"); writeln!(html, "</html>").unwrap();
Ok(HttpResponse::Ok().content_type("text/html; charset=utf-8").body(html)) Ok(HttpResponse::Ok().content_type("text/html; charset=utf-8").body(html))
} }