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/h2/service.rs

468 lines
13 KiB
Rust
Raw Normal View History

2021-06-17 16:57:58 +00:00
use std::{
future::Future,
marker::PhantomData,
mem, net,
2021-06-17 16:57:58 +00:00
pin::Pin,
rc::Rc,
task::{Context, Poll},
};
2019-02-02 04:18:44 +00:00
2019-12-13 04:59:02 +00:00
use actix_codec::{AsyncRead, AsyncWrite};
2019-12-02 11:33:11 +00:00
use actix_rt::net::TcpStream;
use actix_service::{
2021-12-08 06:01:11 +00:00
fn_factory, fn_service, IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt as _,
2019-12-02 11:33:11 +00:00
};
2021-04-01 14:26:13 +00:00
use actix_utils::future::ready;
use futures_core::{future::LocalBoxFuture, ready};
2022-03-10 03:12:29 +00:00
use tracing::{error, trace};
2019-02-02 04:18:44 +00:00
2023-07-17 01:38:12 +00:00
use super::{dispatcher::Dispatcher, handshake_with_timeout, HandshakeWithTimeout};
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::HttpFlow,
ConnectCallback, OnConnectData, Request, Response,
};
2019-02-02 04:18:44 +00:00
2021-01-04 00:49:02 +00:00
/// `ServiceFactory` implementation for HTTP/2 transport
2019-12-02 11:33:11 +00:00
pub struct H2Service<T, S, B> {
2019-02-02 04:18:44 +00:00
srv: S,
cfg: ServiceConfig,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData<(T, B)>,
2019-02-02 04:18:44 +00:00
}
2019-12-02 11:33:11 +00:00
impl<T, S, B> H2Service<T, S, B>
2019-02-02 04:18:44 +00:00
where
S: ServiceFactory<Request, Config = ()>,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>> + 'static,
2019-11-19 12:54:19 +00:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-02-06 19:44:15 +00:00
B: MessageBody + 'static,
2019-02-02 04:18:44 +00:00
{
2021-01-04 00:49:02 +00:00
/// Create new `H2Service` instance with config.
pub(crate) fn with_config<F: IntoServiceFactory<S, Request>>(
cfg: ServiceConfig,
service: F,
) -> Self {
H2Service {
cfg,
on_connect_ext: None,
srv: service.into_factory(),
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
}
}
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-02-02 04:18:44 +00:00
}
2019-12-02 11:33:11 +00:00
impl<S, B> H2Service<TcpStream, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>> + 'static,
2019-12-02 11:33:11 +00:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-12-02 11:33:11 +00:00
B: MessageBody + 'static,
{
2021-01-04 00:49:02 +00:00
/// Create plain TCP based service
2019-12-02 11:33:11 +00:00
pub fn tcp(
self,
) -> impl ServiceFactory<
TcpStream,
2019-12-02 11:33:11 +00:00
Config = (),
Response = (),
Error = DispatchError,
InitError = S::InitError,
> {
2021-04-16 19:28:21 +00:00
fn_factory(|| {
ready(Ok::<_, S::InitError>(fn_service(|io: TcpStream| {
2020-02-27 02:10:55 +00:00
let peer_addr = io.peer_addr().ok();
ready(Ok::<_, DispatchError>((io, peer_addr)))
})))
2021-04-16 19:28:21 +00:00
})
2019-12-02 11:33:11 +00:00
.and_then(self)
}
}
#[cfg(feature = "openssl")]
mod openssl {
use actix_service::ServiceFactoryExt as _;
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
use super::*;
2021-02-27 19:57:09 +00:00
impl<S, B> H2Service<TlsStream<TcpStream>, S, B>
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>> + 'static,
2019-12-02 11:33:11 +00:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-12-02 11:33:11 +00:00
B: MessageBody + 'static,
{
/// 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 = S::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>| {
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
}
}
}
#[cfg(feature = "rustls-0_20")]
2024-02-03 23:55:01 +00:00
mod rustls_0_20 {
2019-12-13 05:24:57 +00:00
use std::io;
2019-12-05 17:35:43 +00:00
use actix_service::ServiceFactoryExt as _;
use actix_tls::accept::{
2021-11-30 14:12:04 +00:00
rustls::{reexports::ServerConfig, Acceptor, TlsStream},
TlsError,
};
use super::*;
2019-12-05 17:35:43 +00:00
impl<S, B> H2Service<TlsStream<TcpStream>, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>> + 'static,
2019-12-05 17:35:43 +00:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-12-05 17:35:43 +00:00
B: MessageBody + 'static,
{
/// Create Rustls v0.20 based service.
2019-12-05 17:35:43 +00:00
pub fn rustls(
self,
mut 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 = S::InitError,
> {
let mut protos = vec![b"h2".to_vec()];
protos.extend_from_slice(&config.alpn_protocols);
config.alpn_protocols = protos;
2019-12-05 17:35:43 +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>| {
let peer_addr = io.get_ref().0.peer_addr().ok();
(io, peer_addr)
})
.and_then(self.map_err(TlsError::Service))
}
}
}
#[cfg(feature = "rustls-0_21")]
2024-02-03 23:55:01 +00:00
mod rustls_0_21 {
use std::io;
use actix_service::ServiceFactoryExt as _;
use actix_tls::accept::{
rustls_0_21::{reexports::ServerConfig, Acceptor, TlsStream},
TlsError,
};
use super::*;
impl<S, B> H2Service<TlsStream<TcpStream>, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
{
/// Create Rustls v0.21 based service.
pub fn rustls_021(
self,
mut config: ServerConfig,
) -> impl ServiceFactory<
TcpStream,
Config = (),
Response = (),
Error = TlsError<io::Error, DispatchError>,
InitError = S::InitError,
> {
let mut protos = vec![b"h2".to_vec()];
protos.extend_from_slice(&config.alpn_protocols);
config.alpn_protocols = protos;
2024-02-03 23:55:01 +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>| {
let peer_addr = io.get_ref().0.peer_addr().ok();
(io, peer_addr)
})
.and_then(self.map_err(TlsError::Service))
}
}
}
#[cfg(feature = "rustls-0_22")]
mod rustls_0_22 {
use std::io;
use actix_service::ServiceFactoryExt as _;
use actix_tls::accept::{
rustls_0_22::{reexports::ServerConfig, Acceptor, TlsStream},
TlsError,
};
use super::*;
impl<S, B> H2Service<TlsStream<TcpStream>, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
S::Error: Into<Response<BoxBody>> + 'static,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
B: MessageBody + 'static,
{
/// Create Rustls v0.22 based service.
pub fn rustls_0_22(
self,
mut config: ServerConfig,
) -> impl ServiceFactory<
TcpStream,
Config = (),
Response = (),
Error = TlsError<io::Error, DispatchError>,
InitError = S::InitError,
> {
let mut protos = vec![b"h2".to_vec()];
protos.extend_from_slice(&config.alpn_protocols);
config.alpn_protocols = protos;
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>| {
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
}
}
}
impl<T, S, B> ServiceFactory<(T, Option<net::SocketAddr>)> for H2Service<T, S, B>
2019-02-02 04:18:44 +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>> + 'static,
2019-11-19 12:54:19 +00:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-02-06 19:44:15 +00:00
B: MessageBody + 'static,
2019-02-02 04:18:44 +00:00
{
2019-02-06 19:44:15 +00:00
type Response = ();
2019-03-07 06:56:34 +00:00
type Error = DispatchError;
type Config = ();
2019-12-02 11:33:11 +00:00
type Service = H2ServiceHandler<T, S::Service, B>;
type InitError = S::InitError;
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
2019-02-02 04:18:44 +00:00
2019-12-02 15:37:13 +00:00
fn new_service(&self, _: ()) -> Self::Future {
let service = self.srv.new_service(());
let cfg = self.cfg.clone();
let on_connect_ext = self.on_connect_ext.clone();
2019-02-02 04:18:44 +00:00
Box::pin(async move {
let service = service.await?;
Ok(H2ServiceHandler::new(cfg, on_connect_ext, service))
2021-01-04 00:49:02 +00:00
})
2019-02-02 04:18:44 +00:00
}
}
2021-02-11 22:39:54 +00:00
/// `Service` implementation for HTTP/2 transport
pub struct H2ServiceHandler<T, S, B>
where
S: Service<Request>,
{
flow: Rc<HttpFlow<S, (), ()>>,
2019-02-02 04:18:44 +00:00
cfg: ServiceConfig,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData<B>,
2019-02-02 04:18:44 +00:00
}
2019-12-02 11:33:11 +00:00
impl<T, S, B> H2ServiceHandler<T, S, B>
2019-02-02 04:18:44 +00:00
where
S: Service<Request>,
2021-12-04 19:40:47 +00:00
S::Error: Into<Response<BoxBody>> + 'static,
2019-11-19 12:54:19 +00:00
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
2019-02-06 19:44:15 +00:00
B: MessageBody + 'static,
2019-02-02 04:18:44 +00:00
{
2019-06-28 08:34:26 +00:00
fn new(
cfg: ServiceConfig,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
service: S,
2019-12-02 11:33:11 +00:00
) -> H2ServiceHandler<T, S, B> {
2019-02-02 04:18:44 +00:00
H2ServiceHandler {
2021-01-06 18:52:06 +00:00
flow: HttpFlow::new(service, (), None),
2019-02-02 04:18:44 +00:00
cfg,
on_connect_ext,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
2019-02-02 04:18:44 +00:00
}
}
}
impl<T, S, B> Service<(T, Option<net::SocketAddr>)> for H2ServiceHandler<T, S, B>
2019-02-02 04:18:44 +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>> + 'static,
2019-11-19 12:54:19 +00:00
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
2019-02-06 19:44:15 +00:00
B: MessageBody + 'static,
2019-02-02 04:18:44 +00:00
{
2019-02-06 19:44:15 +00:00
type Response = ();
2019-03-07 06:56:34 +00:00
type Error = DispatchError;
2019-02-02 04:18:44 +00:00
type Future = H2ServiceHandlerResponse<T, S, B>;
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.flow.service.poll_ready(cx).map_err(|err| {
let err = err.into();
error!("Service readiness error: {:?}", err);
DispatchError::Service(err)
2021-01-07 00:35:19 +00:00
})
2019-02-02 04:18:44 +00:00
}
fn call(&self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
2021-12-08 06:01:11 +00:00
let on_connect_data = OnConnectData::from_io(&io, self.on_connect_ext.as_deref());
2019-06-28 08:34:26 +00:00
2019-02-02 04:18:44 +00:00
H2ServiceHandlerResponse {
2019-02-06 19:44:15 +00:00
state: State::Handshake(
2021-01-06 18:52:06 +00:00
Some(self.flow.clone()),
2019-02-06 19:44:15 +00:00
Some(self.cfg.clone()),
2019-12-02 11:33:11 +00:00
addr,
on_connect_data,
handshake_with_timeout(io, &self.cfg),
2019-02-06 19:44:15 +00:00
),
2019-02-02 04:18:44 +00:00
}
}
}
enum State<T, S: Service<Request>, B: MessageBody>
2019-04-04 17:59:34 +00:00
where
2019-12-02 11:33:11 +00:00
T: AsyncRead + AsyncWrite + Unpin,
2019-04-04 17:59:34 +00:00
S::Future: 'static,
{
Handshake(
Option<Rc<HttpFlow<S, (), ()>>>,
Option<ServiceConfig>,
Option<net::SocketAddr>,
OnConnectData,
HandshakeWithTimeout<T>,
),
Established(Dispatcher<T, S, B, (), ()>),
2019-02-02 04:18:44 +00:00
}
pub struct H2ServiceHandlerResponse<T, S, B>
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>> + 'static,
2019-11-19 12:54:19 +00:00
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
2019-02-06 19:44:15 +00:00
B: MessageBody + 'static,
2019-02-02 04:18:44 +00:00
{
2019-02-06 19:44:15 +00:00
state: State<T, S, B>,
2019-02-02 04:18:44 +00:00
}
impl<T, S, B> Future for H2ServiceHandlerResponse<T, S, B>
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>> + 'static,
2019-11-19 12:54:19 +00:00
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
2019-02-02 04:18:44 +00:00
B: MessageBody,
{
type Output = Result<(), DispatchError>;
2019-02-02 04:18:44 +00:00
2019-12-07 18:46:51 +00:00
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2019-02-06 19:44:15 +00:00
match self.state {
State::Handshake(
ref mut srv,
ref mut config,
ref peer_addr,
ref mut conn_data,
ref mut handshake,
2021-01-04 00:49:02 +00:00
) => match ready!(Pin::new(handshake).poll(cx)) {
Ok((conn, timer)) => {
let on_connect_data = mem::take(conn_data);
self.state = State::Established(Dispatcher::new(
conn,
srv.take().unwrap(),
config.take().unwrap(),
*peer_addr,
on_connect_data,
timer,
));
self.poll(cx)
2019-02-06 19:44:15 +00:00
}
2021-01-04 00:49:02 +00:00
Err(err) => {
2022-03-10 03:12:29 +00:00
trace!("H2 handshake error: {}", err);
Poll::Ready(Err(err))
}
},
State::Established(ref mut disp) => Pin::new(disp).poll(cx),
2019-02-06 19:44:15 +00:00
}
2019-02-02 04:18:44 +00:00
}
}