2018-10-04 16:22:00 -07:00
|
|
|
use std::collections::VecDeque;
|
2018-10-04 23:39:11 -07:00
|
|
|
use std::time::Instant;
|
2019-04-06 00:16:04 -07:00
|
|
|
use std::{fmt, io};
|
2018-10-04 16:22:00 -07:00
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed, FramedParts};
|
2018-12-10 18:08:33 -08:00
|
|
|
use actix_service::Service;
|
2019-02-09 20:27:39 -08:00
|
|
|
use actix_utils::cloneable::CloneableService;
|
2018-12-06 14:32:52 -08:00
|
|
|
use bitflags::bitflags;
|
2019-04-06 00:16:04 -07:00
|
|
|
use bytes::{BufMut, BytesMut};
|
|
|
|
use futures::{Async, Future, Poll};
|
|
|
|
use log::{error, trace};
|
2018-10-04 23:39:11 -07:00
|
|
|
use tokio_timer::Delay;
|
2018-10-04 16:22:00 -07:00
|
|
|
|
2019-03-27 09:24:55 -07:00
|
|
|
use crate::body::{Body, BodySize, MessageBody, ResponseBody};
|
2018-12-06 14:32:52 -08:00
|
|
|
use crate::config::ServiceConfig;
|
2019-04-05 16:46:44 -07:00
|
|
|
use crate::error::{DispatchError, Error};
|
2018-12-06 14:32:52 -08:00
|
|
|
use crate::error::{ParseError, PayloadError};
|
|
|
|
use crate::request::Request;
|
|
|
|
use crate::response::Response;
|
2018-10-04 20:02:10 -07:00
|
|
|
|
2018-10-22 18:18:05 -07:00
|
|
|
use super::codec::Codec;
|
2019-04-07 10:29:26 -07:00
|
|
|
use super::payload::{Payload, PayloadSender, PayloadStatus};
|
2019-03-06 22:56:34 -08:00
|
|
|
use super::{Message, MessageType};
|
2018-10-04 16:22:00 -07:00
|
|
|
|
2019-04-06 00:16:04 -07:00
|
|
|
const LW_BUFFER_SIZE: usize = 4096;
|
|
|
|
const HW_BUFFER_SIZE: usize = 32_768;
|
2018-10-04 16:22:00 -07:00
|
|
|
const MAX_PIPELINED_MESSAGES: usize = 16;
|
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
pub struct Flags: u8 {
|
|
|
|
const STARTED = 0b0000_0001;
|
2019-04-06 00:16:04 -07:00
|
|
|
const KEEPALIVE = 0b0000_0010;
|
|
|
|
const POLLED = 0b0000_0100;
|
|
|
|
const SHUTDOWN = 0b0000_1000;
|
|
|
|
const READ_DISCONNECT = 0b0001_0000;
|
|
|
|
const WRITE_DISCONNECT = 0b0010_0000;
|
2019-04-08 17:49:27 -07:00
|
|
|
const UPGRADE = 0b0100_0000;
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dispatcher for HTTP/1.1 protocol
|
2019-04-08 14:51:16 -07:00
|
|
|
pub struct Dispatcher<T, S, B, X, U>
|
2018-11-16 21:09:33 -08:00
|
|
|
where
|
2019-04-05 16:46:44 -07:00
|
|
|
S: Service<Request = Request>,
|
|
|
|
S::Error: Into<Error>,
|
|
|
|
B: MessageBody,
|
|
|
|
X: Service<Request = Request, Response = Request>,
|
|
|
|
X::Error: Into<Error>,
|
2019-04-08 14:51:16 -07:00
|
|
|
U: Service<Request = (Request, Framed<T, Codec>), Response = ()>,
|
|
|
|
U::Error: fmt::Display,
|
2018-11-16 21:09:33 -08:00
|
|
|
{
|
2019-04-08 17:49:27 -07:00
|
|
|
inner: DispatcherState<T, S, B, X, U>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum DispatcherState<T, S, B, X, U>
|
|
|
|
where
|
|
|
|
S: Service<Request = Request>,
|
|
|
|
S::Error: Into<Error>,
|
|
|
|
B: MessageBody,
|
|
|
|
X: Service<Request = Request, Response = Request>,
|
|
|
|
X::Error: Into<Error>,
|
|
|
|
U: Service<Request = (Request, Framed<T, Codec>), Response = ()>,
|
|
|
|
U::Error: fmt::Display,
|
|
|
|
{
|
|
|
|
Normal(InnerDispatcher<T, S, B, X, U>),
|
|
|
|
Upgrade(U::Future),
|
|
|
|
None,
|
2018-11-16 21:09:33 -08:00
|
|
|
}
|
|
|
|
|
2019-04-08 14:51:16 -07:00
|
|
|
struct InnerDispatcher<T, S, B, X, U>
|
2018-10-04 16:22:00 -07:00
|
|
|
where
|
2019-04-05 16:46:44 -07:00
|
|
|
S: Service<Request = Request>,
|
|
|
|
S::Error: Into<Error>,
|
|
|
|
B: MessageBody,
|
|
|
|
X: Service<Request = Request, Response = Request>,
|
|
|
|
X::Error: Into<Error>,
|
2019-04-08 14:51:16 -07:00
|
|
|
U: Service<Request = (Request, Framed<T, Codec>), Response = ()>,
|
|
|
|
U::Error: fmt::Display,
|
2018-10-04 16:22:00 -07:00
|
|
|
{
|
2019-02-09 20:27:39 -08:00
|
|
|
service: CloneableService<S>,
|
2019-04-05 16:46:44 -07:00
|
|
|
expect: CloneableService<X>,
|
2019-04-08 14:51:16 -07:00
|
|
|
upgrade: Option<CloneableService<U>>,
|
2018-10-04 16:22:00 -07:00
|
|
|
flags: Flags,
|
2019-03-06 22:56:34 -08:00
|
|
|
error: Option<DispatchError>,
|
2018-10-04 16:22:00 -07:00
|
|
|
|
2019-04-05 16:46:44 -07:00
|
|
|
state: State<S, B, X>,
|
2018-10-05 07:02:09 -07:00
|
|
|
payload: Option<PayloadSender>,
|
2018-10-22 18:18:05 -07:00
|
|
|
messages: VecDeque<DispatcherMessage>,
|
2018-10-04 21:14:18 -07:00
|
|
|
|
2018-10-04 23:39:11 -07:00
|
|
|
ka_expire: Instant,
|
|
|
|
ka_timer: Option<Delay>,
|
2019-04-06 00:16:04 -07:00
|
|
|
|
|
|
|
io: T,
|
|
|
|
read_buf: BytesMut,
|
|
|
|
write_buf: BytesMut,
|
|
|
|
codec: Codec,
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
|
2018-10-22 18:18:05 -07:00
|
|
|
enum DispatcherMessage {
|
2018-10-06 21:07:32 -07:00
|
|
|
Item(Request),
|
2019-04-08 17:49:27 -07:00
|
|
|
Upgrade(Request),
|
2018-11-18 13:48:42 -08:00
|
|
|
Error(Response<()>),
|
2018-10-06 21:07:32 -07:00
|
|
|
}
|
|
|
|
|
2019-04-05 16:46:44 -07:00
|
|
|
enum State<S, B, X>
|
|
|
|
where
|
|
|
|
S: Service<Request = Request>,
|
|
|
|
X: Service<Request = Request, Response = Request>,
|
|
|
|
B: MessageBody,
|
|
|
|
{
|
2018-10-04 16:22:00 -07:00
|
|
|
None,
|
2019-04-05 16:46:44 -07:00
|
|
|
ExpectCall(X::Future),
|
2018-10-07 00:04:38 -07:00
|
|
|
ServiceCall(S::Future),
|
2018-11-21 07:49:24 -08:00
|
|
|
SendPayload(ResponseBody<B>),
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-05 16:46:44 -07:00
|
|
|
impl<S, B, X> State<S, B, X>
|
|
|
|
where
|
|
|
|
S: Service<Request = Request>,
|
|
|
|
X: Service<Request = Request, Response = Request>,
|
|
|
|
B: MessageBody,
|
|
|
|
{
|
2018-10-04 16:22:00 -07:00
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
if let State::None = self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
|
|
|
|
fn is_call(&self) -> bool {
|
|
|
|
if let State::ServiceCall(_) = self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
enum PollResponse {
|
|
|
|
Upgrade(Request),
|
|
|
|
DoNothing,
|
|
|
|
DrainWriteBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for PollResponse {
|
|
|
|
fn eq(&self, other: &PollResponse) -> bool {
|
2019-04-06 00:16:04 -07:00
|
|
|
match self {
|
2019-04-08 17:49:27 -07:00
|
|
|
PollResponse::DrainWriteBuf => match other {
|
|
|
|
PollResponse::DrainWriteBuf => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
PollResponse::DoNothing => match other {
|
|
|
|
PollResponse::DoNothing => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
2019-04-06 00:16:04 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-08 14:51:16 -07:00
|
|
|
impl<T, S, B, X, U> Dispatcher<T, S, B, X, U>
|
2018-10-04 16:22:00 -07:00
|
|
|
where
|
2018-10-04 20:02:10 -07:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2019-04-04 10:59:34 -07:00
|
|
|
S: Service<Request = Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
S::Error: Into<Error>,
|
2019-02-09 08:44:22 -08:00
|
|
|
S::Response: Into<Response<B>>,
|
2018-11-17 20:21:28 -08:00
|
|
|
B: MessageBody,
|
2019-04-05 16:46:44 -07:00
|
|
|
X: Service<Request = Request, Response = Request>,
|
|
|
|
X::Error: Into<Error>,
|
2019-04-08 14:51:16 -07:00
|
|
|
U: Service<Request = (Request, Framed<T, Codec>), Response = ()>,
|
|
|
|
U::Error: fmt::Display,
|
2018-10-04 16:22:00 -07:00
|
|
|
{
|
2018-10-04 20:02:10 -07:00
|
|
|
/// Create http/1 dispatcher.
|
2019-04-05 16:46:44 -07:00
|
|
|
pub fn new(
|
|
|
|
stream: T,
|
|
|
|
config: ServiceConfig,
|
|
|
|
service: CloneableService<S>,
|
|
|
|
expect: CloneableService<X>,
|
2019-04-08 14:51:16 -07:00
|
|
|
upgrade: Option<CloneableService<U>>,
|
2019-04-05 16:46:44 -07:00
|
|
|
) -> Self {
|
2019-03-06 22:56:34 -08:00
|
|
|
Dispatcher::with_timeout(
|
2019-04-06 00:16:04 -07:00
|
|
|
stream,
|
|
|
|
Codec::new(config.clone()),
|
2019-03-06 22:56:34 -08:00
|
|
|
config,
|
2019-04-06 00:16:04 -07:00
|
|
|
BytesMut::with_capacity(HW_BUFFER_SIZE),
|
2019-03-06 22:56:34 -08:00
|
|
|
None,
|
|
|
|
service,
|
2019-04-05 16:46:44 -07:00
|
|
|
expect,
|
2019-04-08 14:51:16 -07:00
|
|
|
upgrade,
|
2019-03-06 22:56:34 -08:00
|
|
|
)
|
2018-10-04 23:39:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create http/1 dispatcher with slow request timeout.
|
|
|
|
pub fn with_timeout(
|
2019-04-06 00:16:04 -07:00
|
|
|
io: T,
|
|
|
|
codec: Codec,
|
2018-10-29 16:39:46 -07:00
|
|
|
config: ServiceConfig,
|
2019-04-06 00:16:04 -07:00
|
|
|
read_buf: BytesMut,
|
2018-10-29 16:39:46 -07:00
|
|
|
timeout: Option<Delay>,
|
2019-02-09 20:27:39 -08:00
|
|
|
service: CloneableService<S>,
|
2019-04-05 16:46:44 -07:00
|
|
|
expect: CloneableService<X>,
|
2019-04-08 14:51:16 -07:00
|
|
|
upgrade: Option<CloneableService<U>>,
|
2018-10-04 23:39:11 -07:00
|
|
|
) -> Self {
|
2018-10-05 10:03:10 -07:00
|
|
|
let keepalive = config.keep_alive_enabled();
|
|
|
|
let flags = if keepalive {
|
2019-04-06 00:16:04 -07:00
|
|
|
Flags::KEEPALIVE
|
2018-10-04 23:39:11 -07:00
|
|
|
} else {
|
2018-11-18 17:52:56 -08:00
|
|
|
Flags::empty()
|
2018-10-04 23:39:11 -07:00
|
|
|
};
|
2018-10-04 16:22:00 -07:00
|
|
|
|
2018-10-08 15:24:51 -07:00
|
|
|
// keep-alive timer
|
2018-10-04 23:39:11 -07: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-04 20:02:10 -07:00
|
|
|
Dispatcher {
|
2019-04-08 17:49:27 -07:00
|
|
|
inner: DispatcherState::Normal(InnerDispatcher {
|
2019-04-06 00:16:04 -07:00
|
|
|
io,
|
|
|
|
codec,
|
|
|
|
read_buf,
|
|
|
|
write_buf: BytesMut::with_capacity(HW_BUFFER_SIZE),
|
2018-11-16 21:09:33 -08:00
|
|
|
payload: None,
|
|
|
|
state: State::None,
|
|
|
|
error: None,
|
|
|
|
messages: VecDeque::new(),
|
|
|
|
service,
|
2019-04-05 16:46:44 -07:00
|
|
|
expect,
|
2019-04-08 14:51:16 -07:00
|
|
|
upgrade,
|
2018-11-16 21:09:33 -08:00
|
|
|
flags,
|
|
|
|
ka_expire,
|
|
|
|
ka_timer,
|
|
|
|
}),
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-16 21:09:33 -08:00
|
|
|
}
|
2018-10-04 16:22:00 -07:00
|
|
|
|
2019-04-08 14:51:16 -07:00
|
|
|
impl<T, S, B, X, U> InnerDispatcher<T, S, B, X, U>
|
2018-11-16 21:09:33 -08:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite,
|
2019-04-04 10:59:34 -07:00
|
|
|
S: Service<Request = Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
S::Error: Into<Error>,
|
2019-02-09 08:44:22 -08:00
|
|
|
S::Response: Into<Response<B>>,
|
2018-11-17 20:21:28 -08:00
|
|
|
B: MessageBody,
|
2019-04-05 16:46:44 -07:00
|
|
|
X: Service<Request = Request, Response = Request>,
|
|
|
|
X::Error: Into<Error>,
|
2019-04-08 14:51:16 -07:00
|
|
|
U: Service<Request = (Request, Framed<T, Codec>), Response = ()>,
|
|
|
|
U::Error: fmt::Display,
|
2018-11-16 21:09:33 -08:00
|
|
|
{
|
2018-10-04 16:22:00 -07:00
|
|
|
fn can_read(&self) -> bool {
|
2019-04-08 17:49:27 -07:00
|
|
|
if self
|
|
|
|
.flags
|
|
|
|
.intersects(Flags::READ_DISCONNECT | Flags::UPGRADE)
|
|
|
|
{
|
2019-04-07 23:06:21 -07:00
|
|
|
false
|
2019-04-06 00:16:04 -07:00
|
|
|
} else if let Some(ref info) = self.payload {
|
2018-10-04 16:22:00 -07:00
|
|
|
info.need_read() == PayloadStatus::Read
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if checked is set to true, delay disconnect until all tasks have finished.
|
2018-10-06 21:07:32 -07:00
|
|
|
fn client_disconnected(&mut self) {
|
2019-04-06 00:16:04 -07:00
|
|
|
self.flags
|
|
|
|
.insert(Flags::READ_DISCONNECT | Flags::WRITE_DISCONNECT);
|
2018-10-04 16:22:00 -07:00
|
|
|
if let Some(mut payload) = self.payload.take() {
|
2018-11-14 09:38:16 -08:00
|
|
|
payload.set_error(PayloadError::Incomplete(None));
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Flush stream
|
2019-04-06 00:16:04 -07:00
|
|
|
///
|
|
|
|
/// true - got whouldblock
|
|
|
|
/// false - didnt get whouldblock
|
|
|
|
fn poll_flush(&mut self) -> Result<bool, DispatchError> {
|
|
|
|
if self.write_buf.is_empty() {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
let len = self.write_buf.len();
|
|
|
|
let mut written = 0;
|
|
|
|
while written < len {
|
|
|
|
match self.io.write(&self.write_buf[written..]) {
|
|
|
|
Ok(0) => {
|
|
|
|
return Err(DispatchError::Io(io::Error::new(
|
|
|
|
io::ErrorKind::WriteZero,
|
|
|
|
"",
|
|
|
|
)));
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
Ok(n) => {
|
|
|
|
written += n;
|
|
|
|
}
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
|
|
|
|
if written > 0 {
|
|
|
|
let _ = self.write_buf.split_to(written);
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
return Ok(true);
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
Err(err) => return Err(DispatchError::Io(err)),
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
if written > 0 {
|
|
|
|
if written == self.write_buf.len() {
|
|
|
|
unsafe { self.write_buf.set_len(0) }
|
|
|
|
} else {
|
|
|
|
let _ = self.write_buf.split_to(written);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(false)
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
|
2018-11-21 07:49:24 -08:00
|
|
|
fn send_response(
|
2018-11-16 21:09:33 -08:00
|
|
|
&mut self,
|
2018-11-18 13:48:42 -08:00
|
|
|
message: Response<()>,
|
2018-11-21 07:49:24 -08:00
|
|
|
body: ResponseBody<B>,
|
2019-04-05 16:46:44 -07:00
|
|
|
) -> Result<State<S, B, X>, DispatchError> {
|
2019-04-06 00:16:04 -07:00
|
|
|
self.codec
|
|
|
|
.encode(Message::Item((message, body.length())), &mut self.write_buf)
|
2018-11-16 21:09:33 -08:00
|
|
|
.map_err(|err| {
|
|
|
|
if let Some(mut payload) = self.payload.take() {
|
|
|
|
payload.set_error(PayloadError::Incomplete(None));
|
|
|
|
}
|
|
|
|
DispatchError::Io(err)
|
|
|
|
})?;
|
|
|
|
|
2019-04-06 00:16:04 -07:00
|
|
|
self.flags.set(Flags::KEEPALIVE, self.codec.keepalive());
|
2018-11-17 20:21:28 -08:00
|
|
|
match body.length() {
|
2019-03-27 09:24:55 -07:00
|
|
|
BodySize::None | BodySize::Empty => Ok(State::None),
|
2018-11-17 20:21:28 -08:00
|
|
|
_ => Ok(State::SendPayload(body)),
|
2018-11-16 21:09:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 00:16:04 -07:00
|
|
|
fn send_continue(&mut self) {
|
|
|
|
self.write_buf
|
|
|
|
.extend_from_slice(b"HTTP/1.1 100 Continue\r\n\r\n");
|
2019-04-05 16:46:44 -07:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
fn poll_response(&mut self) -> Result<PollResponse, DispatchError> {
|
2018-10-04 16:22:00 -07:00
|
|
|
loop {
|
2019-04-06 00:16:04 -07:00
|
|
|
let state = match self.state {
|
2018-11-16 21:09:33 -08:00
|
|
|
State::None => match self.messages.pop_front() {
|
|
|
|
Some(DispatcherMessage::Item(req)) => {
|
|
|
|
Some(self.handle_request(req)?)
|
2018-10-29 16:39:46 -07:00
|
|
|
}
|
2018-11-16 21:09:33 -08:00
|
|
|
Some(DispatcherMessage::Error(res)) => {
|
2019-04-06 00:16:04 -07:00
|
|
|
Some(self.send_response(res, ResponseBody::Other(Body::Empty))?)
|
2018-11-16 21:09:33 -08:00
|
|
|
}
|
2019-04-08 17:49:27 -07:00
|
|
|
Some(DispatcherMessage::Upgrade(req)) => {
|
|
|
|
return Ok(PollResponse::Upgrade(req));
|
|
|
|
}
|
2018-11-16 21:09:33 -08:00
|
|
|
None => None,
|
2018-10-04 16:22:00 -07:00
|
|
|
},
|
2019-04-06 00:16:04 -07:00
|
|
|
State::ExpectCall(ref mut fut) => match fut.poll() {
|
2019-04-05 16:46:44 -07:00
|
|
|
Ok(Async::Ready(req)) => {
|
2019-04-06 00:16:04 -07:00
|
|
|
self.send_continue();
|
|
|
|
self.state = State::ServiceCall(self.service.call(req));
|
|
|
|
continue;
|
2019-04-05 16:46:44 -07:00
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
Ok(Async::NotReady) => None,
|
2019-04-05 16:46:44 -07:00
|
|
|
Err(e) => {
|
2019-04-06 00:16:04 -07:00
|
|
|
let res: Response = e.into().into();
|
2019-04-05 16:46:44 -07:00
|
|
|
let (res, body) = res.replace_body(());
|
|
|
|
Some(self.send_response(res, body.into_body())?)
|
|
|
|
}
|
|
|
|
},
|
2019-04-06 00:16:04 -07:00
|
|
|
State::ServiceCall(ref mut fut) => match fut.poll() {
|
2019-03-11 16:42:33 -07:00
|
|
|
Ok(Async::Ready(res)) => {
|
|
|
|
let (res, body) = res.into().replace_body(());
|
2019-04-06 00:16:04 -07:00
|
|
|
self.state = self.send_response(res, body)?;
|
|
|
|
continue;
|
2019-03-11 16:42:33 -07:00
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
Ok(Async::NotReady) => None,
|
|
|
|
Err(e) => {
|
|
|
|
let res: Response = e.into().into();
|
2019-03-11 16:42:33 -07:00
|
|
|
let (res, body) = res.replace_body(());
|
|
|
|
Some(self.send_response(res, body.into_body())?)
|
|
|
|
}
|
|
|
|
},
|
2019-04-06 00:16:04 -07:00
|
|
|
State::SendPayload(ref mut stream) => {
|
2018-11-14 10:52:40 -08:00
|
|
|
loop {
|
2019-04-06 00:16:04 -07:00
|
|
|
if self.write_buf.len() < HW_BUFFER_SIZE {
|
2018-11-17 20:21:28 -08:00
|
|
|
match stream
|
|
|
|
.poll_next()
|
|
|
|
.map_err(|_| DispatchError::Unknown)?
|
|
|
|
{
|
2018-11-14 10:52:40 -08:00
|
|
|
Async::Ready(Some(item)) => {
|
2019-04-06 00:16:04 -07:00
|
|
|
self.codec.encode(
|
|
|
|
Message::Chunk(Some(item)),
|
|
|
|
&mut self.write_buf,
|
|
|
|
)?;
|
2018-10-08 15:24:51 -07:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-14 10:52:40 -08:00
|
|
|
Async::Ready(None) => {
|
2019-04-06 00:16:04 -07:00
|
|
|
self.codec.encode(
|
|
|
|
Message::Chunk(None),
|
|
|
|
&mut self.write_buf,
|
|
|
|
)?;
|
|
|
|
self.state = State::None;
|
2018-10-08 15:24:51 -07:00
|
|
|
}
|
2019-04-08 17:49:27 -07:00
|
|
|
Async::NotReady => return Ok(PollResponse::DoNothing),
|
2018-11-14 10:52:40 -08:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-08 17:49:27 -07:00
|
|
|
return Ok(PollResponse::DrainWriteBuf);
|
2018-10-08 15:24:51 -07:00
|
|
|
}
|
2018-11-14 10:52:40 -08:00
|
|
|
break;
|
2018-10-08 15:24:51 -07:00
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
continue;
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-06 00:16:04 -07:00
|
|
|
// set new state
|
|
|
|
if let Some(state) = state {
|
|
|
|
self.state = state;
|
|
|
|
if !self.state.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if read-backpressure is enabled and we consumed some data.
|
|
|
|
// we may read more data and retry
|
|
|
|
if self.state.is_call() {
|
|
|
|
if self.poll_request()? {
|
2018-10-04 16:22:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
} else if !self.messages.is_empty() {
|
|
|
|
continue;
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
break;
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
Ok(PollResponse::DoNothing)
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-05 16:46:44 -07:00
|
|
|
fn handle_request(&mut self, req: Request) -> Result<State<S, B, X>, DispatchError> {
|
|
|
|
// Handle `EXPECT: 100-Continue` header
|
|
|
|
let req = if req.head().expect() {
|
|
|
|
let mut task = self.expect.call(req);
|
|
|
|
match task.poll() {
|
|
|
|
Ok(Async::Ready(req)) => {
|
2019-04-06 00:16:04 -07:00
|
|
|
self.send_continue();
|
2019-04-05 16:46:44 -07:00
|
|
|
req
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => return Ok(State::ExpectCall(task)),
|
|
|
|
Err(e) => {
|
|
|
|
let e = e.into();
|
|
|
|
let res: Response = e.into();
|
|
|
|
let (res, body) = res.replace_body(());
|
|
|
|
return self.send_response(res, body.into_body());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
req
|
|
|
|
};
|
|
|
|
|
|
|
|
// Call service
|
2018-10-06 22:36:57 -07:00
|
|
|
let mut task = self.service.call(req);
|
2019-03-11 16:42:33 -07:00
|
|
|
match task.poll() {
|
|
|
|
Ok(Async::Ready(res)) => {
|
2019-02-09 08:44:22 -08:00
|
|
|
let (res, body) = res.into().replace_body(());
|
2018-11-16 21:09:33 -08:00
|
|
|
self.send_response(res, body)
|
2018-10-06 22:36:57 -07:00
|
|
|
}
|
2019-03-11 16:42:33 -07:00
|
|
|
Ok(Async::NotReady) => Ok(State::ServiceCall(task)),
|
2019-04-06 00:16:04 -07:00
|
|
|
Err(e) => {
|
|
|
|
let res: Response = e.into().into();
|
2019-03-11 16:42:33 -07:00
|
|
|
let (res, body) = res.replace_body(());
|
|
|
|
self.send_response(res, body.into_body())
|
|
|
|
}
|
2018-10-06 22:36:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:36:40 -07:00
|
|
|
/// Process one incoming requests
|
2019-03-06 22:56:34 -08:00
|
|
|
pub(self) fn poll_request(&mut self) -> Result<bool, DispatchError> {
|
2018-10-09 10:36:40 -07:00
|
|
|
// limit a mount of non processed requests
|
2019-04-06 00:16:04 -07:00
|
|
|
if self.messages.len() >= MAX_PIPELINED_MESSAGES || !self.can_read() {
|
2018-10-09 10:36:40 -07:00
|
|
|
return Ok(false);
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut updated = false;
|
2018-10-29 16:39:46 -07:00
|
|
|
loop {
|
2019-04-06 00:16:04 -07:00
|
|
|
match self.codec.decode(&mut self.read_buf) {
|
|
|
|
Ok(Some(msg)) => {
|
2018-10-09 10:36:40 -07:00
|
|
|
updated = true;
|
|
|
|
self.flags.insert(Flags::STARTED);
|
|
|
|
|
|
|
|
match msg {
|
2019-02-06 11:44:15 -08:00
|
|
|
Message::Item(mut req) => {
|
2019-04-08 17:49:27 -07:00
|
|
|
let pl = self.codec.message_type();
|
|
|
|
|
|
|
|
if pl == MessageType::Stream && self.upgrade.is_some() {
|
|
|
|
self.messages.push_back(DispatcherMessage::Upgrade(req));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if pl == MessageType::Payload || pl == MessageType::Stream {
|
|
|
|
let (ps, pl) = Payload::create(false);
|
|
|
|
let (req1, _) =
|
|
|
|
req.replace_payload(crate::Payload::H1(pl));
|
|
|
|
req = req1;
|
|
|
|
self.payload = Some(ps);
|
2018-10-09 10:36:40 -07:00
|
|
|
}
|
2018-10-04 16:22:00 -07:00
|
|
|
|
2018-10-09 10:36:40 -07:00
|
|
|
// handle request early
|
|
|
|
if self.state.is_empty() {
|
|
|
|
self.state = self.handle_request(req)?;
|
|
|
|
} else {
|
2018-10-22 18:18:05 -07:00
|
|
|
self.messages.push_back(DispatcherMessage::Item(req));
|
2018-10-09 10:36:40 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-22 18:18:05 -07:00
|
|
|
Message::Chunk(Some(chunk)) => {
|
2018-10-09 10:36:40 -07:00
|
|
|
if let Some(ref mut payload) = self.payload {
|
|
|
|
payload.feed_data(chunk);
|
|
|
|
} else {
|
|
|
|
error!(
|
|
|
|
"Internal server error: unexpected payload chunk"
|
|
|
|
);
|
2019-04-06 00:16:04 -07:00
|
|
|
self.flags.insert(Flags::READ_DISCONNECT);
|
2018-10-22 18:18:05 -07:00
|
|
|
self.messages.push_back(DispatcherMessage::Error(
|
2018-11-18 13:48:42 -08:00
|
|
|
Response::InternalServerError().finish().drop_body(),
|
2018-10-09 10:36:40 -07:00
|
|
|
));
|
|
|
|
self.error = Some(DispatchError::InternalError);
|
2018-11-05 19:32:03 -08:00
|
|
|
break;
|
2018-10-09 10:36:40 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-22 18:18:05 -07:00
|
|
|
Message::Chunk(None) => {
|
2018-10-09 10:36:40 -07:00
|
|
|
if let Some(mut payload) = self.payload.take() {
|
|
|
|
payload.feed_eof();
|
|
|
|
} else {
|
|
|
|
error!("Internal server error: unexpected eof");
|
2019-04-06 00:16:04 -07:00
|
|
|
self.flags.insert(Flags::READ_DISCONNECT);
|
2018-10-22 18:18:05 -07:00
|
|
|
self.messages.push_back(DispatcherMessage::Error(
|
2018-11-18 13:48:42 -08:00
|
|
|
Response::InternalServerError().finish().drop_body(),
|
2018-10-09 10:36:40 -07:00
|
|
|
));
|
|
|
|
self.error = Some(DispatchError::InternalError);
|
2018-11-05 19:32:03 -08:00
|
|
|
break;
|
2018-10-09 10:36:40 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
2018-10-09 10:36:40 -07:00
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
Ok(None) => break,
|
2018-10-09 10:36:40 -07:00
|
|
|
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 09:48:53 -07:00
|
|
|
}
|
2018-10-04 16:22:00 -07:00
|
|
|
|
2018-10-09 10:36:40 -07:00
|
|
|
// Malformed requests should be responded with 400
|
2018-10-22 18:18:05 -07:00
|
|
|
self.messages.push_back(DispatcherMessage::Error(
|
2018-11-18 13:48:42 -08:00
|
|
|
Response::BadRequest().finish().drop_body(),
|
2018-10-22 18:18:05 -07:00
|
|
|
));
|
2019-04-06 00:16:04 -07:00
|
|
|
self.flags.insert(Flags::READ_DISCONNECT);
|
2018-10-09 10:36:40 -07:00
|
|
|
self.error = Some(e.into());
|
|
|
|
break;
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 00:16:04 -07:00
|
|
|
if updated && self.ka_timer.is_some() {
|
|
|
|
if let Some(expire) = self.codec.config.keep_alive_expire() {
|
2018-10-04 23:39:11 -07:00
|
|
|
self.ka_expire = expire;
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 16:22:00 -07:00
|
|
|
Ok(updated)
|
|
|
|
}
|
2018-10-04 23:39:11 -07:00
|
|
|
|
|
|
|
/// keep-alive timer
|
2019-03-06 22:56:34 -08:00
|
|
|
fn poll_keepalive(&mut self) -> Result<(), DispatchError> {
|
2018-11-18 17:52:56 -08:00
|
|
|
if self.ka_timer.is_none() {
|
2019-03-18 09:44:48 -07:00
|
|
|
// shutdown timeout
|
|
|
|
if self.flags.contains(Flags::SHUTDOWN) {
|
2019-04-06 00:16:04 -07:00
|
|
|
if let Some(interval) = self.codec.config.client_disconnect_timer() {
|
2019-03-18 09:44:48 -07:00
|
|
|
self.ka_timer = Some(Delay::new(interval));
|
|
|
|
} else {
|
2019-04-06 00:16:04 -07:00
|
|
|
self.flags.insert(Flags::READ_DISCONNECT);
|
2019-03-18 09:44:48 -07:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-11-16 21:09:33 -08:00
|
|
|
}
|
2019-03-18 09:44:48 -07:00
|
|
|
|
2018-11-16 21:09:33 -08:00
|
|
|
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 10:55:50 -08:00
|
|
|
// check for any outstanding tasks
|
2019-04-06 00:16:04 -07:00
|
|
|
if self.state.is_empty() && self.write_buf.is_empty() {
|
2018-11-16 21:09:33 -08:00
|
|
|
if self.flags.contains(Flags::STARTED) {
|
|
|
|
trace!("Keep-alive timeout, close connection");
|
|
|
|
self.flags.insert(Flags::SHUTDOWN);
|
|
|
|
|
|
|
|
// start shutdown timer
|
2019-04-06 00:16:04 -07:00
|
|
|
if let Some(deadline) =
|
|
|
|
self.codec.config.client_disconnect_timer()
|
2018-11-16 21:09:33 -08:00
|
|
|
{
|
2019-01-29 10:14:00 -08:00
|
|
|
if let Some(timer) = self.ka_timer.as_mut() {
|
2018-11-08 09:30:53 -08:00
|
|
|
timer.reset(deadline);
|
|
|
|
let _ = timer.poll();
|
2019-01-29 10:14:00 -08:00
|
|
|
}
|
2018-10-07 10:09:48 -07:00
|
|
|
} else {
|
2019-03-18 09:44:48 -07:00
|
|
|
// no shutdown timeout, drop socket
|
2019-04-06 00:16:04 -07:00
|
|
|
self.flags.insert(Flags::WRITE_DISCONNECT);
|
2018-11-16 21:09:33 -08:00
|
|
|
return Ok(());
|
2018-10-04 23:39:11 -07:00
|
|
|
}
|
2018-11-16 21:09:33 -08:00
|
|
|
} else {
|
|
|
|
// timeout on first request (slow request) return 408
|
2018-11-20 10:55:50 -08:00
|
|
|
if !self.flags.contains(Flags::STARTED) {
|
|
|
|
trace!("Slow request timeout");
|
|
|
|
let _ = self.send_response(
|
|
|
|
Response::RequestTimeout().finish().drop_body(),
|
2018-11-21 07:49:24 -08:00
|
|
|
ResponseBody::Other(Body::Empty),
|
2018-11-20 10:55:50 -08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
trace!("Keep-alive connection timeout");
|
|
|
|
}
|
|
|
|
self.flags.insert(Flags::STARTED | Flags::SHUTDOWN);
|
2018-11-17 20:21:28 -08:00
|
|
|
self.state = State::None;
|
2018-11-16 21:09:33 -08:00
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
} else if let Some(deadline) = self.codec.config.keep_alive_expire()
|
|
|
|
{
|
2019-01-29 10:14:00 -08:00
|
|
|
if let Some(timer) = self.ka_timer.as_mut() {
|
2018-11-08 09:30:53 -08:00
|
|
|
timer.reset(deadline);
|
|
|
|
let _ = timer.poll();
|
2019-01-29 10:14:00 -08:00
|
|
|
}
|
2018-10-04 23:39:11 -07:00
|
|
|
}
|
2019-01-29 10:14:00 -08:00
|
|
|
} else if let Some(timer) = self.ka_timer.as_mut() {
|
|
|
|
timer.reset(self.ka_expire);
|
|
|
|
let _ = timer.poll();
|
2018-10-04 23:39:11 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-16 21:09:33 -08:00
|
|
|
Async::NotReady => (),
|
2018-10-04 23:39:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-08 14:51:16 -07:00
|
|
|
impl<T, S, B, X, U> Future for Dispatcher<T, S, B, X, U>
|
2018-10-04 16:22:00 -07:00
|
|
|
where
|
2018-10-04 20:02:10 -07:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2019-03-09 07:37:23 -08:00
|
|
|
S: Service<Request = Request>,
|
2019-04-05 16:46:44 -07:00
|
|
|
S::Error: Into<Error>,
|
2019-02-09 08:44:22 -08:00
|
|
|
S::Response: Into<Response<B>>,
|
2018-11-17 20:21:28 -08:00
|
|
|
B: MessageBody,
|
2019-04-05 16:46:44 -07:00
|
|
|
X: Service<Request = Request, Response = Request>,
|
|
|
|
X::Error: Into<Error>,
|
2019-04-08 14:51:16 -07:00
|
|
|
U: Service<Request = (Request, Framed<T, Codec>), Response = ()>,
|
|
|
|
U::Error: fmt::Display,
|
2018-10-04 16:22:00 -07:00
|
|
|
{
|
2019-03-06 22:56:34 -08:00
|
|
|
type Item = ();
|
|
|
|
type Error = DispatchError;
|
2018-10-04 16:22:00 -07:00
|
|
|
|
|
|
|
#[inline]
|
2018-10-13 23:57:31 -07:00
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-04-08 17:49:27 -07:00
|
|
|
match self.inner {
|
|
|
|
DispatcherState::Normal(ref mut inner) => {
|
|
|
|
inner.poll_keepalive()?;
|
|
|
|
|
|
|
|
if inner.flags.contains(Flags::SHUTDOWN) {
|
|
|
|
if inner.flags.contains(Flags::WRITE_DISCONNECT) {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
} else {
|
|
|
|
// flush buffer
|
|
|
|
inner.poll_flush()?;
|
|
|
|
if !inner.write_buf.is_empty() {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
} else {
|
|
|
|
match inner.io.shutdown()? {
|
|
|
|
Async::Ready(_) => Ok(Async::Ready(())),
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
} else {
|
2019-04-08 17:49:27 -07:00
|
|
|
// read socket into a buf
|
|
|
|
if !inner.flags.contains(Flags::READ_DISCONNECT) {
|
|
|
|
if let Some(true) =
|
|
|
|
read_available(&mut inner.io, &mut inner.read_buf)?
|
|
|
|
{
|
|
|
|
inner.flags.insert(Flags::READ_DISCONNECT)
|
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
inner.poll_request()?;
|
|
|
|
loop {
|
|
|
|
if inner.write_buf.remaining_mut() < LW_BUFFER_SIZE {
|
|
|
|
inner.write_buf.reserve(HW_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
let result = inner.poll_response()?;
|
|
|
|
let drain = result == PollResponse::DrainWriteBuf;
|
2019-04-06 00:16:04 -07:00
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
// switch to upgrade handler
|
|
|
|
if let PollResponse::Upgrade(req) = result {
|
|
|
|
if let DispatcherState::Normal(inner) =
|
|
|
|
std::mem::replace(&mut self.inner, DispatcherState::None)
|
|
|
|
{
|
|
|
|
let mut parts = FramedParts::with_read_buf(
|
|
|
|
inner.io,
|
|
|
|
inner.codec,
|
|
|
|
inner.read_buf,
|
|
|
|
);
|
|
|
|
parts.write_buf = inner.write_buf;
|
|
|
|
let framed = Framed::from_parts(parts);
|
|
|
|
self.inner = DispatcherState::Upgrade(
|
|
|
|
inner.upgrade.unwrap().call((req, framed)),
|
|
|
|
);
|
|
|
|
return self.poll();
|
|
|
|
} else {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
2018-11-18 17:52:56 -08:00
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
// we didnt get WouldBlock from write operation,
|
|
|
|
// so data get written to kernel completely (OSX)
|
|
|
|
// and we have to write again otherwise response can get stuck
|
|
|
|
if inner.poll_flush()? || !drain {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 09:44:48 -07:00
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
// client is gone
|
|
|
|
if inner.flags.contains(Flags::WRITE_DISCONNECT) {
|
|
|
|
return Ok(Async::Ready(()));
|
|
|
|
}
|
2019-04-06 00:16:04 -07:00
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
let is_empty = inner.state.is_empty();
|
2019-04-06 00:16:04 -07:00
|
|
|
|
2019-04-08 17:49:27 -07:00
|
|
|
// read half is closed and we do not processing any responses
|
|
|
|
if inner.flags.contains(Flags::READ_DISCONNECT) && is_empty {
|
|
|
|
inner.flags.insert(Flags::SHUTDOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep-alive and stream errors
|
|
|
|
if is_empty && inner.write_buf.is_empty() {
|
|
|
|
if let Some(err) = inner.error.take() {
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
// disconnect if keep-alive is not enabled
|
|
|
|
else if inner.flags.contains(Flags::STARTED)
|
|
|
|
&& !inner.flags.intersects(Flags::KEEPALIVE)
|
|
|
|
{
|
|
|
|
inner.flags.insert(Flags::SHUTDOWN);
|
|
|
|
self.poll()
|
|
|
|
}
|
|
|
|
// disconnect if shutdown
|
|
|
|
else if inner.flags.contains(Flags::SHUTDOWN) {
|
|
|
|
self.poll()
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-08 17:49:27 -07:00
|
|
|
DispatcherState::Upgrade(ref mut fut) => fut.poll().map_err(|e| {
|
|
|
|
error!("Upgrade handler error: {}", e);
|
|
|
|
DispatchError::Upgrade
|
|
|
|
}),
|
|
|
|
DispatcherState::None => panic!(),
|
2019-03-18 09:44:48 -07:00
|
|
|
}
|
2018-10-04 16:22:00 -07:00
|
|
|
}
|
|
|
|
}
|
2019-01-29 10:14:00 -08:00
|
|
|
|
2019-04-06 00:16:04 -07:00
|
|
|
fn read_available<T>(io: &mut T, buf: &mut BytesMut) -> Result<Option<bool>, io::Error>
|
|
|
|
where
|
|
|
|
T: io::Read,
|
|
|
|
{
|
|
|
|
let mut read_some = false;
|
|
|
|
loop {
|
|
|
|
if buf.remaining_mut() < LW_BUFFER_SIZE {
|
|
|
|
buf.reserve(HW_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
let read = unsafe { io.read(buf.bytes_mut()) };
|
|
|
|
match read {
|
|
|
|
Ok(n) => {
|
|
|
|
if n == 0 {
|
|
|
|
return Ok(Some(true));
|
|
|
|
} else {
|
|
|
|
read_some = true;
|
|
|
|
unsafe {
|
|
|
|
buf.advance_mut(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return if e.kind() == io::ErrorKind::WouldBlock {
|
|
|
|
if read_some {
|
|
|
|
Ok(Some(false))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
} else if e.kind() == io::ErrorKind::ConnectionReset && read_some {
|
|
|
|
Ok(Some(true))
|
|
|
|
} else {
|
|
|
|
Err(e)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 10:14:00 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::{cmp, io};
|
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
|
|
|
use actix_service::IntoService;
|
2019-04-06 00:16:04 -07:00
|
|
|
use bytes::{Buf, Bytes, BytesMut};
|
2019-01-29 10:14:00 -08:00
|
|
|
use futures::future::{lazy, ok};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::error::Error;
|
2019-04-05 16:46:44 -07:00
|
|
|
use crate::h1::ExpectHandler;
|
2019-01-29 10:14:00 -08:00
|
|
|
|
|
|
|
struct Buffer {
|
|
|
|
buf: Bytes,
|
2019-04-06 00:16:04 -07:00
|
|
|
write_buf: BytesMut,
|
2019-01-29 10:14:00 -08:00
|
|
|
err: Option<io::Error>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Buffer {
|
|
|
|
fn new(data: &'static str) -> Buffer {
|
|
|
|
Buffer {
|
|
|
|
buf: Bytes::from(data),
|
2019-04-06 00:16:04 -07:00
|
|
|
write_buf: BytesMut::new(),
|
2019-01-29 10:14:00 -08:00
|
|
|
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> {
|
2019-04-06 00:16:04 -07:00
|
|
|
self.write_buf.extend(buf);
|
2019-01-29 10:14:00 -08:00
|
|
|
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 mut h1 = Dispatcher::new(
|
|
|
|
buf,
|
|
|
|
ServiceConfig::default(),
|
2019-02-09 21:32:44 -08:00
|
|
|
CloneableService::new(
|
2019-03-23 09:40:20 -07:00
|
|
|
(|_| ok::<_, Error>(Response::Ok().finish())).into_service(),
|
2019-02-09 21:32:44 -08:00
|
|
|
),
|
2019-04-05 16:46:44 -07:00
|
|
|
CloneableService::new(ExpectHandler),
|
2019-01-29 10:14:00 -08:00
|
|
|
);
|
2019-04-06 00:16:04 -07:00
|
|
|
assert!(h1.poll().is_err());
|
2019-01-29 10:14:00 -08:00
|
|
|
assert!(h1
|
|
|
|
.inner
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.flags
|
2019-04-06 00:16:04 -07:00
|
|
|
.contains(Flags::READ_DISCONNECT));
|
|
|
|
assert_eq!(
|
|
|
|
&h1.inner.as_ref().unwrap().io.write_buf[..26],
|
|
|
|
b"HTTP/1.1 400 Bad Request\r\n"
|
|
|
|
);
|
2019-01-29 10:14:00 -08:00
|
|
|
ok::<_, ()>(())
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|