2019-03-03 04:19:56 +01:00
|
|
|
use std::cell::{Ref, RefMut};
|
2019-03-06 07:10:08 +01:00
|
|
|
use std::fmt;
|
2019-03-07 00:47:15 +01:00
|
|
|
use std::marker::PhantomData;
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2019-03-06 07:10:08 +01:00
|
|
|
use actix_http::body::{Body, MessageBody, ResponseBody};
|
2019-04-02 22:35:01 +02:00
|
|
|
use actix_http::http::{HeaderMap, Method, StatusCode, Uri, Version};
|
2019-03-02 07:51:32 +01:00
|
|
|
use actix_http::{
|
2019-04-08 08:06:21 +02:00
|
|
|
Error, Extensions, HttpMessage, Payload, PayloadStream, RequestHead, Response,
|
|
|
|
ResponseHead,
|
2019-03-02 07:51:32 +01:00
|
|
|
};
|
2019-03-04 20:47:53 +01:00
|
|
|
use actix_router::{Path, Resource, Url};
|
2019-03-03 06:35:31 +01:00
|
|
|
use futures::future::{ok, FutureResult, IntoFuture};
|
2019-03-02 07:51:32 +01:00
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
use crate::config::{AppConfig, ServiceConfig};
|
2019-04-07 23:43:07 +02:00
|
|
|
use crate::data::Data;
|
2019-03-02 07:51:32 +01:00
|
|
|
use crate::request::HttpRequest;
|
|
|
|
|
2019-03-07 00:47:15 +01:00
|
|
|
pub trait HttpServiceFactory<P> {
|
2019-03-09 23:06:24 +01:00
|
|
|
fn register(self, config: &mut ServiceConfig<P>);
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait ServiceFactory<P> {
|
2019-03-09 23:06:24 +01:00
|
|
|
fn register(&mut self, config: &mut ServiceConfig<P>);
|
2019-03-07 00:47:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ServiceFactoryWrapper<T, P> {
|
|
|
|
factory: Option<T>,
|
|
|
|
_t: PhantomData<P>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, P> ServiceFactoryWrapper<T, P> {
|
|
|
|
pub fn new(factory: T) -> Self {
|
|
|
|
Self {
|
|
|
|
factory: Some(factory),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, P> ServiceFactory<P> for ServiceFactoryWrapper<T, P>
|
|
|
|
where
|
|
|
|
T: HttpServiceFactory<P>,
|
|
|
|
{
|
2019-03-09 23:06:24 +01:00
|
|
|
fn register(&mut self, config: &mut ServiceConfig<P>) {
|
2019-03-07 00:47:15 +01:00
|
|
|
if let Some(item) = self.factory.take() {
|
|
|
|
item.register(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 19:08:08 +01:00
|
|
|
pub struct ServiceRequest<P = PayloadStream> {
|
2019-03-02 07:51:32 +01:00
|
|
|
req: HttpRequest,
|
|
|
|
payload: Payload<P>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> ServiceRequest<P> {
|
2019-03-26 23:14:32 +01:00
|
|
|
/// Construct service request from parts
|
|
|
|
pub fn from_parts(req: HttpRequest, payload: Payload<P>) -> Self {
|
|
|
|
ServiceRequest { req, payload }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deconstruct request into parts
|
|
|
|
pub fn into_parts(self) -> (HttpRequest, Payload<P>) {
|
|
|
|
(self.req, self.payload)
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create service response
|
|
|
|
#[inline]
|
|
|
|
pub fn into_response<B>(self, res: Response<B>) -> ServiceResponse<B> {
|
|
|
|
ServiceResponse::new(self.req, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create service response for error
|
|
|
|
#[inline]
|
2019-03-10 05:40:09 +01:00
|
|
|
pub fn error_response<B, E: Into<Error>>(self, err: E) -> ServiceResponse<B> {
|
|
|
|
let res: Response = err.into().into();
|
|
|
|
ServiceResponse::new(self.req, res.into_body())
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 04:19:56 +01:00
|
|
|
/// This method returns reference to the request head
|
|
|
|
#[inline]
|
|
|
|
pub fn head(&self) -> &RequestHead {
|
2019-04-08 08:06:21 +02:00
|
|
|
&self.req.head()
|
2019-03-03 04:19:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This method returns reference to the request head
|
|
|
|
#[inline]
|
|
|
|
pub fn head_mut(&mut self) -> &mut RequestHead {
|
2019-04-08 08:06:21 +02:00
|
|
|
self.req.head_mut()
|
2019-03-03 04:19:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:10:45 +01:00
|
|
|
#[inline]
|
2019-04-02 22:35:01 +02:00
|
|
|
/// Returns request's headers.
|
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
|
|
|
&self.head().headers
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns mutable request's headers.
|
2019-03-06 04:10:45 +01:00
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
|
|
|
&mut self.head_mut().headers
|
|
|
|
}
|
|
|
|
|
2019-03-03 04:19:56 +01:00
|
|
|
/// 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 {
|
|
|
|
if let Some(query) = self.uri().query().as_ref() {
|
|
|
|
query
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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<Url> {
|
2019-04-08 08:06:21 +02:00
|
|
|
self.req.match_info()
|
2019-03-03 04:19:56 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn match_info_mut(&mut self) -> &mut Path<Url> {
|
2019-04-08 08:06:21 +02:00
|
|
|
self.req.match_info_mut()
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2019-03-03 04:19:56 +01:00
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
/// Service configuration
|
2019-03-03 04:19:56 +01:00
|
|
|
#[inline]
|
2019-03-09 23:06:24 +01:00
|
|
|
pub fn app_config(&self) -> &AppConfig {
|
2019-04-07 23:43:07 +02:00
|
|
|
self.req.app_config()
|
2019-03-03 04:19:56 +01:00
|
|
|
}
|
2019-04-04 00:25:52 +02:00
|
|
|
|
|
|
|
/// Get an application data stored with `App::data()` method during
|
|
|
|
/// application configuration.
|
|
|
|
pub fn app_data<T: 'static>(&self) -> Option<Data<T>> {
|
2019-04-07 23:43:07 +02:00
|
|
|
if let Some(st) = self.req.app_config().extensions().get::<Data<T>>() {
|
2019-04-04 00:25:52 +02:00
|
|
|
Some(st.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 20:47:53 +01:00
|
|
|
impl<P> Resource<Url> for ServiceRequest<P> {
|
|
|
|
fn resource_path(&mut self) -> &mut Path<Url> {
|
|
|
|
self.match_info_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P> HttpMessage for ServiceRequest<P> {
|
|
|
|
type Stream = P;
|
|
|
|
|
|
|
|
#[inline]
|
2019-03-06 03:47:18 +01:00
|
|
|
/// Returns Request's headers.
|
2019-03-02 07:51:32 +01:00
|
|
|
fn headers(&self) -> &HeaderMap {
|
2019-03-06 03:47:18 +01:00
|
|
|
&self.head().headers
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Request extensions
|
|
|
|
#[inline]
|
|
|
|
fn extensions(&self) -> Ref<Extensions> {
|
2019-04-08 08:06:21 +02:00
|
|
|
self.req.extensions()
|
2019-03-06 03:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable reference to a the request's extensions
|
|
|
|
#[inline]
|
|
|
|
fn extensions_mut(&self) -> RefMut<Extensions> {
|
2019-04-08 08:06:21 +02:00
|
|
|
self.req.extensions_mut()
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn take_payload(&mut self) -> Payload<Self::Stream> {
|
|
|
|
std::mem::replace(&mut self.payload, Payload::None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 07:10:08 +01:00
|
|
|
impl<P> fmt::Debug for ServiceRequest<P> {
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
pub struct ServiceResponse<B = Body> {
|
|
|
|
request: HttpRequest,
|
|
|
|
response: Response<B>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<B> ServiceResponse<B> {
|
|
|
|
/// Create service response instance
|
|
|
|
pub fn new(request: HttpRequest, response: Response<B>) -> Self {
|
|
|
|
ServiceResponse { request, response }
|
|
|
|
}
|
|
|
|
|
2019-03-06 07:10:08 +01:00
|
|
|
/// Create service response from the error
|
|
|
|
pub fn from_err<E: Into<Error>>(err: E, request: HttpRequest) -> Self {
|
|
|
|
let e: Error = err.into();
|
|
|
|
let res: Response = e.into();
|
|
|
|
ServiceResponse {
|
|
|
|
request,
|
|
|
|
response: res.into_body(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 05:40:09 +01:00
|
|
|
/// Create service response for error
|
|
|
|
#[inline]
|
|
|
|
pub fn error_response<E: Into<Error>>(self, err: E) -> Self {
|
|
|
|
Self::from_err(err, self.request)
|
|
|
|
}
|
|
|
|
|
2019-03-11 00:35:38 +01:00
|
|
|
/// Create service response
|
|
|
|
#[inline]
|
|
|
|
pub fn into_response<B1>(self, response: Response<B1>) -> ServiceResponse<B1> {
|
|
|
|
ServiceResponse::new(self.request, response)
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Get reference to original request
|
|
|
|
#[inline]
|
|
|
|
pub fn request(&self) -> &HttpRequest {
|
|
|
|
&self.request
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get reference to response
|
|
|
|
#[inline]
|
|
|
|
pub fn response(&self) -> &Response<B> {
|
|
|
|
&self.response
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get mutable reference to response
|
|
|
|
#[inline]
|
|
|
|
pub fn response_mut(&mut self) -> &mut Response<B> {
|
|
|
|
&mut self.response
|
|
|
|
}
|
|
|
|
|
2019-04-02 22:35:01 +02:00
|
|
|
/// 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Returns mutable response's headers.
|
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
|
|
|
self.response.headers_mut()
|
|
|
|
}
|
|
|
|
|
2019-03-06 03:47:18 +01:00
|
|
|
/// Execute closure and in case of error convert it to response.
|
|
|
|
pub fn checked_expr<F, E>(mut self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<(), E>,
|
|
|
|
E: Into<Error>,
|
|
|
|
{
|
|
|
|
match f(&mut self) {
|
|
|
|
Ok(_) => self,
|
|
|
|
Err(err) => {
|
|
|
|
let res: Response = err.into().into();
|
|
|
|
ServiceResponse::new(self.request, res.into_body())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
|
|
|
/// Extract response body
|
|
|
|
pub fn take_body(&mut self) -> ResponseBody<B> {
|
|
|
|
self.response.take_body()
|
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-06 03:47:18 +01:00
|
|
|
impl<B> ServiceResponse<B> {
|
2019-03-02 07:51:32 +01:00
|
|
|
/// Set a new body
|
2019-03-06 03:47:18 +01:00
|
|
|
pub fn map_body<F, B2>(self, f: F) -> ServiceResponse<B2>
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut ResponseHead, ResponseBody<B>) -> ResponseBody<B2>,
|
|
|
|
{
|
|
|
|
let response = self.response.map_body(f);
|
|
|
|
|
|
|
|
ServiceResponse {
|
|
|
|
response,
|
|
|
|
request: self.request,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 03:47:18 +01:00
|
|
|
impl<B> Into<Response<B>> for ServiceResponse<B> {
|
2019-03-02 07:51:32 +01:00
|
|
|
fn into(self) -> Response<B> {
|
|
|
|
self.response
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 06:35:31 +01:00
|
|
|
|
2019-03-06 03:47:18 +01:00
|
|
|
impl<B> IntoFuture for ServiceResponse<B> {
|
2019-03-03 06:35:31 +01:00
|
|
|
type Item = ServiceResponse<B>;
|
|
|
|
type Error = Error;
|
|
|
|
type Future = FutureResult<ServiceResponse<B>, Error>;
|
|
|
|
|
|
|
|
fn into_future(self) -> Self::Future {
|
|
|
|
ok(self)
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
|
|
|
impl<B: MessageBody> fmt::Debug for ServiceResponse<B> {
|
|
|
|
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);
|
|
|
|
}
|
2019-04-10 21:24:17 +02:00
|
|
|
let _ = writeln!(f, " body: {:?}", self.response.body().size());
|
2019-03-06 07:10:08 +01:00
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 05:43:00 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::test::TestRequest;
|
|
|
|
use crate::HttpResponse;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fmt_debug() {
|
|
|
|
let req = TestRequest::get()
|
|
|
|
.uri("/index.html?test=1")
|
|
|
|
.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().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"));
|
|
|
|
}
|
|
|
|
}
|