1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00
actix-web/actix-http/src/service.rs

706 lines
22 KiB
Rust
Raw Normal View History

2019-03-07 06:56:34 +00:00
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
2019-06-28 08:34:26 +00:00
use std::{fmt, io, net, rc};
2019-03-07 06:56:34 +00:00
2019-04-08 21:51:16 +00:00
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_server_config::{
Io as ServerIo, IoStream, Protocol, ServerConfig as SrvConfig,
};
use actix_service::{IntoServiceFactory, Service, ServiceFactory};
2019-03-07 06:56:34 +00:00
use bytes::{Buf, BufMut, Bytes, BytesMut};
use futures::{ready, Future};
2019-03-07 06:56:34 +00:00
use h2::server::{self, Handshake};
use crate::body::MessageBody;
2019-03-09 18:39:06 +00:00
use crate::builder::HttpServiceBuilder;
use crate::cloneable::CloneableService;
2019-03-07 06:56:34 +00:00
use crate::config::{KeepAlive, ServiceConfig};
2019-04-05 23:46:44 +00:00
use crate::error::{DispatchError, Error};
2019-06-28 08:34:26 +00:00
use crate::helpers::DataFactory;
2019-03-07 06:56:34 +00:00
use crate::request::Request;
use crate::response::Response;
use crate::{h1, h2::Dispatcher};
/// `ServiceFactory` HTTP1.1/HTTP2 transport implementation
2019-04-08 21:51:16 +00:00
pub struct HttpService<T, P, S, B, X = h1::ExpectHandler, U = h1::UpgradeHandler<T>> {
2019-03-07 06:56:34 +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>,
2019-07-17 09:48:37 +00:00
on_connect: Option<rc::Rc<dyn Fn(&T) -> Box<dyn DataFactory>>>,
2019-03-11 22:09:42 +00:00
_t: PhantomData<(T, P, B)>,
2019-03-07 06:56:34 +00:00
}
2019-03-11 22:09:42 +00:00
impl<T, S, B> HttpService<T, (), S, B>
where
S: ServiceFactory<Config = SrvConfig, Request = Request>,
S::Error: Into<Error> + Unpin + 'static,
2019-04-05 23:46:44 +00:00
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + Unpin + 'static,
S::Future: Unpin,
S::Service: Unpin,
<S::Service as Service>::Future: Unpin + 'static,
2019-03-11 22:09:42 +00:00
B: MessageBody + 'static,
{
/// Create builder for `HttpService` instance.
pub fn build() -> HttpServiceBuilder<T, S> {
HttpServiceBuilder::new()
}
}
impl<T, P, S, B> HttpService<T, P, S, B>
2019-03-07 06:56:34 +00:00
where
S: ServiceFactory<Config = SrvConfig, Request = Request>,
S::Error: Into<Error> + Unpin + 'static,
2019-04-05 23:46:44 +00:00
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + Unpin + 'static,
S::Future: Unpin,
S::Service: Unpin,
<S::Service as Service>::Future: Unpin + 'static,
2019-03-07 06:56:34 +00:00
B: MessageBody + 'static,
P: Unpin,
2019-03-07 06:56:34 +00:00
{
/// Create new `HttpService` instance.
pub fn new<F: IntoServiceFactory<S>>(service: F) -> Self {
2019-03-07 06:56:34 +00:00
let cfg = ServiceConfig::new(KeepAlive::Timeout(5), 5000, 0);
HttpService {
cfg,
srv: service.into_factory(),
2019-04-05 23:46:44 +00:00
expect: h1::ExpectHandler,
2019-04-08 21:51:16 +00:00
upgrade: None,
2019-06-28 08:34:26 +00:00
on_connect: None,
_t: PhantomData,
}
}
/// Create new `HttpService` instance with config.
pub(crate) fn with_config<F: IntoServiceFactory<S>>(
cfg: ServiceConfig,
service: F,
) -> Self {
HttpService {
cfg,
srv: service.into_factory(),
2019-04-05 23:46:44 +00:00
expect: h1::ExpectHandler,
2019-04-08 21:51:16 +00:00
upgrade: None,
2019-06-28 08:34:26 +00:00
on_connect: None,
2019-03-07 06:56:34 +00:00
_t: PhantomData,
}
}
}
2019-04-08 21:51:16 +00:00
impl<T, P, S, B, X, U> HttpService<T, P, S, B, X, U>
2019-04-05 23:46:44 +00:00
where
S: ServiceFactory<Config = SrvConfig, Request = Request>,
S::Error: Into<Error> + Unpin + 'static,
2019-04-05 23:46:44 +00:00
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + Unpin + 'static,
S::Future: Unpin,
S::Service: Unpin,
<S::Service as Service>::Future: Unpin + 'static,
2019-04-05 23:46:44 +00:00
B: MessageBody,
P: Unpin,
2019-04-05 23:46:44 +00:00
{
/// Provide service for `EXPECT: 100-Continue` support.
///
/// Service get called with request that contains `EXPECT` header.
/// Service must return request in case of success, in that case
/// request will be forwarded to main service.
2019-04-08 21:51:16 +00:00
pub fn expect<X1>(self, expect: X1) -> HttpService<T, P, S, B, X1, U>
2019-04-05 23:46:44 +00:00
where
X1: ServiceFactory<Config = SrvConfig, Request = Request, Response = Request>,
2019-04-08 21:51:16 +00:00
X1::Error: Into<Error>,
X1::InitError: fmt::Debug,
X1::Future: Unpin,
X1::Service: Unpin,
<X1::Service as Service>::Future: Unpin + 'static,
2019-04-05 23:46:44 +00:00
{
HttpService {
expect,
cfg: self.cfg,
srv: self.srv,
2019-04-08 21:51:16 +00:00
upgrade: self.upgrade,
2019-06-28 08:34:26 +00:00
on_connect: self.on_connect,
2019-04-08 21:51:16 +00:00
_t: PhantomData,
}
}
/// Provide service for custom `Connection: UPGRADE` support.
///
/// If service is provided then normal requests handling get halted
/// and this service get called with original request and framed object.
pub fn upgrade<U1>(self, upgrade: Option<U1>) -> HttpService<T, P, S, B, X, U1>
where
U1: ServiceFactory<
2019-05-12 15:34:51 +00:00
Config = SrvConfig,
Request = (Request, Framed<T, h1::Codec>),
Response = (),
>,
2019-04-08 21:51:16 +00:00
U1::Error: fmt::Display,
U1::InitError: fmt::Debug,
U1::Future: Unpin,
U1::Service: Unpin,
<U1::Service as Service>::Future: Unpin + 'static,
2019-04-08 21:51:16 +00:00
{
HttpService {
upgrade,
cfg: self.cfg,
srv: self.srv,
expect: self.expect,
2019-06-28 08:34:26 +00:00
on_connect: self.on_connect,
2019-04-05 23:46:44 +00:00
_t: PhantomData,
}
}
2019-06-28 08:34:26 +00:00
/// Set on connect callback.
pub(crate) fn on_connect(
mut self,
2019-07-17 09:48:37 +00:00
f: Option<rc::Rc<dyn Fn(&T) -> Box<dyn DataFactory>>>,
2019-06-28 08:34:26 +00:00
) -> Self {
self.on_connect = f;
self
}
2019-04-05 23:46:44 +00:00
}
impl<T, P, S, B, X, U> ServiceFactory for HttpService<T, P, S, B, X, U>
2019-03-07 06:56:34 +00:00
where
T: IoStream + Unpin,
S: ServiceFactory<Config = SrvConfig, Request = Request>,
S::Service: Unpin,
S::Error: Into<Error> + Unpin + 'static,
2019-04-05 23:46:44 +00:00
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + Unpin + 'static,
S::Future: Unpin,
S::Service: Unpin,
<S::Service as Service>::Future: Unpin + 'static,
2019-03-07 06:56:34 +00:00
B: MessageBody + 'static,
X: ServiceFactory<Config = SrvConfig, Request = Request, Response = Request>,
2019-04-05 23:46:44 +00:00
X::Error: Into<Error>,
X::InitError: fmt::Debug,
X::Future: Unpin,
X::Service: Unpin,
<X::Service as Service>::Future: Unpin + 'static,
U: ServiceFactory<
2019-05-12 15:34:51 +00:00
Config = SrvConfig,
Request = (Request, Framed<T, h1::Codec>),
Response = (),
>,
2019-04-08 21:51:16 +00:00
U::Error: fmt::Display,
U::InitError: fmt::Debug,
U::Future: Unpin,
U::Service: Unpin,
<U::Service as Service>::Future: Unpin + 'static,
P: Unpin,
2019-03-07 06:56:34 +00:00
{
2019-05-12 15:34:51 +00:00
type Config = SrvConfig;
2019-03-11 22:09:42 +00:00
type Request = ServerIo<T, P>;
2019-03-07 06:56:34 +00:00
type Response = ();
type Error = DispatchError;
2019-04-05 23:46:44 +00:00
type InitError = ();
2019-04-08 21:51:16 +00:00
type Service = HttpServiceHandler<T, P, S::Service, B, X::Service, U::Service>;
type Future = HttpServiceResponse<T, P, S, B, X, U>;
2019-03-07 06:56:34 +00:00
fn new_service(&self, cfg: &SrvConfig) -> Self::Future {
2019-03-07 06:56:34 +00:00
HttpServiceResponse {
fut: self.srv.new_service(cfg),
2019-05-12 15:34:51 +00:00
fut_ex: Some(self.expect.new_service(cfg)),
fut_upg: self.upgrade.as_ref().map(|f| f.new_service(cfg)),
2019-04-05 23:46:44 +00:00
expect: None,
2019-04-08 21:51:16 +00:00
upgrade: None,
2019-06-28 08:34:26 +00:00
on_connect: self.on_connect.clone(),
2019-03-07 06:56:34 +00:00
cfg: Some(self.cfg.clone()),
_t: PhantomData,
}
}
}
#[doc(hidden)]
pub struct HttpServiceResponse<
T,
P,
S: ServiceFactory,
B,
X: ServiceFactory,
U: ServiceFactory,
> {
2019-04-05 23:46:44 +00:00
fut: S::Future,
fut_ex: Option<X::Future>,
2019-04-08 21:51:16 +00:00
fut_upg: Option<U::Future>,
2019-04-05 23:46:44 +00:00
expect: Option<X::Service>,
2019-04-08 21:51:16 +00:00
upgrade: Option<U::Service>,
2019-07-17 09:48:37 +00:00
on_connect: Option<rc::Rc<dyn Fn(&T) -> Box<dyn DataFactory>>>,
2019-03-07 06:56:34 +00:00
cfg: Option<ServiceConfig>,
2019-03-11 22:09:42 +00:00
_t: PhantomData<(T, P, B)>,
2019-03-07 06:56:34 +00:00
}
2019-04-08 21:51:16 +00:00
impl<T, P, S, B, X, U> Future for HttpServiceResponse<T, P, S, B, X, U>
2019-03-07 06:56:34 +00:00
where
T: IoStream,
S: ServiceFactory<Request = Request>,
S::Error: Into<Error> + Unpin + 'static,
2019-04-05 23:46:44 +00:00
S::InitError: fmt::Debug,
S::Response: Into<Response<B>> + Unpin + 'static,
S::Future: Unpin,
S::Service: Unpin,
<S::Service as Service>::Future: Unpin + 'static,
2019-03-07 06:56:34 +00:00
B: MessageBody + 'static,
X: ServiceFactory<Request = Request, Response = Request>,
2019-04-05 23:46:44 +00:00
X::Error: Into<Error>,
X::InitError: fmt::Debug,
X::Future: Unpin,
X::Service: Unpin,
<X::Service as Service>::Future: Unpin + 'static,
U: ServiceFactory<Request = (Request, Framed<T, h1::Codec>), Response = ()>,
2019-04-08 21:51:16 +00:00
U::Error: fmt::Display,
U::InitError: fmt::Debug,
U::Future: Unpin,
U::Service: Unpin,
<U::Service as Service>::Future: Unpin + 'static,
P: Unpin,
2019-03-07 06:56:34 +00:00
{
type Output =
Result<HttpServiceHandler<T, P, S::Service, B, X::Service, U::Service>, ()>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = self.get_mut();
if let Some(ref mut fut) = this.fut_ex {
let expect = ready!(Pin::new(fut)
.poll(cx)
.map_err(|e| log::error!("Init http service error: {:?}", e)))?;
this.expect = Some(expect);
this.fut_ex.take();
2019-04-05 23:46:44 +00:00
}
if let Some(ref mut fut) = this.fut_upg {
let upgrade = ready!(Pin::new(fut)
.poll(cx)
.map_err(|e| log::error!("Init http service error: {:?}", e)))?;
this.upgrade = Some(upgrade);
this.fut_ex.take();
2019-04-08 21:51:16 +00:00
}
let result = ready!(Pin::new(&mut this.fut)
.poll(cx)
2019-04-05 23:46:44 +00:00
.map_err(|e| log::error!("Init http service error: {:?}", e)));
Poll::Ready(result.map(|service| {
HttpServiceHandler::new(
this.cfg.take().unwrap(),
service,
this.expect.take().unwrap(),
this.upgrade.take(),
this.on_connect.clone(),
)
}))
2019-03-07 06:56:34 +00:00
}
}
/// `Service` implementation for http transport
2019-04-08 21:51:16 +00:00
pub struct HttpServiceHandler<T, P, S, B, X, U> {
2019-03-07 06:56:34 +00:00
srv: CloneableService<S>,
2019-04-05 23:46:44 +00:00
expect: CloneableService<X>,
2019-04-08 21:51:16 +00:00
upgrade: Option<CloneableService<U>>,
2019-03-07 06:56:34 +00:00
cfg: ServiceConfig,
2019-07-17 09:48:37 +00:00
on_connect: Option<rc::Rc<dyn Fn(&T) -> Box<dyn DataFactory>>>,
2019-04-05 23:46:44 +00:00
_t: PhantomData<(T, P, B, X)>,
2019-03-07 06:56:34 +00:00
}
2019-04-08 21:51:16 +00:00
impl<T, P, S, B, X, U> HttpServiceHandler<T, P, S, B, X, U>
2019-03-07 06:56:34 +00:00
where
S: Service<Request = Request> + Unpin,
S::Error: Into<Error> + Unpin + 'static,
2019-04-04 17:59:34 +00:00
S::Future: 'static,
S::Response: Into<Response<B>> + Unpin + 'static,
S::Future: Unpin,
2019-03-07 06:56:34 +00:00
B: MessageBody + 'static,
X: Service<Request = Request, Response = Request> + Unpin,
X::Future: Unpin,
2019-04-05 23:46:44 +00:00
X::Error: Into<Error>,
U: Service<Request = (Request, Framed<T, h1::Codec>), Response = ()> + Unpin,
U::Future: Unpin,
2019-04-08 21:51:16 +00:00
U::Error: fmt::Display,
P: Unpin,
2019-03-07 06:56:34 +00:00
{
2019-04-08 21:51:16 +00:00
fn new(
cfg: ServiceConfig,
srv: S,
expect: X,
upgrade: Option<U>,
2019-07-17 09:48:37 +00:00
on_connect: Option<rc::Rc<dyn Fn(&T) -> Box<dyn DataFactory>>>,
2019-04-08 21:51:16 +00:00
) -> HttpServiceHandler<T, P, S, B, X, U> {
2019-03-07 06:56:34 +00:00
HttpServiceHandler {
cfg,
2019-06-28 08:34:26 +00:00
on_connect,
2019-03-07 06:56:34 +00:00
srv: CloneableService::new(srv),
2019-04-05 23:46:44 +00:00
expect: CloneableService::new(expect),
upgrade: upgrade.map(CloneableService::new),
2019-03-07 06:56:34 +00:00
_t: PhantomData,
}
}
}
2019-04-08 21:51:16 +00:00
impl<T, P, S, B, X, U> Service for HttpServiceHandler<T, P, S, B, X, U>
2019-03-07 06:56:34 +00:00
where
T: IoStream + Unpin,
S: Service<Request = Request> + Unpin,
S::Error: Into<Error> + Unpin + 'static,
S::Future: Unpin + 'static,
S::Response: Into<Response<B>> + Unpin + 'static,
2019-03-07 06:56:34 +00:00
B: MessageBody + 'static,
X: Service<Request = Request, Response = Request> + Unpin,
2019-04-05 23:46:44 +00:00
X::Error: Into<Error>,
X::Future: Unpin,
U: Service<Request = (Request, Framed<T, h1::Codec>), Response = ()> + Unpin,
2019-04-08 21:51:16 +00:00
U::Error: fmt::Display,
U::Future: Unpin,
P: Unpin,
2019-03-07 06:56:34 +00:00
{
2019-03-11 22:09:42 +00:00
type Request = ServerIo<T, P>;
2019-03-07 06:56:34 +00:00
type Response = ();
type Error = DispatchError;
2019-04-08 21:51:16 +00:00
type Future = HttpServiceHandlerResponse<T, S, B, X, U>;
2019-03-07 06:56:34 +00:00
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
2019-04-05 23:46:44 +00:00
let ready = self
.expect
.poll_ready(cx)
2019-04-05 23:46:44 +00:00
.map_err(|e| {
let e = e.into();
log::error!("Http service readiness error: {:?}", e);
DispatchError::Service(e)
})?
.is_ready();
let ready = self
.srv
.poll_ready(cx)
2019-04-05 23:46:44 +00:00
.map_err(|e| {
let e = e.into();
log::error!("Http service readiness error: {:?}", e);
DispatchError::Service(e)
})?
.is_ready()
&& ready;
if ready {
Poll::Ready(Ok(()))
2019-04-05 23:46:44 +00:00
} else {
Poll::Pending
2019-04-05 23:46:44 +00:00
}
2019-03-07 06:56:34 +00:00
}
2019-03-11 22:09:42 +00:00
fn call(&mut self, req: Self::Request) -> Self::Future {
let (io, _, proto) = req.into_parts();
2019-06-28 08:34:26 +00:00
let on_connect = if let Some(ref on_connect) = self.on_connect {
Some(on_connect(&io))
} else {
None
};
2019-03-11 22:09:42 +00:00
match proto {
Protocol::Http2 => {
let peer_addr = io.peer_addr();
2019-03-11 22:09:42 +00:00
let io = Io {
inner: io,
unread: None,
};
HttpServiceHandlerResponse {
state: State::Handshake(Some((
server::handshake(io),
self.cfg.clone(),
self.srv.clone(),
peer_addr,
2019-06-28 08:34:26 +00:00
on_connect,
2019-03-11 22:09:42 +00:00
))),
}
}
Protocol::Http10 | Protocol::Http11 => HttpServiceHandlerResponse {
state: State::H1(h1::Dispatcher::new(
io,
self.cfg.clone(),
self.srv.clone(),
2019-04-05 23:46:44 +00:00
self.expect.clone(),
2019-04-08 21:51:16 +00:00
self.upgrade.clone(),
2019-06-28 08:34:26 +00:00
on_connect,
2019-03-11 22:09:42 +00:00
)),
},
_ => HttpServiceHandlerResponse {
state: State::Unknown(Some((
io,
BytesMut::with_capacity(14),
self.cfg.clone(),
self.srv.clone(),
2019-04-05 23:46:44 +00:00
self.expect.clone(),
2019-04-08 21:51:16 +00:00
self.upgrade.clone(),
2019-06-28 08:34:26 +00:00
on_connect,
2019-03-11 22:09:42 +00:00
))),
},
2019-03-07 06:56:34 +00:00
}
}
}
2019-04-08 21:51:16 +00:00
enum State<T, S, B, X, U>
2019-03-07 06:56:34 +00:00
where
S: Service<Request = Request> + Unpin,
S::Future: Unpin + 'static,
2019-04-05 23:46:44 +00:00
S::Error: Into<Error>,
T: IoStream + Unpin,
2019-04-05 23:46:44 +00:00
B: MessageBody,
X: Service<Request = Request, Response = Request> + Unpin,
2019-04-05 23:46:44 +00:00
X::Error: Into<Error>,
X::Future: Unpin,
U: Service<Request = (Request, Framed<T, h1::Codec>), Response = ()> + Unpin,
2019-04-08 21:51:16 +00:00
U::Error: fmt::Display,
U::Future: Unpin,
2019-03-07 06:56:34 +00:00
{
2019-04-08 21:51:16 +00:00
H1(h1::Dispatcher<T, S, B, X, U>),
2019-03-07 06:56:34 +00:00
H2(Dispatcher<Io<T>, S, B>),
2019-04-05 23:46:44 +00:00
Unknown(
Option<(
T,
BytesMut,
ServiceConfig,
CloneableService<S>,
CloneableService<X>,
2019-04-08 21:51:16 +00:00
Option<CloneableService<U>>,
2019-06-28 08:34:26 +00:00
Option<Box<dyn DataFactory>>,
2019-04-05 23:46:44 +00:00
)>,
),
Handshake(
Option<(
Handshake<Io<T>, Bytes>,
ServiceConfig,
CloneableService<S>,
Option<net::SocketAddr>,
2019-06-28 08:34:26 +00:00
Option<Box<dyn DataFactory>>,
)>,
),
2019-03-07 06:56:34 +00:00
}
2019-04-08 21:51:16 +00:00
pub struct HttpServiceHandlerResponse<T, S, B, X, U>
2019-03-07 06:56:34 +00:00
where
T: IoStream + Unpin,
S: Service<Request = Request> + Unpin,
S::Error: Into<Error> + Unpin + 'static,
S::Future: Unpin + 'static,
S::Response: Into<Response<B>> + Unpin + 'static,
2019-03-07 06:56:34 +00:00
B: MessageBody + 'static,
X: Service<Request = Request, Response = Request> + Unpin,
2019-04-05 23:46:44 +00:00
X::Error: Into<Error>,
X::Future: Unpin,
U: Service<Request = (Request, Framed<T, h1::Codec>), Response = ()> + Unpin,
2019-04-08 21:51:16 +00:00
U::Error: fmt::Display,
U::Future: Unpin,
2019-03-07 06:56:34 +00:00
{
2019-04-08 21:51:16 +00:00
state: State<T, S, B, X, U>,
2019-03-07 06:56:34 +00:00
}
const HTTP2_PREFACE: [u8; 14] = *b"PRI * HTTP/2.0";
2019-04-08 21:51:16 +00:00
impl<T, S, B, X, U> Future for HttpServiceHandlerResponse<T, S, B, X, U>
2019-03-07 06:56:34 +00:00
where
T: IoStream + Unpin,
S: Service<Request = Request> + Unpin,
S::Error: Into<Error> + Unpin + 'static,
S::Future: Unpin + 'static,
S::Response: Into<Response<B>> + Unpin + 'static,
2019-03-07 06:56:34 +00:00
B: MessageBody,
X: Service<Request = Request, Response = Request> + Unpin,
X::Future: Unpin,
2019-04-05 23:46:44 +00:00
X::Error: Into<Error>,
U: Service<Request = (Request, Framed<T, h1::Codec>), Response = ()> + Unpin,
U::Future: Unpin,
2019-04-08 21:51:16 +00:00
U::Error: fmt::Display,
2019-03-07 06:56:34 +00:00
{
type Output = Result<(), DispatchError>;
2019-03-07 06:56:34 +00:00
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
2019-03-07 06:56:34 +00:00
match self.state {
State::H1(ref mut disp) => Pin::new(disp).poll(cx),
State::H2(ref mut disp) => Pin::new(disp).poll(cx),
2019-03-07 06:56:34 +00:00
State::Unknown(ref mut data) => {
if let Some(ref mut item) = data {
loop {
// Safety - we only write to the returned slice.
let b = unsafe { item.1.bytes_mut() };
let n = ready!(Pin::new(&mut item.0).poll_read(cx, b))?;
if n == 0 {
return Poll::Ready(Ok(()));
}
// Safety - we know that 'n' bytes have
// been initialized via the contract of
// 'poll_read'
unsafe { item.1.advance_mut(n) };
if item.1.len() >= HTTP2_PREFACE.len() {
break;
2019-03-07 06:56:34 +00:00
}
}
} else {
panic!()
}
2019-06-28 08:34:26 +00:00
let (io, buf, cfg, srv, expect, upgrade, on_connect) =
data.take().unwrap();
2019-03-07 06:56:34 +00:00
if buf[..14] == HTTP2_PREFACE[..] {
let peer_addr = io.peer_addr();
2019-03-07 06:56:34 +00:00
let io = Io {
inner: io,
unread: Some(buf),
};
self.state = State::Handshake(Some((
server::handshake(io),
cfg,
srv,
peer_addr,
2019-06-28 08:34:26 +00:00
on_connect,
)));
2019-03-07 06:56:34 +00:00
} else {
2019-04-06 07:16:04 +00:00
self.state = State::H1(h1::Dispatcher::with_timeout(
2019-03-07 06:56:34 +00:00
io,
h1::Codec::new(cfg.clone()),
2019-04-06 07:16:04 +00:00
cfg,
2019-03-07 06:56:34 +00:00
buf,
2019-04-06 07:16:04 +00:00
None,
srv,
expect,
2019-04-08 21:51:16 +00:00
upgrade,
2019-06-28 08:34:26 +00:00
on_connect,
2019-04-05 23:46:44 +00:00
))
2019-03-07 06:56:34 +00:00
}
self.poll(cx)
2019-03-07 06:56:34 +00:00
}
State::Handshake(ref mut data) => {
let conn = if let Some(ref mut item) = data {
match Pin::new(&mut item.0).poll(cx) {
Poll::Ready(Ok(conn)) => conn,
Poll::Ready(Err(err)) => {
2019-03-07 06:56:34 +00:00
trace!("H2 handshake error: {}", err);
return Poll::Ready(Err(err.into()));
2019-03-07 06:56:34 +00:00
}
Poll::Pending => return Poll::Pending,
2019-03-07 06:56:34 +00:00
}
} else {
panic!()
};
2019-06-28 08:34:26 +00:00
let (_, cfg, srv, peer_addr, on_connect) = data.take().unwrap();
self.state = State::H2(Dispatcher::new(
srv, conn, on_connect, cfg, None, peer_addr,
));
self.poll(cx)
2019-03-07 06:56:34 +00:00
}
}
}
}
/// Wrapper for `AsyncRead + AsyncWrite` types
struct Io<T> {
unread: Option<BytesMut>,
inner: T,
}
impl<T> Unpin for Io<T> {}
2019-03-07 06:56:34 +00:00
impl<T: io::Read> io::Read for Io<T> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if let Some(mut bytes) = self.unread.take() {
let size = std::cmp::min(buf.len(), bytes.len());
buf[..size].copy_from_slice(&bytes[..size]);
if bytes.len() > size {
bytes.split_to(size);
self.unread = Some(bytes);
}
Ok(size)
} else {
self.inner.read(buf)
}
}
}
impl<T: io::Write> io::Write for Io<T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
impl<T: AsyncRead + Unpin> AsyncRead for Io<T> {
// unsafe fn initializer(&self) -> io::Initializer {
// self.get_mut().inner.initializer()
// }
2019-03-07 06:56:34 +00:00
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
self.inner.prepare_uninitialized_buffer(buf)
}
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.get_mut().inner).poll_read(cx, buf)
}
// fn poll_read_vectored(
// self: Pin<&mut Self>,
// cx: &mut Context<'_>,
// bufs: &mut [io::IoSliceMut<'_>],
// ) -> Poll<io::Result<usize>> {
// self.get_mut().inner.poll_read_vectored(cx, bufs)
// }
2019-03-07 06:56:34 +00:00
}
impl<T: AsyncWrite + Unpin> tokio_io::AsyncWrite for Io<T> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.get_mut().inner).poll_write(cx, buf)
2019-03-07 06:56:34 +00:00
}
// fn poll_write_vectored(
// self: Pin<&mut Self>,
// cx: &mut Context<'_>,
// bufs: &[io::IoSlice<'_>],
// ) -> Poll<io::Result<usize>> {
// self.get_mut().inner.poll_write_vectored(cx, bufs)
// }
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().inner).poll_flush(cx)
}
fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().inner).poll_shutdown(cx)
2019-03-07 06:56:34 +00:00
}
}
impl<T: IoStream> actix_server_config::IoStream for Io<T> {
#[inline]
fn peer_addr(&self) -> Option<net::SocketAddr> {
self.inner.peer_addr()
}
#[inline]
fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
self.inner.set_nodelay(nodelay)
}
#[inline]
fn set_linger(&mut self, dur: Option<std::time::Duration>) -> io::Result<()> {
self.inner.set_linger(dur)
}
#[inline]
fn set_keepalive(&mut self, dur: Option<std::time::Duration>) -> io::Result<()> {
self.inner.set_keepalive(dur)
}
}