2019-11-15 15:54:11 +06:00
|
|
|
use std::future::Future;
|
2018-10-04 20:02:10 -07:00
|
|
|
use std::marker::PhantomData;
|
2019-11-15 15:54:11 +06:00
|
|
|
use std::pin::Pin;
|
2019-06-28 14:34:26 +06:00
|
|
|
use std::rc::Rc;
|
2019-11-15 15:54:11 +06:00
|
|
|
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};
|
2019-12-13 11:24:57 +06:00
|
|
|
use futures_core::ready;
|
2020-12-26 20:46:36 +08:00
|
|
|
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;
|
2020-12-26 20:46:36 +08:00
|
|
|
use crate::error::{DispatchError, Error};
|
2018-12-06 14:32:52 -08:00
|
|
|
use crate::request::Request;
|
|
|
|
use crate::response::Response;
|
2021-01-07 02:43:52 +08:00
|
|
|
use crate::service::HttpFlow;
|
|
|
|
use crate::{ConnectCallback, OnConnectData};
|
2018-10-04 20:02:10 -07:00
|
|
|
|
2018-10-22 18:18:05 -07:00
|
|
|
use super::codec::Codec;
|
2018-10-04 20:02:10 -07:00
|
|
|
use super::dispatcher::Dispatcher;
|
2020-12-26 20:46:36 +08:00
|
|
|
use super::{ExpectHandler, UpgradeHandler};
|
2018-10-04 20:02:10 -07:00
|
|
|
|
2019-11-15 15:54:11 +06:00
|
|
|
/// `ServiceFactory` implementation for HTTP1 transport
|
2021-01-04 07:47:04 +08:00
|
|
|
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>,
|
2020-10-30 02:03:26 +00:00
|
|
|
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
|
2021-01-04 07:47:04 +08:00
|
|
|
S: ServiceFactory<Request, Config = ()>,
|
2019-04-05 16:46:44 -07:00
|
|
|
S::Error: Into<Error>,
|
|
|
|
S::InitError: fmt::Debug,
|
2019-02-09 08:44:22 -08:00
|
|
|
S::Response: Into<Response<B>>,
|
2018-11-17 20:21:28 -08:00
|
|
|
B: MessageBody,
|
2018-10-04 20:02:10 -07:00
|
|
|
{
|
2019-03-04 15:58:39 -08:00
|
|
|
/// Create new `HttpService` instance with config.
|
2021-01-04 07:47:04 +08:00
|
|
|
pub(crate) fn with_config<F: IntoServiceFactory<S, Request>>(
|
2019-11-15 15:54:11 +06:00
|
|
|
cfg: ServiceConfig,
|
|
|
|
service: F,
|
|
|
|
) -> Self {
|
2019-03-04 15:58:39 -08:00
|
|
|
H1Service {
|
|
|
|
cfg,
|
2019-11-15 15:54:11 +06:00
|
|
|
srv: service.into_factory(),
|
2019-04-05 16:46:44 -07:00
|
|
|
expect: ExpectHandler,
|
2019-04-08 14:51:16 -07:00
|
|
|
upgrade: None,
|
2020-10-30 02:03:26 +00:00
|
|
|
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
|
2021-01-04 07:47:04 +08:00
|
|
|
S: ServiceFactory<Request, Config = ()>,
|
2019-12-02 17:33:11 +06:00
|
|
|
S::Error: Into<Error>,
|
|
|
|
S::InitError: fmt::Debug,
|
|
|
|
S::Response: Into<Response<B>>,
|
|
|
|
B: MessageBody,
|
2021-01-04 07:47:04 +08:00
|
|
|
X: ServiceFactory<Request, Config = (), Response = Request>,
|
2019-12-02 17:33:11 +06:00
|
|
|
X::Error: Into<Error>,
|
|
|
|
X::InitError: fmt::Debug,
|
2021-01-04 07:47:04 +08:00
|
|
|
U: ServiceFactory<(Request, Framed<TcpStream, Codec>), Config = (), Response = ()>,
|
2019-12-16 23:34:25 -08:00
|
|
|
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<
|
2021-01-04 07:47:04 +08:00
|
|
|
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();
|
2020-12-26 20:46:36 +08:00
|
|
|
ready(Ok((io, peer_addr)))
|
2019-12-02 17:33:11 +06:00
|
|
|
})
|
|
|
|
.and_then(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
mod openssl {
|
|
|
|
use super::*;
|
|
|
|
|
2021-01-04 07:47:04 +08:00
|
|
|
use actix_service::ServiceFactoryExt;
|
|
|
|
use actix_tls::accept::openssl::{Acceptor, SslAcceptor, SslError, SslStream};
|
|
|
|
use actix_tls::accept::TlsError;
|
2019-12-02 17:33:11 +06:00
|
|
|
|
|
|
|
impl<S, B, X, U> H1Service<SslStream<TcpStream>, S, B, X, U>
|
|
|
|
where
|
2021-01-04 07:47:04 +08:00
|
|
|
S: ServiceFactory<Request, Config = ()>,
|
2019-12-02 17:33:11 +06:00
|
|
|
S::Error: Into<Error>,
|
|
|
|
S::InitError: fmt::Debug,
|
|
|
|
S::Response: Into<Response<B>>,
|
|
|
|
B: MessageBody,
|
2021-01-04 07:47:04 +08:00
|
|
|
X: ServiceFactory<Request, Config = (), Response = Request>,
|
2019-12-02 17:33:11 +06:00
|
|
|
X::Error: Into<Error>,
|
|
|
|
X::InitError: fmt::Debug,
|
|
|
|
U: ServiceFactory<
|
2021-01-04 07:47:04 +08:00
|
|
|
(Request, Framed<SslStream<TcpStream>, Codec>),
|
2019-12-02 17:33:11 +06:00
|
|
|
Config = (),
|
|
|
|
Response = (),
|
|
|
|
>,
|
2019-12-16 23:34:25 -08:00
|
|
|
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<
|
2021-01-04 07:47:04 +08:00
|
|
|
TcpStream,
|
2019-12-02 17:33:11 +06:00
|
|
|
Config = (),
|
|
|
|
Response = (),
|
2021-01-04 07:47:04 +08:00
|
|
|
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!()),
|
|
|
|
)
|
|
|
|
.and_then(|io: SslStream<TcpStream>| {
|
|
|
|
let peer_addr = io.get_ref().peer_addr().ok();
|
2020-12-26 20:46:36 +08:00
|
|
|
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::*;
|
2021-01-04 07:47:04 +08:00
|
|
|
use actix_service::ServiceFactoryExt;
|
|
|
|
use actix_tls::accept::rustls::{Acceptor, ServerConfig, TlsStream};
|
|
|
|
use actix_tls::accept::TlsError;
|
2019-12-05 23:35:43 +06:00
|
|
|
use std::{fmt, io};
|
|
|
|
|
|
|
|
impl<S, B, X, U> H1Service<TlsStream<TcpStream>, S, B, X, U>
|
|
|
|
where
|
2021-01-04 07:47:04 +08:00
|
|
|
S: ServiceFactory<Request, Config = ()>,
|
2019-12-05 23:35:43 +06:00
|
|
|
S::Error: Into<Error>,
|
|
|
|
S::InitError: fmt::Debug,
|
|
|
|
S::Response: Into<Response<B>>,
|
|
|
|
B: MessageBody,
|
2021-01-04 07:47:04 +08:00
|
|
|
X: ServiceFactory<Request, Config = (), Response = Request>,
|
2019-12-05 23:35:43 +06:00
|
|
|
X::Error: Into<Error>,
|
|
|
|
X::InitError: fmt::Debug,
|
|
|
|
U: ServiceFactory<
|
2021-01-04 07:47:04 +08:00
|
|
|
(Request, Framed<TlsStream<TcpStream>, Codec>),
|
2019-12-05 23:35:43 +06:00
|
|
|
Config = (),
|
|
|
|
Response = (),
|
|
|
|
>,
|
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<
|
2021-01-04 07:47:04 +08:00
|
|
|
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();
|
2020-12-26 20:46:36 +08:00
|
|
|
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
|
2021-01-04 07:47:04 +08:00
|
|
|
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
|
2021-01-04 07:47:04 +08:00
|
|
|
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,
|
2020-10-30 02:03:26 +00:00
|
|
|
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
|
2021-01-04 07:47:04 +08:00
|
|
|
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,
|
2020-10-30 02:03:26 +00:00
|
|
|
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
|
|
|
|
2020-10-30 02:03: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 16:46:44 -07:00
|
|
|
}
|
|
|
|
|
2021-01-04 07:47:04 +08: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
|
2019-12-02 17:33:11 +06:00
|
|
|
T: AsyncRead + AsyncWrite + Unpin,
|
2021-01-04 07:47:04 +08:00
|
|
|
S: ServiceFactory<Request, Config = ()>,
|
2019-04-05 16:46:44 -07:00
|
|
|
S::Error: Into<Error>,
|
2019-02-09 08:44:22 -08:00
|
|
|
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,
|
2021-01-04 07:47:04 +08:00
|
|
|
X: ServiceFactory<Request, Config = (), Response = Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
X::Error: Into<Error>,
|
|
|
|
X::InitError: fmt::Debug,
|
2021-01-04 07:47:04 +08:00
|
|
|
U: ServiceFactory<(Request, Framed<T, Codec>), Config = (), Response = ()>,
|
2019-12-16 23:34:25 -08:00
|
|
|
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;
|
2021-01-04 07:47:04 +08:00
|
|
|
type Config = ();
|
2019-12-02 17:33:11 +06:00
|
|
|
type Service = H1ServiceHandler<T, S::Service, B, X::Service, U::Service>;
|
2021-01-04 07:47:04 +08:00
|
|
|
type InitError = ();
|
2019-12-02 17:33:11 +06:00
|
|
|
type Future = H1ServiceResponse<T, S, B, X, U>;
|
2018-10-04 20:02:10 -07:00
|
|
|
|
2019-12-02 21:37:13 +06:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2018-10-04 20:02:10 -07:00
|
|
|
H1ServiceResponse {
|
2019-12-02 21:37:13 +06:00
|
|
|
fut: self.srv.new_service(()),
|
|
|
|
fut_ex: Some(self.expect.new_service(())),
|
|
|
|
fut_upg: self.upgrade.as_ref().map(|f| f.new_service(())),
|
2019-04-05 16:46:44 -07:00
|
|
|
expect: None,
|
2019-04-08 14:51:16 -07:00
|
|
|
upgrade: None,
|
2020-10-30 02:03:26 +00:00
|
|
|
on_connect_ext: self.on_connect_ext.clone(),
|
2018-10-04 20:02:10 -07:00
|
|
|
cfg: Some(self.cfg.clone()),
|
2021-01-04 00:49:02 +00:00
|
|
|
_phantom: PhantomData,
|
2018-10-04 20:02:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 15:56:47 -07:00
|
|
|
#[doc(hidden)]
|
2019-11-19 18:54:19 +06:00
|
|
|
#[pin_project::pin_project]
|
2019-12-02 17:33:11 +06:00
|
|
|
pub struct H1ServiceResponse<T, S, B, X, U>
|
2019-04-05 16:46:44 -07:00
|
|
|
where
|
2021-01-04 07:47:04 +08:00
|
|
|
S: ServiceFactory<Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
S::Error: Into<Error>,
|
|
|
|
S::InitError: fmt::Debug,
|
2021-01-04 07:47:04 +08:00
|
|
|
X: ServiceFactory<Request, Response = Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
X::Error: Into<Error>,
|
|
|
|
X::InitError: fmt::Debug,
|
2021-01-04 07:47:04 +08:00
|
|
|
U: ServiceFactory<(Request, Framed<T, Codec>), Response = ()>,
|
2019-04-08 14:51:16 -07:00
|
|
|
U::Error: fmt::Display,
|
|
|
|
U::InitError: fmt::Debug,
|
2019-04-05 16:46:44 -07:00
|
|
|
{
|
2019-11-19 18:54:19 +06:00
|
|
|
#[pin]
|
2019-04-05 16:46:44 -07:00
|
|
|
fut: S::Future,
|
2019-11-19 18:54:19 +06:00
|
|
|
#[pin]
|
2019-04-05 16:46:44 -07:00
|
|
|
fut_ex: Option<X::Future>,
|
2019-11-19 18:54:19 +06:00
|
|
|
#[pin]
|
2019-04-08 14:51:16 -07:00
|
|
|
fut_upg: Option<U::Future>,
|
2019-04-05 16:46:44 -07:00
|
|
|
expect: Option<X::Service>,
|
2019-04-08 14:51:16 -07:00
|
|
|
upgrade: Option<U::Service>,
|
2020-10-30 02:03:26 +00:00
|
|
|
on_connect_ext: Option<Rc<ConnectCallback<T>>>,
|
2018-10-04 20:02:10 -07:00
|
|
|
cfg: Option<ServiceConfig>,
|
2021-01-07 02:43:52 +08: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> Future for H1ServiceResponse<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,
|
2021-01-04 07:47:04 +08:00
|
|
|
S: ServiceFactory<Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
S::Error: Into<Error>,
|
2019-02-09 08:44:22 -08:00
|
|
|
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,
|
2021-01-04 07:47:04 +08:00
|
|
|
X: ServiceFactory<Request, Response = Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
X::Error: Into<Error>,
|
|
|
|
X::InitError: fmt::Debug,
|
2021-01-04 07:47:04 +08:00
|
|
|
U: ServiceFactory<(Request, Framed<T, Codec>), Response = ()>,
|
2019-04-08 14:51:16 -07:00
|
|
|
U::Error: fmt::Display,
|
|
|
|
U::InitError: fmt::Debug,
|
2018-10-04 20:02:10 -07:00
|
|
|
{
|
2019-12-02 17:33:11 +06:00
|
|
|
type Output = Result<H1ServiceHandler<T, S::Service, B, X::Service, U::Service>, ()>;
|
2019-11-15 15:54:11 +06:00
|
|
|
|
2019-12-08 00:46:51 +06:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-19 18:54:19 +06:00
|
|
|
let mut this = self.as_mut().project();
|
2019-11-15 15:54:11 +06:00
|
|
|
|
2019-11-19 18:54:19 +06:00
|
|
|
if let Some(fut) = this.fut_ex.as_pin_mut() {
|
|
|
|
let expect = ready!(fut
|
2019-11-15 15:54:11 +06:00
|
|
|
.poll(cx)
|
|
|
|
.map_err(|e| log::error!("Init http service error: {:?}", e)))?;
|
2019-11-19 18:54:19 +06:00
|
|
|
this = self.as_mut().project();
|
|
|
|
*this.expect = Some(expect);
|
|
|
|
this.fut_ex.set(None);
|
2019-04-05 16:46:44 -07:00
|
|
|
}
|
|
|
|
|
2019-11-19 18:54:19 +06:00
|
|
|
if let Some(fut) = this.fut_upg.as_pin_mut() {
|
|
|
|
let upgrade = ready!(fut
|
2019-11-15 15:54:11 +06:00
|
|
|
.poll(cx)
|
|
|
|
.map_err(|e| log::error!("Init http service error: {:?}", e)))?;
|
2019-11-19 18:54:19 +06:00
|
|
|
this = self.as_mut().project();
|
|
|
|
*this.upgrade = Some(upgrade);
|
2021-01-07 08:57:34 +08:00
|
|
|
this.fut_upg.set(None);
|
2019-04-08 14:51:16 -07:00
|
|
|
}
|
|
|
|
|
2019-11-19 18:54:19 +06:00
|
|
|
let result = ready!(this
|
|
|
|
.fut
|
2019-11-15 15:54:11 +06:00
|
|
|
.poll(cx)
|
2019-04-05 16:46:44 -07:00
|
|
|
.map_err(|e| log::error!("Init http service error: {:?}", e)));
|
2019-11-15 15:54:11 +06:00
|
|
|
|
|
|
|
Poll::Ready(result.map(|service| {
|
2019-11-19 18:54:19 +06:00
|
|
|
let this = self.as_mut().project();
|
2020-10-30 02:03:26 +00:00
|
|
|
|
2019-11-15 15:54:11 +06:00
|
|
|
H1ServiceHandler::new(
|
|
|
|
this.cfg.take().unwrap(),
|
|
|
|
service,
|
|
|
|
this.expect.take().unwrap(),
|
|
|
|
this.upgrade.take(),
|
2020-10-30 02:03:26 +00:00
|
|
|
this.on_connect_ext.clone(),
|
2019-11-15 15:54:11 +06:00
|
|
|
)
|
|
|
|
}))
|
2018-10-04 20:02:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 02:03:26 +00:00
|
|
|
/// `Service` implementation for HTTP/1 transport
|
2021-01-04 07:47:04 +08:00
|
|
|
pub struct H1ServiceHandler<T, S, B, X, U>
|
|
|
|
where
|
|
|
|
S: Service<Request>,
|
|
|
|
X: Service<Request>,
|
|
|
|
U: Service<(Request, Framed<T, Codec>)>,
|
|
|
|
{
|
2021-02-06 17:00:40 -08:00
|
|
|
flow: Rc<HttpFlow<S, X, U>>,
|
2020-10-30 02:03:26 +00:00
|
|
|
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
|
2021-01-04 07:47:04 +08:00
|
|
|
S: Service<Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
S::Error: Into<Error>,
|
2019-02-09 08:44:22 -08:00
|
|
|
S::Response: Into<Response<B>>,
|
2018-11-17 20:21:28 -08:00
|
|
|
B: MessageBody,
|
2021-01-04 07:47:04 +08:00
|
|
|
X: Service<Request, Response = Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
X::Error: Into<Error>,
|
2021-01-04 07:47:04 +08:00
|
|
|
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,
|
2021-01-07 02:43:52 +08:00
|
|
|
service: S,
|
2019-04-08 14:51:16 -07:00
|
|
|
expect: X,
|
|
|
|
upgrade: Option<U>,
|
2020-10-30 02:03:26 +00:00
|
|
|
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,
|
2020-10-30 02:03:26 +00:00
|
|
|
on_connect_ext,
|
2021-01-04 00:49:02 +00:00
|
|
|
_phantom: PhantomData,
|
2018-10-04 20:02:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 07:47:04 +08: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,
|
2021-01-04 07:47:04 +08:00
|
|
|
S: Service<Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
S::Error: Into<Error>,
|
2019-02-09 08:44:22 -08:00
|
|
|
S::Response: Into<Response<B>>,
|
2018-11-17 20:21:28 -08:00
|
|
|
B: MessageBody,
|
2021-01-04 07:47:04 +08:00
|
|
|
X: Service<Request, Response = Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
X::Error: Into<Error>,
|
2021-01-04 07:47:04 +08:00
|
|
|
U: Service<(Request, Framed<T, Codec>), Response = ()>,
|
2019-12-16 23:34:25 -08:00
|
|
|
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
|
|
|
|
2021-02-06 17:00:40 -08:00
|
|
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
let ready = self
|
|
|
|
.flow
|
2019-04-05 16:46:44 -07:00
|
|
|
.expect
|
2019-11-15 15:54:11 +06:00
|
|
|
.poll_ready(cx)
|
2019-04-05 16:46:44 -07:00
|
|
|
.map_err(|e| {
|
|
|
|
let e = e.into();
|
|
|
|
log::error!("Http service readiness error: {:?}", e);
|
|
|
|
DispatchError::Service(e)
|
|
|
|
})?
|
|
|
|
.is_ready();
|
|
|
|
|
2021-02-06 17:00:40 -08:00
|
|
|
let ready = self
|
|
|
|
.flow
|
2021-01-07 02:43:52 +08:00
|
|
|
.service
|
2019-11-15 15:54:11 +06:00
|
|
|
.poll_ready(cx)
|
2019-04-05 16:46:44 -07:00
|
|
|
.map_err(|e| {
|
|
|
|
let e = e.into();
|
|
|
|
log::error!("Http service readiness error: {:?}", e);
|
|
|
|
DispatchError::Service(e)
|
|
|
|
})?
|
|
|
|
.is_ready()
|
|
|
|
&& ready;
|
2019-12-18 09:30:14 +06:00
|
|
|
|
2021-02-06 17:00:40 -08:00
|
|
|
let ready = if let Some(ref upg) = self.flow.upgrade {
|
2019-12-16 23:34:25 -08:00
|
|
|
upg.poll_ready(cx)
|
|
|
|
.map_err(|e| {
|
|
|
|
let e = e.into();
|
|
|
|
log::error!("Http service readiness error: {:?}", e);
|
|
|
|
DispatchError::Service(e)
|
|
|
|
})?
|
|
|
|
.is_ready()
|
|
|
|
&& ready
|
|
|
|
} else {
|
|
|
|
ready
|
|
|
|
};
|
2019-04-05 16:46:44 -07:00
|
|
|
|
|
|
|
if ready {
|
2019-11-15 15:54:11 +06:00
|
|
|
Poll::Ready(Ok(()))
|
2019-04-05 16:46:44 -07:00
|
|
|
} else {
|
2019-11-15 15:54:11 +06:00
|
|
|
Poll::Pending
|
2019-04-05 16:46:44 -07:00
|
|
|
}
|
2018-10-04 20:02:10 -07:00
|
|
|
}
|
|
|
|
|
2021-02-06 17:00:40 -08:00
|
|
|
fn call(&self, (io, addr): (T, Option<net::SocketAddr>)) -> Self::Future {
|
2021-01-07 02:43:52 +08:00
|
|
|
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(),
|
2021-01-07 02:43:52 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|