mirror of
https://github.com/actix/actix-web.git
synced 2025-01-04 06:18:51 +00:00
AppConfig::secure() is always false. #1202
This commit is contained in:
parent
fbbb4a86e9
commit
a08d8dab70
7 changed files with 135 additions and 91 deletions
|
@ -6,6 +6,11 @@
|
||||||
|
|
||||||
* Move `BodyEncoding` to `dev` module #1220
|
* Move `BodyEncoding` to `dev` module #1220
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fix `AppConfig::secure()` is always false. #1202
|
||||||
|
|
||||||
|
|
||||||
## [2.0.0-alpha.6] - 2019-12-15
|
## [2.0.0-alpha.6] - 2019-12-15
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
19
src/app.rs
19
src/app.rs
|
@ -12,7 +12,7 @@ use actix_service::{
|
||||||
use futures::future::{FutureExt, LocalBoxFuture};
|
use futures::future::{FutureExt, LocalBoxFuture};
|
||||||
|
|
||||||
use crate::app_service::{AppEntry, AppInit, AppRoutingFactory};
|
use crate::app_service::{AppEntry, AppInit, AppRoutingFactory};
|
||||||
use crate::config::{AppConfig, AppConfigInner, ServiceConfig};
|
use crate::config::ServiceConfig;
|
||||||
use crate::data::{Data, DataFactory};
|
use crate::data::{Data, DataFactory};
|
||||||
use crate::dev::ResourceDef;
|
use crate::dev::ResourceDef;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
@ -36,7 +36,6 @@ pub struct App<T, B> {
|
||||||
factory_ref: Rc<RefCell<Option<AppRoutingFactory>>>,
|
factory_ref: Rc<RefCell<Option<AppRoutingFactory>>>,
|
||||||
data: Vec<Box<dyn DataFactory>>,
|
data: Vec<Box<dyn DataFactory>>,
|
||||||
data_factories: Vec<FnDataFactory>,
|
data_factories: Vec<FnDataFactory>,
|
||||||
config: AppConfigInner,
|
|
||||||
external: Vec<ResourceDef>,
|
external: Vec<ResourceDef>,
|
||||||
_t: PhantomData<B>,
|
_t: PhantomData<B>,
|
||||||
}
|
}
|
||||||
|
@ -52,7 +51,6 @@ impl App<AppEntry, Body> {
|
||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
default: None,
|
default: None,
|
||||||
factory_ref: fref,
|
factory_ref: fref,
|
||||||
config: AppConfigInner::default(),
|
|
||||||
external: Vec::new(),
|
external: Vec::new(),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
|
@ -225,18 +223,6 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set server host name.
|
|
||||||
///
|
|
||||||
/// Host name is used by application router as a hostname for url generation.
|
|
||||||
/// Check [ConnectionInfo](./dev/struct.ConnectionInfo.html#method.host)
|
|
||||||
/// documentation for more information.
|
|
||||||
///
|
|
||||||
/// By default host name is set to a "localhost" value.
|
|
||||||
pub fn hostname(mut self, val: &str) -> Self {
|
|
||||||
self.config.host = val.to_owned();
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Default service to be used if no matching resource could be found.
|
/// Default service to be used if no matching resource could be found.
|
||||||
///
|
///
|
||||||
/// It is possible to use services like `Resource`, `Route`.
|
/// It is possible to use services like `Resource`, `Route`.
|
||||||
|
@ -383,7 +369,6 @@ where
|
||||||
services: self.services,
|
services: self.services,
|
||||||
default: self.default,
|
default: self.default,
|
||||||
factory_ref: self.factory_ref,
|
factory_ref: self.factory_ref,
|
||||||
config: self.config,
|
|
||||||
external: self.external,
|
external: self.external,
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
|
@ -445,7 +430,6 @@ where
|
||||||
services: self.services,
|
services: self.services,
|
||||||
default: self.default,
|
default: self.default,
|
||||||
factory_ref: self.factory_ref,
|
factory_ref: self.factory_ref,
|
||||||
config: self.config,
|
|
||||||
external: self.external,
|
external: self.external,
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
|
@ -472,7 +456,6 @@ where
|
||||||
external: RefCell::new(self.external),
|
external: RefCell::new(self.external),
|
||||||
default: self.default,
|
default: self.default,
|
||||||
factory_ref: self.factory_ref,
|
factory_ref: self.factory_ref,
|
||||||
config: RefCell::new(AppConfig(Rc::new(self.config))),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,6 @@ where
|
||||||
pub(crate) endpoint: T,
|
pub(crate) endpoint: T,
|
||||||
pub(crate) data: Rc<Vec<Box<dyn DataFactory>>>,
|
pub(crate) data: Rc<Vec<Box<dyn DataFactory>>>,
|
||||||
pub(crate) data_factories: Rc<Vec<FnDataFactory>>,
|
pub(crate) data_factories: Rc<Vec<FnDataFactory>>,
|
||||||
pub(crate) config: RefCell<AppConfig>,
|
|
||||||
pub(crate) services: Rc<RefCell<Vec<Box<dyn AppServiceFactory>>>>,
|
pub(crate) services: Rc<RefCell<Vec<Box<dyn AppServiceFactory>>>>,
|
||||||
pub(crate) default: Option<Rc<HttpNewService>>,
|
pub(crate) default: Option<Rc<HttpNewService>>,
|
||||||
pub(crate) factory_ref: Rc<RefCell<Option<AppRoutingFactory>>>,
|
pub(crate) factory_ref: Rc<RefCell<Option<AppRoutingFactory>>>,
|
||||||
|
@ -58,7 +57,7 @@ where
|
||||||
InitError = (),
|
InitError = (),
|
||||||
>,
|
>,
|
||||||
{
|
{
|
||||||
type Config = ();
|
type Config = AppConfig;
|
||||||
type Request = Request;
|
type Request = Request;
|
||||||
type Response = ServiceResponse<B>;
|
type Response = ServiceResponse<B>;
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
|
@ -66,7 +65,7 @@ where
|
||||||
type Service = AppInitService<T::Service, B>;
|
type Service = AppInitService<T::Service, B>;
|
||||||
type Future = AppInitResult<T, B>;
|
type Future = AppInitResult<T, B>;
|
||||||
|
|
||||||
fn new_service(&self, _: ()) -> Self::Future {
|
fn new_service(&self, config: AppConfig) -> Self::Future {
|
||||||
// update resource default service
|
// update resource default service
|
||||||
let default = self.default.clone().unwrap_or_else(|| {
|
let default = self.default.clone().unwrap_or_else(|| {
|
||||||
Rc::new(boxed::factory(fn_service(|req: ServiceRequest| {
|
Rc::new(boxed::factory(fn_service(|req: ServiceRequest| {
|
||||||
|
@ -75,11 +74,7 @@ where
|
||||||
});
|
});
|
||||||
|
|
||||||
// App config
|
// App config
|
||||||
let mut config = AppService::new(
|
let mut config = AppService::new(config, default.clone(), self.data.clone());
|
||||||
self.config.borrow().clone(),
|
|
||||||
default.clone(),
|
|
||||||
self.data.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// register services
|
// register services
|
||||||
std::mem::replace(&mut *self.services.borrow_mut(), Vec::new())
|
std::mem::replace(&mut *self.services.borrow_mut(), Vec::new())
|
||||||
|
|
|
@ -124,14 +124,20 @@ impl AppService {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppConfig(pub(crate) Rc<AppConfigInner>);
|
pub struct AppConfig(Rc<AppConfigInner>);
|
||||||
|
|
||||||
|
struct AppConfigInner {
|
||||||
|
secure: bool,
|
||||||
|
host: String,
|
||||||
|
addr: SocketAddr,
|
||||||
|
}
|
||||||
|
|
||||||
impl AppConfig {
|
impl AppConfig {
|
||||||
pub(crate) fn new(inner: AppConfigInner) -> Self {
|
pub(crate) fn new(secure: bool, addr: SocketAddr, host: String) -> Self {
|
||||||
AppConfig(Rc::new(inner))
|
AppConfig(Rc::new(AppConfigInner { secure, addr, host }))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set server host name.
|
/// Server host name.
|
||||||
///
|
///
|
||||||
/// Host name is used by application router as a hostname for url generation.
|
/// Host name is used by application router as a hostname for url generation.
|
||||||
/// Check [ConnectionInfo](./struct.ConnectionInfo.html#method.host)
|
/// Check [ConnectionInfo](./struct.ConnectionInfo.html#method.host)
|
||||||
|
@ -153,19 +159,13 @@ impl AppConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct AppConfigInner {
|
impl Default for AppConfig {
|
||||||
pub(crate) secure: bool,
|
fn default() -> Self {
|
||||||
pub(crate) host: String,
|
AppConfig::new(
|
||||||
pub(crate) addr: SocketAddr,
|
false,
|
||||||
}
|
"127.0.0.1:8080".parse().unwrap(),
|
||||||
|
"localhost:8080".to_owned(),
|
||||||
impl Default for AppConfigInner {
|
)
|
||||||
fn default() -> AppConfigInner {
|
|
||||||
AppConfigInner {
|
|
||||||
secure: false,
|
|
||||||
addr: "127.0.0.1:8080".parse().unwrap(),
|
|
||||||
host: "localhost:8080".to_owned(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,9 @@ use actix_http::{
|
||||||
body::MessageBody, Error, HttpService, KeepAlive, Protocol, Request, Response,
|
body::MessageBody, Error, HttpService, KeepAlive, Protocol, Request, Response,
|
||||||
};
|
};
|
||||||
use actix_server::{Server, ServerBuilder};
|
use actix_server::{Server, ServerBuilder};
|
||||||
use actix_service::{pipeline_factory, IntoServiceFactory, Service, ServiceFactory};
|
use actix_service::{
|
||||||
|
map_config, pipeline_factory, IntoServiceFactory, Service, ServiceFactory,
|
||||||
|
};
|
||||||
use futures::future::ok;
|
use futures::future::ok;
|
||||||
|
|
||||||
use net2::TcpBuilder;
|
use net2::TcpBuilder;
|
||||||
|
@ -16,12 +18,15 @@ use actix_tls::openssl::{AlpnError, SslAcceptor, SslAcceptorBuilder};
|
||||||
#[cfg(feature = "rustls")]
|
#[cfg(feature = "rustls")]
|
||||||
use actix_tls::rustls::ServerConfig as RustlsServerConfig;
|
use actix_tls::rustls::ServerConfig as RustlsServerConfig;
|
||||||
|
|
||||||
|
use crate::config::AppConfig;
|
||||||
|
|
||||||
struct Socket {
|
struct Socket {
|
||||||
scheme: &'static str,
|
scheme: &'static str,
|
||||||
addr: net::SocketAddr,
|
addr: net::SocketAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
|
host: Option<String>,
|
||||||
keep_alive: KeepAlive,
|
keep_alive: KeepAlive,
|
||||||
client_timeout: u64,
|
client_timeout: u64,
|
||||||
client_shutdown: u64,
|
client_shutdown: u64,
|
||||||
|
@ -52,14 +57,13 @@ pub struct HttpServer<F, I, S, B>
|
||||||
where
|
where
|
||||||
F: Fn() -> I + Send + Clone + 'static,
|
F: Fn() -> I + Send + Clone + 'static,
|
||||||
I: IntoServiceFactory<S>,
|
I: IntoServiceFactory<S>,
|
||||||
S: ServiceFactory<Config = (), Request = Request>,
|
S: ServiceFactory<Config = AppConfig, Request = Request>,
|
||||||
S::Error: Into<Error>,
|
S::Error: Into<Error>,
|
||||||
S::InitError: fmt::Debug,
|
S::InitError: fmt::Debug,
|
||||||
S::Response: Into<Response<B>>,
|
S::Response: Into<Response<B>>,
|
||||||
B: MessageBody,
|
B: MessageBody,
|
||||||
{
|
{
|
||||||
pub(super) factory: F,
|
pub(super) factory: F,
|
||||||
pub(super) host: Option<String>,
|
|
||||||
config: Arc<Mutex<Config>>,
|
config: Arc<Mutex<Config>>,
|
||||||
backlog: i32,
|
backlog: i32,
|
||||||
sockets: Vec<Socket>,
|
sockets: Vec<Socket>,
|
||||||
|
@ -71,7 +75,7 @@ impl<F, I, S, B> HttpServer<F, I, S, B>
|
||||||
where
|
where
|
||||||
F: Fn() -> I + Send + Clone + 'static,
|
F: Fn() -> I + Send + Clone + 'static,
|
||||||
I: IntoServiceFactory<S>,
|
I: IntoServiceFactory<S>,
|
||||||
S: ServiceFactory<Config = (), Request = Request>,
|
S: ServiceFactory<Config = AppConfig, Request = Request>,
|
||||||
S::Error: Into<Error> + 'static,
|
S::Error: Into<Error> + 'static,
|
||||||
S::InitError: fmt::Debug,
|
S::InitError: fmt::Debug,
|
||||||
S::Response: Into<Response<B>> + 'static,
|
S::Response: Into<Response<B>> + 'static,
|
||||||
|
@ -82,8 +86,8 @@ where
|
||||||
pub fn new(factory: F) -> Self {
|
pub fn new(factory: F) -> Self {
|
||||||
HttpServer {
|
HttpServer {
|
||||||
factory,
|
factory,
|
||||||
host: None,
|
|
||||||
config: Arc::new(Mutex::new(Config {
|
config: Arc::new(Mutex::new(Config {
|
||||||
|
host: None,
|
||||||
keep_alive: KeepAlive::Timeout(5),
|
keep_alive: KeepAlive::Timeout(5),
|
||||||
client_timeout: 5000,
|
client_timeout: 5000,
|
||||||
client_shutdown: 5000,
|
client_shutdown: 5000,
|
||||||
|
@ -184,8 +188,8 @@ where
|
||||||
/// documentation for more information.
|
/// documentation for more information.
|
||||||
///
|
///
|
||||||
/// By default host name is set to a "localhost" value.
|
/// By default host name is set to a "localhost" value.
|
||||||
pub fn server_hostname<T: AsRef<str>>(mut self, val: T) -> Self {
|
pub fn server_hostname<T: AsRef<str>>(self, val: T) -> Self {
|
||||||
self.host = Some(val.as_ref().to_owned());
|
self.config.lock().unwrap().host = Some(val.as_ref().to_owned());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,11 +250,17 @@ where
|
||||||
lst,
|
lst,
|
||||||
move || {
|
move || {
|
||||||
let c = cfg.lock().unwrap();
|
let c = cfg.lock().unwrap();
|
||||||
|
let cfg = AppConfig::new(
|
||||||
|
false,
|
||||||
|
addr,
|
||||||
|
c.host.clone().unwrap_or_else(|| format!("{}", addr)),
|
||||||
|
);
|
||||||
|
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.keep_alive(c.keep_alive)
|
.keep_alive(c.keep_alive)
|
||||||
.client_timeout(c.client_timeout)
|
.client_timeout(c.client_timeout)
|
||||||
.local_addr(addr)
|
.local_addr(addr)
|
||||||
.finish(factory())
|
.finish(map_config(factory().into_factory(), move |_| cfg.clone()))
|
||||||
.tcp()
|
.tcp()
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
@ -288,11 +298,16 @@ where
|
||||||
lst,
|
lst,
|
||||||
move || {
|
move || {
|
||||||
let c = cfg.lock().unwrap();
|
let c = cfg.lock().unwrap();
|
||||||
|
let cfg = AppConfig::new(
|
||||||
|
true,
|
||||||
|
addr,
|
||||||
|
c.host.clone().unwrap_or_else(|| format!("{}", addr)),
|
||||||
|
);
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.keep_alive(c.keep_alive)
|
.keep_alive(c.keep_alive)
|
||||||
.client_timeout(c.client_timeout)
|
.client_timeout(c.client_timeout)
|
||||||
.client_disconnect(c.client_shutdown)
|
.client_disconnect(c.client_shutdown)
|
||||||
.finish(factory())
|
.finish(map_config(factory().into_factory(), move |_| cfg.clone()))
|
||||||
.openssl(acceptor.clone())
|
.openssl(acceptor.clone())
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
@ -330,11 +345,16 @@ where
|
||||||
lst,
|
lst,
|
||||||
move || {
|
move || {
|
||||||
let c = cfg.lock().unwrap();
|
let c = cfg.lock().unwrap();
|
||||||
|
let cfg = AppConfig::new(
|
||||||
|
true,
|
||||||
|
addr,
|
||||||
|
c.host.clone().unwrap_or_else(|| format!("{}", addr)),
|
||||||
|
);
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.keep_alive(c.keep_alive)
|
.keep_alive(c.keep_alive)
|
||||||
.client_timeout(c.client_timeout)
|
.client_timeout(c.client_timeout)
|
||||||
.client_disconnect(c.client_shutdown)
|
.client_disconnect(c.client_shutdown)
|
||||||
.finish(factory())
|
.finish(map_config(factory().into_factory(), move |_| cfg.clone()))
|
||||||
.rustls(config.clone())
|
.rustls(config.clone())
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
@ -435,24 +455,31 @@ where
|
||||||
|
|
||||||
let cfg = self.config.clone();
|
let cfg = self.config.clone();
|
||||||
let factory = self.factory.clone();
|
let factory = self.factory.clone();
|
||||||
// todo duplicated:
|
let socket_addr = net::SocketAddr::new(
|
||||||
self.sockets.push(Socket {
|
|
||||||
scheme: "http",
|
|
||||||
addr: net::SocketAddr::new(
|
|
||||||
net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)),
|
net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)),
|
||||||
8080,
|
8080,
|
||||||
),
|
);
|
||||||
|
self.sockets.push(Socket {
|
||||||
|
scheme: "http",
|
||||||
|
addr: socket_addr,
|
||||||
});
|
});
|
||||||
|
|
||||||
let addr = format!("actix-web-service-{:?}", lst.local_addr()?);
|
let addr = format!("actix-web-service-{:?}", lst.local_addr()?);
|
||||||
|
|
||||||
self.builder = self.builder.listen_uds(addr, lst, move || {
|
self.builder = self.builder.listen_uds(addr, lst, move || {
|
||||||
let c = cfg.lock().unwrap();
|
let c = cfg.lock().unwrap();
|
||||||
|
let config = AppConfig::new(
|
||||||
|
false,
|
||||||
|
socket_addr,
|
||||||
|
c.host.clone().unwrap_or_else(|| format!("{}", socket_addr)),
|
||||||
|
);
|
||||||
pipeline_factory(|io: UnixStream| ok((io, Protocol::Http1, None))).and_then(
|
pipeline_factory(|io: UnixStream| ok((io, Protocol::Http1, None))).and_then(
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.keep_alive(c.keep_alive)
|
.keep_alive(c.keep_alive)
|
||||||
.client_timeout(c.client_timeout)
|
.client_timeout(c.client_timeout)
|
||||||
.finish(factory()),
|
.finish(map_config(factory().into_factory(), move |_| {
|
||||||
|
config.clone()
|
||||||
|
})),
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
|
@ -470,12 +497,13 @@ where
|
||||||
|
|
||||||
let cfg = self.config.clone();
|
let cfg = self.config.clone();
|
||||||
let factory = self.factory.clone();
|
let factory = self.factory.clone();
|
||||||
self.sockets.push(Socket {
|
let socket_addr = net::SocketAddr::new(
|
||||||
scheme: "http",
|
|
||||||
addr: net::SocketAddr::new(
|
|
||||||
net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)),
|
net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)),
|
||||||
8080,
|
8080,
|
||||||
),
|
);
|
||||||
|
self.sockets.push(Socket {
|
||||||
|
scheme: "http",
|
||||||
|
addr: socket_addr,
|
||||||
});
|
});
|
||||||
|
|
||||||
self.builder = self.builder.bind_uds(
|
self.builder = self.builder.bind_uds(
|
||||||
|
@ -483,12 +511,19 @@ where
|
||||||
addr,
|
addr,
|
||||||
move || {
|
move || {
|
||||||
let c = cfg.lock().unwrap();
|
let c = cfg.lock().unwrap();
|
||||||
|
let config = AppConfig::new(
|
||||||
|
false,
|
||||||
|
socket_addr,
|
||||||
|
c.host.clone().unwrap_or_else(|| format!("{}", socket_addr)),
|
||||||
|
);
|
||||||
pipeline_factory(|io: UnixStream| ok((io, Protocol::Http1, None)))
|
pipeline_factory(|io: UnixStream| ok((io, Protocol::Http1, None)))
|
||||||
.and_then(
|
.and_then(
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.keep_alive(c.keep_alive)
|
.keep_alive(c.keep_alive)
|
||||||
.client_timeout(c.client_timeout)
|
.client_timeout(c.client_timeout)
|
||||||
.finish(factory()),
|
.finish(map_config(factory().into_factory(), move |_| {
|
||||||
|
config.clone()
|
||||||
|
})),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
@ -500,7 +535,7 @@ impl<F, I, S, B> HttpServer<F, I, S, B>
|
||||||
where
|
where
|
||||||
F: Fn() -> I + Send + Clone + 'static,
|
F: Fn() -> I + Send + Clone + 'static,
|
||||||
I: IntoServiceFactory<S>,
|
I: IntoServiceFactory<S>,
|
||||||
S: ServiceFactory<Config = (), Request = Request>,
|
S: ServiceFactory<Config = AppConfig, Request = Request>,
|
||||||
S::Error: Into<Error>,
|
S::Error: Into<Error>,
|
||||||
S::InitError: fmt::Debug,
|
S::InitError: fmt::Debug,
|
||||||
S::Response: Into<Response<B>>,
|
S::Response: Into<Response<B>>,
|
||||||
|
|
66
src/test.rs
66
src/test.rs
|
@ -12,7 +12,9 @@ use actix_http::{cookie::Cookie, ws, Extensions, HttpService, Request};
|
||||||
use actix_router::{Path, ResourceDef, Url};
|
use actix_router::{Path, ResourceDef, Url};
|
||||||
use actix_rt::System;
|
use actix_rt::System;
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
use actix_service::{
|
||||||
|
map_config, IntoService, IntoServiceFactory, Service, ServiceFactory,
|
||||||
|
};
|
||||||
use awc::error::PayloadError;
|
use awc::error::PayloadError;
|
||||||
use awc::{Client, ClientRequest, ClientResponse, Connector};
|
use awc::{Client, ClientRequest, ClientResponse, Connector};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
@ -25,7 +27,7 @@ use serde_json;
|
||||||
|
|
||||||
pub use actix_http::test::TestBuffer;
|
pub use actix_http::test::TestBuffer;
|
||||||
|
|
||||||
use crate::config::{AppConfig, AppConfigInner};
|
use crate::config::AppConfig;
|
||||||
use crate::data::Data;
|
use crate::data::Data;
|
||||||
use crate::dev::{Body, MessageBody, Payload};
|
use crate::dev::{Body, MessageBody, Payload};
|
||||||
use crate::request::HttpRequestPool;
|
use crate::request::HttpRequestPool;
|
||||||
|
@ -79,7 +81,7 @@ pub async fn init_service<R, S, B, E>(
|
||||||
where
|
where
|
||||||
R: IntoServiceFactory<S>,
|
R: IntoServiceFactory<S>,
|
||||||
S: ServiceFactory<
|
S: ServiceFactory<
|
||||||
Config = (),
|
Config = AppConfig,
|
||||||
Request = Request,
|
Request = Request,
|
||||||
Response = ServiceResponse<B>,
|
Response = ServiceResponse<B>,
|
||||||
Error = E,
|
Error = E,
|
||||||
|
@ -87,7 +89,7 @@ where
|
||||||
S::InitError: std::fmt::Debug,
|
S::InitError: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
let srv = app.into_factory();
|
let srv = app.into_factory();
|
||||||
srv.new_service(()).await.unwrap()
|
srv.new_service(AppConfig::default()).await.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calls service and waits for response future completion.
|
/// Calls service and waits for response future completion.
|
||||||
|
@ -296,7 +298,7 @@ where
|
||||||
pub struct TestRequest {
|
pub struct TestRequest {
|
||||||
req: HttpTestRequest,
|
req: HttpTestRequest,
|
||||||
rmap: ResourceMap,
|
rmap: ResourceMap,
|
||||||
config: AppConfigInner,
|
config: AppConfig,
|
||||||
path: Path<Url>,
|
path: Path<Url>,
|
||||||
app_data: Extensions,
|
app_data: Extensions,
|
||||||
}
|
}
|
||||||
|
@ -306,7 +308,7 @@ impl Default for TestRequest {
|
||||||
TestRequest {
|
TestRequest {
|
||||||
req: HttpTestRequest::default(),
|
req: HttpTestRequest::default(),
|
||||||
rmap: ResourceMap::new(ResourceDef::new("")),
|
rmap: ResourceMap::new(ResourceDef::new("")),
|
||||||
config: AppConfigInner::default(),
|
config: AppConfig::default(),
|
||||||
path: Path::new(Url::new(Uri::default())),
|
path: Path::new(Url::new(Uri::default())),
|
||||||
app_data: Extensions::new(),
|
app_data: Extensions::new(),
|
||||||
}
|
}
|
||||||
|
@ -462,7 +464,7 @@ impl TestRequest {
|
||||||
head,
|
head,
|
||||||
payload,
|
payload,
|
||||||
Rc::new(self.rmap),
|
Rc::new(self.rmap),
|
||||||
AppConfig::new(self.config),
|
self.config.clone(),
|
||||||
Rc::new(self.app_data),
|
Rc::new(self.app_data),
|
||||||
HttpRequestPool::create(),
|
HttpRequestPool::create(),
|
||||||
))
|
))
|
||||||
|
@ -483,7 +485,7 @@ impl TestRequest {
|
||||||
head,
|
head,
|
||||||
payload,
|
payload,
|
||||||
Rc::new(self.rmap),
|
Rc::new(self.rmap),
|
||||||
AppConfig::new(self.config),
|
self.config.clone(),
|
||||||
Rc::new(self.app_data),
|
Rc::new(self.app_data),
|
||||||
HttpRequestPool::create(),
|
HttpRequestPool::create(),
|
||||||
)
|
)
|
||||||
|
@ -499,7 +501,7 @@ impl TestRequest {
|
||||||
head,
|
head,
|
||||||
Payload::None,
|
Payload::None,
|
||||||
Rc::new(self.rmap),
|
Rc::new(self.rmap),
|
||||||
AppConfig::new(self.config),
|
self.config.clone(),
|
||||||
Rc::new(self.app_data),
|
Rc::new(self.app_data),
|
||||||
HttpRequestPool::create(),
|
HttpRequestPool::create(),
|
||||||
);
|
);
|
||||||
|
@ -538,7 +540,7 @@ pub fn start<F, I, S, B>(factory: F) -> TestServer
|
||||||
where
|
where
|
||||||
F: Fn() -> I + Send + Clone + 'static,
|
F: Fn() -> I + Send + Clone + 'static,
|
||||||
I: IntoServiceFactory<S>,
|
I: IntoServiceFactory<S>,
|
||||||
S: ServiceFactory<Config = (), Request = Request> + 'static,
|
S: ServiceFactory<Config = AppConfig, Request = Request> + 'static,
|
||||||
S::Error: Into<Error> + 'static,
|
S::Error: Into<Error> + 'static,
|
||||||
S::InitError: fmt::Debug,
|
S::InitError: fmt::Debug,
|
||||||
S::Response: Into<HttpResponse<B>> + 'static,
|
S::Response: Into<HttpResponse<B>> + 'static,
|
||||||
|
@ -577,7 +579,7 @@ pub fn start_with<F, I, S, B>(cfg: TestServerConfig, factory: F) -> TestServer
|
||||||
where
|
where
|
||||||
F: Fn() -> I + Send + Clone + 'static,
|
F: Fn() -> I + Send + Clone + 'static,
|
||||||
I: IntoServiceFactory<S>,
|
I: IntoServiceFactory<S>,
|
||||||
S: ServiceFactory<Config = (), Request = Request> + 'static,
|
S: ServiceFactory<Config = AppConfig, Request = Request> + 'static,
|
||||||
S::Error: Into<Error> + 'static,
|
S::Error: Into<Error> + 'static,
|
||||||
S::InitError: fmt::Debug,
|
S::InitError: fmt::Debug,
|
||||||
S::Response: Into<HttpResponse<B>> + 'static,
|
S::Response: Into<HttpResponse<B>> + 'static,
|
||||||
|
@ -607,63 +609,87 @@ where
|
||||||
match cfg.stream {
|
match cfg.stream {
|
||||||
StreamType::Tcp => match cfg.tp {
|
StreamType::Tcp => match cfg.tp {
|
||||||
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
||||||
|
let cfg =
|
||||||
|
AppConfig::new(false, local_addr, format!("{}", local_addr));
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(ctimeout)
|
.client_timeout(ctimeout)
|
||||||
.h1(factory())
|
.h1(map_config(factory().into_factory(), move |_| cfg.clone()))
|
||||||
.tcp()
|
.tcp()
|
||||||
}),
|
}),
|
||||||
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
||||||
|
let cfg =
|
||||||
|
AppConfig::new(false, local_addr, format!("{}", local_addr));
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(ctimeout)
|
.client_timeout(ctimeout)
|
||||||
.h2(factory())
|
.h2(map_config(factory().into_factory(), move |_| cfg.clone()))
|
||||||
.tcp()
|
.tcp()
|
||||||
}),
|
}),
|
||||||
HttpVer::Both => builder.listen("test", tcp, move || {
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
||||||
|
let cfg =
|
||||||
|
AppConfig::new(false, local_addr, format!("{}", local_addr));
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(ctimeout)
|
.client_timeout(ctimeout)
|
||||||
.finish(factory())
|
.finish(map_config(factory().into_factory(), move |_| {
|
||||||
|
cfg.clone()
|
||||||
|
}))
|
||||||
.tcp()
|
.tcp()
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
StreamType::Openssl(acceptor) => match cfg.tp {
|
StreamType::Openssl(acceptor) => match cfg.tp {
|
||||||
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
||||||
|
let cfg =
|
||||||
|
AppConfig::new(true, local_addr, format!("{}", local_addr));
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(ctimeout)
|
.client_timeout(ctimeout)
|
||||||
.h1(factory())
|
.h1(map_config(factory().into_factory(), move |_| cfg.clone()))
|
||||||
.openssl(acceptor.clone())
|
.openssl(acceptor.clone())
|
||||||
}),
|
}),
|
||||||
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
||||||
|
let cfg =
|
||||||
|
AppConfig::new(true, local_addr, format!("{}", local_addr));
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(ctimeout)
|
.client_timeout(ctimeout)
|
||||||
.h2(factory())
|
.h2(map_config(factory().into_factory(), move |_| cfg.clone()))
|
||||||
.openssl(acceptor.clone())
|
.openssl(acceptor.clone())
|
||||||
}),
|
}),
|
||||||
HttpVer::Both => builder.listen("test", tcp, move || {
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
||||||
|
let cfg =
|
||||||
|
AppConfig::new(true, local_addr, format!("{}", local_addr));
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(ctimeout)
|
.client_timeout(ctimeout)
|
||||||
.finish(factory())
|
.finish(map_config(factory().into_factory(), move |_| {
|
||||||
|
cfg.clone()
|
||||||
|
}))
|
||||||
.openssl(acceptor.clone())
|
.openssl(acceptor.clone())
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
#[cfg(feature = "rustls")]
|
#[cfg(feature = "rustls")]
|
||||||
StreamType::Rustls(config) => match cfg.tp {
|
StreamType::Rustls(config) => match cfg.tp {
|
||||||
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
HttpVer::Http1 => builder.listen("test", tcp, move || {
|
||||||
|
let cfg =
|
||||||
|
AppConfig::new(true, local_addr, format!("{}", local_addr));
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(ctimeout)
|
.client_timeout(ctimeout)
|
||||||
.h1(factory())
|
.h1(map_config(factory().into_factory(), move |_| cfg.clone()))
|
||||||
.rustls(config.clone())
|
.rustls(config.clone())
|
||||||
}),
|
}),
|
||||||
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
HttpVer::Http2 => builder.listen("test", tcp, move || {
|
||||||
|
let cfg =
|
||||||
|
AppConfig::new(true, local_addr, format!("{}", local_addr));
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(ctimeout)
|
.client_timeout(ctimeout)
|
||||||
.h2(factory())
|
.h2(map_config(factory().into_factory(), move |_| cfg.clone()))
|
||||||
.rustls(config.clone())
|
.rustls(config.clone())
|
||||||
}),
|
}),
|
||||||
HttpVer::Both => builder.listen("test", tcp, move || {
|
HttpVer::Both => builder.listen("test", tcp, move || {
|
||||||
|
let cfg =
|
||||||
|
AppConfig::new(true, local_addr, format!("{}", local_addr));
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(ctimeout)
|
.client_timeout(ctimeout)
|
||||||
.finish(factory())
|
.finish(map_config(factory().into_factory(), move |_| {
|
||||||
|
cfg.clone()
|
||||||
|
}))
|
||||||
.rustls(config.clone())
|
.rustls(config.clone())
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,8 +5,7 @@ use std::{net, thread, time::Duration};
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
use open_ssl::ssl::SslAcceptorBuilder;
|
use open_ssl::ssl::SslAcceptorBuilder;
|
||||||
|
|
||||||
use actix_http::Response;
|
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
|
||||||
use actix_web::{web, App, HttpServer};
|
|
||||||
|
|
||||||
fn unused_addr() -> net::SocketAddr {
|
fn unused_addr() -> net::SocketAddr {
|
||||||
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||||
|
@ -28,7 +27,7 @@ async fn test_start() {
|
||||||
|
|
||||||
let srv = HttpServer::new(|| {
|
let srv = HttpServer::new(|| {
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/").route(web::to(|| Response::Ok().body("test"))),
|
web::resource("/").route(web::to(|| HttpResponse::Ok().body("test"))),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.workers(1)
|
.workers(1)
|
||||||
|
@ -99,9 +98,10 @@ async fn test_start_ssl() {
|
||||||
let builder = ssl_acceptor().unwrap();
|
let builder = ssl_acceptor().unwrap();
|
||||||
|
|
||||||
let srv = HttpServer::new(|| {
|
let srv = HttpServer::new(|| {
|
||||||
App::new().service(
|
App::new().service(web::resource("/").route(web::to(|req: HttpRequest| {
|
||||||
web::resource("/").route(web::to(|| Response::Ok().body("test"))),
|
assert!(req.app_config().secure());
|
||||||
)
|
HttpResponse::Ok().body("test")
|
||||||
|
})))
|
||||||
})
|
})
|
||||||
.workers(1)
|
.workers(1)
|
||||||
.shutdown_timeout(1)
|
.shutdown_timeout(1)
|
||||||
|
|
Loading…
Reference in a new issue