//! For path segment extractor documentation, see [`Path`]. use std::{fmt, ops, sync::Arc}; use actix_http::error::{Error, ErrorNotFound}; use actix_router::PathDeserializer; use futures_util::future::{ready, Ready}; use serde::de; use crate::{dev::Payload, error::PathError, FromRequest, HttpRequest}; /// Extract typed data from request path segments. /// /// Use [`PathConfig`] to configure extraction process. /// /// # Examples /// ``` /// use actix_web::{get, web}; /// /// // extract path info from "/{name}/{count}/index.html" into tuple /// // {name} - deserialize a String /// // {count} - deserialize a u32 /// #[get("/")] /// async fn index(path: web::Path<(String, u32)>) -> String { /// let (name, count) = path.into_inner(); /// format!("Welcome {}! {}", name, count) /// } /// ``` /// /// Path segments also can be deserialized into any type that implements [`serde::Deserialize`]. /// Path segment labels will be matched with struct field names. /// /// ``` /// use actix_web::{get, web}; /// use serde::Deserialize; /// /// #[derive(Deserialize)] /// struct Info { /// name: String, /// } /// /// // extract `Info` from a path using serde /// #[get("/")] /// async fn index(info: web::Path) -> String { /// format!("Welcome {}!", info.name) /// } /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord)] pub struct Path(T); impl Path { /// Unwrap into inner `T` value. pub fn into_inner(self) -> T { self.0 } } impl AsRef for Path { fn as_ref(&self) -> &T { &self.0 } } impl ops::Deref for Path { type Target = T; fn deref(&self) -> &T { &self.0 } } impl ops::DerefMut for Path { fn deref_mut(&mut self) -> &mut T { &mut self.0 } } impl From for Path { fn from(inner: T) -> Path { Path(inner) } } impl fmt::Debug for Path { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl fmt::Display for Path { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } /// See [here](#usage) for example of usage as an extractor. impl FromRequest for Path where T: de::DeserializeOwned, { type Error = Error; type Future = Ready>; type Config = PathConfig; #[inline] fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { let error_handler = req .app_data::() .map(|c| c.ehandler.clone()) .unwrap_or(None); ready( de::Deserialize::deserialize(PathDeserializer::new(req.match_info())) .map(Path) .map_err(move |e| { log::debug!( "Failed during Path extractor deserialization. \ Request path: {:?}", req.path() ); if let Some(error_handler) = error_handler { let e = PathError::Deserialize(e); (error_handler)(e, req) } else { ErrorNotFound(e) } }), ) } } /// Path extractor configuration /// /// ``` /// use actix_web::web::PathConfig; /// use actix_web::{error, web, App, FromRequest, HttpResponse}; /// use serde::Deserialize; /// /// #[derive(Deserialize, Debug)] /// enum Folder { /// #[serde(rename = "inbox")] /// Inbox, /// #[serde(rename = "outbox")] /// Outbox, /// } /// /// // deserialize `Info` from request's path /// async fn index(folder: web::Path) -> String { /// format!("Selected folder: {:?}!", folder) /// } /// /// fn main() { /// let app = App::new().service( /// web::resource("/messages/{folder}") /// .app_data(PathConfig::default().error_handler(|err, req| { /// error::InternalError::from_response( /// err, /// HttpResponse::Conflict().finish(), /// ) /// .into() /// })) /// .route(web::post().to(index)), /// ); /// } /// ``` #[derive(Clone)] pub struct PathConfig { ehandler: Option Error + Send + Sync>>, } impl PathConfig { /// Set custom error handler pub fn error_handler(mut self, f: F) -> Self where F: Fn(PathError, &HttpRequest) -> Error + Send + Sync + 'static, { self.ehandler = Some(Arc::new(f)); self } } impl Default for PathConfig { fn default() -> Self { PathConfig { ehandler: None } } } #[cfg(test)] mod tests { use actix_router::ResourceDef; use derive_more::Display; use serde::Deserialize; use super::*; use crate::test::TestRequest; use crate::{error, http, HttpResponse}; #[derive(Deserialize, Debug, Display)] #[display(fmt = "MyStruct({}, {})", key, value)] struct MyStruct { key: String, value: String, } #[derive(Deserialize)] struct Test2 { key: String, value: u32, } #[actix_rt::test] async fn test_extract_path_single() { let resource = ResourceDef::new("/{value}/"); let mut req = TestRequest::with_uri("/32/").to_srv_request(); resource.match_path(req.match_info_mut()); let (req, mut pl) = req.into_parts(); assert_eq!(*Path::::from_request(&req, &mut pl).await.unwrap(), 32); assert!(Path::::from_request(&req, &mut pl).await.is_err()); } #[actix_rt::test] async fn test_tuple_extract() { let resource = ResourceDef::new("/{key}/{value}/"); let mut req = TestRequest::with_uri("/name/user1/?id=test").to_srv_request(); resource.match_path(req.match_info_mut()); let (req, mut pl) = req.into_parts(); let (Path(res),) = <(Path<(String, String)>,)>::from_request(&req, &mut pl) .await .unwrap(); assert_eq!(res.0, "name"); assert_eq!(res.1, "user1"); let (Path(a), Path(b)) = <(Path<(String, String)>, Path<(String, String)>)>::from_request( &req, &mut pl, ) .await .unwrap(); assert_eq!(a.0, "name"); assert_eq!(a.1, "user1"); assert_eq!(b.0, "name"); assert_eq!(b.1, "user1"); let () = <()>::from_request(&req, &mut pl).await.unwrap(); } #[actix_rt::test] async fn test_request_extract() { let mut req = TestRequest::with_uri("/name/user1/?id=test").to_srv_request(); let resource = ResourceDef::new("/{key}/{value}/"); resource.match_path(req.match_info_mut()); let (req, mut pl) = req.into_parts(); let mut s = Path::::from_request(&req, &mut pl).await.unwrap(); assert_eq!(s.key, "name"); assert_eq!(s.value, "user1"); s.value = "user2".to_string(); assert_eq!(s.value, "user2"); assert_eq!( format!("{}, {:?}", s, s), "MyStruct(name, user2), MyStruct { key: \"name\", value: \"user2\" }" ); let s = s.into_inner(); assert_eq!(s.value, "user2"); let Path(s) = Path::<(String, String)>::from_request(&req, &mut pl) .await .unwrap(); assert_eq!(s.0, "name"); assert_eq!(s.1, "user1"); let mut req = TestRequest::with_uri("/name/32/").to_srv_request(); let resource = ResourceDef::new("/{key}/{value}/"); resource.match_path(req.match_info_mut()); let (req, mut pl) = req.into_parts(); let s = Path::::from_request(&req, &mut pl).await.unwrap(); assert_eq!(s.as_ref().key, "name"); assert_eq!(s.value, 32); let Path(s) = Path::<(String, u8)>::from_request(&req, &mut pl) .await .unwrap(); assert_eq!(s.0, "name"); assert_eq!(s.1, 32); let res = Path::>::from_request(&req, &mut pl) .await .unwrap(); assert_eq!(res[0], "name".to_owned()); assert_eq!(res[1], "32".to_owned()); } #[actix_rt::test] async fn test_custom_err_handler() { let (req, mut pl) = TestRequest::with_uri("/name/user1/") .app_data(PathConfig::default().error_handler(|err, _| { error::InternalError::from_response( err, HttpResponse::Conflict().finish(), ) .into() })) .to_http_parts(); let s = Path::<(usize,)>::from_request(&req, &mut pl) .await .unwrap_err(); let res: HttpResponse = s.into(); assert_eq!(res.status(), http::StatusCode::CONFLICT); } }