2020-11-29 23:12:56 +00:00
|
|
|
// Sort of an internal API endpoint to make testing easy.
|
|
|
|
// 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
|
|
|
|
|
|
|
|
use std::{
|
2022-03-25 20:40:36 +00:00
|
|
|
collections::*,
|
2020-11-29 23:12:56 +00:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
|
|
|
|
use percent_encoding::percent_decode;
|
|
|
|
use tokio::{
|
|
|
|
fs::{
|
|
|
|
File,
|
|
|
|
read_dir,
|
|
|
|
ReadDir,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg (test)]
|
|
|
|
use always_equal::test::AlwaysEqual;
|
|
|
|
|
|
|
|
#[cfg (not (test))]
|
|
|
|
use always_equal::prod::AlwaysEqual;
|
|
|
|
|
|
|
|
use ptth_core::{
|
|
|
|
http_serde::Method,
|
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
load_toml,
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
errors::FileServerError,
|
|
|
|
range,
|
|
|
|
};
|
|
|
|
|
2021-04-03 15:17:28 +00:00
|
|
|
#[cfg (feature = "markdown")]
|
|
|
|
use super::markdown::{
|
|
|
|
self,
|
|
|
|
render_styled,
|
|
|
|
};
|
|
|
|
|
2020-12-15 05:15:17 +00:00
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
pub enum OutputFormat {
|
|
|
|
Json,
|
|
|
|
Html,
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:12:56 +00:00
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
pub struct ServeDirParams {
|
|
|
|
pub path: PathBuf,
|
|
|
|
pub dir: AlwaysEqual <ReadDir>,
|
2020-12-15 05:15:17 +00:00
|
|
|
pub format: OutputFormat,
|
2020-11-29 23:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
pub struct ServeFileParams {
|
|
|
|
pub send_body: bool,
|
|
|
|
pub range: range::ValidParsed,
|
|
|
|
pub file: AlwaysEqual <File>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Debug, PartialEq)]
|
2020-11-29 23:15:45 +00:00
|
|
|
pub enum Response {
|
2020-11-29 23:12:56 +00:00
|
|
|
Favicon,
|
|
|
|
Forbidden,
|
|
|
|
MethodNotAllowed,
|
|
|
|
NotFound,
|
|
|
|
RangeNotSatisfiable (u64),
|
|
|
|
Redirect (String),
|
2020-12-15 05:15:17 +00:00
|
|
|
InvalidQuery,
|
|
|
|
|
2020-11-29 23:12:56 +00:00
|
|
|
Root,
|
|
|
|
ServeDir (ServeDirParams),
|
|
|
|
ServeFile (ServeFileParams),
|
|
|
|
|
2021-04-03 15:17:28 +00:00
|
|
|
MarkdownErr (MarkdownErrWrapper),
|
2020-11-29 23:12:56 +00:00
|
|
|
MarkdownPreview (String),
|
|
|
|
}
|
|
|
|
|
2021-04-03 15:17:28 +00:00
|
|
|
#[cfg (feature = "markdown")]
|
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
pub struct MarkdownErrWrapper {
|
|
|
|
pub inner: markdown::Error,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg (feature = "markdown")]
|
|
|
|
impl MarkdownErrWrapper {
|
|
|
|
fn new (inner: markdown::Error) -> Self {
|
|
|
|
Self { inner }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg (not (feature = "markdown"))]
|
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
pub struct MarkdownErrWrapper {}
|
|
|
|
|
2020-11-29 23:15:45 +00:00
|
|
|
fn serve_dir (
|
2020-11-29 23:12:56 +00:00
|
|
|
path_s: &str,
|
|
|
|
path: &Path,
|
|
|
|
dir: tokio::fs::ReadDir,
|
|
|
|
full_path: PathBuf,
|
2020-12-15 05:15:17 +00:00
|
|
|
uri: &http::Uri,
|
|
|
|
format: OutputFormat
|
2020-11-29 23:12:56 +00:00
|
|
|
)
|
2020-11-29 23:15:45 +00:00
|
|
|
-> Result <Response, FileServerError>
|
2020-11-29 23:12:56 +00:00
|
|
|
{
|
|
|
|
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)?;
|
2020-11-29 23:15:45 +00:00
|
|
|
return Ok (Response::Redirect (format! ("{}/", file_name)));
|
2020-11-29 23:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if uri.query ().is_some () {
|
2020-11-29 23:15:45 +00:00
|
|
|
return Ok (Response::InvalidQuery);
|
2020-11-29 23:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let dir = dir.into ();
|
|
|
|
|
2020-11-29 23:15:45 +00:00
|
|
|
Ok (Response::ServeDir (ServeDirParams {
|
2020-11-29 23:12:56 +00:00
|
|
|
dir,
|
|
|
|
path: full_path,
|
2020-12-15 05:15:17 +00:00
|
|
|
format,
|
2020-11-29 23:12:56 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:15:45 +00:00
|
|
|
async fn serve_file (
|
2021-04-03 15:30:32 +00:00
|
|
|
file: tokio::fs::File,
|
2020-11-29 23:12:56 +00:00
|
|
|
uri: &http::Uri,
|
|
|
|
send_body: bool,
|
|
|
|
headers: &HashMap <String, Vec <u8>>
|
|
|
|
)
|
2020-11-29 23:15:45 +00:00
|
|
|
-> Result <Response, FileServerError>
|
2020-11-29 23:12:56 +00:00
|
|
|
{
|
2021-04-03 14:27:17 +00:00
|
|
|
use range::Parsed::*;
|
|
|
|
|
2020-11-29 23:12:56 +00:00
|
|
|
let file_md = file.metadata ().await.map_err (FileServerError::CantGetFileMetadata)?;
|
2020-12-10 06:24:56 +00:00
|
|
|
|
|
|
|
#[cfg (unix)]
|
2020-11-29 23:12:56 +00:00
|
|
|
{
|
2020-12-10 06:24:56 +00:00
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
if file_md.permissions ().mode () == load_toml::CONFIG_PERMISSIONS_MODE
|
|
|
|
{
|
|
|
|
return Ok (Response::Forbidden);
|
|
|
|
}
|
2020-11-29 23:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let file_len = file_md.len ();
|
|
|
|
|
|
|
|
let range_header = headers.get ("range").and_then (|v| std::str::from_utf8 (v).ok ());
|
|
|
|
|
2021-04-03 14:27:17 +00:00
|
|
|
let range = match range::check (range_header, file_len) {
|
|
|
|
NotSatisfiable (file_len) => return Ok (Response::RangeNotSatisfiable (file_len)),
|
|
|
|
Valid (range) => range,
|
|
|
|
};
|
|
|
|
|
2021-04-03 15:17:28 +00:00
|
|
|
#[cfg (feature = "markdown")]
|
|
|
|
{
|
|
|
|
if uri.query () == Some ("as_markdown") {
|
|
|
|
const MAX_BUF_SIZE: u32 = 1_000_000;
|
|
|
|
|
|
|
|
if range.range_requested {
|
|
|
|
return Ok (Response::InvalidQuery);
|
|
|
|
}
|
2021-04-03 14:27:17 +00:00
|
|
|
|
2021-04-03 15:17:28 +00:00
|
|
|
if file_len > MAX_BUF_SIZE.into () {
|
|
|
|
return Ok (Response::MarkdownErr (MarkdownErrWrapper::new (markdown::Error::TooBig)));
|
|
|
|
}
|
|
|
|
else {
|
2021-04-03 15:30:32 +00:00
|
|
|
use std::convert::TryInto;
|
|
|
|
use tokio::io::AsyncReadExt;
|
|
|
|
|
2021-04-03 15:17:28 +00:00
|
|
|
let mut buffer = vec! [0_u8; MAX_BUF_SIZE.try_into ().expect ("Couldn't fit u32 into usize")];
|
2021-04-03 15:30:32 +00:00
|
|
|
let mut file = file;
|
2021-04-03 15:17:28 +00:00
|
|
|
let bytes_read = file.read (&mut buffer).await?;
|
|
|
|
buffer.truncate (bytes_read);
|
|
|
|
|
|
|
|
return Ok (match render_styled (&buffer) {
|
|
|
|
Ok (x) => Response::MarkdownPreview (x),
|
|
|
|
Err (x) => Response::MarkdownErr (MarkdownErrWrapper::new (x)),
|
|
|
|
});
|
|
|
|
}
|
2021-04-03 14:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let file = file.into ();
|
|
|
|
|
|
|
|
Ok (Response::ServeFile (ServeFileParams {
|
|
|
|
file,
|
|
|
|
send_body,
|
|
|
|
range,
|
|
|
|
}))
|
2020-11-29 23:12:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 05:15:17 +00:00
|
|
|
async fn serve_api (
|
|
|
|
root: &Path,
|
|
|
|
uri: &http::Uri,
|
|
|
|
hidden_path: Option <&Path>,
|
|
|
|
path: &str
|
|
|
|
)
|
|
|
|
-> Result <Response, FileServerError>
|
|
|
|
{
|
|
|
|
use Response::*;
|
|
|
|
|
2020-12-18 23:41:52 +00:00
|
|
|
// API versioning will be major-only, so I'll keep adding stuff to v1
|
|
|
|
// until I need to deprecate or break something.
|
|
|
|
|
2021-04-17 22:43:13 +00:00
|
|
|
if let Some (path) = path.strip_prefix ("/v1/dir/") {
|
2020-12-18 23:41:52 +00:00
|
|
|
let encoded_path = &path [0..];
|
|
|
|
|
|
|
|
let path_s = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?;
|
|
|
|
let path = Path::new (&*path_s);
|
|
|
|
|
|
|
|
let full_path = root.join (path);
|
|
|
|
|
|
|
|
debug! ("full_path = {:?}", full_path);
|
|
|
|
|
|
|
|
if let Some (hidden_path) = hidden_path {
|
|
|
|
if full_path == hidden_path {
|
|
|
|
return Ok (Forbidden);
|
2020-12-15 05:15:17 +00:00
|
|
|
}
|
2020-12-18 23:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return if let Ok (dir) = read_dir (&full_path).await {
|
|
|
|
serve_dir (
|
|
|
|
&path_s,
|
|
|
|
path,
|
|
|
|
dir,
|
|
|
|
full_path,
|
|
|
|
&uri,
|
|
|
|
OutputFormat::Json
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Ok (NotFound)
|
|
|
|
};
|
|
|
|
}
|
2020-12-15 05:15:17 +00:00
|
|
|
|
|
|
|
Ok (NotFound)
|
|
|
|
}
|
|
|
|
|
2022-03-25 20:40:36 +00:00
|
|
|
#[derive (Clone, Copy)]
|
|
|
|
pub struct FileRoots <'a> {
|
|
|
|
pub files: &'a Path,
|
|
|
|
pub dirs: &'a BTreeMap <String, PathBuf>,
|
|
|
|
}
|
|
|
|
|
2022-03-25 20:47:34 +00:00
|
|
|
struct RoutedPath <'a> {
|
|
|
|
root: &'a Path,
|
2022-03-25 21:04:51 +00:00
|
|
|
path_s: std::borrow::Cow <'a, str>,
|
2022-03-25 20:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a> FileRoots <'a> {
|
|
|
|
fn route (self, input: &'a str) -> Result <Option <RoutedPath>, FileServerError> {
|
|
|
|
let path = match input.strip_prefix ("/files/") {
|
|
|
|
Some (x) => x,
|
|
|
|
None => return Ok (None),
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: There is totally a dir traversal attack in here somewhere
|
|
|
|
|
|
|
|
let encoded_path = &path [0..];
|
|
|
|
|
2022-03-25 21:04:51 +00:00
|
|
|
let path_s = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?;
|
2022-03-25 20:47:34 +00:00
|
|
|
|
|
|
|
Ok (Some (RoutedPath {
|
|
|
|
root: self.files,
|
2022-03-25 21:04:51 +00:00
|
|
|
path_s,
|
2022-03-25 20:47:34 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:43:34 +00:00
|
|
|
// Handle the requests internally without knowing anything about PTTH or
|
|
|
|
// HTML / handlebars
|
|
|
|
|
2020-11-29 23:15:45 +00:00
|
|
|
pub async fn serve_all (
|
2022-03-25 20:40:36 +00:00
|
|
|
roots: FileRoots <'_>,
|
2020-11-29 23:12:56 +00:00
|
|
|
method: Method,
|
|
|
|
uri: &str,
|
|
|
|
headers: &HashMap <String, Vec <u8>>,
|
|
|
|
hidden_path: Option <&Path>
|
|
|
|
)
|
2020-11-29 23:15:45 +00:00
|
|
|
-> Result <Response, FileServerError>
|
2020-11-29 23:12:56 +00:00
|
|
|
{
|
|
|
|
use std::str::FromStr;
|
2020-11-29 23:15:45 +00:00
|
|
|
use Response::*;
|
2020-11-29 23:12:56 +00:00
|
|
|
|
2022-02-05 15:57:40 +00:00
|
|
|
trace! ("Client requested {}", uri);
|
2020-11-29 23:12:56 +00:00
|
|
|
|
|
|
|
let uri = http::Uri::from_str (uri).map_err (FileServerError::InvalidUri)?;
|
|
|
|
|
|
|
|
let send_body = match &method {
|
|
|
|
Method::Get => true,
|
|
|
|
Method::Head => false,
|
|
|
|
m => {
|
|
|
|
debug! ("Unsupported method {:?}", m);
|
|
|
|
return Ok (MethodNotAllowed);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-15 05:15:17 +00:00
|
|
|
let path = uri.path ();
|
|
|
|
|
|
|
|
if path == "/favicon.ico" {
|
2020-11-29 23:12:56 +00:00
|
|
|
return Ok (Favicon);
|
|
|
|
}
|
|
|
|
|
2020-12-15 05:15:17 +00:00
|
|
|
if path == "/" {
|
|
|
|
return Ok (Root);
|
|
|
|
}
|
2020-11-29 23:12:56 +00:00
|
|
|
|
2021-04-17 22:43:13 +00:00
|
|
|
if let Some (path) = path.strip_prefix ("/api") {
|
2022-03-25 20:40:36 +00:00
|
|
|
return serve_api (roots.files, &uri, hidden_path, path).await;
|
2020-11-29 23:12:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 21:04:51 +00:00
|
|
|
let RoutedPath {
|
|
|
|
root,
|
|
|
|
path_s,
|
|
|
|
} = match roots.route (path)? {
|
2020-12-15 05:15:17 +00:00
|
|
|
None => return Ok (NotFound),
|
2022-03-25 21:04:51 +00:00
|
|
|
Some (x) => x,
|
2020-12-15 05:15:17 +00:00
|
|
|
};
|
|
|
|
|
2020-11-29 23:12:56 +00:00
|
|
|
let path = Path::new (&*path_s);
|
|
|
|
|
2022-03-25 21:04:51 +00:00
|
|
|
let full_path = root.join (path);
|
2020-11-29 23:12:56 +00:00
|
|
|
|
2022-02-05 16:52:56 +00:00
|
|
|
trace! ("full_path = {:?}", full_path);
|
2020-11-29 23:12:56 +00:00
|
|
|
|
|
|
|
if let Some (hidden_path) = hidden_path {
|
|
|
|
if full_path == hidden_path {
|
|
|
|
return Ok (Forbidden);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok (dir) = read_dir (&full_path).await {
|
2020-11-29 23:15:45 +00:00
|
|
|
serve_dir (
|
2020-11-29 23:12:56 +00:00
|
|
|
&path_s,
|
|
|
|
path,
|
|
|
|
dir,
|
|
|
|
full_path,
|
2020-12-15 05:15:17 +00:00
|
|
|
&uri,
|
|
|
|
OutputFormat::Html
|
2020-11-29 23:12:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
else if let Ok (file) = File::open (&full_path).await {
|
2020-11-29 23:15:45 +00:00
|
|
|
serve_file (
|
2020-11-29 23:12:56 +00:00
|
|
|
file,
|
|
|
|
&uri,
|
|
|
|
send_body,
|
|
|
|
headers
|
|
|
|
).await
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Ok (NotFound)
|
|
|
|
}
|
|
|
|
}
|