1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-02-01 03:20:07 +01:00
actix-web/actix-http/src/h1/service.rs

411 lines
12 KiB
Rust
Raw Normal View History

2018-10-04 20:02:10 -07:00
use std::marker::PhantomData;
2019-06-28 14:34:26 +06:00
use std::rc::Rc;
use std::task::{Context, Poll};
2019-12-02 17:33:11 +06:00
use std::{fmt, net};
2018-10-04 20:02:10 -07:00
2019-12-02 17:33:11 +06:00
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_rt::net::TcpStream;
use actix_service::{pipeline_factory, IntoServiceFactory, Service, ServiceFactory};
use futures_core::{future::LocalBoxFuture, ready};
use futures_util::future::ready;
2018-10-04 20:02:10 -07:00
2018-12-06 14:32:52 -08:00
use crate::body::MessageBody;
2019-12-02 17:33:11 +06:00
use crate::config::ServiceConfig;
use crate::error::{DispatchError, Error};
2018-12-06 14:32:52 -08:00
use crate::request::Request;
use crate::response::Response;
use crate::service::HttpFlow;
use crate::{ConnectCallback, OnConnectData};
2018-10-04 20:02:10 -07:00
use super::codec::Codec;
2018-10-04 20:02:10 -07:00
use super::dispatcher::Dispatcher;
use super::{ExpectHandler, UpgradeHandler};
2018-10-04 20:02:10 -07:00
/// `ServiceFactory` implementation for HTTP1 transport
pub struct H1Service<T, S, B, X = ExpectHandler, U = UpgradeHandler> {
2018-10-04 20:02:10 -07:00
srv: S,
cfg: ServiceConfig,
2019-04-05 16:46:44 -07:00
expect: X,
2019-04-08 14:51:16 -07:00
upgrade: Option<U>,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData<B>,
2018-10-04 20:02:10 -07:00
}
2019-12-02 17:33:11 +06:00
impl<T, S, B> H1Service<T, S, B>
2018-10-04 20:02:10 -07:00
where
S: ServiceFactory<Request, Config = ()>,
2019-04-05 16:46:44 -07:00
S::Error: Into<Error>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
2018-11-17 20:21:28 -08:00
B: MessageBody,
2018-10-04 20:02:10 -07:00
{
/// Create new `HttpService` instance with config.
pub(crate) fn with_config<F: IntoServiceFactory<S, Request>>(
cfg: ServiceConfig,
service: F,
) -> Self {
H1Service {
cfg,
srv: service.into_factory(),
2019-04-05 16:46:44 -07:00
expect: ExpectHandler,
2019-04-08 14:51:16 -07:00
upgrade: None,
on_connect_ext: None,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
2018-10-04 20:02:10 -07:00
}
}
}
2019-12-02 17:33:11 +06:00
impl<S, B, X, U> H1Service<TcpStream, S, B, X, U>
2019-04-05 16:46:44 -07:00
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2019-12-02 17:33:11 +06:00
S::Error: Into<Error>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
B: MessageBody,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
2019-12-02 17:33:11 +06:00
X::Error: Into<Error>,
X::InitError: fmt::Debug,
U: ServiceFactory<(Request, Framed<TcpStream, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
2019-12-02 17:33:11 +06:00
U::InitError: fmt::Debug,
{
/// Create simple tcp stream service
pub fn tcp(
self,
) -> impl ServiceFactory<
TcpStream,
2019-12-02 17:33:11 +06:00
Config = (),
Response = (),
Error = DispatchError,
InitError = (),
> {
pipeline_factory(|io: TcpStream| {
let peer_addr = io.peer_addr().ok();
ready(Ok((io, peer_addr)))
2019-12-02 17:33:11 +06:00
})
.and_then(self)
}
}
#[cfg(feature = "openssl")]
mod openssl {
use super::*;
use actix_service::ServiceFactoryExt;
use actix_tls::accept::{
openssl::{Acceptor, SslAcceptor, SslError, TlsStream},
TlsError,
};
2019-12-02 17:33:11 +06:00
2021-02-27 19:57:09 +00:00
impl<S, B, X, U> H1Service<TlsStream<TcpStream>, S, B, X, U>
2019-12-02 17:33:11 +06:00
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2019-12-02 17:33:11 +06:00
S::Error: Into<Error>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
B: MessageBody,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
2019-12-02 17:33:11 +06:00
X::Error: Into<Error>,
X::InitError: fmt::Debug,
U: ServiceFactory<
2021-02-27 19:57:09 +00:00
(Request, Framed<TlsStream<TcpStream>, Codec>),
2019-12-02 17:33:11 +06:00
Config = (),
Response = (),
>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
2019-12-02 17:33:11 +06:00
U::InitError: fmt::Debug,
{
/// Create openssl based service
pub fn openssl(
self,
acceptor: SslAcceptor,
) -> impl ServiceFactory<
TcpStream,
2019-12-02 17:33:11 +06:00
Config = (),
Response = (),
Error = TlsError<SslError, DispatchError>,
2019-12-02 17:33:11 +06:00
InitError = (),
> {
pipeline_factory(
Acceptor::new(acceptor)
2020-09-09 09:20:54 +01:00
.map_err(TlsError::Tls)
2019-12-02 17:33:11 +06:00
.map_init_err(|_| panic!()),
)
2021-02-27 19:57:09 +00:00
.and_then(|io: TlsStream<TcpStream>| {
2019-12-02 17:33:11 +06:00
let peer_addr = io.get_ref().peer_addr().ok();
ready(Ok((io, peer_addr)))
2019-12-02 17:33:11 +06:00
})
2020-09-09 09:20:54 +01:00
.and_then(self.map_err(TlsError::Service))
2019-12-02 17:33:11 +06:00
}
}
}
2019-12-05 23:35:43 +06:00
#[cfg(feature = "rustls")]
mod rustls {
use super::*;
use std::io;
use actix_service::ServiceFactoryExt;
use actix_tls::accept::{
rustls::{Acceptor, ServerConfig, TlsStream},
TlsError,
};
2019-12-05 23:35:43 +06:00
impl<S, B, X, U> H1Service<TlsStream<TcpStream>, S, B, X, U>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2019-12-05 23:35:43 +06:00
S::Error: Into<Error>,
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
B: MessageBody,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
2019-12-05 23:35:43 +06:00
X::Error: Into<Error>,
X::InitError: fmt::Debug,
U: ServiceFactory<
(Request, Framed<TlsStream<TcpStream>, Codec>),
2019-12-05 23:35:43 +06:00
Config = (),
Response = (),
>,
U::Future: 'static,
2019-12-20 13:50:07 +06:00
U::Error: fmt::Display + Into<Error>,
2019-12-05 23:35:43 +06:00
U::InitError: fmt::Debug,
{
/// Create rustls based service
pub fn rustls(
self,
config: ServerConfig,
) -> impl ServiceFactory<
TcpStream,
2019-12-05 23:35:43 +06:00
Config = (),
Response = (),
2020-09-09 09:20:54 +01:00
Error = TlsError<io::Error, DispatchError>,
2019-12-05 23:35:43 +06:00
InitError = (),
> {
pipeline_factory(
Acceptor::new(config)
2020-09-09 09:20:54 +01:00
.map_err(TlsError::Tls)
2019-12-05 23:35:43 +06:00
.map_init_err(|_| panic!()),
)
.and_then(|io: TlsStream<TcpStream>| {
let peer_addr = io.get_ref().0.peer_addr().ok();
ready(Ok((io, peer_addr)))
2019-12-05 23:35:43 +06:00
})
2020-09-09 09:20:54 +01:00
.and_then(self.map_err(TlsError::Service))
2019-12-05 23:35:43 +06:00
}
}
}
2019-12-02 17:33:11 +06:00
impl<T, S, B, X, U> H1Service<T, S, B, X, U>
where
S: ServiceFactory<Request, Config = ()>,
2019-04-05 16:46:44 -07:00
S::Error: Into<Error>,
S::Response: Into<Response<B>>,
S::InitError: fmt::Debug,
B: MessageBody,
{
2019-12-02 17:33:11 +06:00
pub fn expect<X1>(self, expect: X1) -> H1Service<T, S, B, X1, U>
2019-04-05 16:46:44 -07:00
where
X1: ServiceFactory<Request, Response = Request>,
2019-04-08 14:51:16 -07:00
X1::Error: Into<Error>,
X1::InitError: fmt::Debug,
2019-04-05 16:46:44 -07:00
{
H1Service {
expect,
cfg: self.cfg,
srv: self.srv,
2019-04-08 14:51:16 -07:00
upgrade: self.upgrade,
on_connect_ext: self.on_connect_ext,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
2019-04-08 14:51:16 -07:00
}
}
2019-12-02 17:33:11 +06:00
pub fn upgrade<U1>(self, upgrade: Option<U1>) -> H1Service<T, S, B, X, U1>
2019-04-08 14:51:16 -07:00
where
U1: ServiceFactory<(Request, Framed<T, Codec>), Response = ()>,
2019-04-08 14:51:16 -07:00
U1::Error: fmt::Display,
U1::InitError: fmt::Debug,
{
H1Service {
upgrade,
cfg: self.cfg,
srv: self.srv,
expect: self.expect,
on_connect_ext: self.on_connect_ext,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
2019-04-05 16:46:44 -07:00
}
}
2019-06-28 14:34:26 +06:00
/// Set on connect callback.
pub(crate) fn on_connect_ext(mut self, f: Option<Rc<ConnectCallback<T>>>) -> Self {
self.on_connect_ext = f;
self
}
2019-04-05 16:46:44 -07:00
}
impl<T, S, B, X, U> ServiceFactory<(T, Option<net::SocketAddr>)>
for H1Service<T, S, B, X, U>
2018-10-04 20:02:10 -07:00
where
T: AsyncRead + AsyncWrite + Unpin + 'static,
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2019-04-05 16:46:44 -07:00
S::Error: Into<Error>,
S::Response: Into<Response<B>>,
2019-04-05 16:46:44 -07:00
S::InitError: fmt::Debug,
2018-11-17 20:21:28 -08:00
B: MessageBody,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
2019-04-05 16:46:44 -07:00
X::Error: Into<Error>,
X::InitError: fmt::Debug,
U: ServiceFactory<(Request, Framed<T, Codec>), Config = (), Response = ()>,
U::Future: 'static,
U::Error: fmt::Display + Into<Error>,
2019-04-08 14:51:16 -07:00
U::InitError: fmt::Debug,
2018-10-04 20:02:10 -07:00
{
2019-03-06 22:56:34 -08:00
type Response = ();
type Error = DispatchError;
type Config = ();
2019-12-02 17:33:11 +06:00
type Service = H1ServiceHandler<T, S::Service, B, X::Service, U::Service>;
type InitError = ();
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
2018-10-04 20:02:10 -07:00
2019-12-02 21:37:13 +06:00
fn new_service(&self, _: ()) -> Self::Future {
let service = self.srv.new_service(());
let expect = self.expect.new_service(());
let upgrade = self.upgrade.as_ref().map(|s| s.new_service(()));
let on_connect_ext = self.on_connect_ext.clone();
let cfg = self.cfg.clone();
Box::pin(async move {
let expect = expect
.await
.map_err(|e| log::error!("Init http expect service error: {:?}", e))?;
let upgrade = match upgrade {
Some(upgrade) => {
let upgrade = upgrade.await.map_err(|e| {
log::error!("Init http upgrade service error: {:?}", e)
})?;
Some(upgrade)
}
None => None,
};
let service = service
.await
.map_err(|e| log::error!("Init http service error: {:?}", e))?;
Ok(H1ServiceHandler::new(
cfg,
service,
expect,
upgrade,
on_connect_ext,
))
})
2018-10-04 20:02:10 -07:00
}
}
/// `Service` implementation for HTTP/1 transport
pub struct H1ServiceHandler<T, S, B, X, U>
where
S: Service<Request>,
X: Service<Request>,
U: Service<(Request, Framed<T, Codec>)>,
{
flow: Rc<HttpFlow<S, X, U>>,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
2018-10-04 20:02:10 -07:00
cfg: ServiceConfig,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData<B>,
2018-10-04 20:02:10 -07:00
}
2019-12-02 17:33:11 +06:00
impl<T, S, B, X, U> H1ServiceHandler<T, S, B, X, U>
2018-10-04 20:02:10 -07:00
where
S: Service<Request>,
2019-04-05 16:46:44 -07:00
S::Error: Into<Error>,
S::Response: Into<Response<B>>,
2018-11-17 20:21:28 -08:00
B: MessageBody,
X: Service<Request, Response = Request>,
2019-04-05 16:46:44 -07:00
X::Error: Into<Error>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
2019-04-08 14:51:16 -07:00
U::Error: fmt::Display,
2018-10-04 20:02:10 -07:00
{
2019-04-08 14:51:16 -07:00
fn new(
cfg: ServiceConfig,
service: S,
2019-04-08 14:51:16 -07:00
expect: X,
upgrade: Option<U>,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
2019-12-02 17:33:11 +06:00
) -> H1ServiceHandler<T, S, B, X, U> {
2018-10-04 20:02:10 -07:00
H1ServiceHandler {
2021-01-06 18:52:06 +00:00
flow: HttpFlow::new(service, expect, upgrade),
2018-10-04 20:02:10 -07:00
cfg,
on_connect_ext,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
2018-10-04 20:02:10 -07:00
}
}
}
impl<T, S, B, X, U> Service<(T, Option<net::SocketAddr>)>
for H1ServiceHandler<T, S, B, X, U>
2018-10-04 20:02:10 -07:00
where
2019-12-02 17:33:11 +06:00
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
2019-04-05 16:46:44 -07:00
S::Error: Into<Error>,
S::Response: Into<Response<B>>,
2018-11-17 20:21:28 -08:00
B: MessageBody,
X: Service<Request, Response = Request>,
2019-04-05 16:46:44 -07:00
X::Error: Into<Error>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
U::Error: fmt::Display + Into<Error>,
2018-10-04 20:02:10 -07:00
{
2019-03-06 22:56:34 -08:00
type Response = ();
type Error = DispatchError;
2019-04-08 14:51:16 -07:00
type Future = Dispatcher<T, S, B, X, U>;
2018-10-04 20:02:10 -07:00
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
ready!(self.flow.expect.poll_ready(cx)).map_err(|e| {
let e = e.into();
log::error!("Http expect service readiness error: {:?}", e);
DispatchError::Service(e)
})?;
if let Some(ref upg) = self.flow.upgrade {
ready!(upg.poll_ready(cx)).map_err(|e| {
2019-04-05 16:46:44 -07:00
let e = e.into();
log::error!("Http upgrade service readiness error: {:?}", e);
2019-04-05 16:46:44 -07:00
DispatchError::Service(e)
})?;
};
2019-04-05 16:46:44 -07:00
ready!(self.flow.service.poll_ready(cx)).map_err(|e| {
let e = e.into();
log::error!("Http service readiness error: {:?}", e);
DispatchError::Service(e)
})?;
Poll::Ready(Ok(()))
2018-10-04 20:02:10 -07:00
}
fn call(&self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
let on_connect_data =
OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
2019-06-28 14:34:26 +06:00
2019-04-05 16:46:44 -07:00
Dispatcher::new(
2019-06-28 14:34:26 +06:00
io,
2019-04-05 16:46:44 -07:00
self.cfg.clone(),
2021-01-06 18:52:06 +00:00
self.flow.clone(),
on_connect_data,
2019-12-02 17:33:11 +06:00
addr,
2019-04-05 16:46:44 -07:00
)
2018-10-04 20:02:10 -07:00
}
}