mirror of https://github.com/fafhrd91/actix-web
Merge 4b098d31ae
into 265fa0d050
This commit is contained in:
commit
fbd6bfbecf
|
@ -1,16 +1,19 @@
|
||||||
use std::{
|
use std::{
|
||||||
any::Any,
|
any::Any,
|
||||||
cmp, fmt, io,
|
cmp, fmt,
|
||||||
marker::PhantomData,
|
future::Future,
|
||||||
net,
|
io, net,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
time::Duration,
|
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_server::{Server, ServerBuilder};
|
||||||
use actix_service::{
|
use actix_service::{
|
||||||
map_config, IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt as _,
|
fn_factory_with_config, map_config, IntoServiceFactory, ServiceFactory,
|
||||||
|
ServiceFactoryExt as _,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
|
@ -49,42 +52,96 @@ struct Config {
|
||||||
/// .await
|
/// .await
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub struct HttpServer<F, I, S, B>
|
pub struct HttpServer<SF> {
|
||||||
where
|
pub(super) factory: SF,
|
||||||
F: Fn() -> I + Send + Clone + 'static,
|
|
||||||
I: IntoServiceFactory<S, Request>,
|
|
||||||
S: ServiceFactory<Request, Config = AppConfig>,
|
|
||||||
S::Error: Into<Error>,
|
|
||||||
S::InitError: fmt::Debug,
|
|
||||||
S::Response: Into<Response<B>>,
|
|
||||||
B: MessageBody,
|
|
||||||
{
|
|
||||||
pub(super) factory: F,
|
|
||||||
config: Arc<Mutex<Config>>,
|
config: Arc<Mutex<Config>>,
|
||||||
backlog: u32,
|
backlog: u32,
|
||||||
sockets: Vec<Socket>,
|
sockets: Vec<Socket>,
|
||||||
builder: ServerBuilder,
|
builder: ServerBuilder,
|
||||||
#[allow(clippy::type_complexity)]
|
#[allow(clippy::type_complexity)]
|
||||||
on_connect_fn: Option<Arc<dyn Fn(&dyn Any, &mut Extensions) + Send + Sync>>,
|
on_connect_fn: Option<Arc<dyn Fn(&dyn Any, &mut Extensions) + Send + Sync>>,
|
||||||
_phantom: PhantomData<(S, B)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, I, S, B> HttpServer<F, I, S, B>
|
impl HttpServer<()> {
|
||||||
|
/// Create new HTTP server with application factory
|
||||||
|
pub fn new<F, I, SF, B>(
|
||||||
|
factory_fn: F,
|
||||||
|
) -> HttpServer<
|
||||||
|
impl ServiceFactory<
|
||||||
|
Request,
|
||||||
|
Config = AppConfig,
|
||||||
|
Error = Response<BoxBody>,
|
||||||
|
InitError = SF::InitError,
|
||||||
|
Response = Response<B>,
|
||||||
|
> + Send
|
||||||
|
+ Clone,
|
||||||
|
>
|
||||||
where
|
where
|
||||||
F: Fn() -> I + Send + Clone + 'static,
|
F: FnOnce() -> I + Send + Clone + 'static,
|
||||||
I: IntoServiceFactory<S, Request>,
|
I: IntoServiceFactory<SF, Request>,
|
||||||
|
SF: ServiceFactory<Request, Config = AppConfig> + 'static,
|
||||||
S: ServiceFactory<Request, Config = AppConfig> + 'static,
|
SF::Error: Into<Error>,
|
||||||
S::Error: Into<Error> + 'static,
|
SF::InitError: fmt::Debug,
|
||||||
S::InitError: fmt::Debug,
|
SF::Response: Into<Response<B>>,
|
||||||
S::Response: Into<Response<B>> + 'static,
|
|
||||||
<S::Service as Service<Request>>::Future: 'static,
|
|
||||||
S::Service: 'static,
|
|
||||||
|
|
||||||
B: MessageBody + 'static,
|
B: MessageBody + 'static,
|
||||||
{
|
{
|
||||||
/// Create new HTTP server with application factory
|
let factory = fn_factory_with_config(move |cfg| {
|
||||||
pub fn new(factory: F) -> Self {
|
let factory_fn = factory_fn.clone();
|
||||||
|
async move { factory_fn().into_factory().new_service(cfg).await }
|
||||||
|
})
|
||||||
|
.map(|resp| resp.into())
|
||||||
|
.map_err(|err| err.into().error_response().into());
|
||||||
|
|
||||||
|
HttpServer::new2(factory)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_async<F, Fut, SF, B>(
|
||||||
|
factory_fn: F,
|
||||||
|
) -> HttpServer<
|
||||||
|
impl ServiceFactory<
|
||||||
|
Request,
|
||||||
|
Config = AppConfig,
|
||||||
|
Error = Response<BoxBody>,
|
||||||
|
InitError = SF::InitError,
|
||||||
|
Response = Response<B>,
|
||||||
|
> + Send
|
||||||
|
+ Clone,
|
||||||
|
>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> Fut + Send + Clone + 'static,
|
||||||
|
Fut: Future,
|
||||||
|
Fut::Output: IntoServiceFactory<SF, Request>,
|
||||||
|
SF: ServiceFactory<Request, Config = AppConfig> + 'static,
|
||||||
|
SF::Error: Into<Error>,
|
||||||
|
SF::InitError: fmt::Debug,
|
||||||
|
SF::Response: Into<Response<B>>,
|
||||||
|
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(|resp| resp.into())
|
||||||
|
.map_err(|err| err.into().error_response().into());
|
||||||
|
|
||||||
|
HttpServer::new2(factory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<SF, B> HttpServer<SF>
|
||||||
|
where
|
||||||
|
SF: ServiceFactory<
|
||||||
|
Request,
|
||||||
|
Config = AppConfig,
|
||||||
|
Response = Response<B>,
|
||||||
|
Error = Response<BoxBody>,
|
||||||
|
> + Send
|
||||||
|
+ Clone
|
||||||
|
+ 'static,
|
||||||
|
SF::InitError: fmt::Debug,
|
||||||
|
B: MessageBody + 'static,
|
||||||
|
{
|
||||||
|
fn new2(factory: SF) -> Self {
|
||||||
HttpServer {
|
HttpServer {
|
||||||
factory,
|
factory,
|
||||||
config: Arc::new(Mutex::new(Config {
|
config: Arc::new(Mutex::new(Config {
|
||||||
|
@ -97,7 +154,6 @@ where
|
||||||
sockets: Vec::new(),
|
sockets: Vec::new(),
|
||||||
builder: ServerBuilder::default(),
|
builder: ServerBuilder::default(),
|
||||||
on_connect_fn: None,
|
on_connect_fn: None,
|
||||||
_phantom: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +167,7 @@ where
|
||||||
/// - `actix_web::rt::net::TcpStream` when no encryption is used.
|
/// - `actix_web::rt::net::TcpStream` when no encryption is used.
|
||||||
///
|
///
|
||||||
/// See the `on_connect` example for additional details.
|
/// See the `on_connect` example for additional details.
|
||||||
pub fn on_connect<CB>(self, f: CB) -> HttpServer<F, I, S, B>
|
pub fn on_connect<CB>(self, f: CB) -> Self
|
||||||
where
|
where
|
||||||
CB: Fn(&dyn Any, &mut Extensions) + Send + Sync + 'static,
|
CB: Fn(&dyn Any, &mut Extensions) + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
|
@ -122,7 +178,6 @@ where
|
||||||
sockets: self.sockets,
|
sockets: self.sockets,
|
||||||
builder: self.builder,
|
builder: self.builder,
|
||||||
on_connect_fn: Some(Arc::new(f)),
|
on_connect_fn: Some(Arc::new(f)),
|
||||||
_phantom: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -314,11 +369,7 @@ where
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
let fac = factory()
|
svc.finish(map_config(factory.clone(), move |_| {
|
||||||
.into_factory()
|
|
||||||
.map_err(|err| err.into().error_response());
|
|
||||||
|
|
||||||
svc.finish(map_config(fac, move |_| {
|
|
||||||
AppConfig::new(false, host.clone(), addr)
|
AppConfig::new(false, host.clone(), addr)
|
||||||
}))
|
}))
|
||||||
.tcp()
|
.tcp()
|
||||||
|
@ -372,11 +423,7 @@ where
|
||||||
svc
|
svc
|
||||||
};
|
};
|
||||||
|
|
||||||
let fac = factory()
|
svc.finish(map_config(factory.clone(), move |_| {
|
||||||
.into_factory()
|
|
||||||
.map_err(|err| err.into().error_response());
|
|
||||||
|
|
||||||
svc.finish(map_config(fac, move |_| {
|
|
||||||
AppConfig::new(true, host.clone(), addr)
|
AppConfig::new(true, host.clone(), addr)
|
||||||
}))
|
}))
|
||||||
.openssl(acceptor.clone())
|
.openssl(acceptor.clone())
|
||||||
|
@ -430,11 +477,7 @@ where
|
||||||
svc
|
svc
|
||||||
};
|
};
|
||||||
|
|
||||||
let fac = factory()
|
svc.finish(map_config(factory.clone(), move |_| {
|
||||||
.into_factory()
|
|
||||||
.map_err(|err| err.into().error_response());
|
|
||||||
|
|
||||||
svc.finish(map_config(fac, move |_| {
|
|
||||||
AppConfig::new(true, host.clone(), addr)
|
AppConfig::new(true, host.clone(), addr)
|
||||||
}))
|
}))
|
||||||
.rustls(config.clone())
|
.rustls(config.clone())
|
||||||
|
@ -556,11 +599,7 @@ where
|
||||||
.on_connect_ext(move |io: &_, ext: _| (handler)(io as &dyn Any, ext));
|
.on_connect_ext(move |io: &_, ext: _| (handler)(io as &dyn Any, ext));
|
||||||
}
|
}
|
||||||
|
|
||||||
let fac = factory()
|
svc.finish(map_config(factory.clone(), move |_| config.clone()))
|
||||||
.into_factory()
|
|
||||||
.map_err(|err| err.into().error_response());
|
|
||||||
|
|
||||||
svc.finish(map_config(fac, move |_| config.clone()))
|
|
||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
|
@ -597,35 +636,19 @@ where
|
||||||
socket_addr,
|
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(
|
fn_service(|io: UnixStream| async { Ok((io, Protocol::Http1, None)) }).and_then(
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.keep_alive(c.keep_alive)
|
.keep_alive(c.keep_alive)
|
||||||
.client_request_timeout(c.client_request_timeout)
|
.client_request_timeout(c.client_request_timeout)
|
||||||
.client_disconnect_timeout(c.client_disconnect_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)
|
Ok(self)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, I, S, B> HttpServer<F, I, S, B>
|
|
||||||
where
|
|
||||||
F: Fn() -> I + Send + Clone + 'static,
|
|
||||||
I: IntoServiceFactory<S, Request>,
|
|
||||||
S: ServiceFactory<Request, Config = AppConfig>,
|
|
||||||
S::Error: Into<Error>,
|
|
||||||
S::InitError: fmt::Debug,
|
|
||||||
S::Response: Into<Response<B>>,
|
|
||||||
S::Service: 'static,
|
|
||||||
B: MessageBody,
|
|
||||||
{
|
|
||||||
/// Start listening for incoming connections.
|
/// Start listening for incoming connections.
|
||||||
///
|
///
|
||||||
/// This method starts number of HTTP workers in separate threads.
|
/// This method starts number of HTTP workers in separate threads.
|
||||||
|
|
Loading…
Reference in New Issue