From 91b66de6becddc0704a47f414b4c93e1bb729f14 Mon Sep 17 00:00:00 2001 From: Douman Date: Tue, 27 Nov 2018 23:04:23 +0300 Subject: [PATCH] Move default handler to static files config --- src/fs.rs | 55 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index f175b3e9c..106cfb654 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -20,7 +20,7 @@ use mime_guess::{get_mime_type, guess_mime_type}; use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET}; use error::{Error, StaticFileError}; -use handler::{AsyncResult, Handler, Responder, RouteHandler, WrapHandler}; +use handler::{AsyncResult, Handler, Responder}; use header; use header::{ContentDisposition, DispositionParam, DispositionType}; use http::{ContentEncoding, Method, StatusCode}; @@ -112,6 +112,13 @@ pub trait StaticFileConfig: Default { fn directory_listing(dir: &Directory, req: &HttpRequest) -> Result { directory_listing(dir, req) } + + /// Default handler for StaticFiles. + /// + /// Responses with NotFound by default + fn default_handler(_req: &HttpRequest) -> AsyncResult { + HttpResponse::new(StatusCode::NOT_FOUND).into() + } } ///Default content disposition as described in @@ -665,22 +672,21 @@ fn directory_listing( /// .finish(); /// } /// ``` -pub struct StaticFiles { +pub struct StaticFiles { directory: PathBuf, cpu_pool: CpuPool, - default: Box>, _chunk_size: usize, _follow_symlinks: bool, _cd_map: PhantomData, } -impl StaticFiles { +impl StaticFiles { /// Create new `StaticFiles` instance for specified base directory. /// /// `StaticFile` uses `CpuPool` for blocking filesystem operations. /// By default pool with 20 threads is used. /// Pool size can be changed by setting ACTIX_CPU_POOL environment variable. - pub fn new>(dir: T) -> Result, Error> { + pub fn new>(dir: T) -> Result { Self::with_config(dir, DefaultConfig) } @@ -689,19 +695,19 @@ impl StaticFiles { pub fn with_pool>( dir: T, pool: CpuPool, - ) -> Result, Error> { + ) -> Result { Self::with_config_pool(dir, pool, DefaultConfig) } } -impl StaticFiles { +impl StaticFiles { /// Create new `StaticFiles` instance for specified base directory. /// /// Identical with `new` but allows to specify configiration to use. pub fn with_config>( dir: T, config: C, - ) -> Result, Error> { + ) -> Result, Error> { // use default CpuPool let pool = { DEFAULT_CPUPOOL.lock().clone() }; @@ -714,7 +720,7 @@ impl StaticFiles { dir: T, pool: CpuPool, _: C, - ) -> Result, Error> { + ) -> Result, Error> { let dir = dir.into().canonicalize()?; if !dir.is_dir() { @@ -724,22 +730,13 @@ impl StaticFiles { Ok(StaticFiles { directory: dir, cpu_pool: pool, - default: Box::new(WrapHandler::new(|_: &_| { - HttpResponse::new(StatusCode::NOT_FOUND) - })), _chunk_size: 0, _follow_symlinks: false, _cd_map: PhantomData, }) } - /// Sets default handler which is used when no matched file could be found. - pub fn default_handler>(mut self, handler: H) -> StaticFiles { - self.default = Box::new(WrapHandler::new(handler)); - self - } - - fn try_handle( + fn try_handle( &self, req: &HttpRequest, ) -> Result, Error> { @@ -767,13 +764,13 @@ impl StaticFiles { } } -impl Handler for StaticFiles { +impl Handler for StaticFiles { type Result = Result, Error>; fn handle(&self, req: &HttpRequest) -> Self::Result { self.try_handle(req).or_else(|e| { debug!("StaticFiles: Failed to handle {}: {}", req.path(), e); - Ok(self.default.handle(req)) + Ok(C::default_handler(req)) }) } } @@ -1392,18 +1389,24 @@ mod tests { #[test] fn test_static_files_bad_directory() { - let st: Result, Error> = StaticFiles::new("missing"); + let st: Result = StaticFiles::new("missing"); assert!(st.is_err()); - let st: Result, Error> = StaticFiles::new("Cargo.toml"); + let st: Result = StaticFiles::new("Cargo.toml"); assert!(st.is_err()); } #[test] fn test_default_handler_file_missing() { - let st = StaticFiles::new(".") - .unwrap() - .default_handler(|_: &_| "default content"); + #[derive(Default)] + struct DefaultHandler; + impl StaticFileConfig for DefaultHandler { + fn default_handler(_: &HttpRequest) -> AsyncResult { + AsyncResult::ok("default content") + } + } + let st = StaticFiles::with_config(".", DefaultHandler) + .unwrap(); let req = TestRequest::with_uri("/missing") .param("tail", "missing") .finish();