♻️ Fix clippy issues

main
_ 2020-11-29 22:41:48 +00:00
parent b94a3a1e17
commit ab95485d92
3 changed files with 74 additions and 76 deletions

View File

@ -55,11 +55,6 @@ use errors::{
FileServerError,
MarkdownError,
};
use range::{
check_range,
ParsedRange,
ValidParsedRange,
};
mod emoji {
pub const VIDEO: &str = "\u{1f39e}\u{fe0f}";
@ -228,7 +223,7 @@ async fn serve_dir (
async fn serve_file (
mut f: File,
should_send_body: bool,
range: ValidParsedRange
range: range::ValidParsed
)
-> Result <Response, FileServerError>
{
@ -375,7 +370,7 @@ struct ServeDirParams {
#[derive (Debug, PartialEq)]
struct ServeFileParams {
send_body: bool,
range: ValidParsedRange,
range: range::ValidParsed,
file: AlwaysEqual <File>,
}
@ -445,15 +440,16 @@ async fn internal_serve_file (
let range_header = headers.get ("range").and_then (|v| std::str::from_utf8 (v).ok ());
Ok (match check_range (range_header, file_len) {
ParsedRange::RangeNotSatisfiable (file_len) => InternalResponse::RangeNotSatisfiable (file_len),
ParsedRange::Valid (range) => {
Ok (match range::check (range_header, file_len) {
range::Parsed::NotSatisfiable (file_len) => InternalResponse::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);
}
const MAX_BUF_SIZE: u32 = 1_000_000;
if file_len > MAX_BUF_SIZE.into () {
InternalResponse::MarkdownErr (MarkdownError::TooBig)
}

View File

@ -2,7 +2,8 @@ use std::ops::Range;
use regex::Regex;
pub fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
fn parse (range_str: &str) -> (Option <u64>, Option <u64>)
{
use lazy_static::lazy_static;
lazy_static! {
@ -27,33 +28,32 @@ pub fn parse_range_header (range_str: &str) -> (Option <u64>, Option <u64>) {
}
#[derive (Debug, PartialEq)]
pub struct ValidParsedRange {
pub struct ValidParsed {
pub range: Range <u64>,
pub range_requested: bool,
}
#[derive (Debug, PartialEq)]
pub enum ParsedRange {
Valid (ValidParsedRange),
RangeNotSatisfiable (u64),
pub enum Parsed {
Valid (ValidParsed),
NotSatisfiable (u64),
}
pub fn check_range (range_str: Option <&str>, file_len: u64)
-> ParsedRange
pub fn check (range_str: Option <&str>, file_len: u64) -> Parsed
{
use ParsedRange::*;
use Parsed::*;
let not_satisfiable = RangeNotSatisfiable (file_len);
let not_satisfiable = NotSatisfiable (file_len);
let range_str = match range_str {
None => return Valid (ValidParsedRange {
None => return Valid (ValidParsed {
range: 0..file_len,
range_requested: false,
}),
Some (x) => x,
};
let (start, end) = parse_range_header (range_str);
let (start, end) = parse (range_str);
let start = start.unwrap_or (0);
if start >= file_len {
@ -68,8 +68,60 @@ pub fn check_range (range_str: Option <&str>, file_len: u64)
return not_satisfiable;
}
Valid (ValidParsedRange {
Valid (ValidParsed {
range: start..end,
range_requested: true,
})
}
#[cfg (test)]
mod tests {
#[test]
fn test_byte_ranges () {
use super::*;
for (input, expected) in vec! [
("", (None, None)),
("bytes=0-", (Some (0), None)),
("bytes=0-999", (Some (0), Some (1000))),
("bytes=111-999", (Some (111), Some (1000))),
].into_iter () {
let actual = parse (input);
assert_eq! (actual, expected);
}
let ok_range = |range| Parsed::Valid (ValidParsed {
range,
range_requested: false,
});
let partial_content = |range| Parsed::Valid (ValidParsed {
range,
range_requested: true,
});
let not_satisfiable = |file_len| Parsed::NotSatisfiable (file_len);
for (header, file_len, expected) in vec! [
(None, 0, ok_range (0..0)),
(None, 1024, ok_range (0..1024)),
(Some (""), 0, not_satisfiable (0)),
(Some (""), 1024, partial_content (0..1024)),
(Some ("bytes=0-"), 1024, partial_content (0..1024)),
(Some ("bytes=0-999"), 1024, partial_content (0..1000)),
(Some ("bytes=0-1023"), 1024, partial_content (0..1024)),
(Some ("bytes=111-999"), 1024, partial_content (111..1000)),
(Some ("bytes=111-1023"), 1024, partial_content (111..1024)),
(Some ("bytes=200-100"), 1024, not_satisfiable (1024)),
(Some ("bytes=0-"), 512, partial_content (0..512)),
(Some ("bytes=0-1023"), 512, not_satisfiable (512)),
(Some ("bytes=1000-1023"), 512, not_satisfiable (512)),
].into_iter () {
let actual = check (header, file_len);
assert_eq! (actual, expected);
}
}
}

View File

@ -32,56 +32,6 @@ fn icons () {
}
}
#[test]
fn parse_range_header () {
use super::range::{
*,
ParsedRange::*,
};
for (input, expected) in vec! [
("", (None, None)),
("bytes=0-", (Some (0), None)),
("bytes=0-999", (Some (0), Some (1000))),
("bytes=111-999", (Some (111), Some (1000))),
].into_iter () {
let actual = parse_range_header (input);
assert_eq! (actual, expected);
}
let ok_range = |range| Valid (ValidParsedRange {
range,
range_requested: false,
});
let partial_content = |range| Valid (ValidParsedRange {
range,
range_requested: true,
});
for (header, file_len, expected) in vec! [
(None, 0, ok_range (0..0)),
(None, 1024, ok_range (0..1024)),
(Some (""), 0, RangeNotSatisfiable (0)),
(Some (""), 1024, partial_content (0..1024)),
(Some ("bytes=0-"), 1024, partial_content (0..1024)),
(Some ("bytes=0-999"), 1024, partial_content (0..1000)),
(Some ("bytes=0-1023"), 1024, partial_content (0..1024)),
(Some ("bytes=111-999"), 1024, partial_content (111..1000)),
(Some ("bytes=111-1023"), 1024, partial_content (111..1024)),
(Some ("bytes=200-100"), 1024, RangeNotSatisfiable (1024)),
(Some ("bytes=0-"), 512, partial_content (0..512)),
(Some ("bytes=0-1023"), 512, RangeNotSatisfiable (512)),
(Some ("bytes=1000-1023"), 512, RangeNotSatisfiable (512)),
].into_iter () {
let actual = super::check_range (header, file_len);
assert_eq! (actual, expected);
}
}
#[test]
fn pretty_print_bytes () {
for (input_after, expected_before, expected_after) in vec! [
@ -152,7 +102,7 @@ fn file_server () {
("/files/src/?", InvalidQuery),
(bad_passwords_path, ServeFile (ServeFileParams {
send_body: true,
range: ValidParsedRange {
range: range::ValidParsed {
range: 0..1_048_576,
range_requested: false,
},
@ -160,7 +110,7 @@ fn file_server () {
})),
("/files/test/test.md", ServeFile (ServeFileParams {
send_body: true,
range: ValidParsedRange {
range: range::ValidParsed {
range: 0..144,
range_requested: false,
},
@ -217,7 +167,7 @@ fn file_server () {
assert_eq! (resp.expect ("Should be Ok (_)"), ServeFile (ServeFileParams {
send_body: false,
range: ValidParsedRange {
range: range::ValidParsed {
range: 0..1_048_576,
range_requested: false,
},