diff --git a/response/src/get_response.rs b/response/src/get_response.rs index ea03ea7..fec230f 100644 --- a/response/src/get_response.rs +++ b/response/src/get_response.rs @@ -9,35 +9,24 @@ use tokio::fs; use tokio_util::io::ReaderStream; use crate::content_type::get_content_type; -use crate::last_resort_response::build_last_resort_response; -use crate::range_response::build_range_response; -use crate::response_paths::{ - add_extension, get_encodings, get_filepath, get_path_from_request_url, -}; +use crate::last_resort_response; +use crate::range_response; +use crate::response_paths::{add_extension, get_encodings, get_path, get_path_from_request_url}; use crate::type_flyweight::{BoxedResponse, ResponseParams, NOT_FOUND_404}; -pub async fn build_get_response( +pub async fn build_response( req: Request, res_params: ResponseParams, ) -> Result { // check for range request - if let Some(res) = build_range_response(&req, &res_params).await { + if let Some(res) = range_response::build_response(&req, &res_params).await { return res; } // fallback to file response - build_file_response(req, &res_params).await -} - -async fn build_file_response( - req: Request, - res_params: &ResponseParams, -) -> Result { - // request file let encodings = get_encodings(&req, &res_params.available_encodings); // serve file - // if get_path_from_request_url, build response if let Some(res) = build_req_path_response(&req, &res_params.directory, &encodings).await { return res; }; @@ -49,7 +38,7 @@ async fn build_file_response( return res; }; - build_last_resort_response(StatusCode::NOT_FOUND, NOT_FOUND_404) + last_resort_response::build_response(StatusCode::NOT_FOUND, NOT_FOUND_404) } async fn build_req_path_response( @@ -62,7 +51,7 @@ async fn build_req_path_response( _ => return None, }; - build_response(&filepath, StatusCode::OK, &encodings).await + build_get_response(&filepath, StatusCode::OK, &encodings).await } async fn build_not_found_response( @@ -76,15 +65,15 @@ async fn build_not_found_response( }; // file starts with directory - let filepath_404 = match get_filepath(directory, fallback).await { + let filepath_404 = match get_path(directory, fallback).await { Some(fb) => fb, _ => return None, }; - build_response(&filepath_404, StatusCode::NOT_FOUND, &encodings).await + build_get_response(&filepath_404, StatusCode::NOT_FOUND, &encodings).await } -async fn build_response( +async fn build_get_response( filepath: &PathBuf, status_code: StatusCode, encodings: &Option>, diff --git a/response/src/head_response.rs b/response/src/head_response.rs index 9abdcb9..c1485ab 100644 --- a/response/src/head_response.rs +++ b/response/src/head_response.rs @@ -7,11 +7,11 @@ use std::path::PathBuf; use tokio::fs; use crate::content_type::get_content_type; -use crate::last_resort_response::build_last_resort_response; +use crate::last_resort_response; use crate::response_paths::{add_extension, get_encodings, get_path_from_request_url}; use crate::type_flyweight::{BoxedResponse, ResponseParams, NOT_FOUND_404}; -pub async fn build_head_response( +pub async fn build_response( req: Request, res_params: ResponseParams, ) -> Result { @@ -31,7 +31,7 @@ pub async fn build_head_response( } }; - build_last_resort_response(StatusCode::NOT_FOUND, NOT_FOUND_404) + last_resort_response::build_response(StatusCode::NOT_FOUND, NOT_FOUND_404) } async fn compose_encoded_response( diff --git a/response/src/last_resort_response.rs b/response/src/last_resort_response.rs index e576b38..bc1a1ac 100644 --- a/response/src/last_resort_response.rs +++ b/response/src/last_resort_response.rs @@ -7,7 +7,7 @@ use hyper::StatusCode; use crate::content_type::HTML; use crate::type_flyweight::BoxedResponse; -pub fn build_last_resort_response( +pub fn build_response( status_code: StatusCode, body: &'static str, ) -> Result { diff --git a/response/src/range_response.rs b/response/src/range_response.rs index f0dfcbc..967aff6 100644 --- a/response/src/range_response.rs +++ b/response/src/range_response.rs @@ -11,9 +11,11 @@ use tokio::io::AsyncSeekExt; use tokio_util::io::ReaderStream; use crate::content_type::get_content_type; -use crate::last_resort_response::build_last_resort_response; +use crate::last_resort_response; use crate::response_paths::{add_extension, get_encodings, get_path_from_request_url}; -use crate::type_flyweight::{BoxedResponse, ResponseParams, BAD_REQUEST_400, NOT_FOUND_404}; +use crate::type_flyweight::{ + BoxedResponse, ResponseParams, BAD_REQUEST_400, NOT_FOUND_404, RANGE_NOT_SATISFIABLE_416, +}; // Range: =- // Range: =- @@ -22,9 +24,7 @@ use crate::type_flyweight::{BoxedResponse, ResponseParams, BAD_REQUEST_400, NOT_ // multi range requests require an entirely different strategy // Range: =-, …, - -pub const RANGE_NOT_SATISFIABLE_416: &str = "416 range not satisfiable"; - -pub async fn build_range_response( +pub async fn build_response( req: &Request, res_params: &ResponseParams, ) -> Option> { @@ -43,13 +43,13 @@ pub async fn build_range_response( } }; - return Some(build_last_resort_response( + return Some(last_resort_response::build_response( StatusCode::RANGE_NOT_SATISFIABLE, RANGE_NOT_SATISFIABLE_416, )); } - Some(build_last_resort_response( + Some(last_resort_response::build_response( StatusCode::NOT_FOUND, NOT_FOUND_404, )) @@ -207,10 +207,10 @@ async fn compose_single_range_response( _ => return None, }; - let (start, end) = match get_start_end(ranges, size) { + let (start, end) = match get_start_and_end(ranges, size) { Some(se) => se, _ => { - return Some(build_last_resort_response( + return Some(last_resort_response::build_response( StatusCode::BAD_REQUEST, BAD_REQUEST_400, )) @@ -242,7 +242,7 @@ async fn compose_single_range_response( return Some(builder.body(boxed_body)); } -fn get_start_end( +fn get_start_and_end( ranges: &Vec<(Option, Option)>, size: usize, ) -> Option<(usize, usize)> { diff --git a/response/src/response_paths.rs b/response/src/response_paths.rs index c35ca27..1b62bb3 100644 --- a/response/src/response_paths.rs +++ b/response/src/response_paths.rs @@ -19,10 +19,10 @@ pub async fn get_path_from_request_url( _ => uri_path, }; - get_filepath(directory, &PathBuf::from(stripped)).await + get_path(directory, &PathBuf::from(stripped)).await } -pub async fn get_filepath(directory: &PathBuf, filepath: &PathBuf) -> Option { +pub async fn get_path(directory: &PathBuf, filepath: &PathBuf) -> Option { let mut target_path = match path::absolute(directory.join(&filepath)) { Ok(pb) => pb, _ => return None, @@ -33,28 +33,28 @@ pub async fn get_filepath(directory: &PathBuf, filepath: &PathBuf) -> Option sdf, + let metadata = match fs::metadata(&target_path).await { + Ok(md) => md, _ => return None, }; // if file bail early - if mtdt.is_file() { + if metadata.is_file() { return Some(target_path); } // if directory try an index.html file - if mtdt.is_dir() { + if metadata.is_dir() { target_path.push("index.html"); - } - let mtdt = match fs::metadata(&target_path).await { - Ok(sdf) => sdf, - _ => return None, - }; + let updated_metadata = match fs::metadata(&target_path).await { + Ok(md) => md, + _ => return None, + }; - if mtdt.is_file() { - return Some(target_path); + if updated_metadata.is_file() { + return Some(target_path); + } } None diff --git a/response/src/responses.rs b/response/src/responses.rs index b29c275..052fbfb 100644 --- a/response/src/responses.rs +++ b/response/src/responses.rs @@ -3,20 +3,21 @@ use hyper::http::Request; use hyper::Method; use hyper::StatusCode; -use crate::get_response::build_get_response; -use crate::head_response::build_head_response; -use crate::last_resort_response::build_last_resort_response; -use crate::type_flyweight::{BoxedResponse, ResponseParams}; - -pub const METHOD_NOT_ALLOWED_405: &str = "405 method not allowed"; +use crate::get_response; +use crate::head_response; +use crate::last_resort_response; +use crate::type_flyweight::{BoxedResponse, ResponseParams, METHOD_NOT_ALLOWED_405}; pub async fn build_response( req: Request, res_params: ResponseParams, ) -> Result { match req.method() { - &Method::GET => build_get_response(req, res_params).await, - &Method::HEAD => build_head_response(req, res_params).await, - _ => build_last_resort_response(StatusCode::METHOD_NOT_ALLOWED, METHOD_NOT_ALLOWED_405), + &Method::GET => get_response::build_response(req, res_params).await, + &Method::HEAD => head_response::build_response(req, res_params).await, + _ => last_resort_response::build_response( + StatusCode::METHOD_NOT_ALLOWED, + METHOD_NOT_ALLOWED_405, + ), } } diff --git a/response/src/type_flyweight.rs b/response/src/type_flyweight.rs index 5456639..3c96087 100644 --- a/response/src/type_flyweight.rs +++ b/response/src/type_flyweight.rs @@ -8,8 +8,10 @@ use crate::available_encodings::AvailableEncodings; pub type BoxedResponse = Response>; -pub const NOT_FOUND_404: &str = "404 not found"; pub const BAD_REQUEST_400: &str = "400 bad request"; +pub const NOT_FOUND_404: &str = "404 not found"; +pub const METHOD_NOT_ALLOWED_405: &str = "405 method not allowed"; +pub const RANGE_NOT_SATISFIABLE_416: &str = "416 range not satisfiable"; #[derive(Clone, Debug)] pub struct ResponseParams {