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

Merge pull request #119 from DoumanAsh/default_static_files

Add default resource for StaticFiles
This commit is contained in:
Douman 2018-03-16 20:12:07 +03:00 committed by GitHub
commit 52b3b0c362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 51 deletions

View File

@ -12,6 +12,8 @@
* Move brotli encoding to a feature * Move brotli encoding to a feature
* Add option of default handler for `StaticFiles` middleware
## 0.4.8 (2018-03-12) ## 0.4.8 (2018-03-12)
@ -55,7 +57,7 @@
* Better support for `NamedFile` type * Better support for `NamedFile` type
* Add `ResponseError` impl for `SendRequestError`. This improves ergonomics of the client. * Add `ResponseError` impl for `SendRequestError`. This improves ergonomics of the client.
* Add native-tls support for client * Add native-tls support for client
* Allow client connection timeout to be set #108 * Allow client connection timeout to be set #108

View File

@ -21,11 +21,11 @@ use mime_guess::get_mime_type;
use header; use header;
use error::Error; use error::Error;
use param::FromParam; use param::FromParam;
use handler::{Handler, Responder}; use handler::{Handler, RouteHandler, WrapHandler, Responder, Reply};
use httpmessage::HttpMessage; use httpmessage::HttpMessage;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
use httpcodes::{HttpOk, HttpFound, HttpMethodNotAllowed}; use httpcodes::{HttpOk, HttpFound, HttpNotFound, HttpMethodNotAllowed};
/// A file with an associated name; responds with the Content-Type based on the /// A file with an associated name; responds with the Content-Type based on the
/// file extension. /// file extension.
@ -362,27 +362,6 @@ impl Responder for Directory {
} }
} }
/// This enum represents all filesystem elements.
pub enum FilesystemElement {
File(NamedFile),
Directory(Directory),
Redirect(HttpResponse),
}
impl Responder for FilesystemElement {
type Item = HttpResponse;
type Error = io::Error;
fn respond_to(self, req: HttpRequest) -> Result<HttpResponse, io::Error> {
match self {
FilesystemElement::File(file) => file.respond_to(req),
FilesystemElement::Directory(dir) => dir.respond_to(req),
FilesystemElement::Redirect(resp) => Ok(resp),
}
}
}
/// Static files handling /// Static files handling
/// ///
/// `StaticFile` handler must be registered with `Application::handler()` method, /// `StaticFile` handler must be registered with `Application::handler()` method,
@ -398,23 +377,24 @@ impl Responder for FilesystemElement {
/// .finish(); /// .finish();
/// } /// }
/// ``` /// ```
pub struct StaticFiles { pub struct StaticFiles<S> {
directory: PathBuf, directory: PathBuf,
accessible: bool, accessible: bool,
index: Option<String>, index: Option<String>,
show_index: bool, show_index: bool,
cpu_pool: CpuPool, cpu_pool: CpuPool,
default: Box<RouteHandler<S>>,
_chunk_size: usize, _chunk_size: usize,
_follow_symlinks: bool, _follow_symlinks: bool,
} }
impl StaticFiles { impl<S: 'static> StaticFiles<S> {
/// Create new `StaticFiles` instance /// Create new `StaticFiles` instance
/// ///
/// `dir` - base directory /// `dir` - base directory
/// ///
/// `index` - show index for directory /// `index` - show index for directory
pub fn new<T: Into<PathBuf>>(dir: T, index: bool) -> StaticFiles { pub fn new<T: Into<PathBuf>>(dir: T, index: bool) -> StaticFiles<S> {
let dir = dir.into(); let dir = dir.into();
let (dir, access) = match dir.canonicalize() { let (dir, access) = match dir.canonicalize() {
@ -438,6 +418,7 @@ impl StaticFiles {
index: None, index: None,
show_index: index, show_index: index,
cpu_pool: CpuPool::new(40), cpu_pool: CpuPool::new(40),
default: Box::new(WrapHandler::new(|_| HttpNotFound)),
_chunk_size: 0, _chunk_size: 0,
_follow_symlinks: false, _follow_symlinks: false,
} }
@ -447,28 +428,30 @@ impl StaticFiles {
/// ///
/// Redirects to specific index file for directory "/" instead of /// Redirects to specific index file for directory "/" instead of
/// showing files listing. /// showing files listing.
pub fn index_file<T: Into<String>>(mut self, index: T) -> StaticFiles { pub fn index_file<T: Into<String>>(mut self, index: T) -> StaticFiles<S> {
self.index = Some(index.into()); self.index = Some(index.into());
self self
} }
/// Sets default resource which is used when no matched file could be found.
pub fn default_handler<H: Handler<S>>(mut self, handler: H) -> StaticFiles<S> {
self.default = Box::new(WrapHandler::new(handler));
self
}
} }
impl<S> Handler<S> for StaticFiles { impl<S: 'static> Handler<S> for StaticFiles<S> {
type Result = Result<FilesystemElement, io::Error>; type Result = Result<Reply, Error>;
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result { fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
if !self.accessible { if !self.accessible {
Err(io::Error::new(io::ErrorKind::NotFound, "not found")) return Ok(self.default.handle(req))
} else { } else {
let path = if let Some(path) = req.match_info().get("tail") { let relpath = match req.match_info().get("tail").map(PathBuf::from_param) {
path Some(Ok(path)) => path,
} else { _ => return Ok(self.default.handle(req))
return Err(io::Error::new(io::ErrorKind::NotFound, "not found"))
}; };
let relpath = PathBuf::from_param(path)
.map_err(|_| io::Error::new(io::ErrorKind::NotFound, "not found"))?;
// full filepath // full filepath
let path = self.directory.join(&relpath).canonicalize()?; let path = self.directory.join(&relpath).canonicalize()?;
@ -482,21 +465,22 @@ impl<S> Handler<S> for StaticFiles {
new_path.push('/'); new_path.push('/');
} }
new_path.push_str(redir_index); new_path.push_str(redir_index);
Ok(FilesystemElement::Redirect( HttpFound.build()
HttpFound .header(header::http::LOCATION, new_path.as_str())
.build() .finish().unwrap()
.header(header::http::LOCATION, new_path.as_str()) .respond_to(req.without_state())
.finish().unwrap()))
} else if self.show_index { } else if self.show_index {
Ok(FilesystemElement::Directory( Directory::new(self.directory.clone(), path).respond_to(req.without_state())
Directory::new(self.directory.clone(), path))) .map_err(|error| Error::from(error))?
.respond_to(req.without_state())
} else { } else {
Err(io::Error::new(io::ErrorKind::NotFound, "not found")) Ok(self.default.handle(req))
} }
} else { } else {
Ok(FilesystemElement::File( NamedFile::open(path)?.set_cpu_pool(self.cpu_pool.clone())
NamedFile::open(path)? .respond_to(req.without_state())
.set_cpu_pool(self.cpu_pool.clone()).only_get())) .map_err(|error| Error::from(error))?
.respond_to(req.without_state())
} }
} }
} }
@ -542,17 +526,22 @@ mod tests {
fn test_static_files() { fn test_static_files() {
let mut st = StaticFiles::new(".", true); let mut st = StaticFiles::new(".", true);
st.accessible = false; st.accessible = false;
assert!(st.handle(HttpRequest::default()).is_err()); let resp = st.handle(HttpRequest::default()).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
st.accessible = true; st.accessible = true;
st.show_index = false; st.show_index = false;
assert!(st.handle(HttpRequest::default()).is_err()); let resp = st.handle(HttpRequest::default()).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let mut req = HttpRequest::default(); let mut req = HttpRequest::default();
req.match_info_mut().add("tail", ""); req.match_info_mut().add("tail", "");
st.show_index = true; st.show_index = true;
let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap(); let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/html; charset=utf-8"); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/html; charset=utf-8");
assert!(resp.body().is_binary()); assert!(resp.body().is_binary());
assert!(format!("{:?}", resp.body()).contains("README.md")); assert!(format!("{:?}", resp.body()).contains("README.md"));
@ -565,6 +554,7 @@ mod tests {
req.match_info_mut().add("tail", "guide"); req.match_info_mut().add("tail", "guide");
let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap(); let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(resp.status(), StatusCode::FOUND); assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/guide/index.html"); assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/guide/index.html");
@ -572,6 +562,7 @@ mod tests {
req.match_info_mut().add("tail", "guide/"); req.match_info_mut().add("tail", "guide/");
let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap(); let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(resp.status(), StatusCode::FOUND); assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/guide/index.html"); assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/guide/index.html");
} }
@ -583,6 +574,7 @@ mod tests {
req.match_info_mut().add("tail", "examples/basics"); req.match_info_mut().add("tail", "examples/basics");
let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap(); let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap();
let resp = resp.as_response().expect("HTTP Response");
assert_eq!(resp.status(), StatusCode::FOUND); assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/examples/basics/Cargo.toml"); assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/examples/basics/Cargo.toml");
} }