1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-18 05:41:50 +01:00

update http service usage; add app host

This commit is contained in:
Nikolay Kim 2019-03-09 10:53:00 -08:00
parent fde55ffa14
commit c0ce7f0bae
2 changed files with 56 additions and 52 deletions

View File

@ -31,6 +31,7 @@ where
chain: T, chain: T,
extensions: Extensions, extensions: Extensions,
state: Vec<Box<StateFactory>>, state: Vec<Box<StateFactory>>,
host: String,
_t: PhantomData<(P,)>, _t: PhantomData<(P,)>,
} }
@ -42,6 +43,7 @@ impl App<PayloadStream, AppChain> {
chain: AppChain, chain: AppChain,
extensions: Extensions::new(), extensions: Extensions::new(),
state: Vec::new(), state: Vec::new(),
host: "localhost:8080".to_string(),
_t: PhantomData, _t: PhantomData,
} }
} }
@ -140,6 +142,7 @@ where
default: None, default: None,
factory_ref: fref, factory_ref: fref,
extensions: self.extensions, extensions: self.extensions,
host: self.host,
_t: PhantomData, _t: PhantomData,
} }
} }
@ -172,6 +175,7 @@ where
chain, chain,
state: self.state, state: self.state,
extensions: self.extensions, extensions: self.extensions,
host: self.host,
_t: PhantomData, _t: PhantomData,
} }
} }
@ -221,6 +225,7 @@ where
factory_ref: fref, factory_ref: fref,
extensions: self.extensions, extensions: self.extensions,
state: self.state, state: self.state,
host: self.host,
services: vec![Box::new(ServiceFactoryWrapper::new(service))], services: vec![Box::new(ServiceFactoryWrapper::new(service))],
_t: PhantomData, _t: PhantomData,
} }
@ -233,8 +238,8 @@ where
/// html#method.host) documentation for more information. /// html#method.host) 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 hostname(self, _val: &str) -> Self { pub fn hostname(mut self, val: &str) -> Self {
// self.host = val.to_owned(); self.host = val.to_owned();
self self
} }
} }
@ -249,6 +254,7 @@ pub struct AppRouter<C, P, B, T> {
factory_ref: Rc<RefCell<Option<AppRoutingFactory<P>>>>, factory_ref: Rc<RefCell<Option<AppRoutingFactory<P>>>>,
extensions: Extensions, extensions: Extensions,
state: Vec<Box<StateFactory>>, state: Vec<Box<StateFactory>>,
host: String,
_t: PhantomData<(P, B)>, _t: PhantomData<(P, B)>,
} }
@ -343,6 +349,7 @@ where
default: self.default, default: self.default,
factory_ref: self.factory_ref, factory_ref: self.factory_ref,
extensions: self.extensions, extensions: self.extensions,
host: self.host,
_t: PhantomData, _t: PhantomData,
} }
} }

View File

