1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

address clippy warnings

This commit is contained in:
Rob Ede 2020-12-27 20:53:19 +00:00
parent f9fcf56d5c
commit 8c9ea43e23
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6
4 changed files with 25 additions and 19 deletions

View File

@ -1,6 +1,7 @@
# Changes # Changes
## Unreleased - 2020-xx-xx ## Unreleased - 2020-xx-xx
* `HttpRange::parse` now has its own error type.
## 0.5.0 - 2020-12-26 ## 0.5.0 - 2020-12-26

View File

@ -1,3 +1,5 @@
use derive_more::{Display, Error};
/// HTTP Range header representation. /// HTTP Range header representation.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct HttpRange { pub struct HttpRange {
@ -11,17 +13,21 @@ pub struct HttpRange {
const PREFIX: &str = "bytes="; const PREFIX: &str = "bytes=";
const PREFIX_LEN: usize = 6; const PREFIX_LEN: usize = 6;
#[derive(Debug, Clone, Display, Error)]
#[display(fmt = "Parse HTTP Range failed")]
pub struct ParseRangeErr(#[error(not(source))] ());
impl HttpRange { impl HttpRange {
/// Parses Range HTTP header string as per RFC 2616. /// Parses Range HTTP header string as per RFC 2616.
/// ///
/// `header` is HTTP Range header (e.g. `bytes=bytes=0-9`). /// `header` is HTTP Range header (e.g. `bytes=bytes=0-9`).
/// `size` is full size of response (file). /// `size` is full size of response (file).
pub fn parse(header: &str, size: u64) -> Result<Vec<HttpRange>, ()> { pub fn parse(header: &str, size: u64) -> Result<Vec<HttpRange>, ParseRangeErr> {
if header.is_empty() { if header.is_empty() {
return Ok(Vec::new()); return Ok(Vec::new());
} }
if !header.starts_with(PREFIX) { if !header.starts_with(PREFIX) {
return Err(()); return Err(ParseRangeErr(()));
} }
let size_sig = size as i64; let size_sig = size as i64;
@ -34,13 +40,14 @@ impl HttpRange {
.map(|ra| { .map(|ra| {
let mut start_end_iter = ra.split('-'); let mut start_end_iter = ra.split('-');
let start_str = start_end_iter.next().ok_or(())?.trim(); let start_str = start_end_iter.next().ok_or(ParseRangeErr(()))?.trim();
let end_str = start_end_iter.next().ok_or(())?.trim(); let end_str = start_end_iter.next().ok_or(ParseRangeErr(()))?.trim();
if start_str.is_empty() { if start_str.is_empty() {
// If no start is specified, end specifies the // If no start is specified, end specifies the
// range start relative to the end of the file. // range start relative to the end of the file.
let mut length: i64 = end_str.parse().map_err(|_| ())?; let mut length: i64 =
end_str.parse().map_err(|_| ParseRangeErr(()))?;
if length > size_sig { if length > size_sig {
length = size_sig; length = size_sig;
@ -51,10 +58,10 @@ impl HttpRange {
length: length as u64, length: length as u64,
})) }))
} else { } else {
let start: i64 = start_str.parse().map_err(|_| ())?; let start: i64 = start_str.parse().map_err(|_| ParseRangeErr(()))?;
if start < 0 { if start < 0 {
return Err(()); return Err(ParseRangeErr(()));
} }
if start >= size_sig { if start >= size_sig {
no_overlap = true; no_overlap = true;
@ -65,10 +72,11 @@ impl HttpRange {
// If no end is specified, range extends to end of the file. // If no end is specified, range extends to end of the file.
size_sig - start size_sig - start
} else { } else {
let mut end: i64 = end_str.parse().map_err(|_| ())?; let mut end: i64 =
end_str.parse().map_err(|_| ParseRangeErr(()))?;
if start > end { if start > end {
return Err(()); return Err(ParseRangeErr(()));
} }
if end >= size_sig { if end >= size_sig {
@ -89,7 +97,7 @@ impl HttpRange {
let ranges: Vec<HttpRange> = all_ranges.into_iter().filter_map(|x| x).collect(); let ranges: Vec<HttpRange> = all_ranges.into_iter().filter_map(|x| x).collect();
if no_overlap && ranges.is_empty() { if no_overlap && ranges.is_empty() {
return Err(()); return Err(ParseRangeErr(()));
} }
Ok(ranges) Ok(ranges)
@ -333,8 +341,7 @@ mod tests {
if expected.is_empty() { if expected.is_empty() {
continue; continue;
} else { } else {
assert!( panic!(
false,
"parse({}, {}) returned error {:?}", "parse({}, {}) returned error {:?}",
header, header,
size, size,
@ -346,28 +353,24 @@ mod tests {
let got = res.unwrap(); let got = res.unwrap();
if got.len() != expected.len() { if got.len() != expected.len() {
assert!( panic!(
false,
"len(parseRange({}, {})) = {}, want {}", "len(parseRange({}, {})) = {}, want {}",
header, header,
size, size,
got.len(), got.len(),
expected.len() expected.len()
); );
continue;
} }
for i in 0..expected.len() { for i in 0..expected.len() {
if got[i].start != expected[i].start { if got[i].start != expected[i].start {
assert!( panic!(
false,
"parseRange({}, {})[{}].start = {}, want {}", "parseRange({}, {})[{}].start = {}, want {}",
header, size, i, got[i].start, expected[i].start header, size, i, got[i].start, expected[i].start
) )
} }
if got[i].length != expected[i].length { if got[i].length != expected[i].length {
assert!( panic!(
false,
"parseRange({}, {})[{}].length = {}, want {}", "parseRange({}, {})[{}].length = {}, want {}",
header, size, i, got[i].length, expected[i].length header, size, i, got[i].length, expected[i].length
) )

View File

@ -179,6 +179,7 @@ mod tests {
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001"); assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
} }
#[allow(clippy::unnecessary_wraps)]
fn render_500_async<B: 'static>( fn render_500_async<B: 'static>(
mut res: ServiceResponse<B>, mut res: ServiceResponse<B>,
) -> Result<ErrorHandlerResponse<B>> { ) -> Result<ErrorHandlerResponse<B>> {

View File

@ -63,6 +63,7 @@ async fn test_start() {
let _ = sys.stop(); let _ = sys.stop();
} }
#[allow(clippy::unnecessary_wraps)]
#[cfg(feature = "openssl")] #[cfg(feature = "openssl")]
fn ssl_acceptor() -> std::io::Result<SslAcceptorBuilder> { fn ssl_acceptor() -> std::io::Result<SslAcceptorBuilder> {
use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod}; use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod};