mirror of
https://github.com/actix/actix-extras.git
synced 2025-08-31 03:20:20 +02:00
unify service builders
This commit is contained in:
141
src/builder.rs
Normal file
141
src/builder.rs
Normal file
@@ -0,0 +1,141 @@
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use actix_server_config::ServerConfig as SrvConfig;
|
||||
use actix_service::{IntoNewService, NewService};
|
||||
|
||||
use crate::body::MessageBody;
|
||||
use crate::config::{KeepAlive, ServiceConfig};
|
||||
use crate::request::Request;
|
||||
use crate::response::Response;
|
||||
|
||||
use crate::h1::H1Service;
|
||||
use crate::h2::H2Service;
|
||||
use crate::service::HttpService;
|
||||
|
||||
/// A http service builder
|
||||
///
|
||||
/// This type can be used to construct an instance of `http service` through a
|
||||
/// builder-like pattern.
|
||||
pub struct HttpServiceBuilder<T, S> {
|
||||
keep_alive: KeepAlive,
|
||||
client_timeout: u64,
|
||||
client_disconnect: u64,
|
||||
_t: PhantomData<(T, S)>,
|
||||
}
|
||||
|
||||
impl<T, S> HttpServiceBuilder<T, S>
|
||||
where
|
||||
S: NewService<SrvConfig, Request = Request>,
|
||||
S::Error: Debug + 'static,
|
||||
S::Service: 'static,
|
||||
{
|
||||
/// Create instance of `ServiceConfigBuilder`
|
||||
pub fn new() -> HttpServiceBuilder<T, S> {
|
||||
HttpServiceBuilder {
|
||||
keep_alive: KeepAlive::Timeout(5),
|
||||
client_timeout: 5000,
|
||||
client_disconnect: 0,
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set server keep-alive setting.
|
||||
///
|
||||
/// By default keep alive is set to a 5 seconds.
|
||||
pub fn keep_alive<U: Into<KeepAlive>>(mut self, val: U) -> Self {
|
||||
self.keep_alive = val.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server client timeout in milliseconds for first request.
|
||||
///
|
||||
/// Defines a timeout for reading client request header. If a client does not transmit
|
||||
/// the entire set headers within this time, the request is terminated with
|
||||
/// the 408 (Request Time-out) error.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default client timeout is set to 5000 milliseconds.
|
||||
pub fn client_timeout(mut self, val: u64) -> Self {
|
||||
self.client_timeout = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server connection disconnect timeout in milliseconds.
|
||||
///
|
||||
/// Defines a timeout for disconnect connection. If a disconnect procedure does not complete
|
||||
/// within this time, the request get dropped. This timeout affects secure connections.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default disconnect timeout is set to 3000 milliseconds.
|
||||
pub fn client_disconnect(mut self, val: u64) -> Self {
|
||||
self.client_disconnect = val;
|
||||
self
|
||||
}
|
||||
|
||||
// #[cfg(feature = "ssl")]
|
||||
// /// Configure alpn protocols for SslAcceptorBuilder.
|
||||
// pub fn configure_openssl(
|
||||
// builder: &mut openssl::ssl::SslAcceptorBuilder,
|
||||
// ) -> io::Result<()> {
|
||||
// let protos: &[u8] = b"\x02h2";
|
||||
// builder.set_alpn_select_callback(|_, protos| {
|
||||
// const H2: &[u8] = b"\x02h2";
|
||||
// if protos.windows(3).any(|window| window == H2) {
|
||||
// Ok(b"h2")
|
||||
// } else {
|
||||
// Err(openssl::ssl::AlpnError::NOACK)
|
||||
// }
|
||||
// });
|
||||
// builder.set_alpn_protos(&protos)?;
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
/// Finish service configuration and create *http service* for HTTP/1 protocol.
|
||||
pub fn h1<F, B>(self, service: F) -> H1Service<T, S, B>
|
||||
where
|
||||
B: MessageBody + 'static,
|
||||
F: IntoNewService<S, SrvConfig>,
|
||||
S::Response: Into<Response<B>>,
|
||||
{
|
||||
let cfg = ServiceConfig::new(
|
||||
self.keep_alive,
|
||||
self.client_timeout,
|
||||
self.client_disconnect,
|
||||
);
|
||||
H1Service::with_config(cfg, service.into_new_service())
|
||||
}
|
||||
|
||||
/// Finish service configuration and create *http service* for HTTP/2 protocol.
|
||||
pub fn h2<F, B>(self, service: F) -> H2Service<T, S, B>
|
||||
where
|
||||
B: MessageBody + 'static,
|
||||
F: IntoNewService<S, SrvConfig>,
|
||||
S::Response: Into<Response<B>>,
|
||||
{
|
||||
let cfg = ServiceConfig::new(
|
||||
self.keep_alive,
|
||||
self.client_timeout,
|
||||
self.client_disconnect,
|
||||
);
|
||||
H2Service::with_config(cfg, service.into_new_service())
|
||||
}
|
||||
|
||||
/// Finish service configuration and create `HttpService` instance.
|
||||
pub fn finish<F, B>(self, service: F) -> HttpService<T, S, B>
|
||||
where
|
||||
B: MessageBody + 'static,
|
||||
F: IntoNewService<S, SrvConfig>,
|
||||
S::Response: Into<Response<B>>,
|
||||
{
|
||||
let cfg = ServiceConfig::new(
|
||||
self.keep_alive,
|
||||
self.client_timeout,
|
||||
self.client_disconnect,
|
||||
);
|
||||
HttpService::with_config(cfg, service.into_new_service())
|
||||
}
|
||||
}
|
118
src/config.rs
118
src/config.rs
@@ -1,12 +1,11 @@
|
||||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::fmt::Write;
|
||||
use std::rc::Rc;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::{fmt, net};
|
||||
|
||||
use bytes::BytesMut;
|
||||
use futures::{future, Future};
|
||||
use log::error;
|
||||
use time;
|
||||
use tokio_timer::{sleep, Delay};
|
||||
|
||||
@@ -90,11 +89,6 @@ impl ServiceConfig {
|
||||
}))
|
||||
}
|
||||
|
||||
/// Create worker settings builder.
|
||||
pub fn build() -> ServiceConfigBuilder {
|
||||
ServiceConfigBuilder::new()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Keep alive duration if configured.
|
||||
pub fn keep_alive(&self) -> Option<Duration> {
|
||||
@@ -177,116 +171,6 @@ impl ServiceConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// A service config builder
|
||||
///
|
||||
/// This type can be used to construct an instance of `ServiceConfig` through a
|
||||
/// builder-like pattern.
|
||||
pub struct ServiceConfigBuilder {
|
||||
keep_alive: KeepAlive,
|
||||
client_timeout: u64,
|
||||
client_disconnect: u64,
|
||||
host: String,
|
||||
addr: net::SocketAddr,
|
||||
secure: bool,
|
||||
}
|
||||
|
||||
impl ServiceConfigBuilder {
|
||||
/// Create instance of `ServiceConfigBuilder`
|
||||
pub fn new() -> ServiceConfigBuilder {
|
||||
ServiceConfigBuilder {
|
||||
keep_alive: KeepAlive::Timeout(5),
|
||||
client_timeout: 5000,
|
||||
client_disconnect: 0,
|
||||
secure: false,
|
||||
host: "localhost".to_owned(),
|
||||
addr: "127.0.0.1:8080".parse().unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Enable secure flag for current server.
|
||||
/// This flags also enables `client disconnect timeout`.
|
||||
///
|
||||
/// By default this flag is set to false.
|
||||
pub fn secure(mut self) -> Self {
|
||||
self.secure = true;
|
||||
if self.client_disconnect == 0 {
|
||||
self.client_disconnect = 3000;
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server keep-alive setting.
|
||||
///
|
||||
/// By default keep alive is set to a 5 seconds.
|
||||
pub fn keep_alive<T: Into<KeepAlive>>(mut self, val: T) -> Self {
|
||||
self.keep_alive = val.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server client timeout in milliseconds for first request.
|
||||
///
|
||||
/// Defines a timeout for reading client request header. If a client does not transmit
|
||||
/// the entire set headers within this time, the request is terminated with
|
||||
/// the 408 (Request Time-out) error.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default client timeout is set to 5000 milliseconds.
|
||||
pub fn client_timeout(mut self, val: u64) -> Self {
|
||||
self.client_timeout = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server connection disconnect timeout in milliseconds.
|
||||
///
|
||||
/// Defines a timeout for disconnect connection. If a disconnect procedure does not complete
|
||||
/// within this time, the request get dropped. This timeout affects secure connections.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default disconnect timeout is set to 3000 milliseconds.
|
||||
pub fn client_disconnect(mut self, val: u64) -> Self {
|
||||
self.client_disconnect = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server host name.
|
||||
///
|
||||
/// Host name is used by application router aa 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 server_hostname(mut self, val: &str) -> Self {
|
||||
self.host = val.to_owned();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server ip address.
|
||||
///
|
||||
/// Host name is used by application router aa a hostname for url
|
||||
/// generation. Check [ConnectionInfo](./dev/struct.ConnectionInfo.
|
||||
/// html#method.host) documentation for more information.
|
||||
///
|
||||
/// By default server address is set to a "127.0.0.1:8080"
|
||||
pub fn server_address<S: net::ToSocketAddrs>(mut self, addr: S) -> Self {
|
||||
match addr.to_socket_addrs() {
|
||||
Err(err) => error!("Can not convert to SocketAddr: {}", err),
|
||||
Ok(mut addrs) => {
|
||||
if let Some(addr) = addrs.next() {
|
||||
self.addr = addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Finish service configuration and create `ServiceConfig` object.
|
||||
pub fn finish(&mut self) -> ServiceConfig {
|
||||
ServiceConfig::new(self.keep_alive, self.client_timeout, self.client_disconnect)
|
||||
}
|
||||
}
|
||||
|
||||
struct Date {
|
||||
bytes: [u8; DATE_VALUE_LENGTH],
|
||||
pos: usize,
|
||||
|
@@ -1,6 +1,5 @@
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
use std::net;
|
||||
|
||||
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
||||
use actix_server_config::ServerConfig as SrvConfig;
|
||||
@@ -8,7 +7,6 @@ use actix_service::{IntoNewService, NewService, Service};
|
||||
use actix_utils::cloneable::CloneableService;
|
||||
use futures::future::{ok, FutureResult};
|
||||
use futures::{try_ready, Async, Future, IntoFuture, Poll, Stream};
|
||||
use log::error;
|
||||
|
||||
use crate::body::MessageBody;
|
||||
use crate::config::{KeepAlive, ServiceConfig};
|
||||
@@ -57,11 +55,6 @@ where
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create builder for `HttpService` instance.
|
||||
pub fn build() -> H1ServiceBuilder<T, S> {
|
||||
H1ServiceBuilder::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, B> NewService<SrvConfig> for H1Service<T, S, B>
|
||||
@@ -89,135 +82,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// A http/1 new service builder
|
||||
///
|
||||
/// This type can be used to construct an instance of `ServiceConfig` through a
|
||||
/// builder-like pattern.
|
||||
pub struct H1ServiceBuilder<T, S> {
|
||||
keep_alive: KeepAlive,
|
||||
client_timeout: u64,
|
||||
client_disconnect: u64,
|
||||
host: String,
|
||||
addr: net::SocketAddr,
|
||||
secure: bool,
|
||||
_t: PhantomData<(T, S)>,
|
||||
}
|
||||
|
||||
impl<T, S> H1ServiceBuilder<T, S>
|
||||
where
|
||||
S: NewService<SrvConfig, Request = Request>,
|
||||
S::Error: Debug,
|
||||
{
|
||||
/// Create instance of `ServiceConfigBuilder`
|
||||
pub fn new() -> H1ServiceBuilder<T, S> {
|
||||
H1ServiceBuilder {
|
||||
keep_alive: KeepAlive::Timeout(5),
|
||||
client_timeout: 5000,
|
||||
client_disconnect: 0,
|
||||
secure: false,
|
||||
host: "localhost".to_owned(),
|
||||
addr: "127.0.0.1:8080".parse().unwrap(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Enable secure flag for current server.
|
||||
/// This flags also enables `client disconnect timeout`.
|
||||
///
|
||||
/// By default this flag is set to false.
|
||||
pub fn secure(mut self) -> Self {
|
||||
self.secure = true;
|
||||
if self.client_disconnect == 0 {
|
||||
self.client_disconnect = 3000;
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server keep-alive setting.
|
||||
///
|
||||
/// By default keep alive is set to a 5 seconds.
|
||||
pub fn keep_alive<U: Into<KeepAlive>>(mut self, val: U) -> Self {
|
||||
self.keep_alive = val.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server client timeout in milliseconds for first request.
|
||||
///
|
||||
/// Defines a timeout for reading client request header. If a client does not transmit
|
||||
/// the entire set headers within this time, the request is terminated with
|
||||
/// the 408 (Request Time-out) error.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default client timeout is set to 5000 milliseconds.
|
||||
pub fn client_timeout(mut self, val: u64) -> Self {
|
||||
self.client_timeout = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server connection disconnect timeout in milliseconds.
|
||||
///
|
||||
/// Defines a timeout for disconnect connection. If a disconnect procedure does not complete
|
||||
/// within this time, the request get dropped. This timeout affects secure connections.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default disconnect timeout is set to 3000 milliseconds.
|
||||
pub fn client_disconnect(mut self, val: u64) -> Self {
|
||||
self.client_disconnect = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server host name.
|
||||
///
|
||||
/// Host name is used by application router aa 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 server_hostname(mut self, val: &str) -> Self {
|
||||
self.host = val.to_owned();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server ip address.
|
||||
///
|
||||
/// Host name is used by application router aa a hostname for url
|
||||
/// generation. Check [ConnectionInfo](./dev/struct.ConnectionInfo.
|
||||
/// html#method.host) documentation for more information.
|
||||
///
|
||||
/// By default server address is set to a "127.0.0.1:8080"
|
||||
pub fn server_address<U: net::ToSocketAddrs>(mut self, addr: U) -> Self {
|
||||
match addr.to_socket_addrs() {
|
||||
Err(err) => error!("Can not convert to SocketAddr: {}", err),
|
||||
Ok(mut addrs) => {
|
||||
if let Some(addr) = addrs.next() {
|
||||
self.addr = addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Finish service configuration and create `H1Service` instance.
|
||||
pub fn finish<F, B>(self, service: F) -> H1Service<T, S, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
F: IntoNewService<S, SrvConfig>,
|
||||
{
|
||||
let cfg = ServiceConfig::new(
|
||||
self.keep_alive,
|
||||
self.client_timeout,
|
||||
self.client_disconnect,
|
||||
);
|
||||
H1Service {
|
||||
cfg,
|
||||
srv: service.into_new_service(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct H1ServiceResponse<T, S: NewService<SrvConfig, Request = Request>, B> {
|
||||
fut: <S::Future as IntoFuture>::Future,
|
||||
|
@@ -59,11 +59,6 @@ where
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create builder for `HttpService` instance.
|
||||
pub fn build() -> H2ServiceBuilder<T, S> {
|
||||
H2ServiceBuilder::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, B> NewService<SrvConfig> for H2Service<T, S, B>
|
||||
@@ -91,136 +86,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// A http/2 new service builder
|
||||
///
|
||||
/// This type can be used to construct an instance of `ServiceConfig` through a
|
||||
/// builder-like pattern.
|
||||
pub struct H2ServiceBuilder<T, S> {
|
||||
keep_alive: KeepAlive,
|
||||
client_timeout: u64,
|
||||
client_disconnect: u64,
|
||||
host: String,
|
||||
addr: net::SocketAddr,
|
||||
secure: bool,
|
||||
_t: PhantomData<(T, S)>,
|
||||
}
|
||||
|
||||
impl<T, S> H2ServiceBuilder<T, S>
|
||||
where
|
||||
S: NewService<SrvConfig, Request = Request>,
|
||||
S::Service: 'static,
|
||||
S::Error: Debug + 'static,
|
||||
{
|
||||
/// Create instance of `H2ServiceBuilder`
|
||||
pub fn new() -> H2ServiceBuilder<T, S> {
|
||||
H2ServiceBuilder {
|
||||
keep_alive: KeepAlive::Timeout(5),
|
||||
client_timeout: 5000,
|
||||
client_disconnect: 0,
|
||||
secure: false,
|
||||
host: "localhost".to_owned(),
|
||||
addr: "127.0.0.1:8080".parse().unwrap(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Enable secure flag for current server.
|
||||
/// This flags also enables `client disconnect timeout`.
|
||||
///
|
||||
/// By default this flag is set to false.
|
||||
pub fn secure(mut self) -> Self {
|
||||
self.secure = true;
|
||||
if self.client_disconnect == 0 {
|
||||
self.client_disconnect = 3000;
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server keep-alive setting.
|
||||
///
|
||||
/// By default keep alive is set to a 5 seconds.
|
||||
pub fn keep_alive<U: Into<KeepAlive>>(mut self, val: U) -> Self {
|
||||
self.keep_alive = val.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server client timeout in milliseconds for first request.
|
||||
///
|
||||
/// Defines a timeout for reading client request header. If a client does not transmit
|
||||
/// the entire set headers within this time, the request is terminated with
|
||||
/// the 408 (Request Time-out) error.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default client timeout is set to 5000 milliseconds.
|
||||
pub fn client_timeout(mut self, val: u64) -> Self {
|
||||
self.client_timeout = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server connection disconnect timeout in milliseconds.
|
||||
///
|
||||
/// Defines a timeout for disconnect connection. If a disconnect procedure does not complete
|
||||
/// within this time, the request get dropped. This timeout affects secure connections.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default disconnect timeout is set to 3000 milliseconds.
|
||||
pub fn client_disconnect(mut self, val: u64) -> Self {
|
||||
self.client_disconnect = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server host name.
|
||||
///
|
||||
/// Host name is used by application router aa 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 server_hostname(mut self, val: &str) -> Self {
|
||||
self.host = val.to_owned();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server ip address.
|
||||
///
|
||||
/// Host name is used by application router aa a hostname for url
|
||||
/// generation. Check [ConnectionInfo](./dev/struct.ConnectionInfo.
|
||||
/// html#method.host) documentation for more information.
|
||||
///
|
||||
/// By default server address is set to a "127.0.0.1:8080"
|
||||
pub fn server_address<U: net::ToSocketAddrs>(mut self, addr: U) -> Self {
|
||||
match addr.to_socket_addrs() {
|
||||
Err(err) => error!("Can not convert to SocketAddr: {}", err),
|
||||
Ok(mut addrs) => {
|
||||
if let Some(addr) = addrs.next() {
|
||||
self.addr = addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Finish service configuration and create `H1Service` instance.
|
||||
pub fn finish<F, B>(self, service: F) -> H2Service<T, S, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
F: IntoNewService<S, SrvConfig>,
|
||||
{
|
||||
let cfg = ServiceConfig::new(
|
||||
self.keep_alive,
|
||||
self.client_timeout,
|
||||
self.client_disconnect,
|
||||
);
|
||||
H2Service {
|
||||
cfg,
|
||||
srv: service.into_new_service(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct H2ServiceResponse<T, S: NewService<SrvConfig, Request = Request>, B> {
|
||||
fut: <S::Future as IntoFuture>::Future,
|
||||
|
@@ -69,6 +69,7 @@
|
||||
extern crate log;
|
||||
|
||||
pub mod body;
|
||||
mod builder;
|
||||
pub mod client;
|
||||
mod config;
|
||||
mod extensions;
|
||||
@@ -89,7 +90,8 @@ pub mod h2;
|
||||
pub mod test;
|
||||
pub mod ws;
|
||||
|
||||
pub use self::config::{KeepAlive, ServiceConfig, ServiceConfigBuilder};
|
||||
pub use self::builder::HttpServiceBuilder;
|
||||
pub use self::config::{KeepAlive, ServiceConfig};
|
||||
pub use self::error::{Error, ResponseError, Result};
|
||||
pub use self::extensions::Extensions;
|
||||
pub use self::httpmessage::HttpMessage;
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
use std::{fmt, io, net};
|
||||
use std::{fmt, io};
|
||||
|
||||
use actix_codec::{AsyncRead, AsyncWrite, Framed, FramedParts};
|
||||
use actix_server_config::ServerConfig as SrvConfig;
|
||||
@@ -12,11 +12,11 @@ use h2::server::{self, Handshake};
|
||||
use log::error;
|
||||
|
||||
use crate::body::MessageBody;
|
||||
use crate::builder::HttpServiceBuilder;
|
||||
use crate::config::{KeepAlive, ServiceConfig};
|
||||
use crate::error::DispatchError;
|
||||
use crate::request::Request;
|
||||
use crate::response::Response;
|
||||
|
||||
use crate::{h1, h2::Dispatcher};
|
||||
|
||||
/// `NewService` HTTP1.1/HTTP2 transport implementation
|
||||
@@ -46,7 +46,7 @@ where
|
||||
}
|
||||
|
||||
/// Create new `HttpService` instance with config.
|
||||
pub fn with_config<F: IntoNewService<S, SrvConfig>>(
|
||||
pub(crate) fn with_config<F: IntoNewService<S, SrvConfig>>(
|
||||
cfg: ServiceConfig,
|
||||
service: F,
|
||||
) -> Self {
|
||||
@@ -88,155 +88,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// A http service factory builder
|
||||
///
|
||||
/// This type can be used to construct an instance of `ServiceConfig` through a
|
||||
/// builder-like pattern.
|
||||
pub struct HttpServiceBuilder<T, S> {
|
||||
keep_alive: KeepAlive,
|
||||
client_timeout: u64,
|
||||
client_disconnect: u64,
|
||||
host: String,
|
||||
addr: net::SocketAddr,
|
||||
secure: bool,
|
||||
_t: PhantomData<(T, S)>,
|
||||
}
|
||||
|
||||
impl<T, S> HttpServiceBuilder<T, S>
|
||||
where
|
||||
S: NewService<SrvConfig, Request = Request>,
|
||||
S::Service: 'static,
|
||||
S::Error: Debug + 'static,
|
||||
{
|
||||
/// Create instance of `HttpServiceBuilder` type
|
||||
pub fn new() -> HttpServiceBuilder<T, S> {
|
||||
HttpServiceBuilder {
|
||||
keep_alive: KeepAlive::Timeout(5),
|
||||
client_timeout: 5000,
|
||||
client_disconnect: 0,
|
||||
secure: false,
|
||||
host: "localhost".to_owned(),
|
||||
addr: "127.0.0.1:8080".parse().unwrap(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Enable secure flag for current server.
|
||||
/// This flags also enables `client disconnect timeout`.
|
||||
///
|
||||
/// By default this flag is set to false.
|
||||
pub fn secure(mut self) -> Self {
|
||||
self.secure = true;
|
||||
if self.client_disconnect == 0 {
|
||||
self.client_disconnect = 3000;
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server keep-alive setting.
|
||||
///
|
||||
/// By default keep alive is set to a 5 seconds.
|
||||
pub fn keep_alive<U: Into<KeepAlive>>(mut self, val: U) -> Self {
|
||||
self.keep_alive = val.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server client timeout in milliseconds for first request.
|
||||
///
|
||||
/// Defines a timeout for reading client request header. If a client does not transmit
|
||||
/// the entire set headers within this time, the request is terminated with
|
||||
/// the 408 (Request Time-out) error.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default client timeout is set to 5000 milliseconds.
|
||||
pub fn client_timeout(mut self, val: u64) -> Self {
|
||||
self.client_timeout = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server connection disconnect timeout in milliseconds.
|
||||
///
|
||||
/// Defines a timeout for disconnect connection. If a disconnect procedure does not complete
|
||||
/// within this time, the request get dropped. This timeout affects secure connections.
|
||||
///
|
||||
/// To disable timeout set value to 0.
|
||||
///
|
||||
/// By default disconnect timeout is set to 3000 milliseconds.
|
||||
pub fn client_disconnect(mut self, val: u64) -> Self {
|
||||
self.client_disconnect = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server host name.
|
||||
///
|
||||
/// Host name is used by application router aa 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 server_hostname(mut self, val: &str) -> Self {
|
||||
self.host = val.to_owned();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set server ip address.
|
||||
///
|
||||
/// Host name is used by application router aa a hostname for url
|
||||
/// generation. Check [ConnectionInfo](./dev/struct.ConnectionInfo.
|
||||
/// html#method.host) documentation for more information.
|
||||
///
|
||||
/// By default server address is set to a "127.0.0.1:8080"
|
||||
pub fn server_address<U: net::ToSocketAddrs>(mut self, addr: U) -> Self {
|
||||
match addr.to_socket_addrs() {
|
||||
Err(err) => error!("Can not convert to SocketAddr: {}", err),
|
||||
Ok(mut addrs) => {
|
||||
if let Some(addr) = addrs.next() {
|
||||
self.addr = addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
// #[cfg(feature = "ssl")]
|
||||
// /// Configure alpn protocols for SslAcceptorBuilder.
|
||||
// pub fn configure_openssl(
|
||||
// builder: &mut openssl::ssl::SslAcceptorBuilder,
|
||||
// ) -> io::Result<()> {
|
||||
// let protos: &[u8] = b"\x02h2";
|
||||
// builder.set_alpn_select_callback(|_, protos| {
|
||||
// const H2: &[u8] = b"\x02h2";
|
||||
// if protos.windows(3).any(|window| window == H2) {
|
||||
// Ok(b"h2")
|
||||
// } else {
|
||||
// Err(openssl::ssl::AlpnError::NOACK)
|
||||
// }
|
||||
// });
|
||||
// builder.set_alpn_protos(&protos)?;
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
/// Finish service configuration and create `HttpService` instance.
|
||||
pub fn finish<F, B>(self, service: F) -> HttpService<T, S, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
F: IntoNewService<S, SrvConfig>,
|
||||
{
|
||||
let cfg = ServiceConfig::new(
|
||||
self.keep_alive,
|
||||
self.client_timeout,
|
||||
self.client_disconnect,
|
||||
);
|
||||
HttpService {
|
||||
cfg,
|
||||
srv: service.into_new_service(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct HttpServiceResponse<T, S: NewService<SrvConfig>, B> {
|
||||
fut: <S::Future as IntoFuture>::Future,
|
||||
|
Reference in New Issue
Block a user