@ -2,9 +2,7 @@ use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
use std::{fmt, io, net}; use std::{fmt, io, net};
use actix_http::{ use actix_http::{body::MessageBody, HttpService, KeepAlive, Request, Response};
body::MessageBody, HttpService, KeepAlive, Request, Response, ServiceConfig,
};
use actix_rt::System; use actix_rt::System;
use actix_server::{Server, ServerBuilder}; use actix_server::{Server, ServerBuilder};
use actix_server_config::ServerConfig; use actix_server_config::ServerConfig;
@ -13,8 +11,8 @@ use parking_lot::Mutex;
use net2::TcpBuilder; use net2::TcpBuilder;
#[cfg(feature = "tls")] // #[cfg(feature = "tls")]
use native_tls::TlsAcceptor; // use native_tls::TlsAcceptor;
#[cfg(feature = "ssl")] #[cfg(feature = "ssl")]
use openssl::ssl::{SslAcceptor, SslAcceptorBuilder}; use openssl::ssl::{SslAcceptor, SslAcceptorBuilder};
@ -245,27 +243,28 @@ where
lst, lst,
move || { move || {
let c = cfg.lock(); let c = cfg.lock();
let service_config = HttpService::build()
ServiceConfig::new(c.keep_alive, c.client_timeout, 0); .keep_alive(c.keep_alive)
HttpService::with_config(service_config, factory()) .client_timeout(c.client_timeout)
.finish(factory())
}, },
)?); )?);
Ok(self) Ok(self)
} }
#[cfg(feature = "tls")] // #[cfg(feature = "tls")]
/// Use listener for accepting incoming tls connection requests // /// Use listener for accepting incoming tls connection requests
/// // ///
/// HttpServer does not change any configuration for TcpListener, // /// HttpServer does not change any configuration for TcpListener,
/// it needs to be configured before passing it to listen() method. // /// it needs to be configured before passing it to listen() method.
pub fn listen_tls(self, lst: net::TcpListener, acceptor: TlsAcceptor) -> Self { // pub fn listen_nativetls(self, lst: net::TcpListener, acceptor: TlsAcceptor) -> Self {
use actix_net::service::NewServiceExt; // use actix_server::ssl;
self.listen_with(lst, move || { // self.listen_with(lst, move || {
ssl::NativeTlsAcceptor::new(acceptor.clone()).map_err(|_| ()) // ssl::NativeTlsAcceptor::new(acceptor.clone()).map_err(|_| ())
}) // })
} // }
#[cfg(feature = "ssl")] #[cfg(feature = "ssl")]
/// Use listener for accepting incoming tls connection requests /// Use listener for accepting incoming tls connection requests
@ -276,12 +275,16 @@ where
lst: net::TcpListener, lst: net::TcpListener,
builder: SslAcceptorBuilder, builder: SslAcceptorBuilder,
) -> io::Result<Self> { ) -> io::Result<Self> {
self.listen_ssl_inner(lst, openssl_acceptor(builder)?); self.listen_ssl_inner(lst, openssl_acceptor(builder)?)?;
Ok(self) Ok(self)
} }
#[cfg(feature = "ssl")] #[cfg(feature = "ssl")]
fn listen_ssl_inner(&mut self, lst: net::TcpListener, acceptor: SslAcceptor) { fn listen_ssl_inner(
&mut self,
lst: net::TcpListener,
acceptor: SslAcceptor,
) -> io::Result<()> {
use actix_server::ssl::{OpensslAcceptor, SslError}; use actix_server::ssl::{OpensslAcceptor, SslError};
let acceptor = OpensslAcceptor::new(acceptor); let acceptor = OpensslAcceptor::new(acceptor);
@ -298,15 +301,18 @@ where
lst, lst,
move || { move || {
let c = cfg.lock(); let c = cfg.lock();
let service_config =
ServiceConfig::new(c.keep_alive, c.client_timeout, c.client_timeout);
acceptor.clone().map_err(|e| SslError::Ssl(e)).and_then( acceptor.clone().map_err(|e| SslError::Ssl(e)).and_then(
HttpService::with_config(service_config, factory()) HttpService::build()
.keep_alive(c.keep_alive)
.client_timeout(c.client_timeout)
.client_disconnect(c.client_shutdown)
.finish(factory())
.map_err(|e| SslError::Service(e)) .map_err(|e| SslError::Service(e))
.map_init_err(|_| ()), .map_init_err(|_| ()),
) )
}, },
)); )?);
Ok(())
} }
#[cfg(feature = "rust-tls")] #[cfg(feature = "rust-tls")]
@ -315,7 +321,6 @@ where
/// This method sets alpn protocols to "h2" and "http/1.1" /// This method sets alpn protocols to "h2" and "http/1.1"
pub fn listen_rustls(self, lst: net::TcpListener, config: ServerConfig) -> Self { pub fn listen_rustls(self, lst: net::TcpListener, config: ServerConfig) -> Self {
use super::{RustlsAcceptor, ServerFlags}; use super::{RustlsAcceptor, ServerFlags};
use actix_net::service::NewServiceExt;
self.listen_with(lst, move || { self.listen_with(lst, move || {
RustlsAcceptor::with_flags(config.clone(), flags).map_err(|_| ()) RustlsAcceptor::with_flags(config.clone(), flags).map_err(|_| ())
@ -366,22 +371,21 @@ where
} }
} }
#[cfg(feature = "tls")] // #[cfg(feature = "tls")]
/// The ssl socket address to bind // /// The ssl socket address to bind
/// // ///
/// To bind multiple addresses this method can be called multiple times. // /// To bind multiple addresses this method can be called multiple times.
pub fn bind_tls<A: net::ToSocketAddrs>( // pub fn bind_nativetls<A: net::ToSocketAddrs>(
self, // self,
addr: A, // addr: A,
acceptor: TlsAcceptor, // acceptor: TlsAcceptor,
) -> io::Result<Self> { // ) -> io::Result<Self> {
use actix_net::service::NewServiceExt; // use actix_server::ssl::NativeTlsAcceptor;
use actix_net::ssl::NativeTlsAcceptor;
self.bind_with(addr, move || { // self.bind_with(addr, move || {
NativeTlsAcceptor::new(acceptor.clone()).map_err(|_| ()) // NativeTlsAcceptor::new(acceptor.clone()).map_err(|_| ())
}) // })
} // }
#[cfg(feature = "ssl")] #[cfg(feature = "ssl")]
/// Start listening for incoming tls connections. /// Start listening for incoming tls connections.
@ -399,7 +403,7 @@ where
let acceptor = openssl_acceptor(builder)?; let acceptor = openssl_acceptor(builder)?;
for lst in sockets { for lst in sockets {
self.listen_ssl_inner(lst, acceptor.clone()); self.listen_ssl_inner(lst, acceptor.clone())?;
} }
Ok(self) Ok(self)
@ -415,14 +419,7 @@ where
builder: ServerConfig, builder: ServerConfig,
) -> io::Result<Self> { ) -> io::Result<Self> {
use super::{RustlsAcceptor, ServerFlags}; use super::{RustlsAcceptor, ServerFlags};
use actix_net::service::NewServiceExt; use actix_service::NewServiceExt;
// alpn support
let flags = if self.no_http2 {
ServerFlags::HTTP1
} else {
ServerFlags::HTTP1 | ServerFlags::HTTP2
};
self.bind_with(addr, move || { self.bind_with(addr, move || {
RustlsAcceptor::with_flags(builder.clone(), flags).map_err(|_| ()) RustlsAcceptor::with_flags(builder.clone(), flags).map_err(|_| ())