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
4 changes: 2 additions & 2 deletions response/src/get_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Incoming>,
Expand Down
4 changes: 2 additions & 2 deletions response/src/head_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Incoming>,
Expand Down
2 changes: 0 additions & 2 deletions response/src/last_resort_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 27 additions & 41 deletions response/src/range_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: <unit>=<range-start>-
// Range: <unit>=<range-start>-<range-end>
Expand All @@ -23,7 +22,6 @@ use crate::type_flyweight::{BoxedResponse, ResponseParams};
// multi range requests require an entirely different strategy
// Range: <unit>=<range-start>-<range-end>, …, <range-startN>-<range-endN>

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(
Expand All @@ -36,56 +34,39 @@ 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<IncomingBody>) -> Option<String> {
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<IncomingBody>,
directory: &PathBuf,
available_encodings: &AvailableEncodings,
range_header: String,
) -> Option<Result<BoxedResponse, hyper::http::Error>> {
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);
}
};

return Some(build_last_resort_response(
StatusCode::RANGE_NOT_SATISFIABLE,
RANGE_NOT_SATISFIABLE_416,
));
};
}

Some(build_last_resort_response(
StatusCode::NOT_FOUND,
NOT_FOUND_404,
))
}

fn get_range_header(req: &Request<IncomingBody>) -> Option<String> {
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<Vec<(Option<usize>, Option<usize>)>> {
let ranges_str = match range_header_value.trim().strip_prefix("bytes=") {
Expand Down Expand Up @@ -170,9 +151,14 @@ async fn build_single_range_response(
encodings: Option<Vec<String>>,
ranges: Vec<(Option<usize>, Option<usize>)>,
) -> Option<Result<BoxedResponse, hyper::http::Error>> {
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);
};
Expand All @@ -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<Vec<String>>,
Expand Down Expand Up @@ -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,
};
Expand Down
3 changes: 3 additions & 0 deletions response/src/type_flyweight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ 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";

#[derive(Clone, Debug)]
pub struct ResponseParams {
pub directory: PathBuf,
Expand Down