1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-25 00:43:00 +01:00
actix-web/actix-http/src/h2/service.rs

369 lines
11 KiB
Rust
Raw Normal View History

2021-06-17 18:57:58 +02:00
use std::{
error::Error as StdError,
future::Future,
marker::PhantomData,
net,
pin::Pin,
rc::Rc,
task::{Context, Poll},
};
2019-02-02 05:18:44 +01:00
2019-12-13 05:59:02 +01:00
use actix_codec::{AsyncRead, AsyncWrite};
2019-12-02 12:33:11 +01:00
use actix_rt::net::TcpStream;
use actix_service::{
2021-04-16 21:28:21 +02:00
fn_factory, fn_service, IntoServiceFactory, Service, ServiceFactory,
ServiceFactoryExt as _,
2019-12-02 12:33:11 +01:00
};
2021-04-01 16:26:13 +02:00
use actix_utils::future::ready;
2019-02-02 05:18:44 +01:00
use bytes::Bytes;
use futures_core::{future::LocalBoxFuture, ready};
2021-06-17 18:57:58 +02:00
use h2::server::{handshake as h2_handshake, Handshake as H2Handshake};
2019-02-02 05:18:44 +01:00
use log::error;
2021-06-17 18:57:58 +02:00
use crate::{
body::{AnyBody, MessageBody},
config::ServiceConfig,
error::DispatchError,
service::HttpFlow,
ConnectCallback, OnConnectData, Request, Response,
};
2019-02-02 05:18:44 +01:00
2019-02-06 20:44:15 +01:00
use super::dispatcher::Dispatcher;
2019-02-02 05:18:44 +01:00
2021-01-04 01:49:02 +01:00
/// `ServiceFactory` implementation for HTTP/2 transport
2019-12-02 12:33:11 +01:00
pub struct H2Service<T, S, B> {
2019-02-02 05:18:44 +01:00
srv: S,
cfg: ServiceConfig,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
2021-01-04 01:49:02 +01:00
_phantom: PhantomData<(T, B)>,
2019-02-02 05:18:44 +01:00
}
2019-12-02 12:33:11 +01:00
impl<T, S, B> H2Service<T, S, B>
2019-02-02 05:18:44 +01:00
where
S: ServiceFactory<Request, Config = ()>,
2021-06-17 18:57:58 +02:00
S::Error: Into<Response<AnyBody>> + 'static,
2019-11-19 13:54:19 +01:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-02-06 20:44:15 +01:00
B: MessageBody + 'static,
2021-06-17 18:57:58 +02:00
B::Error: Into<Box<dyn StdError>>,
2019-02-02 05:18:44 +01:00
{
2021-01-04 01:49:02 +01: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 01:49:02 +01:00
_phantom: PhantomData,
}
}
2019-06-28 10:34:26 +02: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 05:18:44 +01:00
}
2019-12-02 12:33:11 +01:00
impl<S, B> H2Service<TcpStream, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-06-17 18:57:58 +02:00
S::Error: Into<Response<AnyBody>> + 'static,
2019-12-02 12:33:11 +01:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-12-02 12:33:11 +01:00
B: MessageBody + 'static,
2021-06-17 18:57:58 +02:00
B::Error: Into<Box<dyn StdError>>,
2019-12-02 12:33:11 +01:00
{
2021-01-04 01:49:02 +01:00
/// Create plain TCP based service
2019-12-02 12:33:11 +01:00
pub fn tcp(
self,
) -> impl ServiceFactory<
TcpStream,
2019-12-02 12:33:11 +01:00
Config = (),
Response = (),
Error = DispatchError,
InitError = S::InitError,
> {
2021-04-16 21:28:21 +02:00
fn_factory(|| {
ready(Ok::<_, S::InitError>(fn_service(|io: TcpStream| {
2020-02-27 03:10:55 +01:00
let peer_addr = io.peer_addr().ok();
ready(Ok::<_, DispatchError>((io, peer_addr)))
})))
2021-04-16 21:28:21 +02:00
})
2019-12-02 12:33:11 +01:00
.and_then(self)
}
}
#[cfg(feature = "openssl")]
mod openssl {
use actix_service::{fn_factory, fn_service, ServiceFactoryExt};
2021-02-27 20:57:09 +01:00
use actix_tls::accept::openssl::{Acceptor, SslAcceptor, SslError, TlsStream};
use actix_tls::accept::TlsError;
2019-12-02 12:33:11 +01:00
use super::*;
2021-02-27 20:57:09 +01:00
impl<S, B> H2Service<TlsStream<TcpStream>, S, B>
2019-12-02 12:33:11 +01:00
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-06-17 18:57:58 +02:00
S::Error: Into<Response<AnyBody>> + 'static,
2019-12-02 12:33:11 +01:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-12-02 12:33:11 +01:00
B: MessageBody + 'static,
2021-06-17 18:57:58 +02:00
B::Error: Into<Box<dyn StdError>>,
2019-12-02 12:33:11 +01:00
{
2021-01-04 01:49:02 +01:00
/// Create OpenSSL based service
2019-12-02 12:33:11 +01:00
pub fn openssl(
self,
acceptor: SslAcceptor,
) -> impl ServiceFactory<
TcpStream,
2019-12-02 12:33:11 +01:00
Config = (),
Response = (),
Error = TlsError<SslError, DispatchError>,
2019-12-02 12:33:11 +01:00
InitError = S::InitError,
> {
2021-04-16 21:28:21 +02:00
Acceptor::new(acceptor)
.map_err(TlsError::Tls)
.map_init_err(|_| panic!())
.and_then(fn_factory(|| {
ready(Ok::<_, S::InitError>(fn_service(
|io: TlsStream<TcpStream>| {
let peer_addr = io.get_ref().peer_addr().ok();
ready(Ok((io, peer_addr)))
},
)))
}))
.and_then(self.map_err(TlsError::Service))
2019-12-02 12:33:11 +01:00
}
}
}
2019-12-05 18:35:43 +01:00
#[cfg(feature = "rustls")]
mod rustls {
use super::*;
use actix_service::ServiceFactoryExt;
use actix_tls::accept::rustls::{Acceptor, ServerConfig, TlsStream};
use actix_tls::accept::TlsError;
2019-12-13 06:24:57 +01:00
use std::io;
2019-12-05 18:35:43 +01:00
impl<S, B> H2Service<TlsStream<TcpStream>, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-06-17 18:57:58 +02:00
S::Error: Into<Response<AnyBody>> + 'static,
2019-12-05 18:35:43 +01:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-12-05 18:35:43 +01:00
B: MessageBody + 'static,
2021-06-17 18:57:58 +02:00
B::Error: Into<Box<dyn StdError>>,
2019-12-05 18:35:43 +01:00
{
2021-01-04 01:49:02 +01:00
/// Create Rustls based service
2019-12-05 18:35:43 +01:00
pub fn rustls(
self,
mut config: ServerConfig,
) -> impl ServiceFactory<
TcpStream,
2019-12-05 18:35:43 +01:00
Config = (),
Response = (),
2020-09-09 10:20:54 +02:00
Error = TlsError<io::Error, DispatchError>,
2019-12-05 18:35:43 +01:00
InitError = S::InitError,
> {
let mut protos = vec![b"h2".to_vec()];
protos.extend_from_slice(&config.alpn_protocols);
2019-12-05 18:35:43 +01:00
config.set_protocols(&protos);
2021-04-16 21:28:21 +02:00
Acceptor::new(config)
.map_err(TlsError::Tls)
.map_init_err(|_| panic!())
.and_then(fn_factory(|| {
ready(Ok::<_, S::InitError>(fn_service(
|io: TlsStream<TcpStream>| {
let peer_addr = io.get_ref().0.peer_addr().ok();
ready(Ok((io, peer_addr)))
},
)))
}))
.and_then(self.map_err(TlsError::Service))
2019-12-05 18:35:43 +01:00
}
}
}
impl<T, S, B> ServiceFactory<(T, Option<net::SocketAddr>)> for H2Service<T, S, B>
2019-02-02 05:18:44 +01:00
where
T: AsyncRead + AsyncWrite + Unpin + 'static,
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2021-06-17 18:57:58 +02:00
S::Error: Into<Response<AnyBody>> + 'static,
2019-11-19 13:54:19 +01:00
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-02-06 20:44:15 +01:00
B: MessageBody + 'static,
2021-06-17 18:57:58 +02:00
B::Error: Into<Box<dyn StdError>>,
2019-02-02 05:18:44 +01:00
{
2019-02-06 20:44:15 +01:00
type Response = ();
2019-03-07 07:56:34 +01:00
type Error = DispatchError;
type Config = ();
2019-12-02 12:33:11 +01:00
type Service = H2ServiceHandler<T, S::Service, B>;
type InitError = S::InitError;
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
2019-02-02 05:18:44 +01:00
2019-12-02 16:37:13 +01: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 05:18:44 +01:00
Box::pin(async move {
let service = service.await?;
Ok(H2ServiceHandler::new(cfg, on_connect_ext, service))
2021-01-04 01:49:02 +01:00
})
2019-02-02 05:18:44 +01:00
}
}
2021-02-11 23:39:54 +01:00
/// `Service` implementation for HTTP/2 transport
pub struct H2ServiceHandler<T, S, B>
where
S: Service<Request>,
{
flow: Rc<HttpFlow<S, (), ()>>,
2019-02-02 05:18:44 +01:00
cfg: ServiceConfig,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
2021-01-04 01:49:02 +01:00
_phantom: PhantomData<B>,
2019-02-02 05:18:44 +01:00
}
2019-12-02 12:33:11 +01:00
impl<T, S, B> H2ServiceHandler<T, S, B>
2019-02-02 05:18:44 +01:00
where
S: Service<Request>,
2021-06-17 18:57:58 +02:00
S::Error: Into<Response<AnyBody>> + 'static,
2019-11-19 13:54:19 +01:00
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
2019-02-06 20:44:15 +01:00
B: MessageBody + 'static,
2019-02-02 05:18:44 +01:00
{
2019-06-28 10:34:26 +02:00
fn new(
cfg: ServiceConfig,
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
service: S,
2019-12-02 12:33:11 +01:00
) -> H2ServiceHandler<T, S, B> {
2019-02-02 05:18:44 +01:00
H2ServiceHandler {
2021-01-06 19:52:06 +01:00
flow: HttpFlow::new(service, (), None),
2019-02-02 05:18:44 +01:00
cfg,
on_connect_ext,
2021-01-04 01:49:02 +01:00
_phantom: PhantomData,
2019-02-02 05:18:44 +01:00
}
}
}
impl<T, S, B> Service<(T, Option<net::SocketAddr>)> for H2ServiceHandler<T, S, B>
2019-02-02 05:18:44 +01:00
where
2019-12-02 12:33:11 +01:00
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
2021-06-17 18:57:58 +02:00
S::Error: Into<Response<AnyBody>> + 'static,
2019-11-19 13:54:19 +01:00
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
2019-02-06 20:44:15 +01:00
B: MessageBody + 'static,
2021-06-17 18:57:58 +02:00
B::Error: Into<Box<dyn StdError>>,
2019-02-02 05:18:44 +01:00
{
2019-02-06 20:44:15 +01:00
type Response = ();
2019-03-07 07:56:34 +01:00
type Error = DispatchError;
2019-02-02 05:18:44 +01: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(|e| {
2021-01-07 01:35:19 +01:00
let e = e.into();
error!("Service readiness error: {:?}", e);
DispatchError::Service(e)
})
2019-02-02 05:18:44 +01: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 10:34:26 +02:00
2019-02-02 05:18:44 +01:00
H2ServiceHandlerResponse {
2019-02-06 20:44:15 +01:00
state: State::Handshake(
2021-01-06 19:52:06 +01:00
Some(self.flow.clone()),
2019-02-06 20:44:15 +01:00
Some(self.cfg.clone()),
2019-12-02 12:33:11 +01:00
addr,
on_connect_data,
2021-06-17 18:57:58 +02:00
h2_handshake(io),
2019-02-06 20:44:15 +01:00
),
2019-02-02 05:18:44 +01:00
}
}
}
enum State<T, S: Service<Request>, B: MessageBody>
2019-04-04 19:59:34 +02:00
where
2019-12-02 12:33:11 +01:00
T: AsyncRead + AsyncWrite + Unpin,
2019-04-04 19:59:34 +02:00
S::Future: 'static,
{
Incoming(Dispatcher<T, S, B, (), ()>),
Handshake(
Option<Rc<HttpFlow<S, (), ()>>>,
Option<ServiceConfig>,
Option<net::SocketAddr>,
OnConnectData,
2021-06-17 18:57:58 +02:00
H2Handshake<T, Bytes>,
),
2019-02-02 05:18:44 +01:00
}
pub struct H2ServiceHandlerResponse<T, S, B>
where
2019-12-02 12:33:11 +01:00
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
2021-06-17 18:57:58 +02:00
S::Error: Into<Response<AnyBody>> + 'static,
2019-11-19 13:54:19 +01:00
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
2019-02-06 20:44:15 +01:00
B: MessageBody + 'static,
2019-02-02 05:18:44 +01:00
{
2019-02-06 20:44:15 +01:00
state: State<T, S, B>,
2019-02-02 05:18:44 +01:00
}
impl<T, S, B> Future for H2ServiceHandlerResponse<T, S, B>
where
2019-12-02 12:33:11 +01:00
T: AsyncRead + AsyncWrite + Unpin,
S: Service<Request>,
2021-06-17 18:57:58 +02:00
S::Error: Into<Response<AnyBody>> + 'static,
2019-11-19 13:54:19 +01:00
S::Future: 'static,
S::Response: Into<Response<B>> + 'static,
2019-02-02 05:18:44 +01:00
B: MessageBody,
2021-06-17 18:57:58 +02:00
B::Error: Into<Box<dyn StdError>>,
2019-02-02 05:18:44 +01:00
{
type Output = Result<(), DispatchError>;
2019-02-02 05:18:44 +01:00
2019-12-07 19:46:51 +01:00
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2019-02-06 20:44:15 +01:00
match self.state {
State::Incoming(ref mut disp) => Pin::new(disp).poll(cx),
State::Handshake(
ref mut srv,
ref mut config,
ref peer_addr,
ref mut on_connect_data,
ref mut handshake,
2021-01-04 01:49:02 +01:00
) => match ready!(Pin::new(handshake).poll(cx)) {
Ok(conn) => {
let on_connect_data = std::mem::take(on_connect_data);
self.state = State::Incoming(Dispatcher::new(
srv.take().unwrap(),
conn,
on_connect_data,
config.take().unwrap(),
*peer_addr,
));
self.poll(cx)
2019-02-06 20:44:15 +01:00
}
2021-01-04 01:49:02 +01:00
Err(err) => {
trace!("H2 handshake error: {}", err);
Poll::Ready(Err(err.into()))
}
},
2019-02-06 20:44:15 +01:00
}
2019-02-02 05:18:44 +01:00
}
}