1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 18:02:39 +01:00
actix-web/actix-http/src/h1/service.rs

347 lines
9.3 KiB
Rust
Raw Normal View History

2019-04-06 01:46:44 +02:00
use std::fmt;
2018-10-05 05:02:10 +02:00
use std::marker::PhantomData;
2018-12-11 03:08:33 +01:00
use actix_codec::{AsyncRead, AsyncWrite, Framed};
2019-03-11 23:09:42 +01:00
use actix_server_config::{Io, ServerConfig as SrvConfig};
2018-12-11 03:08:33 +01:00
use actix_service::{IntoNewService, NewService, Service};
use actix_utils::cloneable::CloneableService;
2018-10-16 00:56:47 +02:00
use futures::future::{ok, FutureResult};
2019-03-05 05:46:33 +01:00
use futures::{try_ready, Async, Future, IntoFuture, Poll, Stream};
2018-10-05 05:02:10 +02:00
2018-12-06 23:32:52 +01:00
use crate::body::MessageBody;
use crate::config::{KeepAlive, ServiceConfig};
2019-04-06 01:46:44 +02:00
use crate::error::{DispatchError, Error, ParseError};
2018-12-06 23:32:52 +01:00
use crate::request::Request;
use crate::response::Response;
2018-10-05 05:02:10 +02:00
use super::codec::Codec;
2018-10-05 05:02:10 +02:00
use super::dispatcher::Dispatcher;
2019-04-06 01:46:44 +02:00
use super::{ExpectHandler, Message};
2018-10-05 05:02:10 +02:00
/// `NewService` implementation for HTTP1 transport
2019-04-06 01:46:44 +02:00
pub struct H1Service<T, P, S, B, X = ExpectHandler> {
2018-10-05 05:02:10 +02:00
srv: S,
cfg: ServiceConfig,
2019-04-06 01:46:44 +02:00
expect: X,
2019-03-11 23:09:42 +01:00
_t: PhantomData<(T, P, B)>,
2018-10-05 05:02:10 +02:00
}
2019-03-11 23:09:42 +01:00
impl<T, P, S, B> H1Service<T, P, S, B>
2018-10-05 05:02:10 +02:00
where
S: NewService<SrvConfig, Request = Request>,
2019-04-06 01:46:44 +02:00
S::Error: Into<Error>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
2018-11-18 05:21:28 +01:00
B: MessageBody,
2018-10-05 05:02:10 +02:00
{
/// Create new `HttpService` instance with default config.
pub fn new<F: IntoNewService<S, SrvConfig>>(service: F) -> Self {
2018-10-07 17:28:38 +02:00
let cfg = ServiceConfig::new(KeepAlive::Timeout(5), 5000, 0);
2018-10-05 05:02:10 +02:00
H1Service {
cfg,
srv: service.into_new_service(),
2019-04-06 01:46:44 +02:00
expect: ExpectHandler,
_t: PhantomData,
}
}
/// Create new `HttpService` instance with config.
pub fn with_config<F: IntoNewService<S, SrvConfig>>(
2019-03-05 18:30:11 +01:00
cfg: ServiceConfig,
service: F,
) -> Self {
H1Service {
cfg,
srv: service.into_new_service(),
2019-04-06 01:46:44 +02:00
expect: ExpectHandler,
2018-10-05 05:02:10 +02:00
_t: PhantomData,
}
}
}
2019-04-06 01:46:44 +02:00
impl<T, P, S, B, X> H1Service<T, P, S, B, X>
where
S: NewService<SrvConfig, Request = Request>,
S::Error: Into<Error>,
S::Response: Into<Response<B>>,
S::InitError: fmt::Debug,
B: MessageBody,
{
pub fn expect<U>(self, expect: U) -> H1Service<T, P, S, B, U>
where
U: NewService<Request = Request, Response = Request>,
U::Error: Into<Error>,
U::InitError: fmt::Debug,
{
H1Service {
expect,
cfg: self.cfg,
srv: self.srv,
_t: PhantomData,
}
}
}
impl<T, P, S, B, X> NewService<SrvConfig> for H1Service<T, P, S, B, X>
2018-10-05 05:02:10 +02:00
where
T: AsyncRead + AsyncWrite,
S: NewService<SrvConfig, Request = Request>,
2019-04-06 01:46:44 +02:00
S::Error: Into<Error>,
S::Response: Into<Response<B>>,
2019-04-06 01:46:44 +02:00
S::InitError: fmt::Debug,
2018-11-18 05:21:28 +01:00
B: MessageBody,
2019-04-06 01:46:44 +02:00
X: NewService<Request = Request, Response = Request>,
X::Error: Into<Error>,
X::InitError: fmt::Debug,
2018-10-05 05:02:10 +02:00
{
2019-03-11 23:09:42 +01:00
type Request = Io<T, P>;
2019-03-07 07:56:34 +01:00
type Response = ();
type Error = DispatchError;
2019-04-06 01:46:44 +02:00
type InitError = ();
type Service = H1ServiceHandler<T, P, S::Service, B, X::Service>;
type Future = H1ServiceResponse<T, P, S, B, X>;
2018-10-05 05:02:10 +02:00
fn new_service(&self, cfg: &SrvConfig) -> Self::Future {
2018-10-05 05:02:10 +02:00
H1ServiceResponse {
fut: self.srv.new_service(cfg).into_future(),
2019-04-06 01:46:44 +02:00
fut_ex: Some(self.expect.new_service(&())),
expect: None,
2018-10-05 05:02:10 +02:00
cfg: Some(self.cfg.clone()),
_t: PhantomData,
}
}
}
2018-10-16 00:56:47 +02:00
#[doc(hidden)]
2019-04-06 01:46:44 +02:00
pub struct H1ServiceResponse<T, P, S, B, X>
where
S: NewService<SrvConfig, Request = Request>,
S::Error: Into<Error>,
S::InitError: fmt::Debug,
X: NewService<Request = Request, Response = Request>,
X::Error: Into<Error>,
X::InitError: fmt::Debug,
{
fut: S::Future,
fut_ex: Option<X::Future>,
expect: Option<X::Service>,
2018-10-05 05:02:10 +02:00
cfg: Option<ServiceConfig>,
2019-03-11 23:09:42 +01:00
_t: PhantomData<(T, P, B)>,
2018-10-05 05:02:10 +02:00
}
2019-04-06 01:46:44 +02:00
impl<T, P, S, B, X> Future for H1ServiceResponse<T, P, S, B, X>
2018-10-05 05:02:10 +02:00
where
T: AsyncRead + AsyncWrite,
S: NewService<SrvConfig, Request = Request>,
2019-04-06 01:46:44 +02:00
S::Error: Into<Error>,
S::Response: Into<Response<B>>,
2019-04-06 01:46:44 +02:00
S::InitError: fmt::Debug,
2018-11-18 05:21:28 +01:00
B: MessageBody,
2019-04-06 01:46:44 +02:00
X: NewService<Request = Request, Response = Request>,
X::Error: Into<Error>,
X::InitError: fmt::Debug,
2018-10-05 05:02:10 +02:00
{
2019-04-06 01:46:44 +02:00
type Item = H1ServiceHandler<T, P, S::Service, B, X::Service>;
type Error = ();
2018-10-05 05:02:10 +02:00
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
2019-04-06 01:46:44 +02:00
if let Some(ref mut fut) = self.fut_ex {
let expect = try_ready!(fut
.poll()
.map_err(|e| log::error!("Init http service error: {:?}", e)));
self.expect = Some(expect);
self.fut_ex.take();
}
let service = try_ready!(self
.fut
.poll()
.map_err(|e| log::error!("Init http service error: {:?}", e)));
2018-10-05 05:02:10 +02:00
Ok(Async::Ready(H1ServiceHandler::new(
self.cfg.take().unwrap(),
service,
2019-04-06 01:46:44 +02:00
self.expect.take().unwrap(),
2018-10-05 05:02:10 +02:00
)))
}
}
/// `Service` implementation for HTTP1 transport
2019-04-06 01:46:44 +02:00
pub struct H1ServiceHandler<T, P, S, B, X> {
srv: CloneableService<S>,
2019-04-06 01:46:44 +02:00
expect: CloneableService<X>,
2018-10-05 05:02:10 +02:00
cfg: ServiceConfig,
2019-03-11 23:09:42 +01:00
_t: PhantomData<(T, P, B)>,
2018-10-05 05:02:10 +02:00
}
2019-04-06 01:46:44 +02:00
impl<T, P, S, B, X> H1ServiceHandler<T, P, S, B, X>
2018-10-05 05:02:10 +02:00
where
S: Service<Request = Request>,
2019-04-06 01:46:44 +02:00
S::Error: Into<Error>,
S::Response: Into<Response<B>>,
2018-11-18 05:21:28 +01:00
B: MessageBody,
2019-04-06 01:46:44 +02:00
X: Service<Request = Request, Response = Request>,
X::Error: Into<Error>,
2018-10-05 05:02:10 +02:00
{
2019-04-06 01:46:44 +02:00
fn new(cfg: ServiceConfig, srv: S, expect: X) -> H1ServiceHandler<T, P, S, B, X> {
2018-10-05 05:02:10 +02:00
H1ServiceHandler {
srv: CloneableService::new(srv),
2019-04-06 01:46:44 +02:00
expect: CloneableService::new(expect),
2018-10-05 05:02:10 +02:00
cfg,
_t: PhantomData,
}
}
}
2019-04-06 01:46:44 +02:00
impl<T, P, S, B, X> Service for H1ServiceHandler<T, P, S, B, X>
2018-10-05 05:02:10 +02:00
where
T: AsyncRead + AsyncWrite,
S: Service<Request = Request>,
2019-04-06 01:46:44 +02:00
S::Error: Into<Error>,
S::Response: Into<Response<B>>,
2018-11-18 05:21:28 +01:00
B: MessageBody,
2019-04-06 01:46:44 +02:00
X: Service<Request = Request, Response = Request>,
X::Error: Into<Error>,
2018-10-05 05:02:10 +02:00
{
2019-03-11 23:09:42 +01:00
type Request = Io<T, P>;
2019-03-07 07:56:34 +01:00
type Response = ();
type Error = DispatchError;
2019-04-06 01:46:44 +02:00
type Future = Dispatcher<T, S, B, X>;
2018-10-05 05:02:10 +02:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
2019-04-06 01:46:44 +02:00
let ready = self
.expect
.poll_ready()
.map_err(|e| {
let e = e.into();
log::error!("Http service readiness error: {:?}", e);
DispatchError::Service(e)
})?
.is_ready();
let ready = self
.srv
.poll_ready()
.map_err(|e| {
let e = e.into();
log::error!("Http service readiness error: {:?}", e);
DispatchError::Service(e)
})?
.is_ready()
&& ready;
if ready {
Ok(Async::Ready(()))
} else {
Ok(Async::NotReady)
}
2018-10-05 05:02:10 +02:00
}
2019-03-11 23:09:42 +01:00
fn call(&mut self, req: Self::Request) -> Self::Future {
2019-04-06 01:46:44 +02:00
Dispatcher::new(
req.into_parts().0,
self.cfg.clone(),
self.srv.clone(),
self.expect.clone(),
)
2018-10-05 05:02:10 +02:00
}
}
2018-10-16 00:56:47 +02:00
2018-10-16 01:46:13 +02:00
/// `NewService` implementation for `OneRequestService` service
2019-01-29 19:14:00 +01:00
#[derive(Default)]
2019-03-11 23:09:42 +01:00
pub struct OneRequest<T, P> {
2018-10-16 00:56:47 +02:00
config: ServiceConfig,
2019-03-11 23:09:42 +01:00
_t: PhantomData<(T, P)>,
2018-10-16 00:56:47 +02:00
}
2019-03-11 23:09:42 +01:00
impl<T, P> OneRequest<T, P>
2018-10-22 18:59:20 +02:00
where
T: AsyncRead + AsyncWrite,
{
2018-10-16 00:56:47 +02:00
/// Create new `H1SimpleService` instance.
pub fn new() -> Self {
2018-10-16 01:46:13 +02:00
OneRequest {
2018-10-16 00:56:47 +02:00
config: ServiceConfig::default(),
_t: PhantomData,
}
}
}
2019-03-11 23:09:42 +01:00
impl<T, P> NewService<SrvConfig> for OneRequest<T, P>
2018-10-16 00:56:47 +02:00
where
T: AsyncRead + AsyncWrite,
{
2019-03-11 23:09:42 +01:00
type Request = Io<T, P>;
2018-10-16 00:56:47 +02:00
type Response = (Request, Framed<T, Codec>);
type Error = ParseError;
type InitError = ();
2019-03-11 23:09:42 +01:00
type Service = OneRequestService<T, P>;
2018-10-16 00:56:47 +02:00
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &SrvConfig) -> Self::Future {
2018-10-16 01:46:13 +02:00
ok(OneRequestService {
2018-10-16 00:56:47 +02:00
config: self.config.clone(),
_t: PhantomData,
})
}
}
/// `Service` implementation for HTTP1 transport. Reads one request and returns
/// request and framed object.
2019-03-11 23:09:42 +01:00
pub struct OneRequestService<T, P> {
2018-10-16 00:56:47 +02:00
config: ServiceConfig,
2019-03-11 23:09:42 +01:00
_t: PhantomData<(T, P)>,
2018-10-16 00:56:47 +02:00
}
2019-03-11 23:09:42 +01:00
impl<T, P> Service for OneRequestService<T, P>
2018-10-16 00:56:47 +02:00
where
T: AsyncRead + AsyncWrite,
{
2019-03-11 23:09:42 +01:00
type Request = Io<T, P>;
2018-10-16 00:56:47 +02:00
type Response = (Request, Framed<T, Codec>);
type Error = ParseError;
2018-10-16 01:46:13 +02:00
type Future = OneRequestServiceResponse<T>;
2018-10-16 00:56:47 +02:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
2019-03-11 23:09:42 +01:00
fn call(&mut self, req: Self::Request) -> Self::Future {
2018-10-16 01:46:13 +02:00
OneRequestServiceResponse {
2019-03-11 23:09:42 +01:00
framed: Some(Framed::new(
req.into_parts().0,
Codec::new(self.config.clone()),
)),
2018-10-16 00:56:47 +02:00
}
}
}
#[doc(hidden)]
2018-10-16 01:46:13 +02:00
pub struct OneRequestServiceResponse<T>
2018-10-16 00:56:47 +02:00
where
T: AsyncRead + AsyncWrite,
{
framed: Option<Framed<T, Codec>>,
}
2018-10-16 01:46:13 +02:00
impl<T> Future for OneRequestServiceResponse<T>
2018-10-16 00:56:47 +02:00
where
T: AsyncRead + AsyncWrite,
{
type Item = (Request, Framed<T, Codec>);
type Error = ParseError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self.framed.as_mut().unwrap().poll()? {
Async::Ready(Some(req)) => match req {
Message::Item(req) => {
2018-10-16 00:56:47 +02:00
Ok(Async::Ready((req, self.framed.take().unwrap())))
}
Message::Chunk(_) => unreachable!("Something is wrong"),
2018-10-16 00:56:47 +02:00
},
Async::Ready(None) => Err(ParseError::Incomplete),
Async::NotReady => Ok(Async::NotReady),
}
}
}