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

255 lines
6.6 KiB
Rust
Raw Normal View History

2018-10-07 18:48:53 +02:00
use std::fmt::Debug;
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};
use actix_server_config::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};
use crate::error::{DispatchError, ParseError};
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-03-07 07:56:34 +01:00
use super::Message;
2018-10-05 05:02:10 +02:00
/// `NewService` implementation for HTTP1 transport
2018-11-18 05:21:28 +01:00
pub struct H1Service<T, S, B> {
2018-10-05 05:02:10 +02:00
srv: S,
cfg: ServiceConfig,
2018-11-18 05:21:28 +01:00
_t: PhantomData<(T, B)>,
2018-10-05 05:02:10 +02:00
}
2018-11-18 05:21:28 +01:00
impl<T, S, B> H1Service<T, S, B>
2018-10-05 05:02:10 +02:00
where
S: NewService<SrvConfig, Request = Request>,
2018-10-07 18:48:53 +02:00
S::Error: Debug,
S::Response: Into<Response<B>>,
S::Service: 'static,
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(),
_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(),
2018-10-05 05:02:10 +02:00
_t: PhantomData,
}
}
}
impl<T, S, B> NewService<SrvConfig> for H1Service<T, S, B>
2018-10-05 05:02:10 +02:00
where
T: AsyncRead + AsyncWrite,
S: NewService<SrvConfig, Request = Request>,
2018-10-07 18:48:53 +02:00
S::Error: Debug,
S::Response: Into<Response<B>>,
S::Service: 'static,
2018-11-18 05:21:28 +01:00
B: MessageBody,
2018-10-05 05:02:10 +02:00
{
type Request = T;
2019-03-07 07:56:34 +01:00
type Response = ();
type Error = DispatchError;
2018-10-05 05:02:10 +02:00
type InitError = S::InitError;
2018-11-18 05:21:28 +01:00
type Service = H1ServiceHandler<T, S::Service, B>;
type Future = H1ServiceResponse<T, S, B>;
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(),
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)]
pub struct H1ServiceResponse<T, S: NewService<SrvConfig, Request = Request>, B> {
2019-03-05 05:46:33 +01:00
fut: <S::Future as IntoFuture>::Future,
2018-10-05 05:02:10 +02:00
cfg: Option<ServiceConfig>,
2018-11-18 05:21:28 +01:00
_t: PhantomData<(T, B)>,
2018-10-05 05:02:10 +02:00
}
2018-11-18 05:21:28 +01:00
impl<T, S, B> Future for H1ServiceResponse<T, S, B>
2018-10-05 05:02:10 +02:00
where
T: AsyncRead + AsyncWrite,
S: NewService<SrvConfig, Request = Request>,
S::Service: 'static,
2018-10-07 18:48:53 +02:00
S::Error: Debug,
S::Response: Into<Response<B>>,
2018-11-18 05:21:28 +01:00
B: MessageBody,
2018-10-05 05:02:10 +02:00
{
2018-11-18 05:21:28 +01:00
type Item = H1ServiceHandler<T, S::Service, B>;
2018-10-05 05:02:10 +02:00
type Error = S::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let service = try_ready!(self.fut.poll());
Ok(Async::Ready(H1ServiceHandler::new(
self.cfg.take().unwrap(),
service,
)))
}
}
/// `Service` implementation for HTTP1 transport
pub struct H1ServiceHandler<T, S: 'static, B> {
srv: CloneableService<S>,
2018-10-05 05:02:10 +02:00
cfg: ServiceConfig,
2018-11-18 05:21:28 +01:00
_t: PhantomData<(T, B)>,
2018-10-05 05:02:10 +02:00
}
2018-11-18 05:21:28 +01:00
impl<T, S, B> H1ServiceHandler<T, S, B>
2018-10-05 05:02:10 +02:00
where
S: Service<Request = Request>,
2018-10-07 18:48:53 +02:00
S::Error: Debug,
S::Response: Into<Response<B>>,
2018-11-18 05:21:28 +01:00
B: MessageBody,
2018-10-05 05:02:10 +02:00
{
2018-11-18 05:21:28 +01:00
fn new(cfg: ServiceConfig, srv: S) -> H1ServiceHandler<T, S, B> {
2018-10-05 05:02:10 +02:00
H1ServiceHandler {
srv: CloneableService::new(srv),
2018-10-05 05:02:10 +02:00
cfg,
_t: PhantomData,
}
}
}
impl<T, S, B> Service for H1ServiceHandler<T, S, B>
2018-10-05 05:02:10 +02:00
where
T: AsyncRead + AsyncWrite,
S: Service<Request = Request>,
2018-10-07 18:48:53 +02:00
S::Error: Debug,
S::Response: Into<Response<B>>,
2018-11-18 05:21:28 +01:00
B: MessageBody,
2018-10-05 05:02:10 +02:00
{
type Request = T;
2019-03-07 07:56:34 +01:00
type Response = ();
type Error = DispatchError;
2018-11-18 05:21:28 +01:00
type Future = Dispatcher<T, S, B>;
2018-10-05 05:02:10 +02:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
2019-03-07 07:56:34 +01:00
self.srv.poll_ready().map_err(|e| {
log::error!("Http service readiness error: {:?}", e);
DispatchError::Service
})
2018-10-05 05:02:10 +02:00
}
2018-11-30 20:57:57 +01:00
fn call(&mut self, req: T) -> Self::Future {
2018-10-05 06:14:18 +02:00
Dispatcher::new(req, self.cfg.clone(), self.srv.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)]
2018-10-16 01:46:13 +02:00
pub struct OneRequest<T> {
2018-10-16 00:56:47 +02:00
config: ServiceConfig,
_t: PhantomData<T>,
}
2018-10-22 18:59:20 +02:00
impl<T> OneRequest<T>
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,
}
}
}
impl<T> NewService<SrvConfig> for OneRequest<T>
2018-10-16 00:56:47 +02:00
where
T: AsyncRead + AsyncWrite,
{
type Request = T;
2018-10-16 00:56:47 +02:00
type Response = (Request, Framed<T, Codec>);
type Error = ParseError;
type InitError = ();
2018-10-16 01:46:13 +02:00
type Service = OneRequestService<T>;
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.
2018-10-16 01:46:13 +02:00
pub struct OneRequestService<T> {
2018-10-16 00:56:47 +02:00
config: ServiceConfig,
_t: PhantomData<T>,
}
impl<T> Service for OneRequestService<T>
2018-10-16 00:56:47 +02:00
where
T: AsyncRead + AsyncWrite,
{
type Request = T;
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(()))
}
2018-11-30 20:57:57 +01:00
fn call(&mut self, req: T) -> Self::Future {
2018-10-16 01:46:13 +02:00
OneRequestServiceResponse {
2018-10-16 00:56:47 +02:00
framed: Some(Framed::new(req, Codec::new(self.config.clone()))),
}
}
}
#[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),
}
}
}