use std::cell::{Ref, RefMut}; use std::rc::Rc; use std::{fmt, net}; use actix_http::{ body::{AnyBody, MessageBody}, http::{HeaderMap, Method, StatusCode, Uri, Version}, Extensions, HttpMessage, Payload, PayloadStream, RequestHead, Response, ResponseHead, }; use actix_router::{IntoPatterns, Path, Patterns, Resource, ResourceDef, Url}; use actix_service::{IntoServiceFactory, ServiceFactory}; #[cfg(feature = "cookies")] use cookie::{Cookie, ParseError as CookieParseError}; use crate::{ config::{AppConfig, AppService}, dev::ensure_leading_slash, guard::Guard, info::ConnectionInfo, rmap::ResourceMap, Error, HttpRequest, HttpResponse, }; pub trait HttpServiceFactory { fn register(self, config: &mut AppService); } impl HttpServiceFactory for Vec { fn register(self, config: &mut AppService) { self.into_iter() .for_each(|factory| factory.register(config)); } } pub(crate) trait AppServiceFactory { fn register(&mut self, config: &mut AppService); } pub(crate) struct ServiceFactoryWrapper { factory: Option, } impl ServiceFactoryWrapper { pub fn new(factory: T) -> Self { Self { factory: Some(factory), } } } impl AppServiceFactory for ServiceFactoryWrapper where T: HttpServiceFactory, { fn register(&mut self, config: &mut AppService) { if let Some(item) = self.factory.take() { item.register(config) } } } /// A service level request wrapper. /// /// Allows mutable access to request's internal structures. pub struct ServiceRequest { req: HttpRequest, payload: Payload, } impl ServiceRequest { /// Construct service request pub(crate) fn new(req: HttpRequest, payload: Payload) -> Self { Self { req, payload } } /// Deconstruct request into parts #[inline] pub fn into_parts(self) -> (HttpRequest, Payload) { (self.req, self.payload) } /// Get mutable access to inner `HttpRequest` and `Payload` #[inline] pub fn parts_mut(&mut self) -> (&mut HttpRequest, &mut Payload) { (&mut self.req, &mut self.payload) } /// Construct request from parts. pub fn from_parts(req: HttpRequest, payload: Payload) -> Self { Self { req, payload } } /// Construct request from request. /// /// The returned `ServiceRequest` would have no payload. pub fn from_request(req: HttpRequest) -> Self { ServiceRequest { req, payload: Payload::None, } } /// Create service response #[inline] pub fn into_response>>(self, res: R) -> ServiceResponse { let res = HttpResponse::from(res.into()); ServiceResponse::new(self.req, res) } /// Create service response for error #[inline] pub fn error_response>(self, err: E) -> ServiceResponse { let res = HttpResponse::from_error(err.into()); ServiceResponse::new(self.req, res) } /// This method returns reference to the request head #[inline] pub fn head(&self) -> &RequestHead { self.req.head() } /// This method returns reference to the request head #[inline] pub fn head_mut(&mut self) -> &mut RequestHead { self.req.head_mut() } /// Request's uri. #[inline] pub fn uri(&self) -> &Uri { &self.head().uri } /// Read the Request method. #[inline] pub fn method(&self) -> &Method { &self.head().method } /// Read the Request Version. #[inline] pub fn version(&self) -> Version { self.head().version } #[inline] /// Returns request's headers. pub fn headers(&self) -> &HeaderMap { &self.head().headers } #[inline] /// Returns mutable request's headers. pub fn headers_mut(&mut self) -> &mut HeaderMap { &mut self.head_mut().headers } /// The target path of this Request. #[inline] pub fn path(&self) -> &str { self.head().uri.path() } /// The query string in the URL. /// /// E.g., id=10 #[inline] pub fn query_string(&self) -> &str { self.uri().query().unwrap_or_default() } /// Peer socket address. /// /// Peer address is the directly connected peer's socket address. If a proxy is used in front of /// the Actix Web server, then it would be address of this proxy. /// /// To get client connection information `ConnectionInfo` should be used. /// /// Will only return None when called in unit tests. #[inline] pub fn peer_addr(&self) -> Option { self.head().peer_addr } /// Get *ConnectionInfo* for the current request. #[inline] pub fn connection_info(&self) -> Ref<'_, ConnectionInfo> { ConnectionInfo::get(self.head(), &*self.app_config()) } /// Get a reference to the Path parameters. /// /// Params is a container for url parameters. /// A variable segment is specified in the form `{identifier}`, /// where the identifier can be used later in a request handler to /// access the matched value for that segment. #[inline] pub fn match_info(&self) -> &Path { self.req.match_info() } /// Counterpart to [`HttpRequest::match_name`](super::HttpRequest::match_name()). #[inline] pub fn match_name(&self) -> Option<&str> { self.req.match_name() } /// Counterpart to [`HttpRequest::match_pattern`](super::HttpRequest::match_pattern()). #[inline] pub fn match_pattern(&self) -> Option { self.req.match_pattern() } /// Get a mutable reference to the Path parameters. #[inline] pub fn match_info_mut(&mut self) -> &mut Path { self.req.match_info_mut() } /// Get a reference to a `ResourceMap` of current application. #[inline] pub fn resource_map(&self) -> &ResourceMap { self.req.resource_map() } /// Service configuration #[inline] pub fn app_config(&self) -> &AppConfig { self.req.app_config() } /// Counterpart to [`HttpRequest::app_data`](super::HttpRequest::app_data()). pub fn app_data(&self) -> Option<&T> { for container in self.req.inner.app_data.iter().rev() { if let Some(data) = container.get::() { return Some(data); } } None } #[cfg(feature = "cookies")] pub fn cookies(&self) -> Result>>, CookieParseError> { self.req.cookies() } /// Return request cookie. #[cfg(feature = "cookies")] pub fn cookie(&self, name: &str) -> Option> { self.req.cookie(name) } /// Set request payload. pub fn set_payload(&mut self, payload: Payload) { self.payload = payload; } /// Add data container to request's resolution set. /// /// In middleware, prefer [`extensions_mut`](ServiceRequest::extensions_mut) for request-local /// data since it is assumed that the same app data is presented for every request. pub fn add_data_container(&mut self, extensions: Rc) { Rc::get_mut(&mut (self.req).inner) .unwrap() .app_data .push(extensions); } } impl Resource for ServiceRequest { fn resource_path(&mut self) -> &mut Path { self.match_info_mut() } } impl HttpMessage for ServiceRequest { type Stream = PayloadStream; #[inline] /// Returns Request's headers. fn headers(&self) -> &HeaderMap { &self.head().headers } /// Request extensions #[inline] fn extensions(&self) -> Ref<'_, Extensions> { self.req.extensions() } /// Mutable reference to a the request's extensions #[inline] fn extensions_mut(&self) -> RefMut<'_, Extensions> { self.req.extensions_mut() } #[inline] fn take_payload(&mut self) -> Payload { self.payload.take() } } impl fmt::Debug for ServiceRequest { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!( f, "\nServiceRequest {:?} {}:{}", self.head().version, self.head().method, self.path() )?; if !self.query_string().is_empty() { writeln!(f, " query: ?{:?}", self.query_string())?; } if !self.match_info().is_empty() { writeln!(f, " params: {:?}", self.match_info())?; } writeln!(f, " headers:")?; for (key, val) in self.headers().iter() { writeln!(f, " {:?}: {:?}", key, val)?; } Ok(()) } } /// A service level response wrapper. pub struct ServiceResponse { request: HttpRequest, response: HttpResponse, } impl ServiceResponse { /// Create service response from the error pub fn from_err>(err: E, request: HttpRequest) -> Self { let response = HttpResponse::from_error(err); ServiceResponse { request, response } } } impl ServiceResponse { /// Create service response instance pub fn new(request: HttpRequest, response: HttpResponse) -> Self { ServiceResponse { request, response } } /// Create service response for error #[inline] pub fn error_response>(self, err: E) -> ServiceResponse { ServiceResponse::from_err(err, self.request) } /// Create service response #[inline] pub fn into_response(self, response: HttpResponse) -> ServiceResponse { ServiceResponse::new(self.request, response) } /// Get reference to original request #[inline] pub fn request(&self) -> &HttpRequest { &self.request } /// Get reference to response #[inline] pub fn response(&self) -> &HttpResponse { &self.response } /// Get mutable reference to response #[inline] pub fn response_mut(&mut self) -> &mut HttpResponse { &mut self.response } /// Get the response status code #[inline] pub fn status(&self) -> StatusCode { self.response.status() } #[inline] /// Returns response's headers. pub fn headers(&self) -> &HeaderMap { self.response.headers() } /// Returns mutable response's headers. #[inline] pub fn headers_mut(&mut self) -> &mut HeaderMap { self.response.headers_mut() } /// Extract response body pub fn into_body(self) -> B { self.response.into_body() } } impl ServiceResponse { /// Set a new body pub fn map_body(self, f: F) -> ServiceResponse where F: FnOnce(&mut ResponseHead, B) -> B2, { let response = self.response.map_body(f); ServiceResponse { response, request: self.request, } } } impl From> for HttpResponse { fn from(res: ServiceResponse) -> HttpResponse { res.response } } impl From> for Response { fn from(res: ServiceResponse) -> Response { res.response.into() } } impl fmt::Debug for ServiceResponse where B: MessageBody, B::Error: Into, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let res = writeln!( f, "\nServiceResponse {:?} {}{}", self.response.head().version, self.response.head().status, self.response.head().reason.unwrap_or(""), ); let _ = writeln!(f, " headers:"); for (key, val) in self.response.head().headers.iter() { let _ = writeln!(f, " {:?}: {:?}", key, val); } let _ = writeln!(f, " body: {:?}", self.response.body().size()); res } } pub struct WebService { rdef: Patterns, name: Option, guards: Vec>, } impl WebService { /// Create new `WebService` instance. pub fn new(path: T) -> Self { WebService { rdef: path.patterns(), name: None, guards: Vec::new(), } } /// Set service name. /// /// Name is used for URL generation. pub fn name(mut self, name: &str) -> Self { self.name = Some(name.to_string()); self } /// Add match guard to a web service. /// /// ``` /// use actix_web::{web, guard, dev, App, Error, HttpResponse}; /// /// async fn index(req: dev::ServiceRequest) -> Result { /// Ok(req.into_response(HttpResponse::Ok().finish())) /// } /// /// fn main() { /// let app = App::new() /// .service( /// web::service("/app") /// .guard(guard::Header("content-type", "text/plain")) /// .finish(index) /// ); /// } /// ``` pub fn guard(mut self, guard: G) -> Self { self.guards.push(Box::new(guard)); self } /// Set a service factory implementation and generate web service. pub fn finish(self, service: F) -> impl HttpServiceFactory where F: IntoServiceFactory, T: ServiceFactory< ServiceRequest, Config = (), Response = ServiceResponse, Error = Error, InitError = (), > + 'static, { WebServiceImpl { srv: service.into_factory(), rdef: self.rdef, name: self.name, guards: self.guards, } } } struct WebServiceImpl { srv: T, rdef: Patterns, name: Option, guards: Vec>, } impl HttpServiceFactory for WebServiceImpl where T: ServiceFactory< ServiceRequest, Config = (), Response = ServiceResponse, Error = Error, InitError = (), > + 'static, { fn register(mut self, config: &mut AppService) { let guards = if self.guards.is_empty() { None } else { Some(std::mem::take(&mut self.guards)) }; let mut rdef = if config.is_root() || !self.rdef.is_empty() { ResourceDef::new(ensure_leading_slash(self.rdef)) } else { ResourceDef::new(self.rdef) }; if let Some(ref name) = self.name { rdef.set_name(name); } config.register_service(rdef, guards, self.srv, None) } } /// Macro helping register different types of services at the sametime. /// /// The service type must be implementing [`HttpServiceFactory`](self::HttpServiceFactory) trait. /// /// The max number of services can be grouped together is 12. /// /// # Examples /// /// ``` /// use actix_web::{services, web, App}; /// /// let services = services![ /// web::resource("/test2").to(|| async { "test2" }), /// web::scope("/test3").route("/", web::get().to(|| async { "test3" })) /// ]; /// /// let app = App::new().service(services); /// /// // services macro just convert multiple services to a tuple. /// // below would also work without importing the macro. /// let app = App::new().service(( /// web::resource("/test2").to(|| async { "test2" }), /// web::scope("/test3").route("/", web::get().to(|| async { "test3" })) /// )); /// ``` #[macro_export] macro_rules! services { ($($x:expr),+ $(,)?) => { ($($x,)+) } } /// HttpServiceFactory trait impl for tuples macro_rules! service_tuple ({ $($T:ident)+ } => { impl<$($T: HttpServiceFactory),+> HttpServiceFactory for ($($T,)+) { #[allow(non_snake_case)] fn register(self, config: &mut AppService) { let ($($T,)*) = self; $($T.register(config);)+ } } }); service_tuple! { A } service_tuple! { A B } service_tuple! { A B C } service_tuple! { A B C D } service_tuple! { A B C D E } service_tuple! { A B C D E F } service_tuple! { A B C D E F G } service_tuple! { A B C D E F G H } service_tuple! { A B C D E F G H I } service_tuple! { A B C D E F G H I J } service_tuple! { A B C D E F G H I J K } service_tuple! { A B C D E F G H I J K L } #[cfg(test)] mod tests { use super::*; use crate::test::{init_service, TestRequest}; use crate::{guard, http, web, App, HttpResponse}; use actix_service::Service; use actix_utils::future::ok; #[actix_rt::test] async fn test_service() { let srv = init_service( App::new().service(web::service("/test").name("test").finish( |req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish())), )), ) .await; let req = TestRequest::with_uri("/test").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); let srv = init_service( App::new().service(web::service("/test").guard(guard::Get()).finish( |req: ServiceRequest| ok(req.into_response(HttpResponse::Ok().finish())), )), ) .await; let req = TestRequest::with_uri("/test") .method(http::Method::PUT) .to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::NOT_FOUND); } // allow deprecated App::data #[allow(deprecated)] #[actix_rt::test] async fn test_service_data() { let srv = init_service( App::new() .data(42u32) .service(web::service("/test").name("test").finish( |req: ServiceRequest| { assert_eq!(req.app_data::>().unwrap().as_ref(), &42); ok(req.into_response(HttpResponse::Ok().finish())) }, )), ) .await; let req = TestRequest::with_uri("/test").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); } #[test] fn test_fmt_debug() { let req = TestRequest::get() .uri("/index.html?test=1") .insert_header(("x-test", "111")) .to_srv_request(); let s = format!("{:?}", req); assert!(s.contains("ServiceRequest")); assert!(s.contains("test=1")); assert!(s.contains("x-test")); let res = HttpResponse::Ok().insert_header(("x-test", "111")).finish(); let res = TestRequest::post() .uri("/index.html?test=1") .to_srv_response(res); let s = format!("{:?}", res); assert!(s.contains("ServiceResponse")); assert!(s.contains("x-test")); } #[actix_rt::test] async fn test_services_macro() { let scoped = services![ web::service("/scoped_test1").name("scoped_test1").finish( |req: ServiceRequest| async { Ok(req.into_response(HttpResponse::Ok().finish())) } ), web::resource("/scoped_test2").to(|| async { "test2" }), ]; let services = services![ web::service("/test1") .name("test") .finish(|req: ServiceRequest| async { Ok(req.into_response(HttpResponse::Ok().finish())) }), web::resource("/test2").to(|| async { "test2" }), web::scope("/test3").service(scoped) ]; let srv = init_service(App::new().service(services)).await; let req = TestRequest::with_uri("/test1").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); let req = TestRequest::with_uri("/test2").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); let req = TestRequest::with_uri("/test3/scoped_test1").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); let req = TestRequest::with_uri("/test3/scoped_test2").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); } #[actix_rt::test] async fn test_services_vec() { let services = vec![ web::resource("/test1").to(|| async { "test1" }), web::resource("/test2").to(|| async { "test2" }), ]; let scoped = vec![ web::resource("/scoped_test1").to(|| async { "test1" }), web::resource("/scoped_test2").to(|| async { "test2" }), ]; let srv = init_service( App::new() .service(services) .service(web::scope("/test3").service(scoped)), ) .await; let req = TestRequest::with_uri("/test1").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); let req = TestRequest::with_uri("/test2").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); let req = TestRequest::with_uri("/test3/scoped_test1").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); let req = TestRequest::with_uri("/test3/scoped_test2").to_request(); let resp = srv.call(req).await.unwrap(); assert_eq!(resp.status(), http::StatusCode::OK); } }