1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-22 16:16:40 +00:00

simplify openssl acceptor

This commit is contained in:
Nikolay Kim 2018-09-08 10:16:43 -07:00
parent 8b13236d41
commit bf10f6dfcf

View file

@ -2,13 +2,13 @@ use std::io;
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::{future::ok, future::FutureResult, Async, Future, Poll}; use futures::{future::ok, future::FutureResult, Async, Future, Poll};
use openssl::ssl::{AlpnError, Error, SslAcceptor, SslAcceptorBuilder, SslConnector}; use openssl::ssl::{Error, SslAcceptor, SslConnector};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use tokio_openssl::{AcceptAsync, ConnectAsync, SslAcceptorExt, SslConnectorExt, SslStream}; use tokio_openssl::{AcceptAsync, ConnectAsync, SslAcceptorExt, SslConnectorExt, SslStream};
use super::MAX_CONN_COUNTER; use super::MAX_CONN_COUNTER;
use connector::ConnectionInfo; use connector::ConnectionInfo;
use server_service::{Counter, CounterGuard}; use worker::{Connections, ConnectionsGuard};
use {NewService, Service}; use {NewService, Service};
/// Support `SSL` connections via openssl package /// Support `SSL` connections via openssl package
@ -21,32 +21,12 @@ pub struct OpensslAcceptor<T> {
impl<T> OpensslAcceptor<T> { impl<T> OpensslAcceptor<T> {
/// Create default `OpensslAcceptor` /// Create default `OpensslAcceptor`
pub fn new(builder: SslAcceptorBuilder) -> Self { pub fn new(acceptor: SslAcceptor) -> Self {
OpensslAcceptor { OpensslAcceptor {
acceptor: builder.build(), acceptor,
io: PhantomData, io: PhantomData,
} }
} }
/// Create `OpensslWith` with `HTTP1.1` and `HTTP2`.
pub fn for_http(mut builder: SslAcceptorBuilder) -> io::Result<Self> {
let protos = b"\x08http/1.1\x02h2";
builder.set_alpn_select_callback(|_, protos| {
const H2: &[u8] = b"\x02h2";
if protos.windows(3).any(|window| window == H2) {
Ok(b"h2")
} else {
Err(AlpnError::NOACK)
}
});
builder.set_alpn_protos(&protos[..])?;
Ok(OpensslAcceptor {
acceptor: builder.build(),
io: PhantomData,
})
}
} }
impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> { impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
@ -67,11 +47,11 @@ impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
type Future = FutureResult<Self::Service, io::Error>; type Future = FutureResult<Self::Service, io::Error>;
fn new_service(&self) -> Self::Future { fn new_service(&self) -> Self::Future {
MAX_CONN_COUNTER.with(|counter| { MAX_CONN_COUNTER.with(|conns| {
ok(OpensslAcceptorService { ok(OpensslAcceptorService {
acceptor: self.acceptor.clone(), acceptor: self.acceptor.clone(),
conns: conns.clone(),
io: PhantomData, io: PhantomData,
counter: counter.clone(),
}) })
}) })
} }
@ -80,7 +60,7 @@ impl<T: AsyncRead + AsyncWrite> NewService for OpensslAcceptor<T> {
pub struct OpensslAcceptorService<T> { pub struct OpensslAcceptorService<T> {
acceptor: SslAcceptor, acceptor: SslAcceptor,
io: PhantomData<T>, io: PhantomData<T>,
counter: Counter, conns: Connections,
} }
impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> { impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
@ -90,7 +70,7 @@ impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
type Future = OpensslAcceptorServiceFut<T>; type Future = OpensslAcceptorServiceFut<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> { fn poll_ready(&mut self) -> Poll<(), Self::Error> {
if self.counter.check() { if self.conns.check() {
Ok(Async::Ready(())) Ok(Async::Ready(()))
} else { } else {
Ok(Async::NotReady) Ok(Async::NotReady)
@ -99,7 +79,7 @@ impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
fn call(&mut self, req: Self::Request) -> Self::Future { fn call(&mut self, req: Self::Request) -> Self::Future {
OpensslAcceptorServiceFut { OpensslAcceptorServiceFut {
_guard: self.counter.get(), _guard: self.conns.get(),
fut: SslAcceptorExt::accept_async(&self.acceptor, req), fut: SslAcceptorExt::accept_async(&self.acceptor, req),
} }
} }
@ -110,7 +90,7 @@ where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
{ {
fut: AcceptAsync<T>, fut: AcceptAsync<T>,
_guard: CounterGuard, _guard: ConnectionsGuard,
} }
impl<T: AsyncRead + AsyncWrite> Future for OpensslAcceptorServiceFut<T> { impl<T: AsyncRead + AsyncWrite> Future for OpensslAcceptorServiceFut<T> {