Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions response/src/get_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Incoming>,
res_params: ResponseParams,
) -> Result<BoxedResponse, hyper::http::Error> {
// 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<Incoming>,
res_params: &ResponseParams,
) -> Result<BoxedResponse, hyper::http::Error> {
// 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;
};
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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<Vec<String>>,
Expand Down
6 changes: 3 additions & 3 deletions response/src/head_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Incoming>,
res_params: ResponseParams,
) -> Result<BoxedResponse, hyper::http::Error> {
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion response/src/last_resort_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BoxedResponse, hyper::http::Error> {
Expand Down
20 changes: 10 additions & 10 deletions response/src/range_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: <unit>=<range-start>-
// Range: <unit>=<range-start>-<range-end>
Expand All @@ -22,9 +24,7 @@ use crate::type_flyweight::{BoxedResponse, ResponseParams, BAD_REQUEST_400, NOT_
// multi range requests require an entirely different strategy
// Range: <unit>=<range-start>-<range-end>, …, <range-startN>-<range-endN>

pub const RANGE_NOT_SATISFIABLE_416: &str = "416 range not satisfiable";

pub async fn build_range_response(
pub async fn build_response(
req: &Request<IncomingBody>,
res_params: &ResponseParams,
) -> Option<Result<BoxedResponse, hyper::http::Error>> {
Expand All @@ -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,
))
Expand Down Expand Up @@ -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,
))
Expand Down Expand Up @@ -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<usize>, Option<usize>)>,
size: usize,
) -> Option<(usize, usize)> {
Expand Down
26 changes: 13 additions & 13 deletions response/src/response_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
pub async fn get_path(directory: &PathBuf, filepath: &PathBuf) -> Option<PathBuf> {
let mut target_path = match path::absolute(directory.join(&filepath)) {
Ok(pb) => pb,
_ => return None,
Expand All @@ -33,28 +33,28 @@ pub async fn get_filepath(directory: &PathBuf, filepath: &PathBuf) -> Option<Pat
return None;
}

let mtdt = match fs::metadata(&target_path).await {
Ok(sdf) => 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
Expand Down
19 changes: 10 additions & 9 deletions response/src/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Incoming>,
res_params: ResponseParams,
) -> Result<BoxedResponse, hyper::http::Error> {
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,
),
}
}
4 changes: 3 additions & 1 deletion response/src/type_flyweight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use crate::available_encodings::AvailableEncodings;

pub type BoxedResponse = Response<BoxBody<Bytes, io::Error>>;

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 {
Expand Down