diff --git a/src/server/http.rs b/src/server/http.rs index 6432f18f..22537cb8 100644 --- a/src/server/http.rs +++ b/src/server/http.rs @@ -1,13 +1,11 @@ use std::{io, mem, net}; use actix::{Addr, System}; -use actix_net::server; -use actix_net::service::NewService; +use actix_net::server::Server; use actix_net::ssl; use net2::TcpBuilder; use num_cpus; -use tokio_tcp::TcpStream; #[cfg(feature = "tls")] use native_tls::TlsAcceptor; @@ -21,7 +19,7 @@ use rustls::ServerConfig; use super::acceptor::{AcceptorServiceFactory, DefaultAcceptor}; use super::builder::DefaultPipelineFactory; use super::builder::{HttpServiceBuilder, ServiceProvider}; -use super::{IntoHttpHandler, IoStream, KeepAlive}; +use super::{IntoHttpHandler, KeepAlive}; struct Socket { scheme: &'static str, @@ -233,15 +231,9 @@ where #[doc(hidden)] /// Use listener for accepting incoming connection requests - pub(crate) fn listen_with( - mut self, lst: net::TcpListener, acceptor: A, - ) -> Self + pub(crate) fn listen_with(mut self, lst: net::TcpListener, acceptor: A) -> Self where - A: AcceptorServiceFactory, - T: NewService - + Clone - + 'static, - Io: IoStream + Send, + A: AcceptorServiceFactory, { let addr = lst.local_addr().unwrap(); self.sockets.push(Socket { @@ -266,18 +258,9 @@ where pub fn listen_tls(mut self, lst: net::TcpListener, acceptor: TlsAcceptor) -> Self { use actix_net::service::NewServiceExt; - let addr = lst.local_addr().unwrap(); - self.sockets.push(Socket { - lst, - addr, - scheme: "https", - handler: Box::new(HttpServiceBuilder::new( - self.factory.clone(), - move || ssl::NativeTlsAcceptor::new(acceptor.clone()).map_err(|_| ()), - DefaultPipelineFactory::new(), - )), - }); - self + self.listen_with(lst, move || { + ssl::NativeTlsAcceptor::new(acceptor.clone()).map_err(|_| ()) + }) } #[cfg(any(feature = "alpn", feature = "ssl"))] @@ -285,7 +268,7 @@ where /// /// This method sets alpn protocols to "h2" and "http/1.1" pub fn listen_ssl( - mut self, lst: net::TcpListener, builder: SslAcceptorBuilder, + self, lst: net::TcpListener, builder: SslAcceptorBuilder, ) -> io::Result { use super::{openssl_acceptor_with_flags, ServerFlags}; use actix_net::service::NewServiceExt; @@ -297,20 +280,9 @@ where }; let acceptor = openssl_acceptor_with_flags(builder, flags)?; - - let addr = lst.local_addr().unwrap(); - self.sockets.push(Socket { - lst, - addr, - scheme: "https", - handler: Box::new(HttpServiceBuilder::new( - self.factory.clone(), - move || ssl::OpensslAcceptor::new(acceptor.clone()).map_err(|_| ()), - DefaultPipelineFactory::new(), - )), - }); - - Ok(self) + Ok(self.listen_with(lst, move || { + ssl::OpensslAcceptor::new(acceptor.clone()).map_err(|_| ()) + })) } #[cfg(feature = "rust-tls")] @@ -328,22 +300,9 @@ where ServerFlags::HTTP1 | ServerFlags::HTTP2 }; - let addr = lst.local_addr().unwrap(); - self.sockets.push(Socket { - lst, - addr, - scheme: "https", - handler: Box::new(HttpServiceBuilder::new( - self.factory.clone(), - move || { - RustlsAcceptor::with_flags(config.clone(), flags).map_err(|_| ()) - }, - DefaultPipelineFactory::new(), - )), - }); - - //Ok(self) - self + self.listen_with(lst, move || { + RustlsAcceptor::with_flags(config.clone(), flags).map_err(|_| ()) + }) } /// The socket address to bind @@ -416,33 +375,32 @@ where } } - // #[cfg(feature = "tls")] - // /// The ssl socket address to bind - // /// - // /// To bind multiple addresses this method can be called multiple times. - // pub fn bind_tls( - // self, addr: S, acceptor: TlsAcceptor, - // ) -> io::Result { - // use super::NativeTlsAcceptor; + #[cfg(feature = "tls")] + /// The ssl socket address to bind + /// + /// To bind multiple addresses this method can be called multiple times. + pub fn bind_tls( + self, addr: S, acceptor: TlsAcceptor, + ) -> io::Result { + use actix_net::service::NewServiceExt; + use actix_net::ssl::NativeTlsAcceptor; - // self.bind_with(addr, NativeTlsAcceptor::new(acceptor)) - // } + self.bind_with(addr, move || { + NativeTlsAcceptor::new(acceptor.clone()).map_err(|_| ()) + }) + } #[cfg(any(feature = "alpn", feature = "ssl"))] /// Start listening for incoming tls connections. /// /// This method sets alpn protocols to "h2" and "http/1.1" - pub fn bind_ssl( - mut self, addr: S, builder: SslAcceptorBuilder, - ) -> io::Result + pub fn bind_ssl(self, addr: S, builder: SslAcceptorBuilder) -> io::Result where S: net::ToSocketAddrs, { use super::{openssl_acceptor_with_flags, ServerFlags}; use actix_net::service::NewServiceExt; - let sockets = self.bind2(addr)?; - // alpn support let flags = if !self.no_http2 { ServerFlags::HTTP1 @@ -451,43 +409,32 @@ where }; let acceptor = openssl_acceptor_with_flags(builder, flags)?; - - for lst in sockets { - let addr = lst.local_addr().unwrap(); - let accpt = acceptor.clone(); - self.sockets.push(Socket { - lst, - addr, - scheme: "https", - handler: Box::new(HttpServiceBuilder::new( - self.factory.clone(), - move || ssl::OpensslAcceptor::new(accpt.clone()).map_err(|_| ()), - DefaultPipelineFactory::new(), - )), - }); - } - - Ok(self) + self.bind_with(addr, move || { + ssl::OpensslAcceptor::new(acceptor.clone()).map_err(|_| ()) + }) } - // #[cfg(feature = "rust-tls")] - // /// Start listening for incoming tls connections. - // /// - // /// This method sets alpn protocols to "h2" and "http/1.1" - // pub fn bind_rustls( - // self, addr: S, builder: ServerConfig, - // ) -> io::Result { - // use super::{RustlsAcceptor, ServerFlags}; + #[cfg(feature = "rust-tls")] + /// Start listening for incoming tls connections. + /// + /// This method sets alpn protocols to "h2" and "http/1.1" + pub fn bind_rustls( + self, addr: S, builder: ServerConfig, + ) -> io::Result { + use super::{RustlsAcceptor, ServerFlags}; + use actix_net::service::NewServiceExt; - // // alpn support - // let flags = if !self.no_http2 { - // ServerFlags::HTTP1 - // } else { - // ServerFlags::HTTP1 | ServerFlags::HTTP2 - // }; + // alpn support + let flags = if !self.no_http2 { + ServerFlags::HTTP1 + } else { + ServerFlags::HTTP1 | ServerFlags::HTTP2 + }; - // self.bind_with(addr, RustlsAcceptor::with_flags(builder, flags)) - // } + self.bind_with(addr, move || { + RustlsAcceptor::with_flags(builder.clone(), flags).map_err(|_| ()) + }) + } } impl H + Send + Clone> HttpServer { @@ -516,10 +463,10 @@ impl H + Send + Clone> HttpServer { /// sys.run(); // <- Run actix system, this method starts all async processes /// } /// ``` - pub fn start(mut self) -> Addr { + pub fn start(mut self) -> Addr { ssl::max_concurrent_ssl_connect(self.maxconnrate); - let mut srv = server::Server::new() + let mut srv = Server::new() .workers(self.threads) .maxconn(self.maxconn) .shutdown_timeout(self.shutdown_timeout);