♻️ Remove "internal" from names in the internal module

main
_ 2020-11-29 23:15:45 +00:00
parent bb4c4e803a
commit 8079b3f778
3 changed files with 32 additions and 33 deletions

View File

@ -56,7 +56,7 @@ pub struct ServeFileParams {
}
#[derive (Debug, PartialEq)]
pub enum InternalResponse {
pub enum Response {
Favicon,
Forbidden,
InvalidQuery,
@ -72,49 +72,49 @@ pub enum InternalResponse {
MarkdownPreview (String),
}
fn internal_serve_dir (
fn serve_dir (
path_s: &str,
path: &Path,
dir: tokio::fs::ReadDir,
full_path: PathBuf,
uri: &http::Uri
)
-> Result <InternalResponse, FileServerError>
-> Result <Response, 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)));
return Ok (Response::Redirect (format! ("{}/", file_name)));
}
if uri.query ().is_some () {
return Ok (InternalResponse::InvalidQuery);
return Ok (Response::InvalidQuery);
}
let dir = dir.into ();
Ok (InternalResponse::ServeDir (ServeDirParams {
Ok (Response::ServeDir (ServeDirParams {
dir,
path: full_path,
}))
}
async fn internal_serve_file (
async fn serve_file (
mut file: tokio::fs::File,
uri: &http::Uri,
send_body: bool,
headers: &HashMap <String, Vec <u8>>
)
-> Result <InternalResponse, FileServerError>
-> Result <Response, FileServerError>
{
use std::os::unix::fs::PermissionsExt;
let file_md = file.metadata ().await.map_err (FileServerError::CantGetFileMetadata)?;
if file_md.permissions ().mode () == load_toml::CONFIG_PERMISSIONS_MODE
{
return Ok (InternalResponse::Forbidden);
return Ok (Response::Forbidden);
}
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 ());
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) => {
if uri.query () == Some ("as_markdown") {
const MAX_BUF_SIZE: u32 = 1_000_000;
if range.range_requested {
return Ok (InternalResponse::InvalidQuery);
return Ok (Response::InvalidQuery);
}
if file_len > MAX_BUF_SIZE.into () {
InternalResponse::MarkdownErr (markdown::Error::TooBig)
Response::MarkdownErr (markdown::Error::TooBig)
}
else {
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);
match render_styled (&buffer) {
Ok (x) => InternalResponse::MarkdownPreview (x),
Err (x) => InternalResponse::MarkdownErr (x),
Ok (x) => Response::MarkdownPreview (x),
Err (x) => Response::MarkdownErr (x),
}
}
}
else {
let file = file.into ();
InternalResponse::ServeFile (ServeFileParams {
Response::ServeFile (ServeFileParams {
file,
send_body,
range,
@ -158,17 +158,17 @@ async fn internal_serve_file (
})
}
pub async fn internal_serve_all (
pub async fn serve_all (
root: &Path,
method: Method,
uri: &str,
headers: &HashMap <String, Vec <u8>>,
hidden_path: Option <&Path>
)
-> Result <InternalResponse, FileServerError>
-> Result <Response, FileServerError>
{
use std::str::FromStr;
use InternalResponse::*;
use Response::*;
info! ("Client requested {}", uri);
@ -214,7 +214,7 @@ pub async fn internal_serve_all (
}
if let Ok (dir) = read_dir (&full_path).await {
internal_serve_dir (
serve_dir (
&path_s,
path,
dir,
@ -223,7 +223,7 @@ pub async fn internal_serve_all (
)
}
else if let Ok (file) = File::open (&full_path).await {
internal_serve_file (
serve_file (
file,
&uri,
send_body,

View File

@ -43,7 +43,6 @@ mod markdown;
mod range;
use errors::FileServerError;
use internal::*;
mod emoji {
pub const VIDEO: &str = "\u{1f39e}\u{fe0f}";
@ -317,7 +316,7 @@ pub async fn serve_all (
)
-> Result <Response, FileServerError>
{
use InternalResponse::*;
use internal::Response::*;
fn serve_error <S: Into <Vec <u8>>> (
status_code: StatusCode,
@ -331,7 +330,7 @@ pub async fn serve_all (
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, ""),
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden"),
InvalidQuery => serve_error (StatusCode::BadRequest, "Query is invalid for this object"),
@ -351,11 +350,11 @@ pub async fn serve_all (
resp
},
Root => serve_root (handlebars, server_info).await?,
ServeDir (ServeDirParams {
ServeDir (internal::ServeDirParams {
path,
dir,
}) => serve_dir (handlebars, server_info, path.to_string_lossy (), dir.into_inner ()).await?,
ServeFile (ServeFileParams {
ServeFile (internal::ServeFileParams {
file,
send_body,
range,

View File

@ -94,7 +94,7 @@ fn file_server () {
let headers = Default::default ();
{
use InternalResponse::*;
use internal::Response::*;
use crate::file_server::FileServerError;
let bad_passwords_path = "/files/src/bad_passwords.txt";
@ -105,7 +105,7 @@ fn file_server () {
("/files/?", InvalidQuery),
("/files/src", Redirect ("src/".to_string ())),
("/files/src/?", InvalidQuery),
(bad_passwords_path, ServeFile (ServeFileParams {
(bad_passwords_path, ServeFile (internal::ServeFileParams {
send_body: true,
range: range::ValidParsed {
range: 0..1_048_576,
@ -113,7 +113,7 @@ fn file_server () {
},
file: AlwaysEqual::testing_blank (),
})),
("/files/test/test.md", ServeFile (ServeFileParams {
("/files/test/test.md", ServeFile (internal::ServeFileParams {
send_body: true,
range: range::ValidParsed {
range: 0..144,
@ -122,7 +122,7 @@ fn file_server () {
file: AlwaysEqual::testing_blank (),
})),
] {
let resp = internal_serve_all (
let resp = internal::serve_all (
&file_server_root,
Method::Get,
uri_path,
@ -139,7 +139,7 @@ fn file_server () {
e => panic! ("Expected InvalidUri, got {:?}", e),
}),
] {
let resp = internal_serve_all (
let resp = internal::serve_all (
&file_server_root,
Method::Get,
uri_path,
@ -150,7 +150,7 @@ fn file_server () {
checker (resp.unwrap_err ());
}
let resp = internal_serve_all (
let resp = internal::serve_all (
&file_server_root,
Method::Get,
bad_passwords_path,
@ -162,7 +162,7 @@ fn file_server () {
assert_eq! (resp.expect ("Should be Ok (_)"), RangeNotSatisfiable (1_048_576));
let resp = internal_serve_all (
let resp = internal::serve_all (
&file_server_root,
Method::Head,
bad_passwords_path,
@ -170,7 +170,7 @@ fn file_server () {
None
).await;
assert_eq! (resp.expect ("Should be Ok (_)"), ServeFile (ServeFileParams {
assert_eq! (resp.expect ("Should be Ok (_)"), ServeFile (internal::ServeFileParams {
send_body: false,
range: range::ValidParsed {
range: 0..1_048_576,