diff --git a/response/src/get_response.rs b/response/src/get_response.rs index aa847cb..ea03ea7 100644 --- a/response/src/get_response.rs +++ b/response/src/get_response.rs @@ -9,12 +9,12 @@ use tokio::fs; use tokio_util::io::ReaderStream; use crate::content_type::get_content_type; -use crate::last_resort_response::{build_last_resort_response, NOT_FOUND_404}; +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::type_flyweight::{BoxedResponse, ResponseParams}; +use crate::type_flyweight::{BoxedResponse, ResponseParams, NOT_FOUND_404}; pub async fn build_get_response( req: Request, diff --git a/response/src/head_response.rs b/response/src/head_response.rs index 2099042..9abdcb9 100644 --- a/response/src/head_response.rs +++ b/response/src/head_response.rs @@ -7,9 +7,9 @@ use std::path::PathBuf; use tokio::fs; use crate::content_type::get_content_type; -use crate::last_resort_response::{build_last_resort_response, NOT_FOUND_404}; +use crate::last_resort_response::build_last_resort_response; use crate::response_paths::{add_extension, get_encodings, get_path_from_request_url}; -use crate::type_flyweight::{BoxedResponse, ResponseParams}; +use crate::type_flyweight::{BoxedResponse, ResponseParams, NOT_FOUND_404}; pub async fn build_head_response( req: Request, diff --git a/response/src/last_resort_response.rs b/response/src/last_resort_response.rs index 908bcbb..e576b38 100644 --- a/response/src/last_resort_response.rs +++ b/response/src/last_resort_response.rs @@ -7,8 +7,6 @@ use hyper::StatusCode; use crate::content_type::HTML; use crate::type_flyweight::BoxedResponse; -pub const NOT_FOUND_404: &str = "404 not found"; - pub fn build_last_resort_response( status_code: StatusCode, body: &'static str, diff --git a/response/src/range_response.rs b/response/src/range_response.rs index 330c2b1..f0dfcbc 100644 --- a/response/src/range_response.rs +++ b/response/src/range_response.rs @@ -10,11 +10,10 @@ use tokio::fs::File; use tokio::io::AsyncSeekExt; use tokio_util::io::ReaderStream; -use crate::available_encodings::AvailableEncodings; use crate::content_type::get_content_type; -use crate::last_resort_response::{build_last_resort_response, NOT_FOUND_404}; +use crate::last_resort_response::build_last_resort_response; use crate::response_paths::{add_extension, get_encodings, get_path_from_request_url}; -use crate::type_flyweight::{BoxedResponse, ResponseParams}; +use crate::type_flyweight::{BoxedResponse, ResponseParams, BAD_REQUEST_400, NOT_FOUND_404}; // Range: =- // Range: =- @@ -23,7 +22,6 @@ use crate::type_flyweight::{BoxedResponse, ResponseParams}; // multi range requests require an entirely different strategy // Range: =-, …, - -pub const BAD_REQUEST_400: &str = "400 bad request"; pub const RANGE_NOT_SATISFIABLE_416: &str = "416 range not satisfiable"; pub async fn build_range_response( @@ -36,41 +34,12 @@ pub async fn build_range_response( _ => return None, }; - compose_range_response( - req, - &res_params.directory, - &res_params.available_encodings, - range_header, - ) - .await -} - -fn get_range_header(req: &Request) -> Option { - let accept_encoding_header = match req.headers().get(RANGE) { - Some(enc) => enc, - _ => return None, - }; - - match accept_encoding_header.to_str() { - Ok(s) => Some(s.to_string()), - _ => None, - } -} - -async fn compose_range_response( - req: &Request, - directory: &PathBuf, - available_encodings: &AvailableEncodings, - range_header: String, -) -> Option> { - if let Some(filepath) = get_path_from_request_url(req, directory).await { + if let Some(filepath) = get_path_from_request_url(req, &res_params.directory).await { if let Some(ranges) = get_ranges(&range_header) { - let encodings = get_encodings(req, available_encodings); + let encodings = get_encodings(req, &res_params.available_encodings); - if 1 == ranges.len() { - if let Some(res) = build_single_range_response(&filepath, encodings, ranges).await { - return Some(res); - } + if let Some(res) = build_single_range_response(&filepath, encodings, ranges).await { + return Some(res); } }; @@ -78,7 +47,7 @@ async fn compose_range_response( StatusCode::RANGE_NOT_SATISFIABLE, RANGE_NOT_SATISFIABLE_416, )); - }; + } Some(build_last_resort_response( StatusCode::NOT_FOUND, @@ -86,6 +55,18 @@ async fn compose_range_response( )) } +fn get_range_header(req: &Request) -> Option { + let accept_encoding_header = match req.headers().get(RANGE) { + Some(enc) => enc, + _ => return None, + }; + + match accept_encoding_header.to_str() { + Ok(s) => Some(s.to_string()), + _ => None, + } +} + // on any fail return nothing fn get_ranges(range_header_value: &str) -> Option, Option)>> { let ranges_str = match range_header_value.trim().strip_prefix("bytes=") { @@ -170,9 +151,14 @@ async fn build_single_range_response( encodings: Option>, ranges: Vec<(Option, Option)>, ) -> Option> { + if 1 != ranges.len() { + return None; + }; + let content_type = get_content_type(&filepath); - if let Some(res) = compose_encoded_response(&filepath, content_type, &encodings, &ranges).await + if let Some(res) = + compose_encoded_single_range_response(&filepath, content_type, &encodings, &ranges).await { return Some(res); }; @@ -181,7 +167,7 @@ async fn build_single_range_response( compose_single_range_response(&filepath, content_type, None, &ranges).await } -async fn compose_encoded_response( +async fn compose_encoded_single_range_response( filepath: &PathBuf, content_type: &str, encodings: &Option>, @@ -216,7 +202,7 @@ async fn compose_single_range_response( _ => return None, }; - let size: usize = match file.metadata().await { + let size = match file.metadata().await { Ok(md) => md.len() as usize, _ => return None, }; diff --git a/response/src/type_flyweight.rs b/response/src/type_flyweight.rs index 72122d1..5456639 100644 --- a/response/src/type_flyweight.rs +++ b/response/src/type_flyweight.rs @@ -8,6 +8,9 @@ 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"; + #[derive(Clone, Debug)] pub struct ResponseParams { pub directory: PathBuf,