2019-02-06 20:44:15 +01:00
|
|
|
use std::collections::VecDeque;
|
2019-11-15 10:54:11 +01:00
|
|
|
use std::future::Future;
|
2019-02-06 20:44:15 +01:00
|
|
|
use std::marker::PhantomData;
|
2019-11-15 10:54:11 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2019-02-06 20:44:15 +01:00
|
|
|
use std::time::Instant;
|
2019-04-16 18:54:02 +02:00
|
|
|
use std::{fmt, mem, net};
|
2019-02-06 20:44:15 +01:00
|
|
|
|
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
2019-04-16 18:54:02 +02:00
|
|
|
use actix_server_config::IoStream;
|
2019-02-06 20:44:15 +01:00
|
|
|
use actix_service::Service;
|
|
|
|
use bitflags::bitflags;
|
|
|
|
use bytes::{Bytes, BytesMut};
|
2019-11-15 10:54:11 +01:00
|
|
|
use futures::{ready, Sink, Stream};
|
2019-02-06 20:44:15 +01:00
|
|
|
use h2::server::{Connection, SendResponse};
|
|
|
|
use h2::{RecvStream, SendStream};
|
|
|
|
use http::header::{
|
|
|
|
HeaderValue, ACCEPT_ENCODING, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING,
|
|
|
|
};
|
|
|
|
use http::HttpTryFrom;
|
|
|
|
use log::{debug, error, trace};
|
|
|
|
use tokio_timer::Delay;
|
|
|
|
|
2019-03-27 17:24:55 +01:00
|
|
|
use crate::body::{Body, BodySize, MessageBody, ResponseBody};
|
2019-07-17 09:55:44 +02:00
|
|
|
use crate::cloneable::CloneableService;
|
2019-02-06 20:44:15 +01:00
|
|
|
use crate::config::ServiceConfig;
|
|
|
|
use crate::error::{DispatchError, Error, ParseError, PayloadError, ResponseError};
|
2019-06-28 10:34:26 +02:00
|
|
|
use crate::helpers::DataFactory;
|
2019-09-01 09:15:02 +02:00
|
|
|
use crate::httpmessage::HttpMessage;
|
2019-02-06 20:44:15 +01:00
|
|
|
use crate::message::ResponseHead;
|
2019-02-07 22:41:50 +01:00
|
|
|
use crate::payload::Payload;
|
2019-02-06 20:44:15 +01:00
|
|
|
use crate::request::Request;
|
|
|
|
use crate::response::Response;
|
|
|
|
|
|
|
|
const CHUNK_SIZE: usize = 16_384;
|
|
|
|
|
|
|
|
/// Dispatcher for HTTP/2 protocol
|
2019-04-16 18:54:02 +02:00
|
|
|
pub struct Dispatcher<T: IoStream, S: Service<Request = Request>, B: MessageBody> {
|
2019-02-10 05:27:39 +01:00
|
|
|
service: CloneableService<S>,
|
2019-02-06 20:44:15 +01:00
|
|
|
connection: Connection<T, Bytes>,
|
2019-06-28 10:34:26 +02:00
|
|
|
on_connect: Option<Box<dyn DataFactory>>,
|
2019-02-06 20:44:15 +01:00
|
|
|
config: ServiceConfig,
|
2019-04-16 18:54:02 +02:00
|
|
|
peer_addr: Option<net::SocketAddr>,
|
2019-02-06 20:44:15 +01:00
|
|
|
ka_expire: Instant,
|
|
|
|
ka_timer: Option<Delay>,
|
|
|
|
_t: PhantomData<B>,
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
impl<T, S, B> Unpin for Dispatcher<T, S, B>
|
|
|
|
where
|
|
|
|
T: IoStream,
|
|
|
|
S: Service<Request = Request>,
|
|
|
|
S::Error: Into<Error> + Unpin + 'static,
|
|
|
|
S::Future: Unpin + 'static,
|
|
|
|
S::Response: Into<Response<B>> + Unpin + 'static,
|
|
|
|
B: MessageBody + 'static,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-06 20:44:15 +01:00
|
|
|
impl<T, S, B> Dispatcher<T, S, B>
|
|
|
|
where
|
2019-04-16 18:54:02 +02:00
|
|
|
T: IoStream,
|
2019-04-04 19:59:34 +02:00
|
|
|
S: Service<Request = Request>,
|
2019-11-15 10:54:11 +01:00
|
|
|
S::Error: Into<Error> + Unpin + 'static,
|
|
|
|
S::Future: Unpin + 'static,
|
|
|
|
S::Response: Into<Response<B>> + Unpin + 'static,
|
2019-02-06 20:44:15 +01:00
|
|
|
B: MessageBody + 'static,
|
|
|
|
{
|
2019-06-28 10:34:26 +02:00
|
|
|
pub(crate) fn new(
|
2019-02-10 05:27:39 +01:00
|
|
|
service: CloneableService<S>,
|
2019-02-06 20:44:15 +01:00
|
|
|
connection: Connection<T, Bytes>,
|
2019-06-28 10:34:26 +02:00
|
|
|
on_connect: Option<Box<dyn DataFactory>>,
|
2019-02-06 20:44:15 +01:00
|
|
|
config: ServiceConfig,
|
|
|
|
timeout: Option<Delay>,
|
2019-04-16 18:54:02 +02:00
|
|
|
peer_addr: Option<net::SocketAddr>,
|
2019-02-06 20:44:15 +01:00
|
|
|
) -> Self {
|
2019-03-18 17:44:48 +01:00
|
|
|
// let keepalive = config.keep_alive_enabled();
|
2019-02-06 20:44:15 +01:00
|
|
|
// let flags = if keepalive {
|
|
|
|
// Flags::KEEPALIVE | Flags::KEEPALIVE_ENABLED
|
|
|
|
// } else {
|
|
|
|
// Flags::empty()
|
|
|
|
// };
|
|
|
|
|
|
|
|
// keep-alive timer
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
|
|
|
Dispatcher {
|
|
|
|
service,
|
|
|
|
config,
|
2019-04-16 18:54:02 +02:00
|
|
|
peer_addr,
|
|
|
|
connection,
|
2019-06-28 10:34:26 +02:00
|
|
|
on_connect,
|
2019-02-06 20:44:15 +01:00
|
|
|
ka_expire,
|
|
|
|
ka_timer,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S, B> Future for Dispatcher<T, S, B>
|
|
|
|
where
|
2019-04-16 18:54:02 +02:00
|
|
|
T: IoStream,
|
2019-04-04 19:59:34 +02:00
|
|
|
S: Service<Request = Request>,
|
2019-11-15 10:54:11 +01:00
|
|
|
S::Error: Into<Error> + Unpin + 'static,
|
|
|
|
S::Future: Unpin + 'static,
|
|
|
|
S::Response: Into<Response<B>> + Unpin + 'static,
|
2019-02-06 20:44:15 +01:00
|
|
|
B: MessageBody + 'static,
|
|
|
|
{
|
2019-11-15 10:54:11 +01:00
|
|
|
type Output = Result<(), DispatchError>;
|
2019-02-06 20:44:15 +01:00
|
|
|
|
|
|
|
#[inline]
|
2019-11-15 10:54:11 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let this = self.get_mut();
|
|
|
|
|
2019-02-06 20:44:15 +01:00
|
|
|
loop {
|
2019-11-15 10:54:11 +01:00
|
|
|
match Pin::new(&mut this.connection).poll_accept(cx) {
|
|
|
|
Poll::Ready(None) => return Poll::Ready(Ok(())),
|
|
|
|
Poll::Ready(Some(Err(err))) => return Poll::Ready(Err(err.into())),
|
2019-11-19 06:08:03 +01:00
|
|
|
Poll::Ready(Some(Ok((req, _)))) => {
|
2019-02-06 20:44:15 +01:00
|
|
|
// update keep-alive expire
|
2019-11-15 10:54:11 +01:00
|
|
|
if this.ka_timer.is_some() {
|
|
|
|
if let Some(expire) = this.config.keep_alive_expire() {
|
|
|
|
this.ka_expire = expire;
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (parts, body) = req.into_parts();
|
2019-11-15 10:54:11 +01:00
|
|
|
// let b: () = body;
|
|
|
|
let mut req = Request::with_payload(Payload::<
|
|
|
|
crate::payload::PayloadStream,
|
|
|
|
>::H2(
|
|
|
|
crate::h2::Payload::new(body)
|
|
|
|
));
|
2019-02-06 20:44:15 +01:00
|
|
|
|
2019-02-08 06:16:46 +01:00
|
|
|
let head = &mut req.head_mut();
|
2019-02-06 20:44:15 +01:00
|
|
|
head.uri = parts.uri;
|
|
|
|
head.method = parts.method;
|
|
|
|
head.version = parts.version;
|
2019-04-07 00:02:02 +02:00
|
|
|
head.headers = parts.headers.into();
|
2019-11-15 10:54:11 +01:00
|
|
|
head.peer_addr = this.peer_addr;
|
2019-09-01 09:15:02 +02:00
|
|
|
|
|
|
|
// set on_connect data
|
2019-11-15 10:54:11 +01:00
|
|
|
if let Some(ref on_connect) = this.on_connect {
|
2019-09-01 09:15:02 +02:00
|
|
|
on_connect.set(&mut req.extensions_mut());
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
// tokio_executor::current_thread::spawn(ServiceResponse::<
|
|
|
|
// S::Future,
|
|
|
|
// S::Response,
|
|
|
|
// S::Error,
|
|
|
|
// B,
|
|
|
|
// > {
|
|
|
|
// state: ServiceResponseState::ServiceCall(
|
|
|
|
// this.service.call(req),
|
|
|
|
// Some(res),
|
|
|
|
// ),
|
|
|
|
// config: this.config.clone(),
|
|
|
|
// buffer: None,
|
|
|
|
// _t: PhantomData,
|
|
|
|
// });
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Pending => return Poll::Pending,
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
struct ServiceResponse<F, I, E, B> {
|
2019-04-04 19:59:34 +02:00
|
|
|
state: ServiceResponseState<F, B>,
|
2019-02-06 20:44:15 +01:00
|
|
|
config: ServiceConfig,
|
|
|
|
buffer: Option<Bytes>,
|
2019-11-15 10:54:11 +01:00
|
|
|
_t: PhantomData<(I, E)>,
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 19:59:34 +02:00
|
|
|
enum ServiceResponseState<F, B> {
|
|
|
|
ServiceCall(F, Option<SendResponse<Bytes>>),
|
2019-02-06 20:44:15 +01:00
|
|
|
SendPayload(SendStream<Bytes>, ResponseBody<B>),
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
impl<F, I, E, B> ServiceResponse<F, I, E, B>
|
2019-02-06 20:44:15 +01:00
|
|
|
where
|
2019-11-15 10:54:11 +01:00
|
|
|
F: Future<Output = Result<I, E>> + Unpin,
|
|
|
|
E: Into<Error> + Unpin + 'static,
|
|
|
|
I: Into<Response<B>> + Unpin + 'static,
|
2019-02-06 20:44:15 +01:00
|
|
|
B: MessageBody + 'static,
|
|
|
|
{
|
|
|
|
fn prepare_response(
|
|
|
|
&self,
|
|
|
|
head: &ResponseHead,
|
2019-04-10 21:24:17 +02:00
|
|
|
size: &mut BodySize,
|
2019-02-06 20:44:15 +01:00
|
|
|
) -> http::Response<()> {
|
|
|
|
let mut has_date = false;
|
2019-04-10 21:24:17 +02:00
|
|
|
let mut skip_len = size != &BodySize::Stream;
|
2019-02-06 20:44:15 +01:00
|
|
|
|
|
|
|
let mut res = http::Response::new(());
|
|
|
|
*res.status_mut() = head.status;
|
|
|
|
*res.version_mut() = http::Version::HTTP_2;
|
|
|
|
|
|
|
|
// Content length
|
|
|
|
match head.status {
|
|
|
|
http::StatusCode::NO_CONTENT
|
|
|
|
| http::StatusCode::CONTINUE
|
2019-04-10 21:24:17 +02:00
|
|
|
| http::StatusCode::PROCESSING => *size = BodySize::None,
|
2019-02-06 20:44:15 +01:00
|
|
|
http::StatusCode::SWITCHING_PROTOCOLS => {
|
|
|
|
skip_len = true;
|
2019-04-10 21:24:17 +02:00
|
|
|
*size = BodySize::Stream;
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
2019-04-10 21:24:17 +02:00
|
|
|
let _ = match size {
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::None | BodySize::Stream => None,
|
|
|
|
BodySize::Empty => res
|
2019-02-06 20:44:15 +01:00
|
|
|
.headers_mut()
|
|
|
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")),
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Sized(len) => res.headers_mut().insert(
|
2019-02-06 20:44:15 +01:00
|
|
|
CONTENT_LENGTH,
|
|
|
|
HeaderValue::try_from(format!("{}", len)).unwrap(),
|
|
|
|
),
|
2019-03-27 17:24:55 +01:00
|
|
|
BodySize::Sized64(len) => res.headers_mut().insert(
|
2019-02-06 20:44:15 +01:00
|
|
|
CONTENT_LENGTH,
|
|
|
|
HeaderValue::try_from(format!("{}", len)).unwrap(),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
// copy headers
|
|
|
|
for (key, value) in head.headers.iter() {
|
|
|
|
match *key {
|
|
|
|
CONNECTION | TRANSFER_ENCODING => continue, // http2 specific
|
|
|
|
CONTENT_LENGTH if skip_len => continue,
|
|
|
|
DATE => has_date = true,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
res.headers_mut().append(key, value.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
// set date header
|
|
|
|
if !has_date {
|
|
|
|
let mut bytes = BytesMut::with_capacity(29);
|
|
|
|
self.config.set_date_header(&mut bytes);
|
|
|
|
res.headers_mut()
|
|
|
|
.insert(DATE, HeaderValue::try_from(bytes.freeze()).unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
impl<F, I, E, B> Future for ServiceResponse<F, I, E, B>
|
2019-02-06 20:44:15 +01:00
|
|
|
where
|
2019-11-15 10:54:11 +01:00
|
|
|
F: Future<Output = Result<I, E>> + Unpin,
|
|
|
|
E: Into<Error> + Unpin + 'static,
|
|
|
|
I: Into<Response<B>> + Unpin + 'static,
|
2019-02-06 20:44:15 +01:00
|
|
|
B: MessageBody + 'static,
|
|
|
|
{
|
2019-11-15 10:54:11 +01:00
|
|
|
type Output = ();
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let this = self.get_mut();
|
2019-02-06 20:44:15 +01:00
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
match this.state {
|
2019-02-06 20:44:15 +01:00
|
|
|
ServiceResponseState::ServiceCall(ref mut call, ref mut send) => {
|
2019-11-15 10:54:11 +01:00
|
|
|
match Pin::new(call).poll(cx) {
|
|
|
|
Poll::Ready(Ok(res)) => {
|
2019-02-09 17:44:22 +01:00
|
|
|
let (res, body) = res.into().replace_body(());
|
2019-02-06 20:44:15 +01:00
|
|
|
|
|
|
|
let mut send = send.take().unwrap();
|
2019-04-10 21:24:17 +02:00
|
|
|
let mut size = body.size();
|
2019-11-15 10:54:11 +01:00
|
|
|
let h2_res = this.prepare_response(res.head(), &mut size);
|
2019-02-06 20:44:15 +01:00
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
let stream = match send.send_response(h2_res, size.is_eof()) {
|
|
|
|
Err(e) => {
|
2019-02-06 20:44:15 +01:00
|
|
|
trace!("Error sending h2 response: {:?}", e);
|
2019-11-15 10:54:11 +01:00
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
Ok(stream) => stream,
|
|
|
|
};
|
2019-02-06 20:44:15 +01:00
|
|
|
|
2019-04-10 21:24:17 +02:00
|
|
|
if size.is_eof() {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(())
|
2019-02-06 20:44:15 +01:00
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
this.state = ServiceResponseState::SendPayload(stream, body);
|
|
|
|
Pin::new(this).poll(cx)
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
Poll::Ready(Err(e)) => {
|
2019-09-09 12:24:57 +02:00
|
|
|
let res: Response = e.into().into();
|
2019-02-06 20:44:15 +01:00
|
|
|
let (res, body) = res.replace_body(());
|
|
|
|
|
|
|
|
let mut send = send.take().unwrap();
|
2019-04-10 21:24:17 +02:00
|
|
|
let mut size = body.size();
|
2019-11-15 10:54:11 +01:00
|
|
|
let h2_res = this.prepare_response(res.head(), &mut size);
|
2019-02-06 20:44:15 +01:00
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
let stream = match send.send_response(h2_res, size.is_eof()) {
|
|
|
|
Err(e) => {
|
2019-02-06 20:44:15 +01:00
|
|
|
trace!("Error sending h2 response: {:?}", e);
|
2019-11-15 10:54:11 +01:00
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
Ok(stream) => stream,
|
|
|
|
};
|
2019-02-06 20:44:15 +01:00
|
|
|
|
2019-04-10 21:24:17 +02:00
|
|
|
if size.is_eof() {
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(())
|
2019-02-06 20:44:15 +01:00
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
this.state = ServiceResponseState::SendPayload(
|
2019-02-06 20:44:15 +01:00
|
|
|
stream,
|
|
|
|
body.into_body(),
|
|
|
|
);
|
2019-11-15 10:54:11 +01:00
|
|
|
Pin::new(this).poll(cx)
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ServiceResponseState::SendPayload(ref mut stream, ref mut body) => loop {
|
|
|
|
loop {
|
2019-11-15 10:54:11 +01:00
|
|
|
if let Some(ref mut buffer) = this.buffer {
|
|
|
|
match stream.poll_capacity(cx) {
|
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
Poll::Ready(None) => return Poll::Ready(()),
|
|
|
|
Poll::Ready(Some(Ok(cap))) => {
|
2019-02-06 20:44:15 +01:00
|
|
|
let len = buffer.len();
|
|
|
|
let bytes = buffer.split_to(std::cmp::min(cap, len));
|
|
|
|
|
|
|
|
if let Err(e) = stream.send_data(bytes, false) {
|
|
|
|
warn!("{:?}", e);
|
2019-11-15 10:54:11 +01:00
|
|
|
return Poll::Ready(());
|
2019-02-06 20:44:15 +01:00
|
|
|
} else if !buffer.is_empty() {
|
|
|
|
let cap = std::cmp::min(buffer.len(), CHUNK_SIZE);
|
|
|
|
stream.reserve_capacity(cap);
|
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
this.buffer.take();
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Err(e))) => {
|
|
|
|
warn!("{:?}", e);
|
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
match body.poll_next(cx) {
|
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
Poll::Ready(None) => {
|
2019-02-06 20:44:15 +01:00
|
|
|
if let Err(e) = stream.send_data(Bytes::new(), true) {
|
|
|
|
warn!("{:?}", e);
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
return Poll::Ready(());
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Ok(chunk))) => {
|
2019-02-06 20:44:15 +01:00
|
|
|
stream.reserve_capacity(std::cmp::min(
|
|
|
|
chunk.len(),
|
|
|
|
CHUNK_SIZE,
|
|
|
|
));
|
2019-11-15 10:54:11 +01:00
|
|
|
this.buffer = Some(chunk);
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Some(Err(e))) => {
|
2019-02-06 20:44:15 +01:00
|
|
|
error!("Response payload stream error: {:?}", e);
|
2019-11-15 10:54:11 +01:00
|
|
|
return Poll::Ready(());
|
2019-02-06 20:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|