2018-10-05 01:22:00 +02:00
|
|
|
use std::collections::VecDeque;
|
2018-10-07 18:48:53 +02:00
|
|
|
use std::fmt::Debug;
|
2018-11-17 06:09:33 +01:00
|
|
|
use std::mem;
|
2018-10-05 08:39:11 +02:00
|
|
|
use std::time::Instant;
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-12-11 03:08:33 +01:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
|
|
|
use actix_service::Service;
|
2019-02-10 05:27:39 +01:00
|
|
|
use actix_utils::cloneable::CloneableService;
|
2018-12-06 23:32:52 +01:00
|
|
|
use bitflags::bitflags;
|
|
|
|
use futures::{try_ready, Async, Future, Poll, Sink, Stream};
|
|
|
|
use log::{debug, error, trace};
|
2018-10-05 08:39:11 +02:00
|
|
|
use tokio_timer::Delay;
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-12-06 23:32:52 +01:00
|
|
|
use crate::body::{Body, BodyLength, MessageBody, ResponseBody};
|
|
|
|
use crate::config::ServiceConfig;
|
|
|
|
use crate::error::DispatchError;
|
|
|
|
use crate::error::{ParseError, PayloadError};
|
|
|
|
use crate::request::Request;
|
|
|
|
use crate::response::Response;
|
2018-10-05 05:02:10 +02:00
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
use super::codec::Codec;
|
2019-02-07 20:06:05 +01:00
|
|
|
use super::payload::{Payload, PayloadSender, PayloadStatus, PayloadWriter};
|
2019-03-07 07:56:34 +01:00
|
|
|
use super::{Message, MessageType};
|
2018-10-05 01:22:00 +02:00
|
|
|
|
|
|
|
const MAX_PIPELINED_MESSAGES: usize = 16;
|
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
pub struct Flags: u8 {
|
|
|
|
const STARTED = 0b0000_0001;
|
|
|
|
const KEEPALIVE_ENABLED = 0b0000_0010;
|
|
|
|
const KEEPALIVE = 0b0000_0100;
|
2018-10-07 18:48:53 +02:00
|
|
|
const POLLED = 0b0000_1000;
|
|
|
|
const SHUTDOWN = 0b0010_0000;
|
|
|
|
const DISCONNECTED = 0b0100_0000;
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dispatcher for HTTP/1.1 protocol
|
2019-03-09 16:37:23 +01:00
|
|
|
pub struct Dispatcher<T, S: Service<Request = Request> + 'static, B: MessageBody>
|
2018-11-17 06:09:33 +01:00
|
|
|
where
|
|
|
|
S::Error: Debug,
|
|
|
|
{
|
2018-11-18 05:21:28 +01:00
|
|
|
inner: Option<InnerDispatcher<T, S, B>>,
|
2018-11-17 06:09:33 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 16:37:23 +01:00
|
|
|
struct InnerDispatcher<T, S: Service<Request = Request> + 'static, B: MessageBody>
|
2018-10-05 01:22:00 +02:00
|
|
|
where
|
2018-10-07 18:48:53 +02:00
|
|
|
S::Error: Debug,
|
2018-10-05 01:22:00 +02:00
|
|
|
{
|
2019-02-10 05:27:39 +01:00
|
|
|
service: CloneableService<S>,
|
2018-10-05 01:22:00 +02:00
|
|
|
flags: Flags,
|
2018-11-17 06:09:33 +01:00
|
|
|
framed: Framed<T, Codec>,
|
2019-03-07 07:56:34 +01:00
|
|
|
error: Option<DispatchError>,
|
2018-10-05 08:39:11 +02:00
|
|
|
config: ServiceConfig,
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
state: State<S, B>,
|
2018-10-05 16:02:09 +02:00
|
|
|
payload: Option<PayloadSender>,
|
2018-10-23 03:18:05 +02:00
|
|
|
messages: VecDeque<DispatcherMessage>,
|
2018-10-14 08:57:31 +02:00
|
|
|
unhandled: Option<Request>,
|
2018-10-05 06:14:18 +02:00
|
|
|
|
2018-10-05 08:39:11 +02:00
|
|
|
ka_expire: Instant,
|
|
|
|
ka_timer: Option<Delay>,
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
enum DispatcherMessage {
|
2018-10-07 06:07:32 +02:00
|
|
|
Item(Request),
|
2018-11-18 22:48:42 +01:00
|
|
|
Error(Response<()>),
|
2018-10-07 06:07:32 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 16:37:23 +01:00
|
|
|
enum State<S: Service<Request = Request>, B: MessageBody> {
|
2018-10-05 01:22:00 +02:00
|
|
|
None,
|
2018-10-07 09:04:38 +02:00
|
|
|
ServiceCall(S::Future),
|
2018-11-21 16:49:24 +01:00
|
|
|
SendPayload(ResponseBody<B>),
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 16:37:23 +01:00
|
|
|
impl<S: Service<Request = Request>, B: MessageBody> State<S, B> {
|
2018-10-05 01:22:00 +02:00
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
if let State::None = self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
impl<T, S, B> Dispatcher<T, S, B>
|
2018-10-05 01:22:00 +02:00
|
|
|
where
|
2018-10-05 05:02:10 +02:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2019-03-09 16:37:23 +01:00
|
|
|
S: Service<Request = Request> + 'static,
|
2018-10-07 18:48:53 +02:00
|
|
|
S::Error: Debug,
|
2019-02-09 17:44:22 +01:00
|
|
|
S::Response: Into<Response<B>>,
|
2018-11-18 05:21:28 +01:00
|
|
|
B: MessageBody,
|
2018-10-05 01:22:00 +02:00
|
|
|
{
|
2018-10-05 05:02:10 +02:00
|
|
|
/// Create http/1 dispatcher.
|
2019-02-10 05:27:39 +01:00
|
|
|
pub fn new(stream: T, config: ServiceConfig, service: CloneableService<S>) -> Self {
|
2019-03-07 07:56:34 +01:00
|
|
|
Dispatcher::with_timeout(
|
|
|
|
Framed::new(stream, Codec::new(config.clone())),
|
|
|
|
config,
|
|
|
|
None,
|
|
|
|
service,
|
|
|
|
)
|
2018-10-05 08:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create http/1 dispatcher with slow request timeout.
|
|
|
|
pub fn with_timeout(
|
2019-03-07 07:56:34 +01:00
|
|
|
framed: Framed<T, Codec>,
|
2018-10-30 00:39:46 +01:00
|
|
|
config: ServiceConfig,
|
|
|
|
timeout: Option<Delay>,
|
2019-02-10 05:27:39 +01:00
|
|
|
service: CloneableService<S>,
|
2018-10-05 08:39:11 +02:00
|
|
|
) -> Self {
|
2018-10-05 19:03:10 +02:00
|
|
|
let keepalive = config.keep_alive_enabled();
|
|
|
|
let flags = if keepalive {
|
2018-11-19 02:52:56 +01:00
|
|
|
Flags::KEEPALIVE | Flags::KEEPALIVE_ENABLED
|
2018-10-05 08:39:11 +02:00
|
|
|
} else {
|
2018-11-19 02:52:56 +01:00
|
|
|
Flags::empty()
|
2018-10-05 08:39:11 +02:00
|
|
|
};
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-10-09 00:24:51 +02:00
|
|
|
// keep-alive timer
|
2018-10-05 08:39:11 +02:00
|
|
|
let (ka_expire, ka_timer) = if let Some(delay) = timeout {
|
|
|
|
(delay.deadline(), Some(delay))
|
|
|
|
} else if let Some(delay) = config.keep_alive_timer() {
|
|
|
|
(delay.deadline(), Some(delay))
|
|
|
|
} else {
|
|
|
|
(config.now(), None)
|
|
|
|
};
|
|
|
|
|
2018-10-05 05:02:10 +02:00
|
|
|
Dispatcher {
|
2018-11-17 06:09:33 +01:00
|
|
|
inner: Some(InnerDispatcher {
|
|
|
|
framed,
|
|
|
|
payload: None,
|
|
|
|
state: State::None,
|
|
|
|
error: None,
|
|
|
|
messages: VecDeque::new(),
|
|
|
|
unhandled: None,
|
|
|
|
service,
|
|
|
|
flags,
|
|
|
|
config,
|
|
|
|
ka_expire,
|
|
|
|
ka_timer,
|
|
|
|
}),
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-17 06:09:33 +01:00
|
|
|
}
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
impl<T, S, B> InnerDispatcher<T, S, B>
|
2018-11-17 06:09:33 +01:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite,
|
2019-03-09 16:37:23 +01:00
|
|
|
S: Service<Request = Request> + 'static,
|
2018-11-17 06:09:33 +01:00
|
|
|
S::Error: Debug,
|
2019-02-09 17:44:22 +01:00
|
|
|
S::Response: Into<Response<B>>,
|
2018-11-18 05:21:28 +01:00
|
|
|
B: MessageBody,
|
2018-11-17 06:09:33 +01:00
|
|
|
{
|
2018-10-05 01:22:00 +02:00
|
|
|
fn can_read(&self) -> bool {
|
2018-10-07 18:48:53 +02:00
|
|
|
if self.flags.contains(Flags::DISCONNECTED) {
|
2018-10-05 01:22:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref info) = self.payload {
|
|
|
|
info.need_read() == PayloadStatus::Read
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if checked is set to true, delay disconnect until all tasks have finished.
|
2018-10-07 06:07:32 +02:00
|
|
|
fn client_disconnected(&mut self) {
|
2018-10-07 18:48:53 +02:00
|
|
|
self.flags.insert(Flags::DISCONNECTED);
|
2018-10-05 01:22:00 +02:00
|
|
|
if let Some(mut payload) = self.payload.take() {
|
2018-11-14 18:38:16 +01:00
|
|
|
payload.set_error(PayloadError::Incomplete(None));
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Flush stream
|
2019-03-07 07:56:34 +01:00
|
|
|
fn poll_flush(&mut self) -> Poll<bool, DispatchError> {
|
2018-11-19 02:52:56 +01:00
|
|
|
if !self.framed.is_write_buf_empty() {
|
2018-11-17 06:09:33 +01:00
|
|
|
match self.framed.poll_complete() {
|
2018-10-05 01:22:00 +02:00
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Err(err) => {
|
|
|
|
debug!("Error sending data: {}", err);
|
|
|
|
Err(err.into())
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(_)) => {
|
|
|
|
// if payload is not consumed we can not use connection
|
|
|
|
if self.payload.is_some() && self.state.is_empty() {
|
2018-10-05 05:02:10 +02:00
|
|
|
return Err(DispatchError::PayloadIsNotConsumed);
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
2019-02-19 06:41:38 +01:00
|
|
|
Ok(Async::Ready(true))
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-19 06:41:38 +01:00
|
|
|
Ok(Async::Ready(false))
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 16:49:24 +01:00
|
|
|
fn send_response(
|
2018-11-17 06:09:33 +01:00
|
|
|
&mut self,
|
2018-11-18 22:48:42 +01:00
|
|
|
message: Response<()>,
|
2018-11-21 16:49:24 +01:00
|
|
|
body: ResponseBody<B>,
|
2019-03-07 07:56:34 +01:00
|
|
|
) -> Result<State<S, B>, DispatchError> {
|
2018-11-17 06:09:33 +01:00
|
|
|
self.framed
|
2018-11-19 02:52:56 +01:00
|
|
|
.force_send(Message::Item((message, body.length())))
|
2018-11-17 06:09:33 +01:00
|
|
|
.map_err(|err| {
|
|
|
|
if let Some(mut payload) = self.payload.take() {
|
|
|
|
payload.set_error(PayloadError::Incomplete(None));
|
|
|
|
}
|
|
|
|
DispatchError::Io(err)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
self.flags
|
|
|
|
.set(Flags::KEEPALIVE, self.framed.get_codec().keepalive());
|
2018-11-18 05:21:28 +01:00
|
|
|
match body.length() {
|
2018-11-18 22:48:42 +01:00
|
|
|
BodyLength::None | BodyLength::Empty => Ok(State::None),
|
2018-11-18 05:21:28 +01:00
|
|
|
_ => Ok(State::SendPayload(body)),
|
2018-11-17 06:09:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 07:56:34 +01:00
|
|
|
fn poll_response(&mut self) -> Result<(), DispatchError> {
|
2018-10-05 01:22:00 +02:00
|
|
|
let mut retry = self.can_read();
|
|
|
|
loop {
|
2018-11-17 06:09:33 +01:00
|
|
|
let state = match mem::replace(&mut self.state, State::None) {
|
|
|
|
State::None => match self.messages.pop_front() {
|
|
|
|
Some(DispatcherMessage::Item(req)) => {
|
|
|
|
Some(self.handle_request(req)?)
|
2018-10-30 00:39:46 +01:00
|
|
|
}
|
2018-11-17 06:09:33 +01:00
|
|
|
Some(DispatcherMessage::Error(res)) => {
|
2018-11-21 16:49:24 +01:00
|
|
|
self.send_response(res, ResponseBody::Other(Body::Empty))?;
|
2018-11-18 05:21:28 +01:00
|
|
|
None
|
2018-11-17 06:09:33 +01:00
|
|
|
}
|
|
|
|
None => None,
|
2018-10-05 01:22:00 +02:00
|
|
|
},
|
2019-03-12 00:42:33 +01:00
|
|
|
State::ServiceCall(mut fut) => match fut.poll() {
|
|
|
|
Ok(Async::Ready(res)) => {
|
|
|
|
let (res, body) = res.into().replace_body(());
|
|
|
|
Some(self.send_response(res, body)?)
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
2019-03-12 00:42:33 +01:00
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
self.state = State::ServiceCall(fut);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Err(_e) => {
|
|
|
|
let res: Response = Response::InternalServerError().finish();
|
|
|
|
let (res, body) = res.replace_body(());
|
|
|
|
Some(self.send_response(res, body.into_body())?)
|
|
|
|
}
|
|
|
|
},
|
2018-11-17 06:09:33 +01:00
|
|
|
State::SendPayload(mut stream) => {
|
2018-11-14 19:52:40 +01:00
|
|
|
loop {
|
2018-11-17 06:09:33 +01:00
|
|
|
if !self.framed.is_write_buf_full() {
|
2018-11-18 05:21:28 +01:00
|
|
|
match stream
|
|
|
|
.poll_next()
|
|
|
|
.map_err(|_| DispatchError::Unknown)?
|
|
|
|
{
|
2018-11-14 19:52:40 +01:00
|
|
|
Async::Ready(Some(item)) => {
|
2018-11-17 06:09:33 +01:00
|
|
|
self.framed
|
|
|
|
.force_send(Message::Chunk(Some(item)))?;
|
2018-10-09 00:24:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-14 19:52:40 +01:00
|
|
|
Async::Ready(None) => {
|
2018-11-17 06:09:33 +01:00
|
|
|
self.framed.force_send(Message::Chunk(None))?;
|
|
|
|
}
|
|
|
|
Async::NotReady => {
|
|
|
|
self.state = State::SendPayload(stream);
|
|
|
|
return Ok(());
|
2018-10-09 00:24:51 +02:00
|
|
|
}
|
2018-11-14 19:52:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-17 06:09:33 +01:00
|
|
|
self.state = State::SendPayload(stream);
|
2018-11-14 19:52:40 +01:00
|
|
|
return Ok(());
|
2018-10-09 00:24:51 +02:00
|
|
|
}
|
2018-11-14 19:52:40 +01:00
|
|
|
break;
|
2018-10-09 00:24:51 +02:00
|
|
|
}
|
2018-11-14 19:52:40 +01:00
|
|
|
None
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match state {
|
2018-10-07 07:36:57 +02:00
|
|
|
Some(state) => self.state = state,
|
2018-10-05 01:22:00 +02:00
|
|
|
None => {
|
|
|
|
// if read-backpressure is enabled and we consumed some data.
|
2018-11-17 06:09:33 +01:00
|
|
|
// we may read more data and retry
|
2018-10-07 18:48:53 +02:00
|
|
|
if !retry && self.can_read() && self.poll_request()? {
|
2018-10-05 01:22:00 +02:00
|
|
|
retry = self.can_read();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-03-07 07:56:34 +01:00
|
|
|
fn handle_request(&mut self, req: Request) -> Result<State<S, B>, DispatchError> {
|
2018-10-07 07:36:57 +02:00
|
|
|
let mut task = self.service.call(req);
|
2019-03-12 00:42:33 +01:00
|
|
|
match task.poll() {
|
|
|
|
Ok(Async::Ready(res)) => {
|
2019-02-09 17:44:22 +01:00
|
|
|
let (res, body) = res.into().replace_body(());
|
2018-11-17 06:09:33 +01:00
|
|
|
self.send_response(res, body)
|
2018-10-07 07:36:57 +02:00
|
|
|
}
|
2019-03-12 00:42:33 +01:00
|
|
|
Ok(Async::NotReady) => Ok(State::ServiceCall(task)),
|
|
|
|
Err(_e) => {
|
|
|
|
let res: Response = Response::InternalServerError().finish();
|
|
|
|
let (res, body) = res.replace_body(());
|
|
|
|
self.send_response(res, body.into_body())
|
|
|
|
}
|
2018-10-07 07:36:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:36:40 +02:00
|
|
|
/// Process one incoming requests
|
2019-03-07 07:56:34 +01:00
|
|
|
pub(self) fn poll_request(&mut self) -> Result<bool, DispatchError> {
|
2018-10-09 19:36:40 +02:00
|
|
|
// limit a mount of non processed requests
|
|
|
|
if self.messages.len() >= MAX_PIPELINED_MESSAGES {
|
|
|
|
return Ok(false);
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut updated = false;
|
2018-10-30 00:39:46 +01:00
|
|
|
loop {
|
2018-11-17 06:09:33 +01:00
|
|
|
match self.framed.poll() {
|
2018-10-09 19:36:40 +02:00
|
|
|
Ok(Async::Ready(Some(msg))) => {
|
|
|
|
updated = true;
|
|
|
|
self.flags.insert(Flags::STARTED);
|
|
|
|
|
|
|
|
match msg {
|
2019-02-06 20:44:15 +01:00
|
|
|
Message::Item(mut req) => {
|
2018-11-17 06:09:33 +01:00
|
|
|
match self.framed.get_codec().message_type() {
|
2019-03-18 05:57:53 +01:00
|
|
|
MessageType::Payload | MessageType::Stream => {
|
2019-01-29 19:14:00 +01:00
|
|
|
let (ps, pl) = Payload::create(false);
|
2019-02-13 22:52:11 +01:00
|
|
|
let (req1, _) =
|
|
|
|
req.replace_payload(crate::Payload::H1(pl));
|
|
|
|
req = req1;
|
2018-10-14 08:57:31 +02:00
|
|
|
self.payload = Some(ps);
|
|
|
|
}
|
2019-03-18 05:57:53 +01:00
|
|
|
//MessageType::Stream => {
|
|
|
|
// self.unhandled = Some(req);
|
|
|
|
// return Ok(updated);
|
|
|
|
//}
|
2018-10-14 08:57:31 +02:00
|
|
|
_ => (),
|
2018-10-09 19:36:40 +02:00
|
|
|
}
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-10-09 19:36:40 +02:00
|
|
|
// handle request early
|
|
|
|
if self.state.is_empty() {
|
|
|
|
self.state = self.handle_request(req)?;
|
|
|
|
} else {
|
2018-10-23 03:18:05 +02:00
|
|
|
self.messages.push_back(DispatcherMessage::Item(req));
|
2018-10-09 19:36:40 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
Message::Chunk(Some(chunk)) => {
|
2018-10-09 19:36:40 +02:00
|
|
|
if let Some(ref mut payload) = self.payload {
|
|
|
|
payload.feed_data(chunk);
|
|
|
|
} else {
|
|
|
|
error!(
|
|
|
|
"Internal server error: unexpected payload chunk"
|
|
|
|
);
|
|
|
|
self.flags.insert(Flags::DISCONNECTED);
|
2018-10-23 03:18:05 +02:00
|
|
|
self.messages.push_back(DispatcherMessage::Error(
|
2018-11-18 22:48:42 +01:00
|
|
|
Response::InternalServerError().finish().drop_body(),
|
2018-10-09 19:36:40 +02:00
|
|
|
));
|
|
|
|
self.error = Some(DispatchError::InternalError);
|
2018-11-06 04:32:03 +01:00
|
|
|
break;
|
2018-10-09 19:36:40 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
Message::Chunk(None) => {
|
2018-10-09 19:36:40 +02:00
|
|
|
if let Some(mut payload) = self.payload.take() {
|
|
|
|
payload.feed_eof();
|
|
|
|
} else {
|
|
|
|
error!("Internal server error: unexpected eof");
|
|
|
|
self.flags.insert(Flags::DISCONNECTED);
|
2018-10-23 03:18:05 +02:00
|
|
|
self.messages.push_back(DispatcherMessage::Error(
|
2018-11-18 22:48:42 +01:00
|
|
|
Response::InternalServerError().finish().drop_body(),
|
2018-10-09 19:36:40 +02:00
|
|
|
));
|
|
|
|
self.error = Some(DispatchError::InternalError);
|
2018-11-06 04:32:03 +01:00
|
|
|
break;
|
2018-10-09 19:36:40 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
2018-10-09 19:36:40 +02:00
|
|
|
}
|
|
|
|
Ok(Async::Ready(None)) => {
|
|
|
|
self.client_disconnected();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => break,
|
|
|
|
Err(ParseError::Io(e)) => {
|
|
|
|
self.client_disconnected();
|
|
|
|
self.error = Some(DispatchError::Io(e));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
if let Some(mut payload) = self.payload.take() {
|
|
|
|
payload.set_error(PayloadError::EncodingCorrupted);
|
2018-10-07 18:48:53 +02:00
|
|
|
}
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-10-09 19:36:40 +02:00
|
|
|
// Malformed requests should be responded with 400
|
2018-10-23 03:18:05 +02:00
|
|
|
self.messages.push_back(DispatcherMessage::Error(
|
2018-11-18 22:48:42 +01:00
|
|
|
Response::BadRequest().finish().drop_body(),
|
2018-10-23 03:18:05 +02:00
|
|
|
));
|
2018-10-09 19:36:40 +02:00
|
|
|
self.flags.insert(Flags::DISCONNECTED);
|
|
|
|
self.error = Some(e.into());
|
|
|
|
break;
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 08:39:11 +02:00
|
|
|
if self.ka_timer.is_some() && updated {
|
|
|
|
if let Some(expire) = self.config.keep_alive_expire() {
|
|
|
|
self.ka_expire = expire;
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 01:22:00 +02:00
|
|
|
Ok(updated)
|
|
|
|
}
|
2018-10-05 08:39:11 +02:00
|
|
|
|
|
|
|
/// keep-alive timer
|
2019-03-07 07:56:34 +01:00
|
|
|
fn poll_keepalive(&mut self) -> Result<(), DispatchError> {
|
2018-11-19 02:52:56 +01:00
|
|
|
if self.ka_timer.is_none() {
|
2018-11-17 06:09:33 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
match self.ka_timer.as_mut().unwrap().poll().map_err(|e| {
|
|
|
|
error!("Timer error {:?}", e);
|
|
|
|
DispatchError::Unknown
|
|
|
|
})? {
|
|
|
|
Async::Ready(_) => {
|
|
|
|
// if we get timeout during shutdown, drop connection
|
|
|
|
if self.flags.contains(Flags::SHUTDOWN) {
|
|
|
|
return Err(DispatchError::DisconnectTimeout);
|
|
|
|
} else if self.ka_timer.as_mut().unwrap().deadline() >= self.ka_expire {
|
2018-11-20 19:55:50 +01:00
|
|
|
// check for any outstanding tasks
|
2018-11-19 02:52:56 +01:00
|
|
|
if self.state.is_empty() && self.framed.is_write_buf_empty() {
|
2018-11-17 06:09:33 +01:00
|
|
|
if self.flags.contains(Flags::STARTED) {
|
|
|
|
trace!("Keep-alive timeout, close connection");
|
|
|
|
self.flags.insert(Flags::SHUTDOWN);
|
|
|
|
|
|
|
|
// start shutdown timer
|
|
|
|
if let Some(deadline) = self.config.client_disconnect_timer()
|
|
|
|
{
|
2019-01-29 19:14:00 +01:00
|
|
|
if let Some(timer) = self.ka_timer.as_mut() {
|
2018-11-08 18:30:53 +01:00
|
|
|
timer.reset(deadline);
|
|
|
|
let _ = timer.poll();
|
2019-01-29 19:14:00 +01:00
|
|
|
}
|
2018-10-07 19:09:48 +02:00
|
|
|
} else {
|
2018-11-17 06:09:33 +01:00
|
|
|
return Ok(());
|
2018-10-05 08:39:11 +02:00
|
|
|
}
|
2018-11-17 06:09:33 +01:00
|
|
|
} else {
|
|
|
|
// timeout on first request (slow request) return 408
|
2018-11-20 19:55:50 +01:00
|
|
|
if !self.flags.contains(Flags::STARTED) {
|
|
|
|
trace!("Slow request timeout");
|
|
|
|
let _ = self.send_response(
|
|
|
|
Response::RequestTimeout().finish().drop_body(),
|
2018-11-21 16:49:24 +01:00
|
|
|
ResponseBody::Other(Body::Empty),
|
2018-11-20 19:55:50 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
trace!("Keep-alive connection timeout");
|
|
|
|
}
|
|
|
|
self.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
|
2018-11-18 05:21:28 +01:00
|
|
|
self.state = State::None;
|
2018-11-17 06:09:33 +01:00
|
|
|
}
|
|
|
|
} else if let Some(deadline) = self.config.keep_alive_expire() {
|
2019-01-29 19:14:00 +01:00
|
|
|
if let Some(timer) = self.ka_timer.as_mut() {
|
2018-11-08 18:30:53 +01:00
|
|
|
timer.reset(deadline);
|
|
|
|
let _ = timer.poll();
|
2019-01-29 19:14:00 +01:00
|
|
|
}
|
2018-10-05 08:39:11 +02:00
|
|
|
}
|
2019-01-29 19:14:00 +01:00
|
|
|
} else if let Some(timer) = self.ka_timer.as_mut() {
|
|
|
|
timer.reset(self.ka_expire);
|
|
|
|
let _ = timer.poll();
|
2018-10-05 08:39:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-17 06:09:33 +01:00
|
|
|
Async::NotReady => (),
|
2018-10-05 08:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
impl<T, S, B> Future for Dispatcher<T, S, B>
|
2018-10-05 01:22:00 +02:00
|
|
|
where
|
2018-10-05 05:02:10 +02:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2019-03-09 16:37:23 +01:00
|
|
|
S: Service<Request = Request>,
|
2018-10-07 18:48:53 +02:00
|
|
|
S::Error: Debug,
|
2019-02-09 17:44:22 +01:00
|
|
|
S::Response: Into<Response<B>>,
|
2018-11-18 05:21:28 +01:00
|
|
|
B: MessageBody,
|
2018-10-05 01:22:00 +02:00
|
|
|
{
|
2019-03-07 07:56:34 +01:00
|
|
|
type Item = ();
|
|
|
|
type Error = DispatchError;
|
2018-10-05 01:22:00 +02:00
|
|
|
|
|
|
|
#[inline]
|
2018-10-14 08:57:31 +02:00
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-11-17 06:09:33 +01:00
|
|
|
let shutdown = if let Some(ref mut inner) = self.inner {
|
|
|
|
if inner.flags.contains(Flags::SHUTDOWN) {
|
|
|
|
inner.poll_keepalive()?;
|
|
|
|
try_ready!(inner.poll_flush());
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
inner.poll_keepalive()?;
|
|
|
|
inner.poll_request()?;
|
2019-02-19 06:41:38 +01:00
|
|
|
loop {
|
|
|
|
inner.poll_response()?;
|
|
|
|
if let Async::Ready(false) = inner.poll_flush()? {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 06:09:33 +01:00
|
|
|
|
2018-11-19 02:52:56 +01:00
|
|
|
if inner.flags.contains(Flags::DISCONNECTED) {
|
2019-03-07 07:56:34 +01:00
|
|
|
return Ok(Async::Ready(()));
|
2018-11-19 02:52:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-17 06:09:33 +01:00
|
|
|
// keep-alive and stream errors
|
2018-11-19 02:52:56 +01:00
|
|
|
if inner.state.is_empty() && inner.framed.is_write_buf_empty() {
|
2018-11-17 06:09:33 +01:00
|
|
|
if let Some(err) = inner.error.take() {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
// unhandled request (upgrade or connect)
|
|
|
|
else if inner.unhandled.is_some() {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
// disconnect if keep-alive is not enabled
|
2018-11-20 19:55:50 +01:00
|
|
|
else if inner.flags.contains(Flags::STARTED)
|
|
|
|
&& !inner.flags.intersects(Flags::KEEPALIVE)
|
2018-11-17 06:09:33 +01:00
|
|
|
{
|
|
|
|
true
|
2018-11-20 19:55:50 +01:00
|
|
|
}
|
|
|
|
// disconnect if shutdown
|
|
|
|
else if inner.flags.contains(Flags::SHUTDOWN) {
|
|
|
|
true
|
2018-11-17 06:09:33 +01:00
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
2018-10-07 18:48:53 +02:00
|
|
|
} else {
|
2018-11-17 06:09:33 +01:00
|
|
|
return Ok(Async::NotReady);
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-17 06:09:33 +01:00
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut inner = self.inner.take().unwrap();
|
2019-03-07 07:56:34 +01:00
|
|
|
|
|
|
|
// TODO: shutdown
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
//Ok(Async::Ready(HttpServiceResult::Shutdown(
|
|
|
|
// inner.framed.into_inner(),
|
|
|
|
//)))
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-29 19:14:00 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::{cmp, io};
|
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
|
|
|
use actix_service::IntoService;
|
|
|
|
use bytes::{Buf, Bytes, BytesMut};
|
|
|
|
use futures::future::{lazy, ok};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::error::Error;
|
|
|
|
|
|
|
|
struct Buffer {
|
|
|
|
buf: Bytes,
|
|
|
|
err: Option<io::Error>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Buffer {
|
|
|
|
fn new(data: &'static str) -> Buffer {
|
|
|
|
Buffer {
|
|
|
|
buf: Bytes::from(data),
|
|
|
|
err: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncRead for Buffer {}
|
|
|
|
impl io::Read for Buffer {
|
|
|
|
fn read(&mut self, dst: &mut [u8]) -> Result<usize, io::Error> {
|
|
|
|
if self.buf.is_empty() {
|
|
|
|
if self.err.is_some() {
|
|
|
|
Err(self.err.take().unwrap())
|
|
|
|
} else {
|
|
|
|
Err(io::Error::new(io::ErrorKind::WouldBlock, ""))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let size = cmp::min(self.buf.len(), dst.len());
|
|
|
|
let b = self.buf.split_to(size);
|
|
|
|
dst[..size].copy_from_slice(&b);
|
|
|
|
Ok(size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl io::Write for Buffer {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl AsyncWrite for Buffer {
|
|
|
|
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
fn write_buf<B: Buf>(&mut self, _: &mut B) -> Poll<usize, io::Error> {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_req_parse_err() {
|
|
|
|
let mut sys = actix_rt::System::new("test");
|
|
|
|
let _ = sys.block_on(lazy(|| {
|
|
|
|
let buf = Buffer::new("GET /test HTTP/1\r\n\r\n");
|
|
|
|
let readbuf = BytesMut::new();
|
|
|
|
|
|
|
|
let mut h1 = Dispatcher::new(
|
|
|
|
buf,
|
|
|
|
ServiceConfig::default(),
|
2019-02-10 06:32:44 +01:00
|
|
|
CloneableService::new(
|
|
|
|
(|req| ok::<_, Error>(Response::Ok().finish())).into_service(),
|
|
|
|
),
|
2019-01-29 19:14:00 +01:00
|
|
|
);
|
|
|
|
assert!(h1.poll().is_ok());
|
|
|
|
assert!(h1.poll().is_ok());
|
|
|
|
assert!(h1
|
|
|
|
.inner
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.flags
|
|
|
|
.contains(Flags::DISCONNECTED));
|
|
|
|
// assert_eq!(h1.tasks.len(), 1);
|
|
|
|
ok::<_, ()>(())
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|