mirror of https://github.com/fafhrd91/actix-web
impl FromStr and AsRef<Path> for PathBufWrap
This commit is contained in:
parent
e6d8025db2
commit
3e2ab0ab9a
|
@ -42,7 +42,7 @@ pub use crate::range::HttpRange;
|
||||||
|
|
||||||
use self::directory::{directory_listing, DirectoryRenderer};
|
use self::directory::{directory_listing, DirectoryRenderer};
|
||||||
use self::error::FilesError;
|
use self::error::FilesError;
|
||||||
use self::path_buf::PathBufWrp;
|
use self::path_buf::PathBufWrap;
|
||||||
|
|
||||||
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
||||||
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
||||||
|
@ -354,13 +354,13 @@ impl Service for FilesService {
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let real_path = match PathBufWrp::get_pathbuf(req.match_info().path()) {
|
let real_path: PathBufWrap = match req.match_info().path().parse() {
|
||||||
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(e))),
|
||||||
};
|
};
|
||||||
|
|
||||||
// full file path
|
// full file path
|
||||||
let path = match self.directory.join(real_path.as_pathbuf()).canonicalize() {
|
let path = match self.directory.join(&real_path).canonicalize() {
|
||||||
Ok(path) => path,
|
Ok(path) => path,
|
||||||
Err(e) => return self.handle_err(e, req),
|
Err(e) => return self.handle_err(e, req),
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
use std::path::PathBuf;
|
use std::{
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
str::FromStr,
|
||||||
|
};
|
||||||
|
|
||||||
use actix_web::{dev::Payload, FromRequest, HttpRequest};
|
use actix_web::{dev::Payload, FromRequest, HttpRequest};
|
||||||
use futures_util::future::{ready, Ready};
|
use futures_util::future::{ready, Ready};
|
||||||
|
@ -6,14 +9,12 @@ use futures_util::future::{ready, Ready};
|
||||||
use crate::error::UriSegmentError;
|
use crate::error::UriSegmentError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct PathBufWrp(PathBuf);
|
pub(crate) struct PathBufWrap(PathBuf);
|
||||||
|
|
||||||
impl PathBufWrp {
|
impl FromStr for PathBufWrap {
|
||||||
pub(crate) fn as_pathbuf(&self) -> &PathBuf {
|
type Err = UriSegmentError;
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_pathbuf(path: &str) -> Result<Self, UriSegmentError> {
|
fn from_str(path: &str) -> Result<Self, Self::Err> {
|
||||||
let mut buf = PathBuf::new();
|
let mut buf = PathBuf::new();
|
||||||
for segment in path.split('/') {
|
for segment in path.split('/') {
|
||||||
if segment == ".." {
|
if segment == ".." {
|
||||||
|
@ -37,17 +38,23 @@ impl PathBufWrp {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(PathBufWrp(buf))
|
Ok(PathBufWrap(buf))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRequest for PathBufWrp {
|
impl AsRef<Path> for PathBufWrap {
|
||||||
|
fn as_ref(&self) -> &Path {
|
||||||
|
self.0.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRequest for PathBufWrap {
|
||||||
type Error = UriSegmentError;
|
type Error = UriSegmentError;
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
ready(PathBufWrp::get_pathbuf(req.match_info().path()))
|
ready(req.match_info().path().parse())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,31 +67,31 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_path_buf() {
|
fn test_path_buf() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
PathBufWrp::get_pathbuf("/test/.tt").map(|t| t.0),
|
PathBufWrap::from_str("/test/.tt").map(|t| t.0),
|
||||||
Err(UriSegmentError::BadStart('.'))
|
Err(UriSegmentError::BadStart('.'))
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
PathBufWrp::get_pathbuf("/test/*tt").map(|t| t.0),
|
PathBufWrap::from_str("/test/*tt").map(|t| t.0),
|
||||||
Err(UriSegmentError::BadStart('*'))
|
Err(UriSegmentError::BadStart('*'))
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
PathBufWrp::get_pathbuf("/test/tt:").map(|t| t.0),
|
PathBufWrap::from_str("/test/tt:").map(|t| t.0),
|
||||||
Err(UriSegmentError::BadEnd(':'))
|
Err(UriSegmentError::BadEnd(':'))
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
PathBufWrp::get_pathbuf("/test/tt<").map(|t| t.0),
|
PathBufWrap::from_str("/test/tt<").map(|t| t.0),
|
||||||
Err(UriSegmentError::BadEnd('<'))
|
Err(UriSegmentError::BadEnd('<'))
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
PathBufWrp::get_pathbuf("/test/tt>").map(|t| t.0),
|
PathBufWrap::from_str("/test/tt>").map(|t| t.0),
|
||||||
Err(UriSegmentError::BadEnd('>'))
|
Err(UriSegmentError::BadEnd('>'))
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
PathBufWrp::get_pathbuf("/seg1/seg2/").unwrap().0,
|
PathBufWrap::from_str("/seg1/seg2/").unwrap().0,
|
||||||
PathBuf::from_iter(vec!["seg1", "seg2"])
|
PathBuf::from_iter(vec!["seg1", "seg2"])
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
PathBufWrp::get_pathbuf("/seg1/../seg2/").unwrap().0,
|
PathBufWrap::from_str("/seg1/../seg2/").unwrap().0,
|
||||||
PathBuf::from_iter(vec!["seg2"])
|
PathBuf::from_iter(vec!["seg2"])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue