1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-05-19 16:58:14 +00:00
actix-web/actix-http/src/h1/service.rs

364 lines
10 KiB
Rust
Raw Normal View History

2021-06-17 16:57:58 +00:00
use std::{
fmt,
marker::PhantomData,
net,
rc::Rc,
task::{Context, Poll},
};
2018-10-05 03:02:10 +00:00
2019-12-02 11:33:11 +00:00
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_rt::net::TcpStream;
2021-04-16 19:28:21 +00:00
use actix_service::{
fn_service, IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt as _,
};
2021-04-01 14:26:13 +00:00
use actix_utils::future::ready;
use futures_core::future::LocalBoxFuture;
2022-03-10 03:12:29 +00:00
use tracing::error;
2018-10-05 03:02:10 +00:00
2023-07-17 01:38:12 +00:00
use super::{codec::Codec, dispatcher::Dispatcher, ExpectHandler, UpgradeHandler};
2021-06-17 16:57:58 +00:00
use crate::{
2021-12-04 19:40:47 +00:00
body::{BoxBody, MessageBody},
2021-06-17 16:57:58 +00:00
config::ServiceConfig,
error::DispatchError,
service::HttpServiceHandler,
ConnectCallback, OnConnectData, Request, Response,
};
2018-10-05 03:02:10 +00:00
/// `ServiceFactory` implementation for HTTP1 transport
pub struct H1Service<T, S, B, X = ExpectHandler, U = UpgradeHandler> {
2018-10-05 03:02:10 +00:00
srv: S,
cfg: ServiceConfig,
2019-04-05 23:46:44 +00:00
expect: X,
2019-04-08 21:51:16 +00:00
upgrade: Option<U>,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData<B>,
2018-10-05 03:02:10 +00:00
}
2019-12-02 11:33:11 +00:00
impl<T, S, B> H1Service<T, S, B>
2018-10-05 03:02:10 +00:00
where
S: ServiceFactory<Request, Config = ()>,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>>,
2019-04-05 23:46:44 +00:00
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 config.
pub(crate) fn with_config<F: IntoServiceFactory<S, Request>>(
cfg: ServiceConfig,
service: F,
) -> Self {
H1Service {
cfg,
srv: service.into_factory(),
2019-04-05 23:46:44 +00:00
expect: ExpectHandler,
2019-04-08 21:51:16 +00:00
upgrade: None,
on_connect_ext: None,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
2018-10-05 03:02:10 +00:00
}
}
}
2019-12-02 11:33:11 +00:00
impl<S, B, X, U> H1Service<TcpStream, S, B, X, U>
2019-04-05 23:46:44 +00:00
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>>,
2019-12-02 11:33:11 +00:00
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
2019-12-02 11:33:11 +00:00
B: MessageBody,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
2021-12-04 19:40:47 +00:00
X::Error: Into<Response<BoxBody>>,
2019-12-02 11:33:11 +00:00
X::InitError: fmt::Debug,
U: ServiceFactory<(Request, Framed<TcpStream, Codec>), Config = (), Response = ()>,
U::Future: 'static,
2021-12-04 19:40:47 +00:00
U::Error: fmt::Display + Into<Response<BoxBody>>,
2019-12-02 11:33:11 +00:00
U::InitError: fmt::Debug,
{
/// Create simple tcp stream service
pub fn tcp(
self,
2023-07-17 01:38:12 +00:00
) -> impl ServiceFactory<TcpStream, Config = (), Response = (), Error = DispatchError, InitError = ()>
{
2021-04-16 19:28:21 +00:00
fn_service(|io: TcpStream| {
2019-12-02 11:33:11 +00:00
let peer_addr = io.peer_addr().ok();
ready(Ok((io, peer_addr)))
2019-12-02 11:33:11 +00:00
})
.and_then(self)
}
}
#[cfg(feature = "openssl")]
mod openssl {
use actix_tls::accept::{
2021-11-30 14:12:04 +00:00
openssl::{
reexports::{Error as SslError, SslAcceptor},
Acceptor, TlsStream,
},
TlsError,
};
2019-12-02 11:33:11 +00:00
2023-07-17 01:38:12 +00:00
use super::*;
2021-02-27 19:57:09 +00:00
impl<S, B, X, U> H1Service<TlsStream<TcpStream>, S, B, X, U>
2019-12-02 11:33:11 +00:00
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>>,
2019-12-02 11:33:11 +00:00
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
2019-12-02 11:33:11 +00:00
B: MessageBody,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
2021-12-04 19:40:47 +00:00
X::Error: Into<Response<BoxBody>>,
2019-12-02 11:33:11 +00:00
X::InitError: fmt::Debug,
2019-12-02 11:33:11 +00:00
U: ServiceFactory<
2021-02-27 19:57:09 +00:00
(Request, Framed<TlsStream<TcpStream>, Codec>),
2019-12-02 11:33:11 +00:00
Config = (),
Response = (),
>,
U::Future: 'static,
2021-12-04 19:40:47 +00:00
U::Error: fmt::Display + Into<Response<BoxBody>>,
2019-12-02 11:33:11 +00:00
U::InitError: fmt::Debug,
{
/// Create OpenSSL based service.
2019-12-02 11:33:11 +00:00
pub fn openssl(
self,
acceptor: SslAcceptor,
) -> impl ServiceFactory<
TcpStream,
2019-12-02 11:33:11 +00:00
Config = (),
Response = (),
Error = TlsError<SslError, DispatchError>,
2019-12-02 11:33:11 +00:00
InitError = (),
> {
2021-04-16 19:28:21 +00:00
Acceptor::new(acceptor)
.map_init_err(|_| {
unreachable!("TLS acceptor service factory does not error on init")
})
.map_err(TlsError::into_service_error)
.map(|io: TlsStream<TcpStream>| {
2021-04-16 19:28:21 +00:00
let peer_addr = io.get_ref().peer_addr().ok();
(io, peer_addr)
2021-04-16 19:28:21 +00:00
})
.and_then(self.map_err(TlsError::Service))
2019-12-02 11:33:11 +00:00
}
}
}
2019-12-05 17:35:43 +00:00
#[cfg(feature = "rustls")]
mod rustls {
use std::io;
use actix_service::ServiceFactoryExt as _;
use actix_tls::accept::{
2021-11-30 14:12:04 +00:00
rustls::{reexports::ServerConfig, Acceptor, TlsStream},
TlsError,
};
2019-12-05 17:35:43 +00:00
use super::*;
2019-12-05 17:35:43 +00:00
impl<S, B, X, U> H1Service<TlsStream<TcpStream>, S, B, X, U>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>>,
2019-12-05 17:35:43 +00:00
S::InitError: fmt::Debug,
S::Response: Into<Response<B>>,
2019-12-05 17:35:43 +00:00
B: MessageBody,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
2021-12-04 19:40:47 +00:00
X::Error: Into<Response<BoxBody>>,
2019-12-05 17:35:43 +00:00
X::InitError: fmt::Debug,
2019-12-05 17:35:43 +00:00
U: ServiceFactory<
(Request, Framed<TlsStream<TcpStream>, Codec>),
2019-12-05 17:35:43 +00:00
Config = (),
Response = (),
>,
U::Future: 'static,
2021-12-04 19:40:47 +00:00
U::Error: fmt::Display + Into<Response<BoxBody>>,
2019-12-05 17:35:43 +00:00
U::InitError: fmt::Debug,
{
/// Create Rustls based service.
2019-12-05 17:35:43 +00:00
pub fn rustls(
self,
config: ServerConfig,
) -> impl ServiceFactory<
TcpStream,
2019-12-05 17:35:43 +00:00
Config = (),
Response = (),
2020-09-09 08:20:54 +00:00
Error = TlsError<io::Error, DispatchError>,
2019-12-05 17:35:43 +00:00
InitError = (),
> {
2021-04-16 19:28:21 +00:00
Acceptor::new(config)
.map_init_err(|_| {
unreachable!("TLS acceptor service factory does not error on init")
})
.map_err(TlsError::into_service_error)
.map(|io: TlsStream<TcpStream>| {
2021-04-16 19:28:21 +00:00
let peer_addr = io.get_ref().0.peer_addr().ok();
(io, peer_addr)
2021-04-16 19:28:21 +00:00
})
.and_then(self.map_err(TlsError::Service))
2019-12-05 17:35:43 +00:00
}
}
}
2019-12-02 11:33:11 +00:00
impl<T, S, B, X, U> H1Service<T, S, B, X, U>
where
S: ServiceFactory<Request, Config = ()>,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>>,
2019-04-05 23:46:44 +00:00
S::Response: Into<Response<B>>,
S::InitError: fmt::Debug,
B: MessageBody,
{
2019-12-02 11:33:11 +00:00
pub fn expect<X1>(self, expect: X1) -> H1Service<T, S, B, X1, U>
2019-04-05 23:46:44 +00:00
where
X1: ServiceFactory<Request, Response = Request>,
2021-12-04 19:40:47 +00:00
X1::Error: Into<Response<BoxBody>>,
2019-04-08 21:51:16 +00:00
X1::InitError: fmt::Debug,
2019-04-05 23:46:44 +00:00
{
H1Service {
expect,
cfg: self.cfg,
srv: self.srv,
2019-04-08 21:51:16 +00:00
upgrade: self.upgrade,
on_connect_ext: self.on_connect_ext,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
2019-04-08 21:51:16 +00:00
}
}
2019-12-02 11:33:11 +00:00
pub fn upgrade<U1>(self, upgrade: Option<U1>) -> H1Service<T, S, B, X, U1>
2019-04-08 21:51:16 +00:00
where
U1: ServiceFactory<(Request, Framed<T, Codec>), Response = ()>,
2019-04-08 21:51:16 +00: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 23:46:44 +00:00
}
}
2019-06-28 08:34:26 +00: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 23:46:44 +00:00
}
2021-12-08 06:01:11 +00:00
impl<T, S, B, X, U> ServiceFactory<(T, Option<net::SocketAddr>)> for H1Service<T, S, B, X, U>
2018-10-05 03:02:10 +00:00
where
T: AsyncRead + AsyncWrite + Unpin + 'static,
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>>,
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,
X: ServiceFactory<Request, Config = (), Response = Request>,
X::Future: 'static,
2021-12-04 19:40:47 +00:00
X::Error: Into<Response<BoxBody>>,
2019-04-05 23:46:44 +00:00
X::InitError: fmt::Debug,
U: ServiceFactory<(Request, Framed<T, Codec>), Config = (), Response = ()>,
U::Future: 'static,
2021-12-04 19:40:47 +00:00
U::Error: fmt::Display + Into<Response<BoxBody>>,
2019-04-08 21:51:16 +00:00
U::InitError: fmt::Debug,
2018-10-05 03:02:10 +00:00
{
2019-03-07 06:56:34 +00:00
type Response = ();
type Error = DispatchError;
type Config = ();
2019-12-02 11:33:11 +00: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-05 03:02:10 +00:00
2019-12-02 15:37:13 +00: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
2022-03-10 03:12:29 +00:00
.map_err(|e| error!("Init http expect service error: {:?}", e))?;
let upgrade = match upgrade {
Some(upgrade) => {
2021-12-08 06:01:11 +00:00
let upgrade = upgrade
.await
2022-03-10 03:12:29 +00:00
.map_err(|e| error!("Init http upgrade service error: {:?}", e))?;
Some(upgrade)
}
None => None,
};
let service = service
.await
2022-03-10 03:12:29 +00:00
.map_err(|e| error!("Init http service error: {:?}", e))?;
Ok(H1ServiceHandler::new(
cfg,
service,
expect,
upgrade,
on_connect_ext,
))
})
2018-10-05 03:02:10 +00:00
}
}
/// `Service` implementation for HTTP/1 transport
pub type H1ServiceHandler<T, S, B, X, U> = HttpServiceHandler<T, S, B, X, U>;
2018-10-05 03:02:10 +00:00
2021-12-08 06:01:11 +00:00
impl<T, S, B, X, U> Service<(T, Option<net::SocketAddr>)> for HttpServiceHandler<T, S, B, X, U>
2018-10-05 03:02:10 +00:00
where
2019-12-02 11:33:11 +00:00
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>>,
S::Response: Into<Response<B>>,
2018-11-18 04:21:28 +00:00
B: MessageBody,
X: Service<Request, Response = Request>,
2021-12-04 19:40:47 +00:00
X::Error: Into<Response<BoxBody>>,
U: Service<(Request, Framed<T, Codec>), Response = ()>,
2021-12-04 19:40:47 +00:00
U::Error: fmt::Display + Into<Response<BoxBody>>,
2018-10-05 03:02:10 +00:00
{
2019-03-07 06:56:34 +00:00
type Response = ();
type Error = DispatchError;
2019-04-08 21:51:16 +00:00
type Future = Dispatcher<T, S, B, X, U>;
2018-10-05 03:02:10 +00:00
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self._poll_ready(cx).map_err(|err| {
2022-03-10 03:12:29 +00:00
error!("HTTP/1 service readiness error: {:?}", err);
DispatchError::Service(err)
})
2018-10-05 03:02:10 +00:00
}
fn call(&self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
let conn_data = OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
Dispatcher::new(io, self.flow.clone(), self.cfg.clone(), addr, conn_data)
2018-10-05 03:02:10 +00:00
}
}