use std::cell::{Ref, RefMut}; use std::fmt; use std::marker::PhantomData; use std::rc::Rc; use actix_http::body::{Body, MessageBody, ResponseBody}; use actix_http::http::{HeaderMap, Method, StatusCode, Uri, Version}; use actix_http::{ Error, Extensions, HttpMessage, Payload, PayloadStream, Request, RequestHead, Response, ResponseHead, }; use actix_router::{Path, Resource, Url}; use futures::future::{ok, FutureResult, IntoFuture}; use crate::config::{AppConfig, ServiceConfig}; use crate::data::Data; use crate::request::HttpRequest; use crate::rmap::ResourceMap; pub trait HttpServiceFactory
{ fn register(self, config: &mut ServiceConfig
); } pub(crate) trait ServiceFactory
{ fn register(&mut self, config: &mut ServiceConfig
);
}
pub(crate) struct ServiceFactoryWrapper ,
}
impl for ServiceFactoryWrapper ,
{
fn register(&mut self, config: &mut ServiceConfig ) {
if let Some(item) = self.factory.take() {
item.register(config)
}
}
}
pub struct ServiceRequest {
req: HttpRequest,
payload: Payload ,
}
impl ServiceRequest {
pub(crate) fn new(
path: Path ,
rmap: Rc ) -> Self {
ServiceRequest { req, payload }
}
/// Deconstruct request into parts
pub fn into_parts(self) -> (HttpRequest, Payload ) {
(self.req, self.payload)
}
/// Create service response
#[inline]
pub fn into_response(self, res: Response) -> ServiceResponse {
ServiceResponse::new(self.req, res)
}
/// Create service response for error
#[inline]
pub fn error_response>(self, err: E) -> ServiceResponse {
let res: Response = err.into().into();
ServiceResponse::new(self.req, res.into_body())
}
/// 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 {
&mut self.req.head
}
/// 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 {
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 Resource {
fn resource_path(&mut self) -> &mut Path HttpMessage for ServiceRequest {
type Stream = P;
#[inline]
/// Returns Request's headers.
fn headers(&self) -> &HeaderMap {
&self.head().headers
}
/// Request extensions
#[inline]
fn extensions(&self) -> Ref 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(())
}
}
pub struct ServiceResponse {
request: HttpRequest,
response: Response,
}
impl ServiceResponse {
/// Create service response instance
pub fn new(request: HttpRequest, response: Response) -> Self {
ServiceResponse { request, response }
}
/// Create service response from the error
pub fn from_err