forked from brson/basic-http-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext.rs
More file actions
169 lines (151 loc) · 5.37 KB
/
ext.rs
File metadata and controls
169 lines (151 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Developer extensions for basic-http-server
use super::{Config, HtmlCfg};
use super::{Error, Result};
use comrak::ComrakOptions;
use futures::{future, StreamExt};
use http::{Request, Response, StatusCode};
use hyper::{header, Body};
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use std::ffi::OsStr;
use std::fmt::Write;
use std::io;
use std::path::{Path, PathBuf};
use tokio_fs::DirEntry;
pub async fn serve(
config: Config,
req: Request<Body>,
resp: super::Result<Response<Body>>,
) -> Result<Response<Body>> {
trace!("checking extensions");
if !config.use_extensions {
return resp;
}
let path = super::local_path_for_request(&req.uri(), &config.root_dir);
if path.is_none() {
return resp;
}
let path = path.unwrap();
let file_ext = path.extension().and_then(OsStr::to_str).unwrap_or("");
if file_ext == "md" {
trace!("using markdown extension");
return md_path_to_html(&path).await;
}
if let Err(e) = resp {
match e {
Error::Io(e) => {
if e.kind() == io::ErrorKind::NotFound {
let list_dir_resp = maybe_list_dir(&config.root_dir, &path).await?;
trace!("using directory list extension");
if let Some(f) = list_dir_resp {
Ok(f)
} else {
Err(Error::from(e))
}
} else {
Err(Error::from(e))
}
}
_ => Err(Error::from(e)),
}
} else {
resp
}
}
async fn md_path_to_html(path: &Path) -> Result<Response<Body>> {
let mut options = ComrakOptions::default();
// be like GitHub
options.ext_autolink = true;
options.ext_header_ids = None;
options.ext_table = true;
options.ext_strikethrough = true;
options.ext_tagfilter = true;
options.ext_tasklist = true;
options.github_pre_lang = true;
options.ext_header_ids = Some("user-content-".to_string());
let buf = tokio::fs::read(path).await?;
let s = String::from_utf8(buf).map_err(|_| Error::MarkdownUtf8)?;
let html = comrak::markdown_to_html(&s, &options);
let cfg = HtmlCfg {
title: String::new(),
body: html,
};
let html = super::render_html(cfg)?;
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_LENGTH, html.len() as u64)
.header(header::CONTENT_TYPE, mime::TEXT_HTML.as_ref())
.body(Body::from(html))
.map_err(Error::from)
}
async fn maybe_list_dir(root_dir: &Path, path: &Path) -> Result<Option<Response<Body>>> {
let meta = tokio::fs::metadata(path).await?;
if meta.is_dir() {
Ok(Some(list_dir(&root_dir, path).await?))
} else {
Ok(None)
}
}
async fn list_dir(root_dir: &Path, path: &Path) -> Result<Response<Body>> {
let up_dir = path.join("..");
let path = path.to_owned();
let dents = tokio::fs::read_dir(path).await?;
let dents = dents.filter_map(|dent| match dent {
Ok(dent) => future::ready(Some(dent)),
Err(e) => {
warn!("directory entry error: {}", e);
future::ready(None)
}
});
let paths = dents.map(|dent| DirEntry::path(&dent));
let paths: Vec<_> = paths.collect().await;
let paths = Some(up_dir).into_iter().chain(paths);
let paths: Vec<_> = paths.collect();
let html = make_dir_list_body(&root_dir, &paths)?;
let resp = super::html_str_to_response(html, StatusCode::OK)?;
Ok(resp)
}
fn make_dir_list_body(root_dir: &Path, paths: &[PathBuf]) -> Result<String> {
let mut buf = String::new();
writeln!(buf, "<div>").map_err(Error::WriteInDirList)?;
let dot_dot = OsStr::new("..");
for path in paths {
let full_url = path
.strip_prefix(root_dir)
.map_err(Error::StripPrefixInDirList)?;
let maybe_dot_dot = || {
if path.ends_with("..") {
Some(dot_dot)
} else {
None
}
};
if let Some(file_name) = path.file_name().or_else(maybe_dot_dot) {
if let Some(file_name) = file_name.to_str() {
if let Some(full_url) = full_url.to_str() {
// %-encode filenames
// https://url.spec.whatwg.org/#fragment-percent-encode-set
const FRAGMENT_SET: &AsciiSet =
&CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
const PATH_SET: &AsciiSet =
&FRAGMENT_SET.add(b'#').add(b'?').add(b'{').add(b'}');
let full_url = utf8_percent_encode(full_url, &PATH_SET);
// TODO: Make this a relative URL
writeln!(buf, "<div><a href='/{}'>{}</a></div>", full_url, file_name)
.map_err(Error::WriteInDirList)?;
} else {
warn!("non-unicode url: {}", full_url.to_string_lossy());
}
} else {
warn!("non-unicode path: {}", file_name.to_string_lossy());
}
} else {
warn!("path without file name: {}", path.display());
}
}
writeln!(buf, "</div>").map_err(Error::WriteInDirList)?;
let cfg = HtmlCfg {
title: String::new(),
body: buf,
};
super::render_html(cfg)
}