1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00
actix-web/actix-http/src/h1/service.rs

347 lines
9.3 KiB
Rust
Raw Normal View History

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