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

fix not implemented panic #410

This commit is contained in:
Nikolay Kim 2018-07-21 05:58:08 -07:00
parent f6499d9ba5
commit 4862227df9
5 changed files with 37 additions and 25 deletions

View File

@ -1,5 +1,12 @@
# Changes # Changes
## [0.7.1] - 2018-07-21
### Fixed
* Fixed default_resource 'not yet implemented' panic #410
## [0.7.0] - 2018-07-21 ## [0.7.0] - 2018-07-21
### Added ### Added

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-web" name = "actix-web"
version = "0.7.0" version = "0.7.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust." description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
readme = "README.md" readme = "README.md"

View File

@ -610,6 +610,7 @@ impl<S: 'static> Iterator for App<S> {
mod tests { mod tests {
use super::*; use super::*;
use body::{Binary, Body}; use body::{Binary, Body};
use fs;
use http::StatusCode; use http::StatusCode;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
@ -631,6 +632,7 @@ mod tests {
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
let app = App::new() let app = App::new()
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
.default_resource(|r| r.f(|_| HttpResponse::MethodNotAllowed())) .default_resource(|r| r.f(|_| HttpResponse::MethodNotAllowed()))
.finish(); .finish();
let req = TestRequest::with_uri("/blah").request(); let req = TestRequest::with_uri("/blah").request();

View File

@ -2,11 +2,11 @@
use std::fmt::Write; use std::fmt::Write;
use std::fs::{DirEntry, File, Metadata}; use std::fs::{DirEntry, File, Metadata};
use std::io::{Read, Seek}; use std::io::{Read, Seek};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use std::{cmp, io}; use std::{cmp, io};
use std::marker::PhantomData;
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
@ -22,13 +22,13 @@ use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
use error::{Error, StaticFileError}; use error::{Error, StaticFileError};
use handler::{AsyncResult, Handler, Responder, RouteHandler, WrapHandler}; use handler::{AsyncResult, Handler, Responder, RouteHandler, WrapHandler};
use header; use header;
use header::{ContentDisposition, DispositionParam, DispositionType};
use http::{ContentEncoding, Method, StatusCode}; use http::{ContentEncoding, Method, StatusCode};
use httpmessage::HttpMessage; use httpmessage::HttpMessage;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
use param::FromParam; use param::FromParam;
use server::settings::DEFAULT_CPUPOOL; use server::settings::DEFAULT_CPUPOOL;
use header::{ContentDisposition, DispositionParam, DispositionType};
///Describes `StaticFiles` configiration ///Describes `StaticFiles` configiration
/// ///
@ -106,7 +106,7 @@ pub fn file_extension_to_mime(ext: &str) -> mime::Mime {
/// A file with an associated name. /// A file with an associated name.
#[derive(Debug)] #[derive(Debug)]
pub struct NamedFile<C=DefaultConfig> { pub struct NamedFile<C = DefaultConfig> {
path: PathBuf, path: PathBuf,
file: File, file: File,
content_type: mime::Mime, content_type: mime::Mime,
@ -188,7 +188,7 @@ impl<C: StaticFileConfig> NamedFile<C> {
cpu_pool, cpu_pool,
encoding, encoding,
status_code: StatusCode::OK, status_code: StatusCode::OK,
_cd_map: PhantomData _cd_map: PhantomData,
}) })
} }
@ -366,21 +366,22 @@ impl<C: StaticFileConfig> Responder for NamedFile<C> {
return Ok(resp.streaming(reader)); return Ok(resp.streaming(reader));
} }
if !C::is_method_allowed(req.method()) if !C::is_method_allowed(req.method()) {
{
return Ok(HttpResponse::MethodNotAllowed() return Ok(HttpResponse::MethodNotAllowed()
.header(header::CONTENT_TYPE, "text/plain") .header(header::CONTENT_TYPE, "text/plain")
.header(header::ALLOW, "GET, HEAD") .header(header::ALLOW, "GET, HEAD")
.body("This resource only supports GET and HEAD.")); .body("This resource only supports GET and HEAD."));
} }
let etag = match C::is_use_etag() { let etag = if C::is_use_etag() {
true => self.etag(), self.etag()
false => None, } else {
None
}; };
let last_modified = match C::is_use_last_modifier() { let last_modified = if C::is_use_last_modifier() {
true => self.last_modified(), self.last_modified()
false => None, } else {
None
}; };
// check preconditions // check preconditions
@ -637,7 +638,7 @@ fn directory_listing<S>(
/// .finish(); /// .finish();
/// } /// }
/// ``` /// ```
pub struct StaticFiles<S, C=DefaultConfig> { pub struct StaticFiles<S, C = DefaultConfig> {
directory: PathBuf, directory: PathBuf,
index: Option<String>, index: Option<String>,
show_index: bool, show_index: bool,
@ -672,7 +673,9 @@ impl<S: 'static, C: StaticFileConfig> StaticFiles<S, C> {
/// Create new `StaticFiles` instance for specified base directory. /// Create new `StaticFiles` instance for specified base directory.
/// ///
/// Identical with `new` but allows to specify configiration to use. /// Identical with `new` but allows to specify configiration to use.
pub fn with_config<T: Into<PathBuf>>(dir: T, config: C) -> Result<StaticFiles<S, C>, Error> { pub fn with_config<T: Into<PathBuf>>(
dir: T, config: C,
) -> Result<StaticFiles<S, C>, Error> {
// use default CpuPool // use default CpuPool
let pool = { DEFAULT_CPUPOOL.lock().clone() }; let pool = { DEFAULT_CPUPOOL.lock().clone() };
@ -682,7 +685,7 @@ impl<S: 'static, C: StaticFileConfig> StaticFiles<S, C> {
/// Create new `StaticFiles` instance for specified base directory with config and /// Create new `StaticFiles` instance for specified base directory with config and
/// `CpuPool`. /// `CpuPool`.
pub fn with_config_pool<T: Into<PathBuf>>( pub fn with_config_pool<T: Into<PathBuf>>(
dir: T, pool: CpuPool, _: C dir: T, pool: CpuPool, _: C,
) -> Result<StaticFiles<S, C>, Error> { ) -> Result<StaticFiles<S, C>, Error> {
let dir = dir.into().canonicalize()?; let dir = dir.into().canonicalize()?;
@ -701,7 +704,7 @@ impl<S: 'static, C: StaticFileConfig> StaticFiles<S, C> {
renderer: Box::new(directory_listing), renderer: Box::new(directory_listing),
_chunk_size: 0, _chunk_size: 0,
_follow_symlinks: false, _follow_symlinks: false,
_cd_map: PhantomData _cd_map: PhantomData,
}) })
} }
@ -1064,7 +1067,6 @@ mod tests {
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(), resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
"inline; filename=\"test.png\"" "inline; filename=\"test.png\""
); );
} }
#[test] #[test]
@ -1296,24 +1298,27 @@ mod tests {
fn is_method_allowed(method: &Method) -> bool { fn is_method_allowed(method: &Method) -> bool {
match *method { match *method {
Method::HEAD => true, Method::HEAD => true,
_ => false _ => false,
} }
} }
} }
#[test] #[test]
fn test_named_file_not_allowed() { fn test_named_file_not_allowed() {
let file = NamedFile::open_with_config("Cargo.toml", OnlyMethodHeadConfig).unwrap(); let file =
NamedFile::open_with_config("Cargo.toml", OnlyMethodHeadConfig).unwrap();
let req = TestRequest::default().method(Method::POST).finish(); let req = TestRequest::default().method(Method::POST).finish();
let resp = file.respond_to(&req).unwrap(); let resp = file.respond_to(&req).unwrap();
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED); assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
let file = NamedFile::open_with_config("Cargo.toml", OnlyMethodHeadConfig).unwrap(); let file =
NamedFile::open_with_config("Cargo.toml", OnlyMethodHeadConfig).unwrap();
let req = TestRequest::default().method(Method::PUT).finish(); let req = TestRequest::default().method(Method::PUT).finish();
let resp = file.respond_to(&req).unwrap(); let resp = file.respond_to(&req).unwrap();
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED); assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
let file = NamedFile::open_with_config("Cargo.toml", OnlyMethodHeadConfig).unwrap(); let file =
NamedFile::open_with_config("Cargo.toml", OnlyMethodHeadConfig).unwrap();
let req = TestRequest::default().method(Method::GET).finish(); let req = TestRequest::default().method(Method::GET).finish();
let resp = file.respond_to(&req).unwrap(); let resp = file.respond_to(&req).unwrap();
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED); assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);

View File

@ -408,9 +408,7 @@ pub(crate) trait RouteHandler<S>: 'static {
false false
} }
fn default_resource(&mut self, _: DefaultResource<S>) { fn default_resource(&mut self, _: DefaultResource<S>) {}
unimplemented!()
}
fn finish(&mut self) {} fn finish(&mut self) {}
} }