2018-08-03 08:17:10 +02:00
|
|
|
use std::cell::{RefCell, RefMut, UnsafeCell};
|
2018-06-24 06:30:58 +02:00
|
|
|
use std::collections::VecDeque;
|
2018-03-18 19:05:44 +01:00
|
|
|
use std::fmt::Write;
|
2018-01-12 03:35:05 +01:00
|
|
|
use std::rc::Rc;
|
2018-09-27 05:43:54 +02:00
|
|
|
use std::time::{Duration, Instant};
|
2018-07-06 23:28:08 +02:00
|
|
|
use std::{env, fmt, net};
|
2018-06-17 19:51:20 +02:00
|
|
|
|
|
|
|
use bytes::BytesMut;
|
2018-09-08 18:20:18 +02:00
|
|
|
use futures::{future, Future};
|
2018-06-18 00:56:18 +02:00
|
|
|
use futures_cpupool::CpuPool;
|
2018-06-17 19:51:20 +02:00
|
|
|
use http::StatusCode;
|
2018-07-11 08:56:35 +02:00
|
|
|
use lazycell::LazyCell;
|
2018-06-18 00:56:18 +02:00
|
|
|
use parking_lot::Mutex;
|
2018-03-18 19:05:44 +01:00
|
|
|
use time;
|
2018-09-08 18:20:18 +02:00
|
|
|
use tokio_current_thread::spawn;
|
2018-09-27 05:43:54 +02:00
|
|
|
use tokio_timer::{sleep, Delay};
|
2018-01-12 03:35:05 +01:00
|
|
|
|
|
|
|
use super::channel::Node;
|
2018-06-25 06:58:04 +02:00
|
|
|
use super::message::{Request, RequestPool};
|
2018-04-29 07:55:47 +02:00
|
|
|
use super::KeepAlive;
|
2018-03-23 05:14:57 +01:00
|
|
|
use body::Body;
|
2018-04-14 01:02:01 +02:00
|
|
|
use httpresponse::{HttpResponse, HttpResponseBuilder, HttpResponsePool};
|
2018-01-12 03:35:05 +01:00
|
|
|
|
2018-06-18 00:56:18 +02:00
|
|
|
/// Env variable for default cpu pool size
|
|
|
|
const ENV_CPU_POOL_VAR: &str = "ACTIX_CPU_POOL";
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub(crate) static ref DEFAULT_CPUPOOL: Mutex<CpuPool> = {
|
|
|
|
let default = match env::var(ENV_CPU_POOL_VAR) {
|
|
|
|
Ok(val) => {
|
|
|
|
if let Ok(val) = val.parse() {
|
|
|
|
val
|
|
|
|
} else {
|
|
|
|
error!("Can not parse ACTIX_CPU_POOL value");
|
|
|
|
20
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => 20,
|
|
|
|
};
|
|
|
|
Mutex::new(CpuPool::new(default))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-12 03:35:05 +01:00
|
|
|
/// Various server settings
|
|
|
|
pub struct ServerSettings {
|
2018-10-01 23:43:06 +02:00
|
|
|
addr: net::SocketAddr,
|
2018-01-12 03:35:05 +01:00
|
|
|
secure: bool,
|
|
|
|
host: String,
|
2018-07-06 23:28:08 +02:00
|
|
|
cpu_pool: LazyCell<CpuPool>,
|
2018-06-25 05:08:28 +02:00
|
|
|
responses: &'static HttpResponsePool,
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-05-12 06:19:48 +02:00
|
|
|
impl Clone for ServerSettings {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ServerSettings {
|
|
|
|
addr: self.addr,
|
|
|
|
secure: self.secure,
|
|
|
|
host: self.host.clone(),
|
2018-07-06 23:28:08 +02:00
|
|
|
cpu_pool: LazyCell::new(),
|
2018-06-25 05:08:28 +02:00
|
|
|
responses: HttpResponsePool::get_pool(),
|
2018-03-07 23:56:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 03:35:05 +01:00
|
|
|
impl Default for ServerSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
ServerSettings {
|
2018-10-01 23:43:06 +02:00
|
|
|
addr: "127.0.0.1:8080".parse().unwrap(),
|
2018-01-12 03:35:05 +01:00
|
|
|
secure: false,
|
|
|
|
host: "localhost:8080".to_owned(),
|
2018-06-25 05:08:28 +02:00
|
|
|
responses: HttpResponsePool::get_pool(),
|
2018-07-06 23:28:08 +02:00
|
|
|
cpu_pool: LazyCell::new(),
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerSettings {
|
|
|
|
/// Crate server settings instance
|
2018-10-02 05:04:16 +02:00
|
|
|
pub(crate) fn new(
|
|
|
|
addr: net::SocketAddr, host: &str, secure: bool,
|
|
|
|
) -> ServerSettings {
|
2018-10-01 23:43:06 +02:00
|
|
|
let host = host.to_owned();
|
2018-07-06 23:28:08 +02:00
|
|
|
let cpu_pool = LazyCell::new();
|
2018-06-25 05:08:28 +02:00
|
|
|
let responses = HttpResponsePool::get_pool();
|
2018-04-14 01:02:01 +02:00
|
|
|
ServerSettings {
|
|
|
|
addr,
|
|
|
|
secure,
|
|
|
|
host,
|
|
|
|
cpu_pool,
|
|
|
|
responses,
|
|
|
|
}
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the socket address of the local half of this TCP connection
|
2018-10-01 23:43:06 +02:00
|
|
|
pub fn local_addr(&self) -> net::SocketAddr {
|
2018-01-12 03:35:05 +01:00
|
|
|
self.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if connection is secure(https)
|
|
|
|
pub fn secure(&self) -> bool {
|
|
|
|
self.secure
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns host header value
|
|
|
|
pub fn host(&self) -> &str {
|
|
|
|
&self.host
|
|
|
|
}
|
2018-03-07 23:56:53 +01:00
|
|
|
|
|
|
|
/// Returns default `CpuPool` for server
|
|
|
|
pub fn cpu_pool(&self) -> &CpuPool {
|
2018-07-06 23:28:08 +02:00
|
|
|
self.cpu_pool.borrow_with(|| DEFAULT_CPUPOOL.lock().clone())
|
2018-03-07 23:56:53 +01:00
|
|
|
}
|
2018-03-23 05:14:57 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn get_response(&self, status: StatusCode, body: Body) -> HttpResponse {
|
|
|
|
HttpResponsePool::get_response(&self.responses, status, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-04-14 01:02:01 +02:00
|
|
|
pub(crate) fn get_response_builder(
|
2018-04-29 07:55:47 +02:00
|
|
|
&self, status: StatusCode,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> HttpResponseBuilder {
|
2018-03-23 05:14:57 +01:00
|
|
|
HttpResponsePool::get_builder(&self.responses, status)
|
|
|
|
}
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 19:05:44 +01:00
|
|
|
// "Sun, 06 Nov 1994 08:49:37 GMT".len()
|
|
|
|
const DATE_VALUE_LENGTH: usize = 29;
|
2018-01-12 03:35:05 +01:00
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
/// Http service configuration
|
|
|
|
pub struct ServiceConfig<H>(Rc<Inner<H>>);
|
2018-09-08 18:20:18 +02:00
|
|
|
|
|
|
|
struct Inner<H> {
|
2018-09-09 23:33:45 +02:00
|
|
|
handler: H,
|
2018-09-29 00:04:59 +02:00
|
|
|
keep_alive: Option<Duration>,
|
2018-09-28 02:15:38 +02:00
|
|
|
client_timeout: u64,
|
2018-10-02 05:04:16 +02:00
|
|
|
client_shutdown: u64,
|
2018-03-10 01:21:14 +01:00
|
|
|
ka_enabled: bool,
|
2018-01-15 02:00:28 +01:00
|
|
|
bytes: Rc<SharedBytesPool>,
|
2018-06-25 06:58:04 +02:00
|
|
|
messages: &'static RequestPool,
|
2018-07-11 08:56:35 +02:00
|
|
|
node: RefCell<Node<()>>,
|
2018-09-08 18:20:18 +02:00
|
|
|
date: UnsafeCell<(bool, Date)>,
|
2018-08-09 20:52:32 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
impl<H> Clone for ServiceConfig<H> {
|
2018-09-08 18:20:18 +02:00
|
|
|
fn clone(&self) -> Self {
|
2018-10-03 00:25:32 +02:00
|
|
|
ServiceConfig(self.0.clone())
|
2018-08-09 20:52:32 +02:00
|
|
|
}
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
impl<H> ServiceConfig<H> {
|
|
|
|
/// Create instance of `ServiceConfig`
|
2018-10-02 05:04:16 +02:00
|
|
|
pub(crate) fn new(
|
|
|
|
handler: H, keep_alive: KeepAlive, client_timeout: u64, client_shutdown: u64,
|
|
|
|
settings: ServerSettings,
|
2018-10-03 00:25:32 +02:00
|
|
|
) -> ServiceConfig<H> {
|
2018-03-10 01:21:14 +01:00
|
|
|
let (keep_alive, ka_enabled) = match keep_alive {
|
|
|
|
KeepAlive::Timeout(val) => (val as u64, true),
|
|
|
|
KeepAlive::Os | KeepAlive::Tcp(_) => (0, true),
|
|
|
|
KeepAlive::Disabled => (0, false),
|
|
|
|
};
|
2018-09-29 00:04:59 +02:00
|
|
|
let keep_alive = if ka_enabled && keep_alive > 0 {
|
|
|
|
Some(Duration::from_secs(keep_alive))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-03-10 01:21:14 +01:00
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
ServiceConfig(Rc::new(Inner {
|
2018-09-09 23:33:45 +02:00
|
|
|
handler,
|
2018-09-08 18:20:18 +02:00
|
|
|
keep_alive,
|
|
|
|
ka_enabled,
|
2018-09-28 02:15:38 +02:00
|
|
|
client_timeout,
|
2018-10-02 05:04:16 +02:00
|
|
|
client_shutdown,
|
2018-01-15 02:00:28 +01:00
|
|
|
bytes: Rc::new(SharedBytesPool::new()),
|
2018-07-04 17:01:27 +02:00
|
|
|
messages: RequestPool::pool(settings),
|
2018-07-11 08:56:35 +02:00
|
|
|
node: RefCell::new(Node::head()),
|
2018-09-08 18:20:18 +02:00
|
|
|
date: UnsafeCell::new((false, Date::new())),
|
|
|
|
}))
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-10-02 05:04:16 +02:00
|
|
|
/// Create worker settings builder.
|
2018-10-03 00:25:32 +02:00
|
|
|
pub fn build(handler: H) -> ServiceConfigBuilder<H> {
|
|
|
|
ServiceConfigBuilder::new(handler)
|
2018-10-02 05:04:16 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 02:15:38 +02:00
|
|
|
pub(crate) fn head(&self) -> RefMut<Node<()>> {
|
2018-09-08 18:20:18 +02:00
|
|
|
self.0.node.borrow_mut()
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 02:15:38 +02:00
|
|
|
pub(crate) fn handler(&self) -> &H {
|
2018-09-09 23:33:45 +02:00
|
|
|
&self.0.handler
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 02:15:38 +02:00
|
|
|
#[inline]
|
2018-10-01 23:43:06 +02:00
|
|
|
/// Keep alive duration if configured.
|
2018-09-29 00:04:59 +02:00
|
|
|
pub fn keep_alive(&self) -> Option<Duration> {
|
2018-09-08 18:20:18 +02:00
|
|
|
self.0.keep_alive
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 02:15:38 +02:00
|
|
|
#[inline]
|
2018-10-01 23:43:06 +02:00
|
|
|
/// Return state of connection keep-alive funcitonality
|
2018-01-12 03:35:05 +01:00
|
|
|
pub fn keep_alive_enabled(&self) -> bool {
|
2018-09-08 18:20:18 +02:00
|
|
|
self.0.ka_enabled
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 02:15:38 +02:00
|
|
|
pub(crate) fn get_bytes(&self) -> BytesMut {
|
2018-09-08 18:20:18 +02:00
|
|
|
self.0.bytes.get_bytes()
|
2018-06-24 06:30:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 02:15:38 +02:00
|
|
|
pub(crate) fn release_bytes(&self, bytes: BytesMut) {
|
2018-09-08 18:20:18 +02:00
|
|
|
self.0.bytes.release_bytes(bytes)
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-28 02:15:38 +02:00
|
|
|
pub(crate) fn get_request(&self) -> Request {
|
2018-09-08 18:20:18 +02:00
|
|
|
RequestPool::get(self.0.messages)
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 20:52:32 +02:00
|
|
|
fn update_date(&self) {
|
2018-06-24 18:21:04 +02:00
|
|
|
// Unsafe: WorkerSetting is !Sync and !Send
|
2018-10-02 06:16:56 +02:00
|
|
|
unsafe { (*self.0.date.get()).0 = false };
|
2018-03-18 19:05:44 +01:00
|
|
|
}
|
2018-09-08 18:20:18 +02:00
|
|
|
}
|
2018-03-18 19:05:44 +01:00
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
impl<H: 'static> ServiceConfig<H> {
|
2018-09-29 00:04:59 +02:00
|
|
|
#[inline]
|
2018-10-01 23:43:06 +02:00
|
|
|
/// Client timeout for first request.
|
2018-09-29 00:04:59 +02:00
|
|
|
pub fn client_timer(&self) -> Option<Delay> {
|
|
|
|
let delay = self.0.client_timeout;
|
|
|
|
if delay != 0 {
|
|
|
|
Some(Delay::new(self.now() + Duration::from_millis(delay)))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 09:19:28 +02:00
|
|
|
/// Client timeout for first request.
|
|
|
|
pub fn client_timer_expire(&self) -> Option<Instant> {
|
|
|
|
let delay = self.0.client_timeout;
|
|
|
|
if delay != 0 {
|
|
|
|
Some(self.now() + Duration::from_millis(delay))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 05:04:16 +02:00
|
|
|
/// Client shutdown timer
|
|
|
|
pub fn client_shutdown_timer(&self) -> Option<Instant> {
|
|
|
|
let delay = self.0.client_shutdown;
|
|
|
|
if delay != 0 {
|
|
|
|
Some(self.now() + Duration::from_millis(delay))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 00:04:59 +02:00
|
|
|
#[inline]
|
2018-10-01 23:43:06 +02:00
|
|
|
/// Return keep-alive timer delay is configured.
|
2018-09-29 00:04:59 +02:00
|
|
|
pub fn keep_alive_timer(&self) -> Option<Delay> {
|
|
|
|
if let Some(ka) = self.0.keep_alive {
|
|
|
|
Some(Delay::new(self.now() + ka))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Keep-alive expire time
|
|
|
|
pub fn keep_alive_expire(&self) -> Option<Instant> {
|
|
|
|
if let Some(ka) = self.0.keep_alive {
|
|
|
|
Some(self.now() + ka)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 02:15:38 +02:00
|
|
|
pub(crate) fn set_date(&self, dst: &mut BytesMut, full: bool) {
|
2018-06-24 18:21:04 +02:00
|
|
|
// Unsafe: WorkerSetting is !Sync and !Send
|
2018-09-08 18:20:18 +02:00
|
|
|
let date_bytes = unsafe {
|
|
|
|
let date = &mut (*self.0.date.get());
|
|
|
|
if !date.0 {
|
|
|
|
date.1.update();
|
|
|
|
date.0 = true;
|
|
|
|
|
|
|
|
// periodic date update
|
|
|
|
let s = self.clone();
|
2018-10-03 04:12:08 +02:00
|
|
|
spawn(sleep(Duration::from_millis(500)).then(move |_| {
|
2018-09-08 18:20:18 +02:00
|
|
|
s.update_date();
|
|
|
|
future::ok(())
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
&date.1.bytes
|
|
|
|
};
|
2018-07-06 23:28:08 +02:00
|
|
|
if full {
|
|
|
|
let mut buf: [u8; 39] = [0; 39];
|
|
|
|
buf[..6].copy_from_slice(b"date: ");
|
|
|
|
buf[6..35].copy_from_slice(date_bytes);
|
|
|
|
buf[35..].copy_from_slice(b"\r\n\r\n");
|
|
|
|
dst.extend_from_slice(&buf);
|
|
|
|
} else {
|
|
|
|
dst.extend_from_slice(date_bytes);
|
2018-06-23 06:13:09 +02:00
|
|
|
}
|
2018-03-20 19:40:05 +01:00
|
|
|
}
|
2018-09-29 00:04:59 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn now(&self) -> Instant {
|
|
|
|
unsafe {
|
|
|
|
let date = &mut (*self.0.date.get());
|
|
|
|
if !date.0 {
|
|
|
|
date.1.update();
|
|
|
|
date.0 = true;
|
|
|
|
|
|
|
|
// periodic date update
|
|
|
|
let s = self.clone();
|
2018-10-03 04:12:08 +02:00
|
|
|
spawn(sleep(Duration::from_millis(500)).then(move |_| {
|
2018-09-29 00:04:59 +02:00
|
|
|
s.update_date();
|
|
|
|
future::ok(())
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
date.1.current
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
/// A service config builder
|
2018-10-02 05:04:16 +02:00
|
|
|
///
|
2018-10-03 00:25:32 +02:00
|
|
|
/// This type can be used to construct an instance of `ServiceConfig` through a
|
2018-10-02 05:04:16 +02:00
|
|
|
/// builder-like pattern.
|
2018-10-03 00:25:32 +02:00
|
|
|
pub struct ServiceConfigBuilder<H> {
|
2018-10-02 05:04:16 +02:00
|
|
|
handler: H,
|
|
|
|
keep_alive: KeepAlive,
|
|
|
|
client_timeout: u64,
|
|
|
|
client_shutdown: u64,
|
|
|
|
host: String,
|
|
|
|
addr: net::SocketAddr,
|
|
|
|
secure: bool,
|
|
|
|
}
|
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
impl<H> ServiceConfigBuilder<H> {
|
|
|
|
/// Create instance of `ServiceConfigBuilder`
|
|
|
|
pub fn new(handler: H) -> ServiceConfigBuilder<H> {
|
|
|
|
ServiceConfigBuilder {
|
2018-10-02 05:04:16 +02:00
|
|
|
handler,
|
|
|
|
keep_alive: KeepAlive::Timeout(5),
|
|
|
|
client_timeout: 5000,
|
|
|
|
client_shutdown: 5000,
|
|
|
|
secure: false,
|
|
|
|
host: "localhost".to_owned(),
|
|
|
|
addr: "127.0.0.1:8080".parse().unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable secure flag for current server.
|
|
|
|
///
|
|
|
|
/// By default this flag is set to false.
|
|
|
|
pub fn secure(mut self) -> Self {
|
|
|
|
self.secure = true;
|
|
|
|
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 shutdown timeout in milliseconds.
|
|
|
|
///
|
|
|
|
/// Defines a timeout for shutdown connection. If a shutdown procedure does not complete
|
|
|
|
/// within this time, the request is dropped. This timeout affects only secure connections.
|
|
|
|
///
|
|
|
|
/// To disable timeout set value to 0.
|
|
|
|
///
|
|
|
|
/// By default client timeout is set to 5000 milliseconds.
|
|
|
|
pub fn client_shutdown(mut self, val: u64) -> Self {
|
|
|
|
self.client_shutdown = 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
|
|
|
|
}
|
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
/// Finish service configuration and create `ServiceConfig` object.
|
|
|
|
pub fn finish(self) -> ServiceConfig<H> {
|
2018-10-02 05:04:16 +02:00
|
|
|
let settings = ServerSettings::new(self.addr, &self.host, self.secure);
|
|
|
|
let client_shutdown = if self.secure { self.client_shutdown } else { 0 };
|
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
ServiceConfig::new(
|
2018-10-02 05:04:16 +02:00
|
|
|
self.handler,
|
|
|
|
self.keep_alive,
|
|
|
|
self.client_timeout,
|
|
|
|
client_shutdown,
|
|
|
|
settings,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 19:05:44 +01:00
|
|
|
struct Date {
|
2018-09-29 00:04:59 +02:00
|
|
|
current: Instant,
|
2018-03-18 19:05:44 +01:00
|
|
|
bytes: [u8; DATE_VALUE_LENGTH],
|
|
|
|
pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Date {
|
|
|
|
fn new() -> Date {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut date = Date {
|
2018-09-29 00:04:59 +02:00
|
|
|
current: Instant::now(),
|
2018-04-14 01:02:01 +02:00
|
|
|
bytes: [0; DATE_VALUE_LENGTH],
|
|
|
|
pos: 0,
|
|
|
|
};
|
2018-03-18 19:05:44 +01:00
|
|
|
date.update();
|
|
|
|
date
|
|
|
|
}
|
|
|
|
fn update(&mut self) {
|
|
|
|
self.pos = 0;
|
2018-09-29 00:04:59 +02:00
|
|
|
self.current = Instant::now();
|
2018-03-18 19:05:44 +01:00
|
|
|
write!(self, "{}", time::at_utc(time::get_time()).rfc822()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Write for Date {
|
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
let len = s.len();
|
|
|
|
self.bytes[self.pos..self.pos + len].copy_from_slice(s.as_bytes());
|
|
|
|
self.pos += len;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-24 06:30:58 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct SharedBytesPool(RefCell<VecDeque<BytesMut>>);
|
|
|
|
|
|
|
|
impl SharedBytesPool {
|
|
|
|
pub fn new() -> SharedBytesPool {
|
|
|
|
SharedBytesPool(RefCell::new(VecDeque::with_capacity(128)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_bytes(&self) -> BytesMut {
|
|
|
|
if let Some(bytes) = self.0.borrow_mut().pop_front() {
|
|
|
|
bytes
|
|
|
|
} else {
|
|
|
|
BytesMut::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn release_bytes(&self, mut bytes: BytesMut) {
|
|
|
|
let v = &mut self.0.borrow_mut();
|
|
|
|
if v.len() < 128 {
|
|
|
|
bytes.clear();
|
|
|
|
v.push_front(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 19:05:44 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-09-08 23:55:39 +02:00
|
|
|
use futures::future;
|
|
|
|
use tokio::runtime::current_thread;
|
2018-03-18 19:05:44 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_len() {
|
2018-05-17 21:20:20 +02:00
|
|
|
assert_eq!(DATE_VALUE_LENGTH, "Sun, 06 Nov 1994 08:49:37 GMT".len());
|
2018-03-18 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date() {
|
2018-09-08 23:55:39 +02:00
|
|
|
let mut rt = current_thread::Runtime::new().unwrap();
|
|
|
|
|
|
|
|
let _ = rt.block_on(future::lazy(|| {
|
2018-10-03 00:25:32 +02:00
|
|
|
let settings = ServiceConfig::<()>::new(
|
2018-09-28 02:21:28 +02:00
|
|
|
(),
|
|
|
|
KeepAlive::Os,
|
|
|
|
0,
|
2018-10-02 05:04:16 +02:00
|
|
|
0,
|
2018-09-28 02:21:28 +02:00
|
|
|
ServerSettings::default(),
|
|
|
|
);
|
2018-09-08 23:55:39 +02:00
|
|
|
let mut buf1 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10);
|
|
|
|
settings.set_date(&mut buf1, true);
|
|
|
|
let mut buf2 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10);
|
|
|
|
settings.set_date(&mut buf2, true);
|
|
|
|
assert_eq!(buf1, buf2);
|
|
|
|
future::ok::<_, ()>(())
|
|
|
|
}));
|
2018-03-18 19:05:44 +01:00
|
|
|
}
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|