2020-10-30 22:38:23 +00:00
|
|
|
// Static file server that can plug into the PTTH reverse server
|
|
|
|
|
2020-11-29 19:05:28 +00:00
|
|
|
// I'm not sure if I like this one
|
|
|
|
#![allow (clippy::enum_glob_use)]
|
|
|
|
|
2020-10-30 22:38:23 +00:00
|
|
|
use std::{
|
2020-11-08 17:58:14 +00:00
|
|
|
borrow::Cow,
|
2020-11-08 18:12:45 +00:00
|
|
|
cmp::min,
|
2020-11-29 19:05:28 +00:00
|
|
|
collections::HashMap,
|
2020-11-29 19:59:47 +00:00
|
|
|
convert::{Infallible, TryFrom, TryInto},
|
2020-11-08 18:12:45 +00:00
|
|
|
fmt::Debug,
|
2020-10-30 22:38:23 +00:00
|
|
|
io::SeekFrom,
|
2020-10-30 23:36:32 +00:00
|
|
|
path::{Path, PathBuf},
|
2020-10-30 22:38:23 +00:00
|
|
|
};
|
|
|
|
|
2020-10-31 02:31:03 +00:00
|
|
|
use handlebars::Handlebars;
|
2020-11-29 19:05:28 +00:00
|
|
|
use percent_encoding::{
|
|
|
|
percent_decode,
|
|
|
|
};
|
2020-11-08 15:53:09 +00:00
|
|
|
use serde::Serialize;
|
2020-10-30 22:38:23 +00:00
|
|
|
use tokio::{
|
|
|
|
fs::{
|
2020-11-08 15:53:09 +00:00
|
|
|
DirEntry,
|
2020-10-30 22:38:23 +00:00
|
|
|
File,
|
|
|
|
read_dir,
|
|
|
|
ReadDir,
|
|
|
|
},
|
|
|
|
io::AsyncReadExt,
|
|
|
|
sync::mpsc::{
|
|
|
|
channel,
|
|
|
|
},
|
|
|
|
};
|
2020-11-08 16:00:31 +00:00
|
|
|
use tracing::instrument;
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-08 18:12:45 +00:00
|
|
|
#[cfg (test)]
|
|
|
|
use always_equal::test::AlwaysEqual;
|
|
|
|
|
|
|
|
#[cfg (not (test))]
|
|
|
|
use always_equal::prod::AlwaysEqual;
|
|
|
|
|
2020-11-27 00:03:11 +00:00
|
|
|
use ptth_core::{
|
2020-11-08 17:58:14 +00:00
|
|
|
http_serde::{
|
|
|
|
Method,
|
|
|
|
Response,
|
|
|
|
StatusCode,
|
|
|
|
},
|
2020-11-08 16:00:31 +00:00
|
|
|
prelude::*,
|
2020-11-08 15:53:09 +00:00
|
|
|
prefix_match,
|
|
|
|
};
|
|
|
|
|
2020-11-29 19:19:59 +00:00
|
|
|
pub mod errors;
|
2020-11-29 22:56:25 +00:00
|
|
|
mod markdown;
|
2020-11-29 22:31:54 +00:00
|
|
|
mod range;
|
2020-11-29 22:12:25 +00:00
|
|
|
|
2020-11-29 22:56:25 +00:00
|
|
|
use errors::FileServerError;
|
|
|
|
use markdown::render_styled;
|
2020-11-29 18:50:51 +00:00
|
|
|
|
2020-11-29 22:12:25 +00:00
|
|
|
mod emoji {
|
|
|
|
pub const VIDEO: &str = "\u{1f39e}\u{fe0f}";
|
|
|
|
pub const PICTURE: &str = "\u{1f4f7}";
|
|
|
|
pub const FILE: &str = "\u{1f4c4}";
|
|
|
|
pub const FOLDER: &str = "\u{1f4c1}";
|
|
|
|
pub const ERROR: &str = "\u{26a0}\u{fe0f}";
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:44:21 +00:00
|
|
|
#[derive (Debug, Serialize)]
|
|
|
|
pub struct ServerInfo {
|
|
|
|
pub server_name: String,
|
2020-11-08 15:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Serialize)]
|
|
|
|
struct TemplateDirEntry {
|
|
|
|
icon: &'static str,
|
|
|
|
trailing_slash: &'static str,
|
|
|
|
|
|
|
|
// Unfortunately file_name will allocate as long as some platforms
|
|
|
|
// (Windows!) aren't UTF-8. Cause I don't want to write separate code
|
|
|
|
// for such a small problem.
|
|
|
|
|
|
|
|
file_name: String,
|
|
|
|
|
|
|
|
// This could be a Cow with file_name if no encoding was done but
|
|
|
|
// it's simpler to allocate.
|
|
|
|
|
|
|
|
encoded_file_name: String,
|
|
|
|
|
2020-11-08 17:58:14 +00:00
|
|
|
size: Cow <'static, str>,
|
|
|
|
|
2020-11-08 15:53:09 +00:00
|
|
|
error: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Serialize)]
|
|
|
|
struct TemplateDirPage <'a> {
|
|
|
|
#[serde (flatten)]
|
2020-11-10 00:44:21 +00:00
|
|
|
server_info: &'a ServerInfo,
|
2020-11-08 15:53:09 +00:00
|
|
|
|
|
|
|
path: Cow <'a, str>,
|
|
|
|
entries: Vec <TemplateDirEntry>,
|
|
|
|
}
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-25 00:16:14 +00:00
|
|
|
fn get_icon (file_name: &str) -> &'static str {
|
|
|
|
if
|
|
|
|
file_name.ends_with (".mp4") ||
|
|
|
|
file_name.ends_with (".avi") ||
|
|
|
|
file_name.ends_with (".mkv") ||
|
|
|
|
file_name.ends_with (".webm")
|
|
|
|
{
|
2020-11-29 22:12:25 +00:00
|
|
|
emoji::VIDEO
|
2020-11-25 00:16:14 +00:00
|
|
|
}
|
|
|
|
else if
|
|
|
|
file_name.ends_with (".jpg") ||
|
|
|
|
file_name.ends_with (".jpeg") ||
|
|
|
|
file_name.ends_with (".png") ||
|
|
|
|
file_name.ends_with (".bmp")
|
|
|
|
{
|
2020-11-29 22:12:25 +00:00
|
|
|
emoji::PICTURE
|
2020-11-25 00:16:14 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-11-29 22:12:25 +00:00
|
|
|
emoji::FILE
|
2020-11-25 00:16:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:35:18 +00:00
|
|
|
async fn read_dir_entry (entry: DirEntry) -> TemplateDirEntry
|
|
|
|
{
|
2020-11-29 19:05:28 +00:00
|
|
|
use percent_encoding::{
|
|
|
|
CONTROLS,
|
|
|
|
utf8_percent_encode,
|
|
|
|
};
|
|
|
|
|
2020-10-31 14:35:18 +00:00
|
|
|
let file_name = match entry.file_name ().into_string () {
|
|
|
|
Ok (x) => x,
|
|
|
|
Err (_) => return TemplateDirEntry {
|
2020-11-29 22:12:25 +00:00
|
|
|
icon: emoji::ERROR,
|
2020-10-31 14:35:18 +00:00
|
|
|
trailing_slash: "",
|
2020-11-08 15:53:09 +00:00
|
|
|
file_name: "File / directory name is not UTF-8".into (),
|
2020-10-31 14:35:18 +00:00
|
|
|
encoded_file_name: "".into (),
|
2020-11-08 17:58:14 +00:00
|
|
|
size: "".into (),
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let metadata = match entry.metadata ().await {
|
|
|
|
Ok (x) => x,
|
|
|
|
Err (_) => return TemplateDirEntry {
|
2020-11-29 22:12:25 +00:00
|
|
|
icon: emoji::ERROR,
|
2020-11-08 17:58:14 +00:00
|
|
|
trailing_slash: "",
|
|
|
|
file_name: "Could not fetch metadata".into (),
|
|
|
|
encoded_file_name: "".into (),
|
|
|
|
size: "".into (),
|
2020-10-31 14:35:18 +00:00
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-08 17:58:14 +00:00
|
|
|
let (trailing_slash, icon, size) = {
|
|
|
|
let t = metadata.file_type ();
|
|
|
|
|
|
|
|
if t.is_dir () {
|
2020-11-29 22:12:25 +00:00
|
|
|
("/", emoji::FOLDER, "".into ())
|
2020-11-08 15:53:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-11-25 00:16:14 +00:00
|
|
|
("", get_icon (&file_name), pretty_print_bytes (metadata.len ()).into ())
|
2020-11-08 17:58:14 +00:00
|
|
|
}
|
2020-11-08 15:53:09 +00:00
|
|
|
};
|
|
|
|
|
2020-10-31 14:35:18 +00:00
|
|
|
let encoded_file_name = utf8_percent_encode (&file_name, CONTROLS).to_string ();
|
|
|
|
|
|
|
|
TemplateDirEntry {
|
2020-11-08 15:53:09 +00:00
|
|
|
icon,
|
2020-10-31 14:35:18 +00:00
|
|
|
trailing_slash: &trailing_slash,
|
|
|
|
file_name,
|
|
|
|
encoded_file_name,
|
2020-11-08 17:58:14 +00:00
|
|
|
size,
|
2020-10-31 14:35:18 +00:00
|
|
|
error: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 15:53:09 +00:00
|
|
|
async fn serve_root (
|
|
|
|
handlebars: &Handlebars <'static>,
|
2020-11-10 00:44:21 +00:00
|
|
|
server_info: &ServerInfo
|
2020-11-29 19:19:59 +00:00
|
|
|
) -> Result <Response, FileServerError>
|
2020-11-08 15:53:09 +00:00
|
|
|
{
|
2020-11-29 19:19:59 +00:00
|
|
|
let s = handlebars.render ("file_server_root", &server_info)?;
|
2020-11-08 15:53:09 +00:00
|
|
|
|
2020-11-29 19:19:59 +00:00
|
|
|
Ok (serve_html (s))
|
2020-11-10 01:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serve_html (s: String) -> Response {
|
2020-11-08 17:58:14 +00:00
|
|
|
let mut resp = Response::default ();
|
2020-11-08 15:53:09 +00:00
|
|
|
resp
|
2020-11-10 03:01:00 +00:00
|
|
|
.header ("content-type".to_string (), "text/html; charset=UTF-8".to_string ().into_bytes ())
|
2020-11-10 01:02:59 +00:00
|
|
|
.body_bytes (s.into_bytes ())
|
2020-11-08 15:53:09 +00:00
|
|
|
;
|
|
|
|
resp
|
|
|
|
}
|
|
|
|
|
2020-11-06 20:55:55 +00:00
|
|
|
#[instrument (level = "debug", skip (handlebars, dir))]
|
2020-10-31 02:31:03 +00:00
|
|
|
async fn serve_dir (
|
2020-11-02 04:07:55 +00:00
|
|
|
handlebars: &Handlebars <'static>,
|
2020-11-10 00:44:21 +00:00
|
|
|
server_info: &ServerInfo,
|
2020-10-31 15:28:27 +00:00
|
|
|
path: Cow <'_, str>,
|
2020-10-31 02:31:03 +00:00
|
|
|
mut dir: ReadDir
|
2020-11-29 19:47:40 +00:00
|
|
|
) -> Result <Response, FileServerError>
|
2020-10-31 14:35:18 +00:00
|
|
|
{
|
|
|
|
let mut entries = vec! [];
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-10-31 14:35:18 +00:00
|
|
|
while let Ok (Some (entry)) = dir.next_entry ().await {
|
|
|
|
entries.push (read_dir_entry (entry).await);
|
|
|
|
}
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-29 19:47:40 +00:00
|
|
|
entries.sort_unstable_by (|a, b| a.file_name.cmp (&b.file_name));
|
2020-11-06 20:55:55 +00:00
|
|
|
|
2020-10-31 14:35:18 +00:00
|
|
|
let s = handlebars.render ("file_server_dir", &TemplateDirPage {
|
2020-10-31 15:28:27 +00:00
|
|
|
path,
|
2020-10-31 14:35:18 +00:00
|
|
|
entries,
|
2020-11-08 15:53:09 +00:00
|
|
|
server_info,
|
2020-11-29 19:47:40 +00:00
|
|
|
})?;
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-29 19:47:40 +00:00
|
|
|
Ok (serve_html (s))
|
2020-10-30 22:38:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 01:40:29 +00:00
|
|
|
#[instrument (level = "debug", skip (f))]
|
2020-10-30 22:38:23 +00:00
|
|
|
async fn serve_file (
|
|
|
|
mut f: File,
|
|
|
|
should_send_body: bool,
|
2020-11-29 22:41:48 +00:00
|
|
|
range: range::ValidParsed
|
2020-11-08 17:58:14 +00:00
|
|
|
)
|
2020-11-29 19:47:40 +00:00
|
|
|
-> Result <Response, FileServerError>
|
2020-11-08 17:58:14 +00:00
|
|
|
{
|
2020-11-07 01:34:58 +00:00
|
|
|
let (tx, rx) = channel (1);
|
2020-10-30 22:38:23 +00:00
|
|
|
let body = if should_send_body {
|
2020-10-31 01:35:39 +00:00
|
|
|
Some (rx)
|
2020-10-30 22:38:23 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-11-29 22:31:54 +00:00
|
|
|
let (range, range_requested) = (range.range, range.range_requested);
|
|
|
|
|
2020-11-08 18:12:45 +00:00
|
|
|
info! ("Serving range {}-{}", range.start, range.end);
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-08 18:12:45 +00:00
|
|
|
let content_length = range.end - range.start;
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-09 00:53:07 +00:00
|
|
|
let seek = SeekFrom::Start (range.start);
|
2020-11-29 19:47:40 +00:00
|
|
|
f.seek (seek).await?;
|
2020-11-09 00:53:07 +00:00
|
|
|
|
2020-10-30 22:38:23 +00:00
|
|
|
if should_send_body {
|
|
|
|
tokio::spawn (async move {
|
|
|
|
let mut tx = tx;
|
2020-11-06 18:47:04 +00:00
|
|
|
let mut bytes_sent = 0;
|
2020-11-08 18:12:45 +00:00
|
|
|
let mut bytes_left = content_length;
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-09 16:33:13 +00:00
|
|
|
let mark_interval = 200_000;
|
|
|
|
let mut next_mark = mark_interval;
|
|
|
|
|
2020-10-30 22:38:23 +00:00
|
|
|
loop {
|
2020-11-29 19:05:28 +00:00
|
|
|
let mut buffer = vec! [0_u8; 65_536];
|
2020-11-29 19:59:47 +00:00
|
|
|
let bytes_read = f.read (&mut buffer).await.expect ("Couldn't read from file");
|
2020-10-30 22:38:23 +00:00
|
|
|
|
|
|
|
if bytes_read == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:59:47 +00:00
|
|
|
buffer.truncate (bytes_read);
|
|
|
|
|
|
|
|
let bytes_read_64 = u64::try_from (bytes_read).expect ("Couldn't fit usize into u64");
|
|
|
|
|
|
|
|
let bytes_read_64 = min (bytes_left, bytes_read_64);
|
|
|
|
|
2020-11-07 01:40:29 +00:00
|
|
|
if tx.send (Ok::<_, Infallible> (buffer)).await.is_err () {
|
2020-11-08 18:12:45 +00:00
|
|
|
warn! ("Cancelling file stream (Sent {} out of {} bytes)", bytes_sent, content_length);
|
2020-10-30 22:38:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:59:47 +00:00
|
|
|
bytes_left -= bytes_read_64;
|
2020-10-30 22:38:23 +00:00
|
|
|
if bytes_left == 0 {
|
2020-11-07 23:10:01 +00:00
|
|
|
debug! ("Finished");
|
2020-10-30 22:38:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:59:47 +00:00
|
|
|
bytes_sent += bytes_read_64;
|
2020-11-09 16:33:13 +00:00
|
|
|
while next_mark <= bytes_sent {
|
|
|
|
trace! ("Sent {} bytes", next_mark);
|
|
|
|
next_mark += mark_interval;
|
|
|
|
}
|
2020-10-30 22:38:23 +00:00
|
|
|
|
|
|
|
//delay_for (Duration::from_millis (50)).await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-08 17:58:14 +00:00
|
|
|
let mut response = Response::default ();
|
2020-10-30 22:38:23 +00:00
|
|
|
|
|
|
|
response.header (String::from ("accept-ranges"), b"bytes".to_vec ());
|
|
|
|
|
2020-11-09 00:53:07 +00:00
|
|
|
if range_requested {
|
|
|
|
response.status_code (StatusCode::PartialContent);
|
|
|
|
response.header (String::from ("content-range"), format! ("bytes {}-{}/{}", range.start, range.end - 1, range.end).into_bytes ());
|
2020-11-08 18:12:45 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-11-09 00:53:07 +00:00
|
|
|
response.status_code (StatusCode::Ok);
|
|
|
|
response.header (String::from ("content-length"), range.end.to_string ().into_bytes ());
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:05:28 +00:00
|
|
|
if should_send_body {
|
|
|
|
response.content_length = Some (content_length);
|
2020-10-30 22:38:23 +00:00
|
|
|
}
|
2020-11-09 00:53:07 +00:00
|
|
|
else {
|
2020-11-29 19:05:28 +00:00
|
|
|
response.status_code (StatusCode::NoContent);
|
2020-11-09 00:53:07 +00:00
|
|
|
}
|
2020-10-30 22:38:23 +00:00
|
|
|
|
|
|
|
if let Some (body) = body {
|
|
|
|
response.body (body);
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:47:40 +00:00
|
|
|
Ok (response)
|
2020-10-30 22:38:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 18:12:45 +00:00
|
|
|
// Sort of an internal API endpoint to make testing work better.
|
|
|
|
// Eventually we could expose this as JSON or Msgpack or whatever. For now
|
|
|
|
// it's just a Rust struct that we can test on without caring about
|
|
|
|
// human-readable HTML
|
|
|
|
|
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
struct ServeDirParams {
|
|
|
|
path: PathBuf,
|
|
|
|
dir: AlwaysEqual <ReadDir>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
struct ServeFileParams {
|
|
|
|
send_body: bool,
|
2020-11-29 22:41:48 +00:00
|
|
|
range: range::ValidParsed,
|
2020-11-08 18:12:45 +00:00
|
|
|
file: AlwaysEqual <File>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
enum InternalResponse {
|
|
|
|
Favicon,
|
|
|
|
Forbidden,
|
2020-11-10 02:39:20 +00:00
|
|
|
InvalidQuery,
|
2020-11-08 18:12:45 +00:00
|
|
|
MethodNotAllowed,
|
|
|
|
NotFound,
|
|
|
|
RangeNotSatisfiable (u64),
|
|
|
|
Redirect (String),
|
|
|
|
Root,
|
|
|
|
ServeDir (ServeDirParams),
|
|
|
|
ServeFile (ServeFileParams),
|
2020-11-10 01:02:59 +00:00
|
|
|
|
2020-11-29 22:56:25 +00:00
|
|
|
MarkdownErr (markdown::Error),
|
2020-11-10 02:39:20 +00:00
|
|
|
MarkdownPreview (String),
|
2020-11-08 18:12:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 21:38:23 +00:00
|
|
|
fn internal_serve_dir (
|
|
|
|
path_s: &str,
|
|
|
|
path: &Path,
|
|
|
|
dir: tokio::fs::ReadDir,
|
|
|
|
full_path: PathBuf,
|
2020-11-29 21:55:24 +00:00
|
|
|
uri: &http::Uri
|
2020-11-29 21:38:23 +00:00
|
|
|
)
|
|
|
|
-> Result <InternalResponse, FileServerError>
|
|
|
|
{
|
|
|
|
let has_trailing_slash = path_s.is_empty () || path_s.ends_with ('/');
|
|
|
|
|
|
|
|
if ! has_trailing_slash {
|
|
|
|
let file_name = path.file_name ().ok_or (FileServerError::NoFileNameRequested)?;
|
|
|
|
let file_name = file_name.to_str ().ok_or (FileServerError::FilePathNotUtf8)?;
|
|
|
|
return Ok (InternalResponse::Redirect (format! ("{}/", file_name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if uri.query ().is_some () {
|
|
|
|
return Ok (InternalResponse::InvalidQuery);
|
|
|
|
}
|
|
|
|
|
|
|
|
let dir = dir.into ();
|
|
|
|
|
|
|
|
Ok (InternalResponse::ServeDir (ServeDirParams {
|
|
|
|
dir,
|
|
|
|
path: full_path,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn internal_serve_file (
|
|
|
|
mut file: tokio::fs::File,
|
2020-11-29 21:55:24 +00:00
|
|
|
uri: &http::Uri,
|
2020-11-29 21:38:23 +00:00
|
|
|
send_body: bool,
|
|
|
|
headers: &HashMap <String, Vec <u8>>
|
|
|
|
)
|
|
|
|
-> Result <InternalResponse, FileServerError>
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
|
|
|
|
let file_md = file.metadata ().await.map_err (FileServerError::CantGetFileMetadata)?;
|
|
|
|
if file_md.permissions ().mode () == super::load_toml::CONFIG_PERMISSIONS_MODE
|
|
|
|
{
|
|
|
|
return Ok (InternalResponse::Forbidden);
|
|
|
|
}
|
|
|
|
|
|
|
|
let file_len = file_md.len ();
|
|
|
|
|
|
|
|
let range_header = headers.get ("range").and_then (|v| std::str::from_utf8 (v).ok ());
|
|
|
|
|
2020-11-29 22:41:48 +00:00
|
|
|
Ok (match range::check (range_header, file_len) {
|
|
|
|
range::Parsed::NotSatisfiable (file_len) => InternalResponse::RangeNotSatisfiable (file_len),
|
|
|
|
range::Parsed::Valid (range) => {
|
2020-11-29 21:38:23 +00:00
|
|
|
if uri.query () == Some ("as_markdown") {
|
2020-11-29 22:41:48 +00:00
|
|
|
const MAX_BUF_SIZE: u32 = 1_000_000;
|
|
|
|
|
2020-11-29 22:31:54 +00:00
|
|
|
if range.range_requested {
|
|
|
|
return Ok (InternalResponse::InvalidQuery);
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:38:23 +00:00
|
|
|
if file_len > MAX_BUF_SIZE.into () {
|
2020-11-29 22:56:25 +00:00
|
|
|
InternalResponse::MarkdownErr (markdown::Error::TooBig)
|
2020-11-29 21:38:23 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
let mut buffer = vec! [0_u8; MAX_BUF_SIZE.try_into ().expect ("Couldn't fit u32 into usize")];
|
|
|
|
let bytes_read = file.read (&mut buffer).await?;
|
|
|
|
buffer.truncate (bytes_read);
|
|
|
|
|
2020-11-29 22:56:25 +00:00
|
|
|
match render_styled (&buffer) {
|
|
|
|
Ok (x) => InternalResponse::MarkdownPreview (x),
|
|
|
|
Err (x) => InternalResponse::MarkdownErr (x),
|
|
|
|
}
|
2020-11-29 21:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let file = file.into ();
|
|
|
|
|
|
|
|
InternalResponse::ServeFile (ServeFileParams {
|
|
|
|
file,
|
|
|
|
send_body,
|
|
|
|
range,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:12:45 +00:00
|
|
|
async fn internal_serve_all (
|
2020-10-30 23:36:32 +00:00
|
|
|
root: &Path,
|
2020-11-08 17:58:14 +00:00
|
|
|
method: Method,
|
2020-11-02 04:07:55 +00:00
|
|
|
uri: &str,
|
2020-11-08 15:01:15 +00:00
|
|
|
headers: &HashMap <String, Vec <u8>>,
|
|
|
|
hidden_path: Option <&Path>
|
2020-10-30 22:38:23 +00:00
|
|
|
)
|
2020-11-29 19:47:40 +00:00
|
|
|
-> Result <InternalResponse, FileServerError>
|
2020-10-30 22:38:23 +00:00
|
|
|
{
|
2020-11-10 02:39:20 +00:00
|
|
|
use std::str::FromStr;
|
2020-11-08 18:12:45 +00:00
|
|
|
use InternalResponse::*;
|
|
|
|
|
2020-11-06 20:55:55 +00:00
|
|
|
info! ("Client requested {}", uri);
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-29 21:55:24 +00:00
|
|
|
let uri = http::Uri::from_str (uri).map_err (FileServerError::InvalidUri)?;
|
2020-11-10 02:39:20 +00:00
|
|
|
|
2020-11-08 18:12:45 +00:00
|
|
|
let send_body = match &method {
|
|
|
|
Method::Get => true,
|
|
|
|
Method::Head => false,
|
|
|
|
m => {
|
|
|
|
debug! ("Unsupported method {:?}", m);
|
2020-11-29 19:47:40 +00:00
|
|
|
return Ok (MethodNotAllowed);
|
2020-11-08 18:12:45 +00:00
|
|
|
}
|
|
|
|
};
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-10 02:39:20 +00:00
|
|
|
if uri.path () == "/favicon.ico" {
|
2020-11-29 19:47:40 +00:00
|
|
|
return Ok (Favicon);
|
2020-11-08 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 02:39:20 +00:00
|
|
|
let path = match prefix_match ("/files", uri.path ()) {
|
2020-11-08 15:53:09 +00:00
|
|
|
Some (x) => x,
|
2020-11-29 19:47:40 +00:00
|
|
|
None => return Ok (Root),
|
2020-11-06 20:55:55 +00:00
|
|
|
};
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-10 02:39:20 +00:00
|
|
|
if path == "" {
|
2020-11-29 19:47:40 +00:00
|
|
|
return Ok (Redirect ("files/".to_string ()));
|
2020-11-08 18:12:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 01:35:39 +00:00
|
|
|
// TODO: There is totally a dir traversal attack in here somewhere
|
|
|
|
|
2020-11-10 02:39:20 +00:00
|
|
|
let encoded_path = &path [1..];
|
2020-10-31 01:35:39 +00:00
|
|
|
|
2020-11-29 19:47:40 +00:00
|
|
|
let path_s = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?;
|
2020-10-31 15:46:24 +00:00
|
|
|
let path = Path::new (&*path_s);
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-19 01:45:42 +00:00
|
|
|
let full_path = root.join (path);
|
2020-10-30 22:38:23 +00:00
|
|
|
|
2020-11-08 15:01:15 +00:00
|
|
|
debug! ("full_path = {:?}", full_path);
|
|
|
|
|
|
|
|
if let Some (hidden_path) = hidden_path {
|
|
|
|
if full_path == hidden_path {
|
2020-11-29 19:47:40 +00:00
|
|
|
return Ok (Forbidden);
|
2020-11-08 15:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:38:23 +00:00
|
|
|
if let Ok (dir) = read_dir (&full_path).await {
|
|
|
|
internal_serve_dir (
|
|
|
|
&path_s,
|
|
|
|
path,
|
2020-11-08 18:12:45 +00:00
|
|
|
dir,
|
2020-11-29 21:38:23 +00:00
|
|
|
full_path,
|
|
|
|
&uri
|
|
|
|
)
|
2020-10-30 22:38:23 +00:00
|
|
|
}
|
2020-11-29 21:38:23 +00:00
|
|
|
else if let Ok (file) = File::open (&full_path).await {
|
|
|
|
internal_serve_file (
|
|
|
|
file,
|
|
|
|
&uri,
|
|
|
|
send_body,
|
|
|
|
headers
|
|
|
|
).await
|
2020-10-30 22:38:23 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-11-29 21:38:23 +00:00
|
|
|
Ok (NotFound)
|
|
|
|
}
|
2020-11-08 18:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument (level = "debug", skip (handlebars, headers))]
|
|
|
|
pub async fn serve_all (
|
|
|
|
handlebars: &Handlebars <'static>,
|
2020-11-10 00:44:21 +00:00
|
|
|
server_info: &ServerInfo,
|
2020-11-08 18:12:45 +00:00
|
|
|
root: &Path,
|
|
|
|
method: Method,
|
|
|
|
uri: &str,
|
|
|
|
headers: &HashMap <String, Vec <u8>>,
|
|
|
|
hidden_path: Option <&Path>
|
|
|
|
)
|
2020-11-29 19:19:59 +00:00
|
|
|
-> Result <Response, FileServerError>
|
2020-11-08 18:12:45 +00:00
|
|
|
{
|
|
|
|
use InternalResponse::*;
|
|
|
|
|
2020-11-29 22:56:25 +00:00
|
|
|
fn serve_error <S: Into <Vec <u8>>> (
|
|
|
|
status_code: StatusCode,
|
|
|
|
msg: S
|
|
|
|
)
|
|
|
|
-> Response
|
|
|
|
{
|
|
|
|
let mut resp = Response::default ();
|
|
|
|
resp.status_code (status_code);
|
|
|
|
resp.body_bytes (msg.into ());
|
|
|
|
resp
|
|
|
|
}
|
|
|
|
|
2020-11-29 19:47:40 +00:00
|
|
|
Ok (match internal_serve_all (root, method, uri, headers, hidden_path).await? {
|
2020-11-08 18:12:45 +00:00
|
|
|
Favicon => serve_error (StatusCode::NotFound, ""),
|
|
|
|
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden"),
|
2020-11-10 02:39:20 +00:00
|
|
|
InvalidQuery => serve_error (StatusCode::BadRequest, "Query is invalid for this object"),
|
2020-11-08 18:12:45 +00:00
|
|
|
MethodNotAllowed => serve_error (StatusCode::MethodNotAllowed, "Unsupported method"),
|
|
|
|
NotFound => serve_error (StatusCode::NotFound, "404 Not Found"),
|
|
|
|
RangeNotSatisfiable (file_len) => {
|
|
|
|
let mut resp = Response::default ();
|
|
|
|
resp.status_code (StatusCode::RangeNotSatisfiable)
|
|
|
|
.header ("content-range".to_string (), format! ("bytes */{}", file_len).into_bytes ());
|
|
|
|
resp
|
|
|
|
},
|
2020-11-29 22:12:25 +00:00
|
|
|
Redirect (location) => {
|
|
|
|
let mut resp = Response::default ();
|
|
|
|
resp.status_code (StatusCode::TemporaryRedirect);
|
|
|
|
resp.header ("location".to_string (), location.into_bytes ());
|
|
|
|
resp.body_bytes (b"Redirecting...".to_vec ());
|
|
|
|
resp
|
|
|
|
},
|
2020-11-29 19:19:59 +00:00
|
|
|
Root => serve_root (handlebars, server_info).await?,
|
2020-11-08 18:12:45 +00:00
|
|
|
ServeDir (ServeDirParams {
|
|
|
|
path,
|
|
|
|
dir,
|
2020-11-29 19:47:40 +00:00
|
|
|
}) => serve_dir (handlebars, server_info, path.to_string_lossy (), dir.into_inner ()).await?,
|
2020-11-08 18:12:45 +00:00
|
|
|
ServeFile (ServeFileParams {
|
|
|
|
file,
|
|
|
|
send_body,
|
|
|
|
range,
|
2020-11-29 22:31:54 +00:00
|
|
|
}) => serve_file (file.into_inner (), send_body, range).await?,
|
2020-11-29 22:56:25 +00:00
|
|
|
MarkdownErr (e) => {
|
|
|
|
use markdown::Error::*;
|
|
|
|
let code = match &e {
|
|
|
|
TooBig => StatusCode::InternalServerError,
|
|
|
|
//NotMarkdown => serve_error (StatusCode::BadRequest, "File is not Markdown"),
|
|
|
|
NotUtf8 => StatusCode::BadRequest,
|
|
|
|
};
|
|
|
|
|
|
|
|
serve_error (code, e.to_string ())
|
2020-11-10 02:39:20 +00:00
|
|
|
},
|
|
|
|
MarkdownPreview (s) => serve_html (s),
|
2020-11-29 19:19:59 +00:00
|
|
|
})
|
2020-10-30 22:38:23 +00:00
|
|
|
}
|
2020-10-31 01:35:39 +00:00
|
|
|
|
2020-11-18 23:24:47 +00:00
|
|
|
pub fn load_templates (
|
|
|
|
asset_root: &Path
|
|
|
|
)
|
2020-11-29 21:38:23 +00:00
|
|
|
-> Result <Handlebars <'static>, handlebars::TemplateFileError>
|
2020-10-31 17:56:36 +00:00
|
|
|
{
|
2020-10-31 20:46:38 +00:00
|
|
|
let mut handlebars = Handlebars::new ();
|
|
|
|
handlebars.set_strict_mode (true);
|
2020-10-31 17:56:36 +00:00
|
|
|
|
2020-11-18 23:24:47 +00:00
|
|
|
let asset_root = asset_root.join ("handlebars/server");
|
|
|
|
|
2020-11-29 19:05:28 +00:00
|
|
|
for (k, v) in &[
|
2020-10-31 20:46:38 +00:00
|
|
|
("file_server_dir", "file_server_dir.html"),
|
2020-11-08 15:53:09 +00:00
|
|
|
("file_server_root", "file_server_root.html"),
|
2020-11-29 19:05:28 +00:00
|
|
|
] {
|
2020-11-18 23:24:47 +00:00
|
|
|
handlebars.register_template_file (k, asset_root.join (v))?;
|
2020-10-31 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok (handlebars)
|
|
|
|
}
|
|
|
|
|
2020-11-08 17:58:14 +00:00
|
|
|
fn pretty_print_bytes (b: u64) -> String {
|
|
|
|
if b < 1024 {
|
|
|
|
format! ("{} B", b)
|
|
|
|
}
|
|
|
|
else if (b + 512) < 1024 * 1024 {
|
|
|
|
format! ("{} KiB", (b + 512) / 1024)
|
|
|
|
}
|
|
|
|
else if (b + 512 * 1024) < 1024 * 1024 * 1024 {
|
|
|
|
format! ("{} MiB", (b + 512 * 1024) / 1024 / 1024)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
format! ("{} GiB", (b + 512 * 1024 * 1024) / 1024 / 1024 / 1024)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 01:35:39 +00:00
|
|
|
#[cfg (test)]
|
2020-11-29 18:50:51 +00:00
|
|
|
mod tests;
|