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

255 lines
6.7 KiB
Rust
Raw Normal View History

2018-10-07 16:48:53 +00:00
use std::fmt::Debug;
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};
use crate::error::{DispatchError, ParseError};
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-03-07 06:56:34 +00:00
use super::Message;
2018-10-05 03:02:10 +00:00
/// `NewService` implementation for HTTP1 transport
2019-03-11 22:09:42 +00:00
pub struct H1Service<T, P, S, B> {
2018-10-05 03:02:10 +00:00
srv: S,
cfg: ServiceConfig,
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>,
2018-10-07 16:48:53 +00:00
S::Error: 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(),
_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(),
2018-10-05 03:02:10 +00:00
_t: PhantomData,
}
}
}
2019-03-11 22:09:42 +00:00
impl<T, P, S, B> NewService<SrvConfig> for H1Service<T, P, S, B>
2018-10-05 03:02:10 +00:00
where
T: AsyncRead + AsyncWrite,
S: NewService<SrvConfig, Request = Request>,
2018-10-07 16:48:53 +00:00
S::Error: Debug,
S::Response: Into<Response<B>>,
2018-11-18 04:21:28 +00:00
B: MessageBody,
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;
2018-10-05 03:02:10 +00:00
type InitError = S::InitError;
2019-03-11 22:09:42 +00:00
type Service = H1ServiceHandler<T, P, S::Service, B>;
type Future = H1ServiceResponse<T, P, S, B>;
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(),
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-03-11 22:09:42 +00:00
pub struct H1ServiceResponse<T, P, S: NewService<SrvConfig, Request = Request>, B> {
2019-03-05 04:46:33 +00:00
fut: <S::Future as IntoFuture>::Future,
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-03-11 22:09:42 +00:00
impl<T, P, S, B> Future for H1ServiceResponse<T, P, S, B>
2018-10-05 03:02:10 +00:00
where
T: AsyncRead + AsyncWrite,
S: NewService<SrvConfig, Request = Request>,
2018-10-07 16:48:53 +00:00
S::Error: Debug,
S::Response: Into<Response<B>>,
2018-11-18 04:21:28 +00:00
B: MessageBody,
2018-10-05 03:02:10 +00:00
{
2019-03-11 22:09:42 +00:00
type Item = H1ServiceHandler<T, P, S::Service, B>;
2018-10-05 03:02:10 +00: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
2019-04-04 17:59:34 +00:00
pub struct H1ServiceHandler<T, P, S, B> {
srv: CloneableService<S>,
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-03-11 22:09:42 +00:00
impl<T, P, S, B> H1ServiceHandler<T, P, S, B>
2018-10-05 03:02:10 +00:00
where
S: Service<Request = Request>,
2018-10-07 16:48:53 +00:00
S::Error: Debug,
S::Response: Into<Response<B>>,
2018-11-18 04:21:28 +00:00
B: MessageBody,
2018-10-05 03:02:10 +00:00
{
2019-03-11 22:09:42 +00:00
fn new(cfg: ServiceConfig, srv: S) -> H1ServiceHandler<T, P, S, B> {
2018-10-05 03:02:10 +00:00
H1ServiceHandler {
srv: CloneableService::new(srv),
2018-10-05 03:02:10 +00:00
cfg,
_t: PhantomData,
}
}
}
2019-03-11 22:09:42 +00:00
impl<T, P, S, B> Service for H1ServiceHandler<T, P, S, B>
2018-10-05 03:02:10 +00:00
where
T: AsyncRead + AsyncWrite,
S: Service<Request = Request>,
2018-10-07 16:48:53 +00:00
S::Error: Debug,
S::Response: Into<Response<B>>,
2018-11-18 04:21:28 +00:00
B: MessageBody,
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;
2018-11-18 04:21:28 +00:00
type Future = Dispatcher<T, S, B>;
2018-10-05 03:02:10 +00:00
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
2019-03-07 06:56:34 +00:00
self.srv.poll_ready().map_err(|e| {
log::error!("Http service readiness error: {:?}", e);
DispatchError::Service
})
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 {
Dispatcher::new(req.into_parts().0, self.cfg.clone(), self.srv.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),
}
}
}