From a9e84c58952f6c66272a9b0ad29083aa05578c84 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Tue, 29 Mar 2022 19:45:08 +0300 Subject: [PATCH 1/2] HttpServer::new_async --- actix-web/src/server.rs | 157 +++++++++++++++++++++++----------------- 1 file changed, 90 insertions(+), 67 deletions(-) diff --git a/actix-web/src/server.rs b/actix-web/src/server.rs index 99812600c..77275ebf1 100644 --- a/actix-web/src/server.rs +++ b/actix-web/src/server.rs @@ -1,16 +1,21 @@ use std::{ any::Any, - cmp, fmt, io, + cmp, fmt, + future::Future, + io, marker::PhantomData, net, sync::{Arc, Mutex}, time::Duration, }; -use actix_http::{body::MessageBody, Extensions, HttpService, KeepAlive, Request, Response}; +use actix_http::{ + body::BoxBody, body::MessageBody, Extensions, HttpService, KeepAlive, Request, Response, +}; use actix_server::{Server, ServerBuilder}; use actix_service::{ - map_config, IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt as _, + fn_factory_with_config, map_config, IntoServiceFactory, ServiceFactory, + ServiceFactoryExt as _, }; #[cfg(feature = "openssl")] @@ -18,7 +23,7 @@ use actix_tls::accept::openssl::reexports::{AlpnError, SslAcceptor, SslAcceptorB #[cfg(feature = "rustls")] use actix_tls::accept::rustls::reexports::ServerConfig as RustlsServerConfig; -use crate::{config::AppConfig, Error}; +use crate::{config::AppConfig, Error, HttpResponse}; struct Socket { scheme: &'static str, @@ -49,42 +54,92 @@ struct Config { /// .await /// } /// ``` -pub struct HttpServer -where - F: Fn() -> I + Send + Clone + 'static, - I: IntoServiceFactory, - S: ServiceFactory, - S::Error: Into, - S::InitError: fmt::Debug, - S::Response: Into>, - B: MessageBody, -{ - pub(super) factory: F, +pub struct HttpServer { + pub(super) factory: SF, config: Arc>, backlog: u32, sockets: Vec, builder: ServerBuilder, #[allow(clippy::type_complexity)] on_connect_fn: Option>, - _phantom: PhantomData<(S, B)>, + _phantom: PhantomData<(B,)>, } -impl HttpServer +impl HttpServer { + /// Create new HTTP server with application factory + pub fn new( + factory_fn: F, + ) -> HttpServer< + impl ServiceFactory< + Request, + Config = AppConfig, + Error = HttpResponse, + InitError = SF::InitError, + Response = SF::Response, + > + Send + + Clone, + B, + > + where + F: FnOnce() -> I + Send + Clone + 'static, + I: IntoServiceFactory, + SF: ServiceFactory + 'static, + SF::Error: Into, + SF::InitError: fmt::Debug, + SF::Response: Into>, + B: MessageBody + 'static, + { + let factory = fn_factory_with_config(move |cfg| { + let factory_fn = factory_fn.clone(); + async move { factory_fn().into_factory().new_service(cfg).await } + }) + .map_err(|err| err.into().error_response()); + + HttpServer::new2(factory) + } + + pub fn new_async( + factory_fn: F, + ) -> HttpServer< + impl ServiceFactory< + Request, + Config = AppConfig, + Error = HttpResponse, + InitError = SF::InitError, + Response = SF::Response, + > + Send + + Clone, + B, + > + where + F: FnOnce() -> Fut + Send + Clone + 'static, + Fut: Future, + Fut::Output: IntoServiceFactory, + SF: ServiceFactory + 'static, + SF::Error: Into, + SF::InitError: fmt::Debug, + SF::Response: Into>, + B: MessageBody + 'static, + { + let factory = fn_factory_with_config(move |cfg| { + let factory_fn = factory_fn.clone(); + async move { factory_fn().await.into_factory().new_service(cfg).await } + }) + .map_err(|err| err.into().error_response()); + + HttpServer::new2(factory) + } +} + +impl HttpServer where - F: Fn() -> I + Send + Clone + 'static, - I: IntoServiceFactory, - - S: ServiceFactory + 'static, - S::Error: Into + 'static, - S::InitError: fmt::Debug, - S::Response: Into> + 'static, - >::Future: 'static, - S::Service: 'static, - + SF: ServiceFactory + Send + Clone + 'static, + SF::Error: Into>, + SF::InitError: fmt::Debug, + SF::Response: Into>, B: MessageBody + 'static, { - /// Create new HTTP server with application factory - pub fn new(factory: F) -> Self { + fn new2(factory: SF) -> Self { HttpServer { factory, config: Arc::new(Mutex::new(Config { @@ -111,7 +166,7 @@ where /// - `actix_web::rt::net::TcpStream` when no encryption is used. /// /// See the `on_connect` example for additional details. - pub fn on_connect(self, f: CB) -> HttpServer + pub fn on_connect(self, f: CB) -> Self where CB: Fn(&dyn Any, &mut Extensions) + Send + Sync + 'static, { @@ -314,11 +369,7 @@ where }) }; - let fac = factory() - .into_factory() - .map_err(|err| err.into().error_response()); - - svc.finish(map_config(fac, move |_| { + svc.finish(map_config(factory.clone(), move |_| { AppConfig::new(false, host.clone(), addr) })) .tcp() @@ -372,11 +423,7 @@ where svc }; - let fac = factory() - .into_factory() - .map_err(|err| err.into().error_response()); - - svc.finish(map_config(fac, move |_| { + svc.finish(map_config(factory.clone(), move |_| { AppConfig::new(true, host.clone(), addr) })) .openssl(acceptor.clone()) @@ -430,11 +477,7 @@ where svc }; - let fac = factory() - .into_factory() - .map_err(|err| err.into().error_response()); - - svc.finish(map_config(fac, move |_| { + svc.finish(map_config(factory.clone(), move |_| { AppConfig::new(true, host.clone(), addr) })) .rustls(config.clone()) @@ -556,11 +599,7 @@ where .on_connect_ext(move |io: &_, ext: _| (handler)(io as &dyn Any, ext)); } - let fac = factory() - .into_factory() - .map_err(|err| err.into().error_response()); - - svc.finish(map_config(fac, move |_| config.clone())) + svc.finish(map_config(factory.clone(), move |_| config.clone())) }) })?; Ok(self) @@ -597,35 +636,19 @@ where socket_addr, ); - let fac = factory() - .into_factory() - .map_err(|err| err.into().error_response()); - fn_service(|io: UnixStream| async { Ok((io, Protocol::Http1, None)) }).and_then( HttpService::build() .keep_alive(c.keep_alive) .client_request_timeout(c.client_request_timeout) .client_disconnect_timeout(c.client_disconnect_timeout) - .finish(map_config(fac, move |_| config.clone())), + .finish(map_config(factory.clone(), move |_| config.clone())), ) }, )?; Ok(self) } -} -impl HttpServer -where - F: Fn() -> I + Send + Clone + 'static, - I: IntoServiceFactory, - S: ServiceFactory, - S::Error: Into, - S::InitError: fmt::Debug, - S::Response: Into>, - S::Service: 'static, - B: MessageBody, -{ /// Start listening for incoming connections. /// /// This method starts number of HTTP workers in separate threads. From 4b098d31ae39b7978a057ffbbdc20ab247fd599c Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Tue, 29 Mar 2022 20:25:44 +0300 Subject: [PATCH 2/2] remove B generic --- actix-web/src/server.rs | 42 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/actix-web/src/server.rs b/actix-web/src/server.rs index 77275ebf1..5edb0e7c3 100644 --- a/actix-web/src/server.rs +++ b/actix-web/src/server.rs @@ -2,9 +2,7 @@ use std::{ any::Any, cmp, fmt, future::Future, - io, - marker::PhantomData, - net, + io, net, sync::{Arc, Mutex}, time::Duration, }; @@ -23,7 +21,7 @@ use actix_tls::accept::openssl::reexports::{AlpnError, SslAcceptor, SslAcceptorB #[cfg(feature = "rustls")] use actix_tls::accept::rustls::reexports::ServerConfig as RustlsServerConfig; -use crate::{config::AppConfig, Error, HttpResponse}; +use crate::{config::AppConfig, Error}; struct Socket { scheme: &'static str, @@ -54,7 +52,7 @@ struct Config { /// .await /// } /// ``` -pub struct HttpServer { +pub struct HttpServer { pub(super) factory: SF, config: Arc>, backlog: u32, @@ -62,10 +60,9 @@ pub struct HttpServer { builder: ServerBuilder, #[allow(clippy::type_complexity)] on_connect_fn: Option>, - _phantom: PhantomData<(B,)>, } -impl HttpServer { +impl HttpServer<()> { /// Create new HTTP server with application factory pub fn new( factory_fn: F, @@ -73,12 +70,11 @@ impl HttpServer { impl ServiceFactory< Request, Config = AppConfig, - Error = HttpResponse, + Error = Response, InitError = SF::InitError, - Response = SF::Response, + Response = Response, > + Send + Clone, - B, > where F: FnOnce() -> I + Send + Clone + 'static, @@ -93,7 +89,8 @@ impl HttpServer { let factory_fn = factory_fn.clone(); async move { factory_fn().into_factory().new_service(cfg).await } }) - .map_err(|err| err.into().error_response()); + .map(|resp| resp.into()) + .map_err(|err| err.into().error_response().into()); HttpServer::new2(factory) } @@ -104,12 +101,11 @@ impl HttpServer { impl ServiceFactory< Request, Config = AppConfig, - Error = HttpResponse, + Error = Response, InitError = SF::InitError, - Response = SF::Response, + Response = Response, > + Send + Clone, - B, > where F: FnOnce() -> Fut + Send + Clone + 'static, @@ -125,18 +121,24 @@ impl HttpServer { let factory_fn = factory_fn.clone(); async move { factory_fn().await.into_factory().new_service(cfg).await } }) - .map_err(|err| err.into().error_response()); + .map(|resp| resp.into()) + .map_err(|err| err.into().error_response().into()); HttpServer::new2(factory) } } -impl HttpServer +impl HttpServer where - SF: ServiceFactory + Send + Clone + 'static, - SF::Error: Into>, + SF: ServiceFactory< + Request, + Config = AppConfig, + Response = Response, + Error = Response, + > + Send + + Clone + + 'static, SF::InitError: fmt::Debug, - SF::Response: Into>, B: MessageBody + 'static, { fn new2(factory: SF) -> Self { @@ -152,7 +154,6 @@ where sockets: Vec::new(), builder: ServerBuilder::default(), on_connect_fn: None, - _phantom: PhantomData, } } @@ -177,7 +178,6 @@ where sockets: self.sockets, builder: self.builder, on_connect_fn: Some(Arc::new(f)), - _phantom: PhantomData, } }