From 37c1e78c7a16b52ef0d0c626397ee4e22a408fd7 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 25 Nov 2017 10:52:43 -0800 Subject: [PATCH] added helper Task::error method --- src/error.rs | 17 ++++++++++------- src/pipeline.rs | 2 +- src/staticfiles.rs | 37 +++++++++++++++++-------------------- src/task.rs | 4 ++++ 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/error.rs b/src/error.rs index ba751127..ea977505 100644 --- a/src/error.rs +++ b/src/error.rs @@ -86,6 +86,16 @@ default impl ErrorResponse for T { /// `InternalServerError` for `JsonError` impl ErrorResponse for JsonError {} +/// Return `InternalServerError` for `HttpError`, +/// Response generation can return `HttpError`, so it is internal error +impl ErrorResponse for HttpError {} + +/// Return `InternalServerError` for `io::Error` +impl ErrorResponse for IoError {} + +/// `InternalServerError` for `InvalidHeaderValue` +impl ErrorResponse for header::InvalidHeaderValue {} + /// Internal error #[derive(Fail, Debug)] #[fail(display="Unexpected task frame")] @@ -191,13 +201,6 @@ impl From for PayloadError { } } -/// Return `InternalServerError` for `HttpError`, -/// Response generation can return `HttpError`, so it is internal error -impl ErrorResponse for HttpError {} - -/// Return `InternalServerError` for `io::Error` -impl ErrorResponse for IoError {} - /// Return `BadRequest` for `cookie::ParseError` impl ErrorResponse for cookie::ParseError { fn error_response(&self) -> HttpResponse { diff --git a/src/pipeline.rs b/src/pipeline.rs index 367f90eb..87aa3f8f 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -345,7 +345,7 @@ impl Start { self.prepare(Task::reply(resp)), Rc::clone(&self.middlewares))))) }, - Started::Future(mut fut) => { + Started::Future(fut) => { self.fut = Some(fut); continue 'outer }, diff --git a/src/staticfiles.rs b/src/staticfiles.rs index f963d969..b7e1a80a 100644 --- a/src/staticfiles.rs +++ b/src/staticfiles.rs @@ -1,7 +1,6 @@ //! Static files support. //! //! TODO: needs to re-implement actual files handling, current impl blocks -#![allow(dead_code, unused_variables)] use std::io; use std::io::Read; use std::rc::Rc; @@ -15,7 +14,7 @@ use payload::Payload; use mime_guess::get_mime_type; use httprequest::HttpRequest; use httpresponse::HttpResponse; -use httpcodes::{HTTPOk, HTTPNotFound, HTTPForbidden, HTTPInternalServerError}; +use httpcodes::{HTTPOk, HTTPNotFound, HTTPForbidden}; /// Static files handling /// @@ -34,9 +33,9 @@ use httpcodes::{HTTPOk, HTTPNotFound, HTTPForbidden, HTTPInternalServerError}; pub struct StaticFiles { directory: PathBuf, accessible: bool, - show_index: bool, - chunk_size: usize, - follow_symlinks: bool, + _show_index: bool, + _chunk_size: usize, + _follow_symlinks: bool, prefix: String, } @@ -66,9 +65,9 @@ impl StaticFiles { StaticFiles { directory: dir, accessible: access, - show_index: index, - chunk_size: 0, - follow_symlinks: false, + _show_index: index, + _chunk_size: 0, + _follow_symlinks: false, prefix: String::new(), } } @@ -86,19 +85,19 @@ impl StaticFiles { entry.path().strip_prefix(&self.directory).unwrap().to_string_lossy()); // if file is a directory, add '/' to the end of the name - let file_name = if let Ok(metadata) = entry.metadata() { + if let Ok(metadata) = entry.metadata() { if metadata.is_dir() { //format!("
  • {}
  • ", file_url, file_name)); - write!(body, "
  • {}/
  • ", - file_url, entry.file_name().to_string_lossy()) + let _ = write!(body, "
  • {}/
  • ", + file_url, entry.file_name().to_string_lossy()); } else { // write!(body, "{}/", entry.file_name()) - write!(body, "
  • {}
  • ", - file_url, entry.file_name().to_string_lossy()) + let _ = write!(body, "
  • {}
  • ", + file_url, entry.file_name().to_string_lossy()); } } else { continue - }; + } } } @@ -139,7 +138,7 @@ impl RouteHandler for StaticFiles { } } - fn handle(&self, req: &mut HttpRequest, payload: Payload, state: Rc) -> Task { + fn handle(&self, req: &mut HttpRequest, _: Payload, _: Rc) -> Task { if !self.accessible { Task::reply(HTTPNotFound) } else { @@ -165,7 +164,7 @@ impl RouteHandler for StaticFiles { Err(err) => return match err.kind() { io::ErrorKind::NotFound => Task::reply(HTTPNotFound), io::ErrorKind::PermissionDenied => Task::reply(HTTPForbidden), - _ => Task::reply(HTTPInternalServerError), + _ => Task::error(err), } }; @@ -175,7 +174,7 @@ impl RouteHandler for StaticFiles { Err(err) => match err.kind() { io::ErrorKind::NotFound => Task::reply(HTTPNotFound), io::ErrorKind::PermissionDenied => Task::reply(HTTPForbidden), - _ => Task::reply(HTTPInternalServerError), + _ => Task::error(err), } } } else { @@ -190,9 +189,7 @@ impl RouteHandler for StaticFiles { let _ = file.read_to_end(&mut data); Task::reply(resp.body(data).unwrap()) }, - Err(err) => { - Task::reply(HTTPInternalServerError) - } + Err(err) => Task::error(err), } } } diff --git a/src/task.rs b/src/task.rs index a74389c0..067601d0 100644 --- a/src/task.rs +++ b/src/task.rs @@ -131,6 +131,10 @@ impl Task { middlewares: None } } + pub fn error>(err: E) -> Self { + Task::reply(err.into()) + } + pub(crate) fn with_context(ctx: C) -> Self { Task { state: TaskRunningState::Running, iostate: TaskIOState::ReadingMessage,