use std::borrow::Cow;
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, 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;
use crate::request::HttpRequest;
pub trait HttpServiceFactory
{
fn register(self, config: &mut AppConfig
);
}
pub(crate) trait ServiceFactory
{
fn register(&mut self, config: &mut AppConfig
);
}
pub(crate) struct ServiceFactoryWrapper {
factory: Option,
_t: PhantomData,
}
impl ServiceFactoryWrapper {
pub fn new(factory: T) -> Self {
Self {
factory: Some(factory),
_t: PhantomData,
}
}
}
impl ServiceFactory for ServiceFactoryWrapper
where
T: HttpServiceFactory,
{
fn register(&mut self, config: &mut AppConfig
) {
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,
request: Request,
extensions: Rc,
) -> Self {
let (head, payload) = request.into_parts();
ServiceRequest {
payload,
req: HttpRequest::new(head, path, extensions),
}
}
#[inline]
pub fn into_request(self) -> HttpRequest {
self.req
}
/// 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 {
ServiceResponse::new(self.req, err.into().into())
}
/// 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 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 {
&self.req.path
}
#[inline]
pub fn match_info_mut(&mut self) -> &mut Path {
&mut self.req.path
}
/// Application extensions
#[inline]
pub fn app_extensions(&self) -> &Extensions {
self.req.app_extensions()
}
/// Deconstruct request into parts
pub fn into_parts(self) -> (HttpRequest, Payload) {
(self.req, self.payload)
}
}
impl
Resource for ServiceRequest {
fn resource_path(&mut self) -> &mut Path {
self.match_info_mut()
}
}
impl 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 {
self.req.head.extensions()
}
/// Mutable reference to a the request's extensions
#[inline]
fn extensions_mut(&self) -> RefMut {
self.req.head.extensions_mut()
}
#[inline]
fn take_payload(&mut self) -> Payload {
std::mem::replace(&mut self.payload, Payload::None)
}
}
impl std::ops::Deref for ServiceRequest
{
type Target = RequestHead;
fn deref(&self) -> &RequestHead {
self.req.head()
}
}
impl
std::ops::DerefMut for ServiceRequest
{
fn deref_mut(&mut self) -> &mut RequestHead {
self.head_mut()
}
}
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(())
}
}
pub struct ServiceFromRequest
{
req: HttpRequest,
payload: Payload
,
config: Option>,
}
impl ServiceFromRequest
{
pub(crate) fn new(req: ServiceRequest
, config: Option>) -> Self {
Self {
req: req.req,
payload: req.payload,
config,
}
}
#[inline]
pub fn into_request(self) -> HttpRequest {
self.req
}
#[inline]
pub fn match_info_mut(&mut self) -> &mut Path {
&mut self.req.path
}
/// Create service response for error
#[inline]
pub fn error_response>(self, err: E) -> ServiceResponse {
ServiceResponse::new(self.req, err.into().into())
}
/// Load extractor configuration
pub fn load_config(&self) -> Cow {
if let Some(ref ext) = self.config {
if let Some(cfg) = ext.get::() {
return Cow::Borrowed(cfg);
}
}
Cow::Owned(T::default())
}
}
impl std::ops::Deref for ServiceFromRequest
{
type Target = HttpRequest;
fn deref(&self) -> &HttpRequest {
&self.req
}
}
impl
HttpMessage for ServiceFromRequest
{
type Stream = P;
#[inline]
fn headers(&self) -> &HeaderMap {
self.req.headers()
}
/// Request extensions
#[inline]
fn extensions(&self) -> Ref {
self.req.head.extensions()
}
/// Mutable reference to a the request's extensions
#[inline]
fn extensions_mut(&self) -> RefMut {
self.req.head.extensions_mut()
}
#[inline]
fn take_payload(&mut self) -> Payload {
std::mem::replace(&mut self.payload, Payload::None)
}
}
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>(err: E, request: HttpRequest) -> Self {
let e: Error = err.into();
let res: Response = e.into();
ServiceResponse {
request,
response: res.into_body(),
}
}
/// Get reference to original request
#[inline]
pub fn request(&self) -> &HttpRequest {
&self.request
}
/// Get reference to response
#[inline]
pub fn response(&self) -> &Response {
&self.response
}
/// Get mutable reference to response
#[inline]
pub fn response_mut(&mut self) -> &mut Response {
&mut self.response
}
/// Get the headers from the response
#[inline]
pub fn headers(&self) -> &HeaderMap {
self.response.headers()
}
/// Get a mutable reference to the headers
#[inline]
pub fn headers_mut(&mut self) -> &mut HeaderMap {
self.response.headers_mut()
}
/// Execute closure and in case of error convert it to response.
pub fn checked_expr(mut self, f: F) -> Self
where
F: FnOnce(&mut Self) -> Result<(), E>,
E: Into,
{
match f(&mut self) {
Ok(_) => self,
Err(err) => {
let res: Response = err.into().into();
ServiceResponse::new(self.request, res.into_body())
}
}
}
/// Extract response body
pub fn take_body(&mut self) -> ResponseBody {
self.response.take_body()
}
}
impl ServiceResponse {
/// Set a new body
pub fn map_body(self, f: F) -> ServiceResponse
where
F: FnOnce(&mut ResponseHead, ResponseBody) -> ResponseBody,
{
let response = self.response.map_body(f);
ServiceResponse {
response,
request: self.request,
}
}
}
impl std::ops::Deref for ServiceResponse {
type Target = Response;
fn deref(&self) -> &Response {
self.response()
}
}
impl std::ops::DerefMut for ServiceResponse {
fn deref_mut(&mut self) -> &mut Response {
self.response_mut()
}
}
impl Into> for ServiceResponse {
fn into(self) -> Response {
self.response
}
}
impl IntoFuture for ServiceResponse {
type Item = ServiceResponse;
type Error = Error;
type Future = FutureResult, Error>;
fn into_future(self) -> Self::Future {
ok(self)
}
}
impl fmt::Debug for ServiceResponse {
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().length());
res
}
}