mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 18:44:35 +01:00
deprecate Path::path
(#2590)
This commit is contained in:
parent
3dd98c308c
commit
1cc3e7b24c
@ -85,7 +85,7 @@ impl FromRequest for PathBufWrap {
|
|||||||
type Future = Ready<Result<Self, Self::Error>>;
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
ready(req.match_info().path().parse())
|
ready(req.match_info().unprocessed().parse())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,14 +120,16 @@ impl Service<ServiceRequest> for FilesService {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let real_path =
|
let path_on_disk = match PathBufWrap::parse_path(
|
||||||
match PathBufWrap::parse_path(req.match_info().path(), this.hidden_files) {
|
req.match_info().unprocessed(),
|
||||||
Ok(item) => item,
|
this.hidden_files,
|
||||||
Err(err) => return Ok(req.error_response(err)),
|
) {
|
||||||
};
|
Ok(item) => item,
|
||||||
|
Err(err) => return Ok(req.error_response(err)),
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(filter) = &this.path_filter {
|
if let Some(filter) = &this.path_filter {
|
||||||
if !filter(real_path.as_ref(), req.head()) {
|
if !filter(path_on_disk.as_ref(), req.head()) {
|
||||||
if let Some(ref default) = this.default {
|
if let Some(ref default) = this.default {
|
||||||
return default.call(req).await;
|
return default.call(req).await;
|
||||||
} else {
|
} else {
|
||||||
@ -137,7 +139,7 @@ impl Service<ServiceRequest> for FilesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// full file path
|
// full file path
|
||||||
let path = this.directory.join(&real_path);
|
let path = this.directory.join(&path_on_disk);
|
||||||
if let Err(err) = path.canonicalize() {
|
if let Err(err) = path.canonicalize() {
|
||||||
return this.handle_err(err, req).await;
|
return this.handle_err(err, req).await;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
- Add `Path::as_str`. [#2590]
|
||||||
|
- Deprecate `Path::path`. [#2590]
|
||||||
|
|
||||||
|
[#2590]: https://github.com/actix/actix-web/pull/2590
|
||||||
|
|
||||||
|
|
||||||
## 0.5.0-rc.1 - 2022-01-14
|
## 0.5.0-rc.1 - 2022-01-14
|
||||||
|
@ -37,19 +37,39 @@ impl<T: ResourcePath> Path<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get reference to inner path instance.
|
/// Returns reference to inner path instance.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_ref(&self) -> &T {
|
pub fn get_ref(&self) -> &T {
|
||||||
&self.path
|
&self.path
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get mutable reference to inner path instance.
|
/// Returns mutable reference to inner path instance.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_mut(&mut self) -> &mut T {
|
pub fn get_mut(&mut self) -> &mut T {
|
||||||
&mut self.path
|
&mut self.path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns full path as a string.
|
||||||
|
#[inline]
|
||||||
|
pub fn as_str(&self) -> &str {
|
||||||
|
profile_method!(as_str);
|
||||||
|
self.path.path()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns unprocessed part of the path.
|
/// Returns unprocessed part of the path.
|
||||||
|
///
|
||||||
|
/// Returns empty string if no more is to be processed.
|
||||||
|
#[inline]
|
||||||
|
pub fn unprocessed(&self) -> &str {
|
||||||
|
profile_method!(unprocessed);
|
||||||
|
// clamp skip to path length
|
||||||
|
let skip = (self.skip as usize).min(self.as_str().len());
|
||||||
|
&self.path.path()[skip..]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns unprocessed part of the path.
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[deprecated(since = "0.6.0", note = "Use `.as_str()` or `.unprocessed()`.")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn path(&self) -> &str {
|
pub fn path(&self) -> &str {
|
||||||
profile_method!(path);
|
profile_method!(path);
|
||||||
@ -63,16 +83,6 @@ impl<T: ResourcePath> Path<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns unprocessed part of the path.
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
/// Unlike [`path`](Self::path), this will panic if `skip` indexes further than the path length.
|
|
||||||
#[inline]
|
|
||||||
pub fn unprocessed(&self) -> &str {
|
|
||||||
profile_method!(unprocessed);
|
|
||||||
&self.path.path()[(self.skip as usize)..]
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set new path.
|
/// Set new path.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set(&mut self, path: T) {
|
pub fn set(&mut self, path: T) {
|
||||||
|
@ -692,7 +692,7 @@ impl ResourceDef {
|
|||||||
|
|
||||||
let mut segments = <[PathItem; MAX_DYNAMIC_SEGMENTS]>::default();
|
let mut segments = <[PathItem; MAX_DYNAMIC_SEGMENTS]>::default();
|
||||||
let path = resource.resource_path();
|
let path = resource.resource_path();
|
||||||
let path_str = path.path();
|
let path_str = path.unprocessed();
|
||||||
|
|
||||||
let (matched_len, matched_vars) = match &self.pat_type {
|
let (matched_len, matched_vars) = match &self.pat_type {
|
||||||
PatternType::Static(pattern) => {
|
PatternType::Static(pattern) => {
|
||||||
@ -710,7 +710,7 @@ impl ResourceDef {
|
|||||||
let captures = {
|
let captures = {
|
||||||
profile_section!(pattern_dynamic_regex_exec);
|
profile_section!(pattern_dynamic_regex_exec);
|
||||||
|
|
||||||
match re.captures(path.path()) {
|
match re.captures(path.unprocessed()) {
|
||||||
Some(captures) => captures,
|
Some(captures) => captures,
|
||||||
_ => return false,
|
_ => return false,
|
||||||
}
|
}
|
||||||
@ -738,7 +738,7 @@ impl ResourceDef {
|
|||||||
PatternType::DynamicSet(re, params) => {
|
PatternType::DynamicSet(re, params) => {
|
||||||
profile_section!(pattern_dynamic_set);
|
profile_section!(pattern_dynamic_set);
|
||||||
|
|
||||||
let path = path.path();
|
let path = path.unprocessed();
|
||||||
let (pattern, names) = match re.matches(path).into_iter().next() {
|
let (pattern, names) = match re.matches(path).into_iter().next() {
|
||||||
Some(idx) => ¶ms[idx],
|
Some(idx) => ¶ms[idx],
|
||||||
_ => return false,
|
_ => return false,
|
||||||
|
@ -121,7 +121,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn valid_utf8_multibyte() {
|
fn valid_utf8_multi_byte() {
|
||||||
let test = ('\u{FF00}'..='\u{FFFF}').collect::<String>();
|
let test = ('\u{FF00}'..='\u{FFFF}').collect::<String>();
|
||||||
let encoded = percent_encode(test.as_bytes());
|
let encoded = percent_encode(test.as_bytes());
|
||||||
let path = match_url("/a/{id}/b", format!("/a/{}/b", &encoded));
|
let path = match_url("/a/{id}/b", format!("/a/{}/b", &encoded));
|
||||||
@ -135,6 +135,6 @@ mod tests {
|
|||||||
let path = Path::new(Url::new(uri));
|
let path = Path::new(Url::new(uri));
|
||||||
|
|
||||||
// We should always get a valid utf8 string
|
// We should always get a valid utf8 string
|
||||||
assert!(String::from_utf8(path.path().as_bytes().to_owned()).is_ok());
|
assert!(String::from_utf8(path.as_str().as_bytes().to_owned()).is_ok());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,9 +198,9 @@ impl ServiceRequest {
|
|||||||
self.req.connection_info()
|
self.req.connection_info()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a reference to the Path parameters.
|
/// Returns a reference to the Path parameters.
|
||||||
///
|
///
|
||||||
/// Params is a container for url parameters.
|
/// Params is a container for URL parameters.
|
||||||
/// A variable segment is specified in the form `{identifier}`,
|
/// A variable segment is specified in the form `{identifier}`,
|
||||||
/// where the identifier can be used later in a request handler to
|
/// where the identifier can be used later in a request handler to
|
||||||
/// access the matched value for that segment.
|
/// access the matched value for that segment.
|
||||||
@ -209,6 +209,12 @@ impl ServiceRequest {
|
|||||||
self.req.match_info()
|
self.req.match_info()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable reference to the Path parameters.
|
||||||
|
#[inline]
|
||||||
|
pub fn match_info_mut(&mut self) -> &mut Path<Url> {
|
||||||
|
self.req.match_info_mut()
|
||||||
|
}
|
||||||
|
|
||||||
/// Counterpart to [`HttpRequest::match_name`].
|
/// Counterpart to [`HttpRequest::match_name`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn match_name(&self) -> Option<&str> {
|
pub fn match_name(&self) -> Option<&str> {
|
||||||
@ -221,12 +227,6 @@ impl ServiceRequest {
|
|||||||
self.req.match_pattern()
|
self.req.match_pattern()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a mutable reference to the Path parameters.
|
|
||||||
#[inline]
|
|
||||||
pub fn match_info_mut(&mut self) -> &mut Path<Url> {
|
|
||||||
self.req.match_info_mut()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get a reference to a `ResourceMap` of current application.
|
/// Get a reference to a `ResourceMap` of current application.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn resource_map(&self) -> &ResourceMap {
|
pub fn resource_map(&self) -> &ResourceMap {
|
||||||
|
Loading…
Reference in New Issue
Block a user