2017-11-03 19:29:44 +01:00
|
|
|
use std::{io, cmp, mem};
|
2017-12-08 18:24:05 +01:00
|
|
|
use std::rc::Rc;
|
2017-11-02 20:54:09 +01:00
|
|
|
use std::io::{Read, Write};
|
2017-11-04 21:24:57 +01:00
|
|
|
use std::time::Duration;
|
2017-11-10 22:08:15 +01:00
|
|
|
use std::net::SocketAddr;
|
2017-11-03 19:29:44 +01:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
2017-11-04 21:24:57 +01:00
|
|
|
use actix::Arbiter;
|
2017-11-03 19:29:44 +01:00
|
|
|
use http::request::Parts;
|
2017-11-04 17:07:44 +01:00
|
|
|
use http2::{Reason, RecvStream};
|
2017-11-03 19:29:44 +01:00
|
|
|
use http2::server::{Server, Handshake, Respond};
|
2017-11-02 20:54:09 +01:00
|
|
|
use bytes::{Buf, Bytes};
|
2017-11-03 19:29:44 +01:00
|
|
|
use futures::{Async, Poll, Future, Stream};
|
2017-11-02 20:54:09 +01:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
2017-11-04 21:24:57 +01:00
|
|
|
use tokio_core::reactor::Timeout;
|
2017-11-02 20:54:09 +01:00
|
|
|
|
2017-11-25 07:15:52 +01:00
|
|
|
use pipeline::Pipeline;
|
2017-11-06 10:24:49 +01:00
|
|
|
use h2writer::H2Writer;
|
2017-12-14 06:38:47 +01:00
|
|
|
use server::WorkerSettings;
|
2017-12-09 13:33:40 +01:00
|
|
|
use channel::{HttpHandler, HttpHandlerTask};
|
2017-11-16 07:06:28 +01:00
|
|
|
use error::PayloadError;
|
2017-11-07 01:23:58 +01:00
|
|
|
use encoding::PayloadType;
|
2017-11-25 07:15:52 +01:00
|
|
|
use httpcodes::HTTPNotFound;
|
|
|
|
use httprequest::HttpRequest;
|
2017-11-16 07:06:28 +01:00
|
|
|
use payload::{Payload, PayloadWriter};
|
2017-11-03 19:29:44 +01:00
|
|
|
|
2017-12-08 06:52:46 +01:00
|
|
|
bitflags! {
|
|
|
|
struct Flags: u8 {
|
|
|
|
const DISCONNECTED = 0b0000_0010;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HTTP/2 Transport
|
2017-11-10 22:08:15 +01:00
|
|
|
pub(crate) struct Http2<T, H>
|
|
|
|
where T: AsyncRead + AsyncWrite + 'static, H: 'static
|
2017-11-03 19:29:44 +01:00
|
|
|
{
|
2017-12-08 06:52:46 +01:00
|
|
|
flags: Flags,
|
2017-12-14 06:38:47 +01:00
|
|
|
settings: Rc<WorkerSettings<H>>,
|
2017-11-10 22:08:15 +01:00
|
|
|
addr: Option<SocketAddr>,
|
2017-11-03 19:29:44 +01:00
|
|
|
state: State<IoWrapper<T>>,
|
|
|
|
tasks: VecDeque<Entry>,
|
2017-11-04 21:24:57 +01:00
|
|
|
keepalive_timer: Option<Timeout>,
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
enum State<T: AsyncRead + AsyncWrite> {
|
|
|
|
Handshake(Handshake<T, Bytes>),
|
|
|
|
Server(Server<T, Bytes>),
|
|
|
|
Empty,
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:08:15 +01:00
|
|
|
impl<T, H> Http2<T, H>
|
2017-11-03 19:29:44 +01:00
|
|
|
where T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
H: HttpHandler + 'static
|
|
|
|
{
|
2017-12-14 06:38:47 +01:00
|
|
|
pub fn new(h: Rc<WorkerSettings<H>>, io: T, addr: Option<SocketAddr>, buf: Bytes) -> Self
|
2017-12-08 07:54:44 +01:00
|
|
|
{
|
2017-12-08 18:24:05 +01:00
|
|
|
Http2{ flags: Flags::empty(),
|
2017-12-14 06:38:47 +01:00
|
|
|
settings: h,
|
2017-11-03 19:29:44 +01:00
|
|
|
addr: addr,
|
|
|
|
tasks: VecDeque::new(),
|
|
|
|
state: State::Handshake(
|
2017-12-14 06:38:47 +01:00
|
|
|
Server::handshake(IoWrapper{unread: Some(buf), inner: io})),
|
2017-11-04 21:24:57 +01:00
|
|
|
keepalive_timer: None,
|
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll(&mut self) -> Poll<(), ()> {
|
2017-11-04 17:07:44 +01:00
|
|
|
// server
|
|
|
|
if let State::Server(ref mut server) = self.state {
|
2017-11-04 21:24:57 +01:00
|
|
|
// keep-alive timer
|
|
|
|
if let Some(ref mut timeout) = self.keepalive_timer {
|
|
|
|
match timeout.poll() {
|
2017-11-09 04:31:25 +01:00
|
|
|
Ok(Async::Ready(_)) => {
|
|
|
|
trace!("Keep-alive timeout, close connection");
|
|
|
|
return Ok(Async::Ready(()))
|
|
|
|
}
|
2017-11-04 21:24:57 +01:00
|
|
|
Ok(Async::NotReady) => (),
|
|
|
|
Err(_) => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 17:07:44 +01:00
|
|
|
loop {
|
|
|
|
let mut not_ready = true;
|
|
|
|
|
|
|
|
// check in-flight connections
|
|
|
|
for item in &mut self.tasks {
|
|
|
|
// read payload
|
|
|
|
item.poll_payload();
|
|
|
|
|
2017-12-08 06:52:46 +01:00
|
|
|
if !item.flags.contains(EntryFlags::EOF) {
|
2017-11-25 07:15:52 +01:00
|
|
|
match item.task.poll_io(&mut item.stream) {
|
2017-11-04 17:07:44 +01:00
|
|
|
Ok(Async::Ready(ready)) => {
|
2017-12-08 06:52:46 +01:00
|
|
|
item.flags.insert(EntryFlags::EOF);
|
2017-11-04 17:07:44 +01:00
|
|
|
if ready {
|
2017-12-08 06:52:46 +01:00
|
|
|
item.flags.insert(EntryFlags::FINISHED);
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
not_ready = false;
|
|
|
|
},
|
|
|
|
Ok(Async::NotReady) => (),
|
2017-11-25 18:28:25 +01:00
|
|
|
Err(err) => {
|
|
|
|
error!("Unhandled error: {}", err);
|
2017-12-08 06:52:46 +01:00
|
|
|
item.flags.insert(EntryFlags::EOF);
|
|
|
|
item.flags.insert(EntryFlags::ERROR);
|
2017-11-04 17:07:44 +01:00
|
|
|
item.stream.reset(Reason::INTERNAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 06:52:46 +01:00
|
|
|
} else if !item.flags.contains(EntryFlags::FINISHED) {
|
2017-11-04 17:07:44 +01:00
|
|
|
match item.task.poll() {
|
|
|
|
Ok(Async::NotReady) => (),
|
|
|
|
Ok(Async::Ready(_)) => {
|
|
|
|
not_ready = false;
|
2017-12-08 06:52:46 +01:00
|
|
|
item.flags.insert(EntryFlags::FINISHED);
|
2017-11-04 17:07:44 +01:00
|
|
|
},
|
2017-11-25 18:28:25 +01:00
|
|
|
Err(err) => {
|
2017-12-08 06:52:46 +01:00
|
|
|
item.flags.insert(EntryFlags::ERROR);
|
|
|
|
item.flags.insert(EntryFlags::FINISHED);
|
2017-11-25 18:28:25 +01:00
|
|
|
error!("Unhandled error: {}", err);
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup finished tasks
|
|
|
|
while !self.tasks.is_empty() {
|
2017-12-08 06:52:46 +01:00
|
|
|
if self.tasks[0].flags.contains(EntryFlags::EOF) &&
|
|
|
|
self.tasks[0].flags.contains(EntryFlags::FINISHED) ||
|
|
|
|
self.tasks[0].flags.contains(EntryFlags::ERROR)
|
|
|
|
{
|
2017-11-04 17:07:44 +01:00
|
|
|
self.tasks.pop_front();
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get request
|
2017-12-08 06:52:46 +01:00
|
|
|
if !self.flags.contains(Flags::DISCONNECTED) {
|
2017-11-04 17:07:44 +01:00
|
|
|
match server.poll() {
|
|
|
|
Ok(Async::Ready(None)) => {
|
|
|
|
not_ready = false;
|
2017-12-08 06:52:46 +01:00
|
|
|
self.flags.insert(Flags::DISCONNECTED);
|
2017-11-04 17:07:44 +01:00
|
|
|
for entry in &mut self.tasks {
|
|
|
|
entry.task.disconnected()
|
|
|
|
}
|
|
|
|
},
|
2017-11-18 17:50:07 +01:00
|
|
|
Ok(Async::Ready(Some((req, resp)))) => {
|
2017-11-04 17:07:44 +01:00
|
|
|
not_ready = false;
|
|
|
|
let (parts, body) = req.into_parts();
|
2017-11-10 22:08:15 +01:00
|
|
|
|
|
|
|
// stop keepalive timer
|
2017-11-04 21:24:57 +01:00
|
|
|
self.keepalive_timer.take();
|
2017-11-10 22:08:15 +01:00
|
|
|
|
|
|
|
self.tasks.push_back(
|
2017-12-14 06:38:47 +01:00
|
|
|
Entry::new(parts, body, resp, self.addr, &self.settings));
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
2017-11-04 21:24:57 +01:00
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
// start keep-alive timer
|
2017-12-14 06:38:47 +01:00
|
|
|
if self.tasks.is_empty() {
|
2017-12-14 20:20:45 +01:00
|
|
|
if self.settings.keep_alive_enabled() {
|
|
|
|
let keep_alive = self.settings.keep_alive();
|
2017-12-14 06:38:47 +01:00
|
|
|
if keep_alive > 0 && self.keepalive_timer.is_none() {
|
|
|
|
trace!("Start keep-alive timer");
|
|
|
|
let mut timeout = Timeout::new(
|
2017-12-14 07:36:28 +01:00
|
|
|
Duration::new(keep_alive, 0),
|
2017-12-14 06:38:47 +01:00
|
|
|
Arbiter::handle()).unwrap();
|
|
|
|
// register timeout
|
|
|
|
let _ = timeout.poll();
|
|
|
|
self.keepalive_timer = Some(timeout);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// keep-alive disable, drop connection
|
|
|
|
return Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// keep-alive unset, rely on operating system
|
|
|
|
return Ok(Async::NotReady)
|
2017-11-04 21:24:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
trace!("Connection error: {}", err);
|
2017-12-08 06:52:46 +01:00
|
|
|
self.flags.insert(Flags::DISCONNECTED);
|
2017-11-04 21:24:57 +01:00
|
|
|
for entry in &mut self.tasks {
|
|
|
|
entry.task.disconnected()
|
|
|
|
}
|
|
|
|
self.keepalive_timer.take();
|
|
|
|
},
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if not_ready {
|
2017-12-08 06:52:46 +01:00
|
|
|
if self.tasks.is_empty() && self.flags.contains(Flags::DISCONNECTED) {
|
2017-11-04 17:07:44 +01:00
|
|
|
return Ok(Async::Ready(()))
|
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 19:29:44 +01:00
|
|
|
// handshake
|
|
|
|
self.state = if let State::Handshake(ref mut handshake) = self.state {
|
|
|
|
match handshake.poll() {
|
|
|
|
Ok(Async::Ready(srv)) => {
|
|
|
|
State::Server(srv)
|
|
|
|
},
|
|
|
|
Ok(Async::NotReady) =>
|
|
|
|
return Ok(Async::NotReady),
|
|
|
|
Err(err) => {
|
|
|
|
trace!("Error handling connection: {}", err);
|
|
|
|
return Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mem::replace(&mut self.state, State::Empty)
|
|
|
|
};
|
|
|
|
|
2017-11-04 17:07:44 +01:00
|
|
|
self.poll()
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 06:52:46 +01:00
|
|
|
bitflags! {
|
|
|
|
struct EntryFlags: u8 {
|
|
|
|
const EOF = 0b0000_0001;
|
|
|
|
const REOF = 0b0000_0010;
|
|
|
|
const ERROR = 0b0000_0100;
|
|
|
|
const FINISHED = 0b0000_1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 19:29:44 +01:00
|
|
|
struct Entry {
|
2017-12-09 13:33:40 +01:00
|
|
|
task: Box<HttpHandlerTask>,
|
2017-11-07 01:23:58 +01:00
|
|
|
payload: PayloadType,
|
2017-11-03 19:29:44 +01:00
|
|
|
recv: RecvStream,
|
2017-11-04 17:07:44 +01:00
|
|
|
stream: H2Writer,
|
|
|
|
capacity: usize,
|
2017-12-08 06:52:46 +01:00
|
|
|
flags: EntryFlags,
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Entry {
|
|
|
|
fn new<H>(parts: Parts,
|
|
|
|
recv: RecvStream,
|
|
|
|
resp: Respond<Bytes>,
|
2017-11-10 22:08:15 +01:00
|
|
|
addr: Option<SocketAddr>,
|
2017-12-14 06:38:47 +01:00
|
|
|
settings: &Rc<WorkerSettings<H>>) -> Entry
|
2017-11-03 19:29:44 +01:00
|
|
|
where H: HttpHandler + 'static
|
|
|
|
{
|
2017-11-27 04:00:57 +01:00
|
|
|
// Payload and Content-Encoding
|
|
|
|
let (psender, payload) = Payload::new(false);
|
|
|
|
|
2017-11-03 19:29:44 +01:00
|
|
|
let mut req = HttpRequest::new(
|
2017-12-13 06:32:58 +01:00
|
|
|
parts.method, parts.uri, parts.version, parts.headers, Some(payload));
|
2017-11-06 18:35:52 +01:00
|
|
|
|
2017-11-10 22:08:15 +01:00
|
|
|
// set remote addr
|
2017-12-06 06:38:52 +01:00
|
|
|
req.set_peer_addr(addr);
|
2017-11-10 22:08:15 +01:00
|
|
|
|
2017-11-25 07:15:52 +01:00
|
|
|
// Payload sender
|
|
|
|
let psender = PayloadType::new(req.headers(), psender);
|
|
|
|
|
2017-11-03 19:29:44 +01:00
|
|
|
// start request processing
|
|
|
|
let mut task = None;
|
2017-12-14 06:38:47 +01:00
|
|
|
for h in settings.handlers().iter() {
|
2017-11-29 22:53:52 +01:00
|
|
|
req = match h.handle(req) {
|
|
|
|
Ok(t) => {
|
|
|
|
task = Some(t);
|
|
|
|
break
|
|
|
|
},
|
|
|
|
Err(req) => req,
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 07:15:52 +01:00
|
|
|
Entry {task: task.unwrap_or_else(|| Pipeline::error(HTTPNotFound)),
|
2017-11-07 01:23:58 +01:00
|
|
|
payload: psender,
|
2017-11-03 19:29:44 +01:00
|
|
|
recv: recv,
|
2017-11-04 17:07:44 +01:00
|
|
|
stream: H2Writer::new(resp),
|
2017-12-08 06:52:46 +01:00
|
|
|
flags: EntryFlags::empty(),
|
2017-11-04 17:07:44 +01:00
|
|
|
capacity: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_payload(&mut self) {
|
2017-12-08 06:52:46 +01:00
|
|
|
if !self.flags.contains(EntryFlags::REOF) {
|
2017-11-04 17:07:44 +01:00
|
|
|
match self.recv.poll() {
|
|
|
|
Ok(Async::Ready(Some(chunk))) => {
|
2017-11-07 01:23:58 +01:00
|
|
|
self.payload.feed_data(chunk);
|
2017-11-04 17:07:44 +01:00
|
|
|
},
|
|
|
|
Ok(Async::Ready(None)) => {
|
2017-12-08 06:52:46 +01:00
|
|
|
self.flags.insert(EntryFlags::REOF);
|
2017-11-04 17:07:44 +01:00
|
|
|
},
|
|
|
|
Ok(Async::NotReady) => (),
|
|
|
|
Err(err) => {
|
2017-11-07 01:23:58 +01:00
|
|
|
self.payload.set_error(PayloadError::Http2(err))
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 01:23:58 +01:00
|
|
|
let capacity = self.payload.capacity();
|
2017-11-04 17:07:44 +01:00
|
|
|
if self.capacity != capacity {
|
|
|
|
self.capacity = capacity;
|
|
|
|
if let Err(err) = self.recv.release_capacity().release_capacity(capacity) {
|
2017-11-07 01:23:58 +01:00
|
|
|
self.payload.set_error(PayloadError::Http2(err))
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-02 20:54:09 +01:00
|
|
|
|
|
|
|
struct IoWrapper<T> {
|
|
|
|
unread: Option<Bytes>,
|
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Read> Read for IoWrapper<T> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
if let Some(mut bytes) = self.unread.take() {
|
|
|
|
let size = cmp::min(buf.len(), bytes.len());
|
2017-11-03 19:29:44 +01:00
|
|
|
buf[..size].copy_from_slice(&bytes[..size]);
|
|
|
|
if bytes.len() > size {
|
|
|
|
bytes.split_to(size);
|
2017-11-02 20:54:09 +01:00
|
|
|
self.unread = Some(bytes);
|
|
|
|
}
|
|
|
|
Ok(size)
|
|
|
|
} else {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Write> Write for IoWrapper<T> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.inner.write(buf)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.inner.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncRead + 'static> AsyncRead for IoWrapper<T> {
|
|
|
|
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
|
|
|
|
self.inner.prepare_uninitialized_buffer(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncWrite + 'static> AsyncWrite for IoWrapper<T> {
|
|
|
|
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
|
|
|
self.inner.shutdown()
|
|
|
|
}
|
|
|
|
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
|
|
|
|
self.inner.write_buf(buf)
|
|
|
|
}
|
|
|
|
}
|