2018-10-05 06:14:18 +02:00
|
|
|
use std::cell::UnsafeCell;
|
2019-03-09 19:39:06 +01:00
|
|
|
use std::fmt;
|
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-06-17 19:51:20 +02:00
|
|
|
|
|
|
|
use bytes::BytesMut;
|
2018-09-08 18:20:18 +02:00
|
|
|
use futures::{future, Future};
|
2018-03-18 19:05:44 +01:00
|
|
|
use time;
|
2018-09-27 05:43:54 +02:00
|
|
|
use tokio_timer::{sleep, Delay};
|
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-05 17:00:36 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
|
|
|
/// Server keep-alive setting
|
|
|
|
pub enum KeepAlive {
|
|
|
|
/// Keep alive in seconds
|
|
|
|
Timeout(usize),
|
|
|
|
/// Relay on OS to shutdown tcp connection
|
|
|
|
Os,
|
|
|
|
/// Disabled
|
|
|
|
Disabled,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<usize> for KeepAlive {
|
|
|
|
fn from(keepalive: usize) -> Self {
|
|
|
|
KeepAlive::Timeout(keepalive)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Option<usize>> for KeepAlive {
|
|
|
|
fn from(keepalive: Option<usize>) -> Self {
|
|
|
|
if let Some(keepalive) = keepalive {
|
|
|
|
KeepAlive::Timeout(keepalive)
|
|
|
|
} else {
|
|
|
|
KeepAlive::Disabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
/// Http service configuration
|
2018-10-05 02:00:27 +02:00
|
|
|
pub struct ServiceConfig(Rc<Inner>);
|
2018-09-08 18:20:18 +02:00
|
|
|
|
2018-10-05 02:00:27 +02:00
|
|
|
struct Inner {
|
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-05 08:39:11 +02:00
|
|
|
client_disconnect: u64,
|
2018-03-10 01:21:14 +01:00
|
|
|
ka_enabled: bool,
|
2018-10-08 19:14:29 +02:00
|
|
|
timer: DateService,
|
2018-08-09 20:52:32 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 02:00:27 +02:00
|
|
|
impl Clone for ServiceConfig {
|
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-09 00:52:12 +02:00
|
|
|
impl Default for ServiceConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new(KeepAlive::Timeout(5), 0, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:00:27 +02:00
|
|
|
impl ServiceConfig {
|
2018-10-03 00:25:32 +02:00
|
|
|
/// Create instance of `ServiceConfig`
|
2019-03-05 00:58:39 +01:00
|
|
|
pub fn new(
|
2018-10-30 00:39:46 +01:00
|
|
|
keep_alive: KeepAlive,
|
|
|
|
client_timeout: u64,
|
|
|
|
client_disconnect: u64,
|
2018-10-05 02:00:27 +02:00
|
|
|
) -> ServiceConfig {
|
2018-03-10 01:21:14 +01:00
|
|
|
let (keep_alive, ka_enabled) = match keep_alive {
|
|
|
|
KeepAlive::Timeout(val) => (val as u64, true),
|
2018-10-05 02:00:27 +02:00
|
|
|
KeepAlive::Os => (0, true),
|
2018-03-10 01:21:14 +01:00
|
|
|
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-08 18:20:18 +02:00
|
|
|
keep_alive,
|
|
|
|
ka_enabled,
|
2018-09-28 02:15:38 +02:00
|
|
|
client_timeout,
|
2018-10-05 08:39:11 +02:00
|
|
|
client_disconnect,
|
2018-10-08 19:14:29 +02:00
|
|
|
timer: DateService::with(Duration::from_millis(500)),
|
2018-09-08 18:20:18 +02:00
|
|
|
}))
|
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-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 {
|
2018-10-08 19:14:29 +02:00
|
|
|
Some(Delay::new(
|
|
|
|
self.0.timer.now() + Duration::from_millis(delay),
|
|
|
|
))
|
2018-09-29 00:04:59 +02:00
|
|
|
} 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 {
|
2018-10-08 19:14:29 +02:00
|
|
|
Some(self.0.timer.now() + Duration::from_millis(delay))
|
2018-10-02 09:19:28 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 08:39:11 +02:00
|
|
|
/// Client disconnect timer
|
|
|
|
pub fn client_disconnect_timer(&self) -> Option<Instant> {
|
|
|
|
let delay = self.0.client_disconnect;
|
2018-10-02 05:04:16 +02:00
|
|
|
if delay != 0 {
|
2018-10-08 19:14:29 +02:00
|
|
|
Some(self.0.timer.now() + Duration::from_millis(delay))
|
2018-10-02 05:04:16 +02:00
|
|
|
} 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 {
|
2018-10-08 19:14:29 +02:00
|
|
|
Some(Delay::new(self.0.timer.now() + ka))
|
2018-09-29 00:04:59 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Keep-alive expire time
|
|
|
|
pub fn keep_alive_expire(&self) -> Option<Instant> {
|
|
|
|
if let Some(ka) = self.0.keep_alive {
|
2018-10-08 19:14:29 +02:00
|
|
|
Some(self.0.timer.now() + ka)
|
2018-09-29 00:04:59 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn now(&self) -> Instant {
|
2018-10-08 19:14:29 +02:00
|
|
|
self.0.timer.now()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn set_date(&self, dst: &mut BytesMut) {
|
|
|
|
let mut buf: [u8; 39] = [0; 39];
|
|
|
|
buf[..6].copy_from_slice(b"date: ");
|
|
|
|
buf[6..35].copy_from_slice(&self.0.timer.date().bytes);
|
|
|
|
buf[35..].copy_from_slice(b"\r\n\r\n");
|
|
|
|
dst.extend_from_slice(&buf);
|
2018-09-29 00:04:59 +02:00
|
|
|
}
|
2019-02-06 20:44:15 +01:00
|
|
|
|
|
|
|
pub(crate) fn set_date_header(&self, dst: &mut BytesMut) {
|
|
|
|
dst.extend_from_slice(&self.0.timer.date().bytes);
|
|
|
|
}
|
2018-03-18 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Date {
|
|
|
|
bytes: [u8; DATE_VALUE_LENGTH],
|
|
|
|
pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Date {
|
|
|
|
fn new() -> Date {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut date = Date {
|
|
|
|
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;
|
|
|
|
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-10-08 19:14:29 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct DateService(Rc<DateServiceInner>);
|
|
|
|
|
|
|
|
struct DateServiceInner {
|
|
|
|
interval: Duration,
|
|
|
|
current: UnsafeCell<Option<(Date, Instant)>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DateServiceInner {
|
|
|
|
fn new(interval: Duration) -> Self {
|
|
|
|
DateServiceInner {
|
|
|
|
interval,
|
|
|
|
current: UnsafeCell::new(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_ref(&self) -> &Option<(Date, Instant)> {
|
|
|
|
unsafe { &*self.current.get() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(&self) {
|
|
|
|
unsafe { (&mut *self.current.get()).take() };
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&self) {
|
|
|
|
let now = Instant::now();
|
|
|
|
let date = Date::new();
|
|
|
|
*(unsafe { &mut *self.current.get() }) = Some((date, now));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DateService {
|
|
|
|
fn with(resolution: Duration) -> Self {
|
|
|
|
DateService(Rc::new(DateServiceInner::new(resolution)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_date(&self) {
|
|
|
|
if self.0.get_ref().is_none() {
|
|
|
|
self.0.update();
|
|
|
|
|
|
|
|
// periodic date update
|
|
|
|
let s = self.clone();
|
2019-01-27 19:59:07 +01:00
|
|
|
tokio_current_thread::spawn(sleep(Duration::from_millis(500)).then(
|
|
|
|
move |_| {
|
|
|
|
s.0.reset();
|
|
|
|
future::ok(())
|
|
|
|
},
|
|
|
|
));
|
2018-10-08 19:14:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn now(&self) -> Instant {
|
|
|
|
self.check_date();
|
|
|
|
self.0.get_ref().as_ref().unwrap().1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn date(&self) -> &Date {
|
|
|
|
self.check_date();
|
|
|
|
|
|
|
|
let item = self.0.get_ref().as_ref().unwrap();
|
|
|
|
&item.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 19:05:44 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-12-11 03:08:33 +01:00
|
|
|
use actix_rt::System;
|
2018-09-08 23:55:39 +02:00
|
|
|
use futures::future;
|
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-12-11 03:08:33 +01:00
|
|
|
let mut rt = System::new("test");
|
2018-09-08 23:55:39 +02:00
|
|
|
|
|
|
|
let _ = rt.block_on(future::lazy(|| {
|
2018-10-05 06:14:18 +02:00
|
|
|
let settings = ServiceConfig::new(KeepAlive::Os, 0, 0);
|
2018-09-08 23:55:39 +02:00
|
|
|
let mut buf1 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10);
|
2018-10-08 19:14:29 +02:00
|
|
|
settings.set_date(&mut buf1);
|
2018-09-08 23:55:39 +02:00
|
|
|
let mut buf2 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10);
|
2018-10-08 19:14:29 +02:00
|
|
|
settings.set_date(&mut buf2);
|
2018-09-08 23:55:39 +02:00
|
|
|
assert_eq!(buf1, buf2);
|
|
|
|
future::ok::<_, ()>(())
|
|
|
|
}));
|
2018-03-18 19:05:44 +01:00
|
|
|
}
|
2018-01-12 03:35:05 +01:00
|
|
|
}
|