♻️ Remove "internal" from names in the internal module
parent
bb4c4e803a
commit
8079b3f778
|
@ -56,7 +56,7 @@ pub struct ServeFileParams {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Debug, PartialEq)]
|
#[derive (Debug, PartialEq)]
|
||||||
pub enum InternalResponse {
|
pub enum Response {
|
||||||
Favicon,
|
Favicon,
|
||||||
Forbidden,
|
Forbidden,
|
||||||
InvalidQuery,
|
InvalidQuery,
|
||||||
|
@ -72,49 +72,49 @@ pub enum InternalResponse {
|
||||||
MarkdownPreview (String),
|
MarkdownPreview (String),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn internal_serve_dir (
|
fn serve_dir (
|
||||||
path_s: &str,
|
path_s: &str,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
dir: tokio::fs::ReadDir,
|
dir: tokio::fs::ReadDir,
|
||||||
full_path: PathBuf,
|
full_path: PathBuf,
|
||||||
uri: &http::Uri
|
uri: &http::Uri
|
||||||
)
|
)
|
||||||
-> Result <InternalResponse, FileServerError>
|
-> Result <Response, FileServerError>
|
||||||
{
|
{
|
||||||
let has_trailing_slash = path_s.is_empty () || path_s.ends_with ('/');
|
let has_trailing_slash = path_s.is_empty () || path_s.ends_with ('/');
|
||||||
|
|
||||||
if ! has_trailing_slash {
|
if ! has_trailing_slash {
|
||||||
let file_name = path.file_name ().ok_or (FileServerError::NoFileNameRequested)?;
|
let file_name = path.file_name ().ok_or (FileServerError::NoFileNameRequested)?;
|
||||||
let file_name = file_name.to_str ().ok_or (FileServerError::FilePathNotUtf8)?;
|
let file_name = file_name.to_str ().ok_or (FileServerError::FilePathNotUtf8)?;
|
||||||
return Ok (InternalResponse::Redirect (format! ("{}/", file_name)));
|
return Ok (Response::Redirect (format! ("{}/", file_name)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if uri.query ().is_some () {
|
if uri.query ().is_some () {
|
||||||
return Ok (InternalResponse::InvalidQuery);
|
return Ok (Response::InvalidQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
let dir = dir.into ();
|
let dir = dir.into ();
|
||||||
|
|
||||||
Ok (InternalResponse::ServeDir (ServeDirParams {
|
Ok (Response::ServeDir (ServeDirParams {
|
||||||
dir,
|
dir,
|
||||||
path: full_path,
|
path: full_path,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn internal_serve_file (
|
async fn serve_file (
|
||||||
mut file: tokio::fs::File,
|
mut file: tokio::fs::File,
|
||||||
uri: &http::Uri,
|
uri: &http::Uri,
|
||||||
send_body: bool,
|
send_body: bool,
|
||||||
headers: &HashMap <String, Vec <u8>>
|
headers: &HashMap <String, Vec <u8>>
|
||||||
)
|
)
|
||||||
-> Result <InternalResponse, FileServerError>
|
-> Result <Response, FileServerError>
|
||||||
{
|
{
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
||||||
let file_md = file.metadata ().await.map_err (FileServerError::CantGetFileMetadata)?;
|
let file_md = file.metadata ().await.map_err (FileServerError::CantGetFileMetadata)?;
|
||||||
if file_md.permissions ().mode () == load_toml::CONFIG_PERMISSIONS_MODE
|
if file_md.permissions ().mode () == load_toml::CONFIG_PERMISSIONS_MODE
|
||||||
{
|
{
|
||||||
return Ok (InternalResponse::Forbidden);
|
return Ok (Response::Forbidden);
|
||||||
}
|
}
|
||||||
|
|
||||||
let file_len = file_md.len ();
|
let file_len = file_md.len ();
|
||||||
|
@ -122,17 +122,17 @@ async fn internal_serve_file (
|
||||||
let range_header = headers.get ("range").and_then (|v| std::str::from_utf8 (v).ok ());
|
let range_header = headers.get ("range").and_then (|v| std::str::from_utf8 (v).ok ());
|
||||||
|
|
||||||
Ok (match range::check (range_header, file_len) {
|
Ok (match range::check (range_header, file_len) {
|
||||||
range::Parsed::NotSatisfiable (file_len) => InternalResponse::RangeNotSatisfiable (file_len),
|
range::Parsed::NotSatisfiable (file_len) => Response::RangeNotSatisfiable (file_len),
|
||||||
range::Parsed::Valid (range) => {
|
range::Parsed::Valid (range) => {
|
||||||
if uri.query () == Some ("as_markdown") {
|
if uri.query () == Some ("as_markdown") {
|
||||||
const MAX_BUF_SIZE: u32 = 1_000_000;
|
const MAX_BUF_SIZE: u32 = 1_000_000;
|
||||||
|
|
||||||
if range.range_requested {
|
if range.range_requested {
|
||||||
return Ok (InternalResponse::InvalidQuery);
|
return Ok (Response::InvalidQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
if file_len > MAX_BUF_SIZE.into () {
|
if file_len > MAX_BUF_SIZE.into () {
|
||||||
InternalResponse::MarkdownErr (markdown::Error::TooBig)
|
Response::MarkdownErr (markdown::Error::TooBig)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let mut buffer = vec! [0_u8; MAX_BUF_SIZE.try_into ().expect ("Couldn't fit u32 into usize")];
|
let mut buffer = vec! [0_u8; MAX_BUF_SIZE.try_into ().expect ("Couldn't fit u32 into usize")];
|
||||||
|
@ -140,15 +140,15 @@ async fn internal_serve_file (
|
||||||
buffer.truncate (bytes_read);
|
buffer.truncate (bytes_read);
|
||||||
|
|
||||||
match render_styled (&buffer) {
|
match render_styled (&buffer) {
|
||||||
Ok (x) => InternalResponse::MarkdownPreview (x),
|
Ok (x) => Response::MarkdownPreview (x),
|
||||||
Err (x) => InternalResponse::MarkdownErr (x),
|
Err (x) => Response::MarkdownErr (x),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let file = file.into ();
|
let file = file.into ();
|
||||||
|
|
||||||
InternalResponse::ServeFile (ServeFileParams {
|
Response::ServeFile (ServeFileParams {
|
||||||
file,
|
file,
|
||||||
send_body,
|
send_body,
|
||||||
range,
|
range,
|
||||||
|
@ -158,17 +158,17 @@ async fn internal_serve_file (
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn internal_serve_all (
|
pub async fn serve_all (
|
||||||
root: &Path,
|
root: &Path,
|
||||||
method: Method,
|
method: Method,
|
||||||
uri: &str,
|
uri: &str,
|
||||||
headers: &HashMap <String, Vec <u8>>,
|
headers: &HashMap <String, Vec <u8>>,
|
||||||
hidden_path: Option <&Path>
|
hidden_path: Option <&Path>
|
||||||
)
|
)
|
||||||
-> Result <InternalResponse, FileServerError>
|
-> Result <Response, FileServerError>
|
||||||
{
|
{
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use InternalResponse::*;
|
use Response::*;
|
||||||
|
|
||||||
info! ("Client requested {}", uri);
|
info! ("Client requested {}", uri);
|
||||||
|
|
||||||
|
@ -214,7 +214,7 @@ pub async fn internal_serve_all (
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok (dir) = read_dir (&full_path).await {
|
if let Ok (dir) = read_dir (&full_path).await {
|
||||||
internal_serve_dir (
|
serve_dir (
|
||||||
&path_s,
|
&path_s,
|
||||||
path,
|
path,
|
||||||
dir,
|
dir,
|
||||||
|
@ -223,7 +223,7 @@ pub async fn internal_serve_all (
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
else if let Ok (file) = File::open (&full_path).await {
|
else if let Ok (file) = File::open (&full_path).await {
|
||||||
internal_serve_file (
|
serve_file (
|
||||||
file,
|
file,
|
||||||
&uri,
|
&uri,
|
||||||
send_body,
|
send_body,
|
||||||
|
|
|
@ -43,7 +43,6 @@ mod markdown;
|
||||||
mod range;
|
mod range;
|
||||||
|
|
||||||
use errors::FileServerError;
|
use errors::FileServerError;
|
||||||
use internal::*;
|
|
||||||
|
|
||||||
mod emoji {
|
mod emoji {
|
||||||
pub const VIDEO: &str = "\u{1f39e}\u{fe0f}";
|
pub const VIDEO: &str = "\u{1f39e}\u{fe0f}";
|
||||||
|
@ -317,7 +316,7 @@ pub async fn serve_all (
|
||||||
)
|
)
|
||||||
-> Result <Response, FileServerError>
|
-> Result <Response, FileServerError>
|
||||||
{
|
{
|
||||||
use InternalResponse::*;
|
use internal::Response::*;
|
||||||
|
|
||||||
fn serve_error <S: Into <Vec <u8>>> (
|
fn serve_error <S: Into <Vec <u8>>> (
|
||||||
status_code: StatusCode,
|
status_code: StatusCode,
|
||||||
|
@ -331,7 +330,7 @@ pub async fn serve_all (
|
||||||
resp
|
resp
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok (match internal_serve_all (root, method, uri, headers, hidden_path).await? {
|
Ok (match internal::serve_all (root, method, uri, headers, hidden_path).await? {
|
||||||
Favicon => serve_error (StatusCode::NotFound, ""),
|
Favicon => serve_error (StatusCode::NotFound, ""),
|
||||||
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden"),
|
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden"),
|
||||||
InvalidQuery => serve_error (StatusCode::BadRequest, "Query is invalid for this object"),
|
InvalidQuery => serve_error (StatusCode::BadRequest, "Query is invalid for this object"),
|
||||||
|
@ -351,11 +350,11 @@ pub async fn serve_all (
|
||||||
resp
|
resp
|
||||||
},
|
},
|
||||||
Root => serve_root (handlebars, server_info).await?,
|
Root => serve_root (handlebars, server_info).await?,
|
||||||
ServeDir (ServeDirParams {
|
ServeDir (internal::ServeDirParams {
|
||||||
path,
|
path,
|
||||||
dir,
|
dir,
|
||||||
}) => serve_dir (handlebars, server_info, path.to_string_lossy (), dir.into_inner ()).await?,
|
}) => serve_dir (handlebars, server_info, path.to_string_lossy (), dir.into_inner ()).await?,
|
||||||
ServeFile (ServeFileParams {
|
ServeFile (internal::ServeFileParams {
|
||||||
file,
|
file,
|
||||||
send_body,
|
send_body,
|
||||||
range,
|
range,
|
||||||
|
|
|
@ -94,7 +94,7 @@ fn file_server () {
|
||||||
let headers = Default::default ();
|
let headers = Default::default ();
|
||||||
|
|
||||||
{
|
{
|
||||||
use InternalResponse::*;
|
use internal::Response::*;
|
||||||
use crate::file_server::FileServerError;
|
use crate::file_server::FileServerError;
|
||||||
|
|
||||||
let bad_passwords_path = "/files/src/bad_passwords.txt";
|
let bad_passwords_path = "/files/src/bad_passwords.txt";
|
||||||
|
@ -105,7 +105,7 @@ fn file_server () {
|
||||||
("/files/?", InvalidQuery),
|
("/files/?", InvalidQuery),
|
||||||
("/files/src", Redirect ("src/".to_string ())),
|
("/files/src", Redirect ("src/".to_string ())),
|
||||||
("/files/src/?", InvalidQuery),
|
("/files/src/?", InvalidQuery),
|
||||||
(bad_passwords_path, ServeFile (ServeFileParams {
|
(bad_passwords_path, ServeFile (internal::ServeFileParams {
|
||||||
send_body: true,
|
send_body: true,
|
||||||
range: range::ValidParsed {
|
range: range::ValidParsed {
|
||||||
range: 0..1_048_576,
|
range: 0..1_048_576,
|
||||||
|
@ -113,7 +113,7 @@ fn file_server () {
|
||||||
},
|
},
|
||||||
file: AlwaysEqual::testing_blank (),
|
file: AlwaysEqual::testing_blank (),
|
||||||
})),
|
})),
|
||||||
("/files/test/test.md", ServeFile (ServeFileParams {
|
("/files/test/test.md", ServeFile (internal::ServeFileParams {
|
||||||
send_body: true,
|
send_body: true,
|
||||||
range: range::ValidParsed {
|
range: range::ValidParsed {
|
||||||
range: 0..144,
|
range: 0..144,
|
||||||
|
@ -122,7 +122,7 @@ fn file_server () {
|
||||||
file: AlwaysEqual::testing_blank (),
|
file: AlwaysEqual::testing_blank (),
|
||||||
})),
|
})),
|
||||||
] {
|
] {
|
||||||
let resp = internal_serve_all (
|
let resp = internal::serve_all (
|
||||||
&file_server_root,
|
&file_server_root,
|
||||||
Method::Get,
|
Method::Get,
|
||||||
uri_path,
|
uri_path,
|
||||||
|
@ -139,7 +139,7 @@ fn file_server () {
|
||||||
e => panic! ("Expected InvalidUri, got {:?}", e),
|
e => panic! ("Expected InvalidUri, got {:?}", e),
|
||||||
}),
|
}),
|
||||||
] {
|
] {
|
||||||
let resp = internal_serve_all (
|
let resp = internal::serve_all (
|
||||||
&file_server_root,
|
&file_server_root,
|
||||||
Method::Get,
|
Method::Get,
|
||||||
uri_path,
|
uri_path,
|
||||||
|
@ -150,7 +150,7 @@ fn file_server () {
|
||||||
checker (resp.unwrap_err ());
|
checker (resp.unwrap_err ());
|
||||||
}
|
}
|
||||||
|
|
||||||
let resp = internal_serve_all (
|
let resp = internal::serve_all (
|
||||||
&file_server_root,
|
&file_server_root,
|
||||||
Method::Get,
|
Method::Get,
|
||||||
bad_passwords_path,
|
bad_passwords_path,
|
||||||
|
@ -162,7 +162,7 @@ fn file_server () {
|
||||||
|
|
||||||
assert_eq! (resp.expect ("Should be Ok (_)"), RangeNotSatisfiable (1_048_576));
|
assert_eq! (resp.expect ("Should be Ok (_)"), RangeNotSatisfiable (1_048_576));
|
||||||
|
|
||||||
let resp = internal_serve_all (
|
let resp = internal::serve_all (
|
||||||
&file_server_root,
|
&file_server_root,
|
||||||
Method::Head,
|
Method::Head,
|
||||||
bad_passwords_path,
|
bad_passwords_path,
|
||||||
|
@ -170,7 +170,7 @@ fn file_server () {
|
||||||
None
|
None
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
assert_eq! (resp.expect ("Should be Ok (_)"), ServeFile (ServeFileParams {
|
assert_eq! (resp.expect ("Should be Ok (_)"), ServeFile (internal::ServeFileParams {
|
||||||
send_body: false,
|
send_body: false,
|
||||||
range: range::ValidParsed {
|
range: range::ValidParsed {
|
||||||
range: 0..1_048_576,
|
range: 0..1_048_576,
|
||||||
|
|
Loading…
Reference in New Issue