1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 21:39:26 +00:00
actix-web/actix-http/src/h2/service.rs

351 lines
10 KiB
Rust
Raw Normal View History

use std::future::Future;
2019-02-02 04:18:44 +00:00
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{net, rc::Rc};
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-04-16 19:28:21 +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;
2019-02-02 04:18:44 +00:00
use bytes::Bytes;
use futures_core::{future::LocalBoxFuture, ready};
2021-03-11 03:48:38 +00:00
use h2::server::{handshake, Handshake};
2019-02-02 04:18:44 +00:00
use log::error;
use crate::body::MessageBody;
2019-12-13 04:59:02 +00:00
use crate::config::ServiceConfig;
use crate::error::{DispatchError, Error};
2019-02-02 04:18:44 +00:00
use crate::request::Request;
use crate::response::Response;
use crate::service::HttpFlow;
use crate::{ConnectCallback, OnConnectData};
2019-02-02 04:18:44 +00:00
2019-02-06 19:44:15 +00:00
use super::dispatcher::Dispatcher;
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 = ()>,
2019-11-19 12:54:19 +00:00
S::Error: Into<Error> + 'static,
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,
2019-12-02 11:33:11 +00:00
S::Error: Into<Error> + 'static,
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::{fn_factory, fn_service, ServiceFactoryExt};
2021-02-27 19:57:09 +00:00
use actix_tls::accept::openssl::{Acceptor, SslAcceptor, SslError, TlsStream};
use actix_tls::accept::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,
2019-12-02 11:33:11 +00:00
S::Error: Into<Error> + 'static,
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 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_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 11:33:11 +00:00
}
}
}
2019-12-05 17:35:43 +00: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 05:24:57 +00:00
use std::io;
2019-12-05 17:35:43 +00:00
impl<S, B> H2Service<TlsStream<TcpStream>, S, B>
where
S: ServiceFactory<Request, Config = ()>,
S::Future: 'static,
2019-12-05 17:35:43 +00:00
S::Error: Into<Error> + 'static,
S::Response: Into<Response<B>> + 'static,
<S::Service as Service<Request>>::Future: 'static,
2019-12-05 17:35:43 +00:00
B: MessageBody + 'static,
{
2021-01-04 00:49:02 +00:00
/// Create Rustls 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 protos = vec!["h2".to_string().into()];
config.set_protocols(&protos);
2021-04-16 19:28:21 +00: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 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,
2019-11-19 12:54:19 +00:00
S::Error: Into<Error> + 'static,
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>,
2019-11-19 12:54:19 +00:00
S::Error: Into<Error> + 'static,
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>,
2019-11-19 12:54:19 +00:00
S::Error: Into<Error> + 'static,
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(|e| {
2021-01-07 00:35:19 +00:00
let e = e.into();
error!("Service readiness error: {:?}", e);
DispatchError::Service(e)
})
2019-02-02 04:18:44 +00: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 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,
2021-03-11 03:48:38 +00:00
handshake(io),
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,
{
Incoming(Dispatcher<T, S, B, (), ()>),
Handshake(
Option<Rc<HttpFlow<S, (), ()>>>,
Option<ServiceConfig>,
Option<net::SocketAddr>,
OnConnectData,
Handshake<T, Bytes>,
),
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>,
2019-11-19 12:54:19 +00:00
S::Error: Into<Error> + 'static,
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>,
2019-11-19 12:54:19 +00:00
S::Error: Into<Error> + 'static,
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::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 00:49:02 +00: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 19:44:15 +00:00
}
2021-01-04 00:49:02 +00:00
Err(err) => {
trace!("H2 handshake error: {}", err);
Poll::Ready(Err(err.into()))
}
},
2019-02-06 19:44:15 +00:00
}
2019-02-02 04:18:44 +00:00
}
}