1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-01 02:54:36 +01:00
actix-web/actix-http/src/config.rs

291 lines
7.0 KiB
Rust
Raw Normal View History

2019-07-18 13:37:41 +02:00
use std::cell::UnsafeCell;
2019-03-26 19:54:35 +01:00
use std::fmt;
use std::fmt::Write;
use std::rc::Rc;
use std::time::{Duration, Instant};
use bytes::BytesMut;
use futures::{future, Future, FutureExt};
2019-03-26 19:54:35 +01:00
use time;
use tokio_timer::{delay, delay_for, Delay};
2019-03-26 19:54:35 +01:00
// "Sun, 06 Nov 1994 08:49:37 GMT".len()
const DATE_VALUE_LENGTH: usize = 29;
#[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
}
}
}
/// Http service configuration
pub struct ServiceConfig(Rc<Inner>);
struct Inner {
keep_alive: Option<Duration>,
client_timeout: u64,
client_disconnect: u64,
ka_enabled: bool,
timer: DateService,
}
impl Clone for ServiceConfig {
fn clone(&self) -> Self {
ServiceConfig(self.0.clone())
}
}
impl Default for ServiceConfig {
fn default() -> Self {
Self::new(KeepAlive::Timeout(5), 0, 0)
}
}
impl ServiceConfig {
/// Create instance of `ServiceConfig`
pub fn new(
keep_alive: KeepAlive,
client_timeout: u64,
client_disconnect: u64,
) -> ServiceConfig {
let (keep_alive, ka_enabled) = match keep_alive {
KeepAlive::Timeout(val) => (val as u64, true),
KeepAlive::Os => (0, true),
KeepAlive::Disabled => (0, false),
};
let keep_alive = if ka_enabled && keep_alive > 0 {
Some(Duration::from_secs(keep_alive))
} else {
None
};
ServiceConfig(Rc::new(Inner {
keep_alive,
ka_enabled,
client_timeout,
client_disconnect,
timer: DateService::new(),
}))
}
#[inline]
/// Keep alive duration if configured.
pub fn keep_alive(&self) -> Option<Duration> {
self.0.keep_alive
}
#[inline]
/// Return state of connection keep-alive funcitonality
pub fn keep_alive_enabled(&self) -> bool {
self.0.ka_enabled
}
#[inline]
/// Client timeout for first request.
pub fn client_timer(&self) -> Option<Delay> {
let delay_time = self.0.client_timeout;
if delay_time != 0 {
Some(delay(
self.0.timer.now() + Duration::from_millis(delay_time),
2019-03-26 19:54:35 +01:00
))
} else {
None
}
}
/// Client timeout for first request.
pub fn client_timer_expire(&self) -> Option<Instant> {
let delay = self.0.client_timeout;
if delay != 0 {
Some(self.0.timer.now() + Duration::from_millis(delay))
} else {
None
}
}
/// Client disconnect timer
pub fn client_disconnect_timer(&self) -> Option<Instant> {
let delay = self.0.client_disconnect;
if delay != 0 {
Some(self.0.timer.now() + Duration::from_millis(delay))
} else {
None
}
}
#[inline]
/// Return keep-alive timer delay is configured.
pub fn keep_alive_timer(&self) -> Option<Delay> {
if let Some(ka) = self.0.keep_alive {
Some(delay(self.0.timer.now() + ka))
2019-03-26 19:54:35 +01:00
} else {
None
}
}
/// Keep-alive expire time
pub fn keep_alive_expire(&self) -> Option<Instant> {
if let Some(ka) = self.0.keep_alive {
Some(self.0.timer.now() + ka)
} else {
None
}
}
#[inline]
pub(crate) fn now(&self) -> Instant {
self.0.timer.now()
}
2019-05-14 17:48:11 +02:00
#[doc(hidden)]
pub fn set_date(&self, dst: &mut BytesMut) {
2019-03-26 19:54:35 +01:00
let mut buf: [u8; 39] = [0; 39];
buf[..6].copy_from_slice(b"date: ");
2019-07-18 13:37:41 +02:00
self.0
.timer
.set_date(|date| buf[6..35].copy_from_slice(&date.bytes));
2019-03-26 19:54:35 +01:00
buf[35..].copy_from_slice(b"\r\n\r\n");
dst.extend_from_slice(&buf);
}
pub(crate) fn set_date_header(&self, dst: &mut BytesMut) {
2019-07-18 13:37:41 +02:00
self.0
.timer
.set_date(|date| dst.extend_from_slice(&date.bytes));
2019-03-26 19:54:35 +01:00
}
}
#[derive(Copy, Clone)]
2019-03-26 19:54:35 +01:00
struct Date {
bytes: [u8; DATE_VALUE_LENGTH],
pos: usize,
}
impl Date {
fn new() -> Date {
let mut date = Date {
bytes: [0; DATE_VALUE_LENGTH],
pos: 0,
};
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(())
}
}
#[derive(Clone)]
struct DateService(Rc<DateServiceInner>);
struct DateServiceInner {
2019-07-18 13:37:41 +02:00
current: UnsafeCell<Option<(Date, Instant)>>,
2019-03-26 19:54:35 +01:00
}
impl DateServiceInner {
fn new() -> Self {
DateServiceInner {
2019-07-18 13:37:41 +02:00
current: UnsafeCell::new(None),
2019-03-26 19:54:35 +01:00
}
}
fn reset(&self) {
2019-07-18 13:37:41 +02:00
unsafe { (&mut *self.current.get()).take() };
2019-03-26 19:54:35 +01:00
}
fn update(&self) {
let now = Instant::now();
let date = Date::new();
2019-07-18 13:37:41 +02:00
*(unsafe { &mut *self.current.get() }) = Some((date, now));
2019-03-26 19:54:35 +01:00
}
}
impl DateService {
fn new() -> Self {
DateService(Rc::new(DateServiceInner::new()))
}
fn check_date(&self) {
2019-07-18 13:37:41 +02:00
if unsafe { (&*self.0.current.get()).is_none() } {
2019-03-26 19:54:35 +01:00
self.0.update();
// periodic date update
let s = self.clone();
tokio_executor::current_thread::spawn(
delay_for(Duration::from_millis(500)).then(move |_| {
2019-03-26 19:54:35 +01:00
s.0.reset();
future::ready(())
}),
);
2019-03-26 19:54:35 +01:00
}
}
fn now(&self) -> Instant {
self.check_date();
2019-07-18 13:37:41 +02:00
unsafe { (&*self.0.current.get()).as_ref().unwrap().1 }
2019-03-26 19:54:35 +01:00
}
2019-07-18 13:37:41 +02:00
fn set_date<F: FnMut(&Date)>(&self, mut f: F) {
2019-03-26 19:54:35 +01:00
self.check_date();
2019-07-18 13:37:41 +02:00
f(&unsafe { (&*self.0.current.get()).as_ref().unwrap().0 })
2019-03-26 19:54:35 +01:00
}
}
#[cfg(test)]
mod tests {
use super::*;
use actix_rt::System;
use futures::future;
#[test]
fn test_date_len() {
assert_eq!(DATE_VALUE_LENGTH, "Sun, 06 Nov 1994 08:49:37 GMT".len());
}
#[test]
fn test_date() {
let mut rt = System::new("test");
2019-11-19 13:54:19 +01:00
let _ = rt.block_on(future::lazy(|_| {
2019-03-26 19:54:35 +01:00
let settings = ServiceConfig::new(KeepAlive::Os, 0, 0);
let mut buf1 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10);
settings.set_date(&mut buf1);
let mut buf2 = BytesMut::with_capacity(DATE_VALUE_LENGTH + 10);
settings.set_date(&mut buf2);
assert_eq!(buf1, buf2);
future::ok::<_, ()>(())
}));
}
}