tune files errors

This commit is contained in:
Nikolay Kim 2020-04-11 12:10:07 +06:00
parent 3b1618604e
commit 6d2a26edf6
5 changed files with 19 additions and 34 deletions

View file

@ -8,4 +8,4 @@ members = [
] ]
[patch.crates-io] [patch.crates-io]
ntex = { path = "../ntex/ntex/" } ntex = { git = "https://github.com/ntex-rs/ntex.git" }

View file

@ -18,6 +18,10 @@ pub enum FilesError {
#[display(fmt = "Request did not meet this resource's requirements.")] #[display(fmt = "Request did not meet this resource's requirements.")]
MethodNotAllowed, MethodNotAllowed,
/// Uri segments parsing error
#[display(fmt = "{}", _0)]
Uri(UriSegmentError),
/// IO Error /// IO Error
#[display(fmt = "Error reading: {}", _0)] #[display(fmt = "Error reading: {}", _0)]
Io(std::io::Error), Io(std::io::Error),
@ -28,6 +32,7 @@ impl WebResponseError<DefaultError> for FilesError {
fn status_code(&self) -> StatusCode { fn status_code(&self) -> StatusCode {
match self { match self {
FilesError::MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED, FilesError::MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED,
FilesError::Uri(_) => StatusCode::BAD_REQUEST,
_ => StatusCode::NOT_FOUND, _ => StatusCode::NOT_FOUND,
} }
} }

View file

@ -410,7 +410,6 @@ impl<Err> WebServiceFactory<Err> for Files<Err>
where where
Err: ErrorRenderer, Err: ErrorRenderer,
Err::Container: From<FilesError>, Err::Container: From<FilesError>,
Err::Container: From<UriSegmentError>,
{ {
fn register(mut self, config: &mut WebServiceConfig<Err>) { fn register(mut self, config: &mut WebServiceConfig<Err>) {
if self.default.is_none() { if self.default.is_none() {
@ -429,7 +428,6 @@ impl<Err> ServiceFactory for Files<Err>
where where
Err: ErrorRenderer, Err: ErrorRenderer,
Err::Container: From<FilesError>, Err::Container: From<FilesError>,
Err::Container: From<UriSegmentError>,
{ {
type Request = WebRequest<Err>; type Request = WebRequest<Err>;
type Response = WebResponse; type Response = WebResponse;
@ -506,7 +504,6 @@ impl<Err> Service for FilesService<Err>
where where
Err: ErrorRenderer, Err: ErrorRenderer,
Err::Container: From<FilesError>, Err::Container: From<FilesError>,
Err::Container: From<UriSegmentError>,
{ {
type Request = WebRequest<Err>; type Request = WebRequest<Err>;
type Response = WebResponse; type Response = WebResponse;
@ -536,31 +533,17 @@ where
return Either::Left(ok(req.error_response(FilesError::MethodNotAllowed))); return Either::Left(ok(req.error_response(FilesError::MethodNotAllowed)));
} }
println!("R: {:?}", req.match_info().path());
let real_path = match PathBufWrp::get_pathbuf(req.match_info().path()) { let real_path = match PathBufWrp::get_pathbuf(req.match_info().path()) {
Ok(item) => item, Ok(item) => item,
Err(e) => return Either::Left(ok(req.error_response(e))), Err(e) => return Either::Left(ok(req.error_response(FilesError::from(e)))),
}; };
println!(
"R: {:?} {:?} {:?}",
real_path,
std::env::current_dir(),
self.directory.join(&real_path.0)
);
// full filepath // full filepath
let path = match self.directory.join(&real_path.0).canonicalize() { let path = match self.directory.join(&real_path.0).canonicalize() {
Ok(path) => path, Ok(path) => path,
Err(e) => { Err(e) => return self.handle_io_error(e, req),
println!("TEST: {:?}", e);
return self.handle_io_error(e, req);
}
}; };
println!("R2: {:?}", path);
if path.is_dir() { if path.is_dir() {
if let Some(ref redir_index) = self.index { if let Some(ref redir_index) = self.index {
if self.redirect_to_slash && !req.path().ends_with('/') { if self.redirect_to_slash && !req.path().ends_with('/') {
@ -1053,8 +1036,6 @@ mod tests {
#[ntex::test] #[ntex::test]
async fn test_named_file_content_length_headers() { async fn test_named_file_content_length_headers() {
// use actix_web::body::{MessageBody, ResponseBody};
let mut srv = test::init_service( let mut srv = test::init_service(
App::new().service(Files::new("test", ".").index_file("tests/test.binary")), App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
) )
@ -1067,7 +1048,7 @@ mod tests {
.to_request(); .to_request();
let _response = test::call_service(&mut srv, request).await; let _response = test::call_service(&mut srv, request).await;
// let contentlength = response // let contentlength = _response
// .headers() // .headers()
// .get(header::CONTENT_LENGTH) // .get(header::CONTENT_LENGTH)
// .unwrap() // .unwrap()
@ -1154,7 +1135,6 @@ mod tests {
.uri("/tests/test%20space.binary") .uri("/tests/test%20space.binary")
.to_request(); .to_request();
let response = test::call_service(&srv, request).await; let response = test::call_service(&srv, request).await;
println!("TEST- {:?}", response);
assert_eq!(response.status(), StatusCode::OK); assert_eq!(response.status(), StatusCode::OK);
let bytes = test::read_body(response).await; let bytes = test::read_body(response).await;

View file

@ -362,7 +362,7 @@ impl<Err: ErrorRenderer> CookieIdentityInner<Err> {
CookieIdentityInner { CookieIdentityInner {
key: Key::from_master(key), key: Key::from_master(key),
key_v2: Key::from_master(&key_v2), key_v2: Key::from_master(&key_v2),
name: "actix-identity".to_owned(), name: "ntex-identity".to_owned(),
path: "/".to_owned(), path: "/".to_owned(),
domain: None, domain: None,
secure: true, secure: true,
@ -492,7 +492,7 @@ impl<Err: ErrorRenderer> CookieIdentityInner<Err> {
/// // <- create identity middleware /// // <- create identity middleware
/// CookieIdentityPolicy::new(&[0; 32]) // <- construct cookie policy /// CookieIdentityPolicy::new(&[0; 32]) // <- construct cookie policy
/// .domain("www.rust-lang.org") /// .domain("www.rust-lang.org")
/// .name("actix_auth") /// .name("ntex-auth")
/// .path("/") /// .path("/")
/// .secure(true), /// .secure(true),
/// )); /// ));

View file

@ -71,7 +71,7 @@ impl<Err> CookieSessionInner<Err> {
CookieSessionInner { CookieSessionInner {
security, security,
key: Key::from_master(key), key: Key::from_master(key),
name: "actix-session".to_owned(), name: "ntex-session".to_owned(),
path: "/".to_owned(), path: "/".to_owned(),
domain: None, domain: None,
secure: true, secure: true,
@ -418,7 +418,7 @@ mod tests {
assert!(response assert!(response
.response() .response()
.cookies() .cookies()
.find(|c| c.name() == "actix-session") .find(|c| c.name() == "ntex-session")
.is_some()); .is_some());
} }
@ -439,7 +439,7 @@ mod tests {
assert!(response assert!(response
.response() .response()
.cookies() .cookies()
.find(|c| c.name() == "actix-session") .find(|c| c.name() == "ntex-session")
.is_some()); .is_some());
} }
@ -460,7 +460,7 @@ mod tests {
assert!(response assert!(response
.response() .response()
.cookies() .cookies()
.find(|c| c.name() == "actix-session") .find(|c| c.name() == "ntex-session")
.is_some()); .is_some());
} }
@ -471,7 +471,7 @@ mod tests {
.wrap( .wrap(
CookieSession::signed(&[0; 32]) CookieSession::signed(&[0; 32])
.path("/test/") .path("/test/")
.name("actix-test") .name("ntex-test")
.domain("localhost") .domain("localhost")
.http_only(true) .http_only(true)
.same_site(SameSite::Lax) .same_site(SameSite::Lax)
@ -493,7 +493,7 @@ mod tests {
let cookie = response let cookie = response
.response() .response()
.cookies() .cookies()
.find(|c| c.name() == "actix-test") .find(|c| c.name() == "ntex-test")
.unwrap() .unwrap()
.clone(); .clone();
assert_eq!(cookie.path().unwrap(), "/test/"); assert_eq!(cookie.path().unwrap(), "/test/");
@ -526,7 +526,7 @@ mod tests {
let expires_1 = response let expires_1 = response
.response() .response()
.cookies() .cookies()
.find(|c| c.name() == "actix-session") .find(|c| c.name() == "ntex-session")
.expect("Cookie is set") .expect("Cookie is set")
.expires() .expires()
.expect("Expiration is set"); .expect("Expiration is set");
@ -538,7 +538,7 @@ mod tests {
let expires_2 = response let expires_2 = response
.response() .response()
.cookies() .cookies()
.find(|c| c.name() == "actix-session") .find(|c| c.name() == "ntex-session")
.expect("Cookie is set") .expect("Cookie is set")
.expires() .expires()
.expect("Expiration is set"); .expect("Expiration is set");