2018-04-14 01:02:01 +02:00
|
|
|
use std::net::{Shutdown, SocketAddr};
|
2017-12-08 18:24:05 +01:00
|
|
|
use std::rc::Rc;
|
2018-04-29 18:09:08 +02:00
|
|
|
use std::{io, ptr, time};
|
2017-11-04 20:33:14 +01:00
|
|
|
|
2018-08-04 04:32:46 +02:00
|
|
|
use bytes::{Buf, BufMut, BytesMut};
|
2018-04-14 01:02:01 +02:00
|
|
|
use futures::{Async, Future, Poll};
|
2017-11-04 20:33:14 +01:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
|
2018-01-12 03:35:05 +01:00
|
|
|
use super::settings::WorkerSettings;
|
2018-08-09 20:52:32 +02:00
|
|
|
use super::{h1, h2, ConnectionTag, HttpHandler, IoStream};
|
2017-11-04 20:33:14 +01:00
|
|
|
|
2018-01-12 06:48:36 +01:00
|
|
|
const HTTP2_PREFACE: [u8; 14] = *b"PRI * HTTP/2.0";
|
|
|
|
|
2018-06-18 01:45:54 +02:00
|
|
|
enum HttpProtocol<T: IoStream, H: HttpHandler + 'static> {
|
2017-11-10 22:08:15 +01:00
|
|
|
H1(h1::Http1<T, H>),
|
|
|
|
H2(h2::Http2<T, H>),
|
2018-01-12 06:48:36 +01:00
|
|
|
Unknown(Rc<WorkerSettings<H>>, Option<SocketAddr>, T, BytesMut),
|
|
|
|
}
|
2018-01-12 07:06:06 +01:00
|
|
|
|
2018-01-12 06:48:36 +01:00
|
|
|
enum ProtocolKind {
|
|
|
|
Http1,
|
|
|
|
Http2,
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
|
|
|
|
2017-11-27 07:53:28 +01:00
|
|
|
#[doc(hidden)]
|
2018-04-14 01:02:01 +02:00
|
|
|
pub struct HttpChannel<T, H>
|
|
|
|
where
|
|
|
|
T: IoStream,
|
|
|
|
H: HttpHandler + 'static,
|
|
|
|
{
|
2017-11-10 22:08:15 +01:00
|
|
|
proto: Option<HttpProtocol<T, H>>,
|
2018-01-04 07:43:44 +01:00
|
|
|
node: Option<Node<HttpChannel<T, H>>>,
|
2018-08-09 20:52:32 +02:00
|
|
|
_tag: ConnectionTag,
|
2018-01-04 07:43:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<T, H> HttpChannel<T, H>
|
|
|
|
where
|
|
|
|
T: IoStream,
|
|
|
|
H: HttpHandler + 'static,
|
2017-11-04 20:33:14 +01:00
|
|
|
{
|
2018-04-14 01:02:01 +02:00
|
|
|
pub(crate) fn new(
|
2018-08-04 04:32:46 +02:00
|
|
|
settings: Rc<WorkerSettings<H>>, io: T, peer: Option<SocketAddr>,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> HttpChannel<T, H> {
|
2018-08-09 20:52:32 +02:00
|
|
|
let _tag = settings.connection();
|
2018-03-03 20:16:55 +01:00
|
|
|
|
2018-08-04 04:32:46 +02:00
|
|
|
HttpChannel {
|
2018-08-09 20:52:32 +02:00
|
|
|
_tag,
|
2018-08-04 04:32:46 +02:00
|
|
|
node: None,
|
|
|
|
proto: Some(HttpProtocol::Unknown(
|
|
|
|
settings,
|
|
|
|
peer,
|
|
|
|
io,
|
|
|
|
BytesMut::with_capacity(8192),
|
|
|
|
)),
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-04 07:43:44 +01:00
|
|
|
|
|
|
|
fn shutdown(&mut self) {
|
|
|
|
match self.proto {
|
|
|
|
Some(HttpProtocol::H1(ref mut h1)) => {
|
2018-01-04 08:41:55 +01:00
|
|
|
let io = h1.io();
|
|
|
|
let _ = IoStream::set_linger(io, Some(time::Duration::new(0, 0)));
|
|
|
|
let _ = IoStream::shutdown(io, Shutdown::Both);
|
2018-01-04 07:43:44 +01:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
Some(HttpProtocol::H2(ref mut h2)) => h2.shutdown(),
|
2018-01-12 06:48:36 +01:00
|
|
|
_ => (),
|
2018-01-04 07:43:44 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<T, H> Future for HttpChannel<T, H>
|
|
|
|
where
|
|
|
|
T: IoStream,
|
|
|
|
H: HttpHandler + 'static,
|
2017-11-04 20:33:14 +01:00
|
|
|
{
|
|
|
|
type Item = ();
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-04-05 05:15:47 +02:00
|
|
|
if self.node.is_some() {
|
2018-03-03 20:16:55 +01:00
|
|
|
let el = self as *mut _;
|
|
|
|
self.node = Some(Node::new(el));
|
|
|
|
let _ = match self.proto {
|
2018-05-17 19:58:08 +02:00
|
|
|
Some(HttpProtocol::H1(ref mut h1)) => {
|
2018-07-11 08:56:35 +02:00
|
|
|
self.node.as_mut().map(|n| h1.settings().head().insert(n))
|
2018-05-17 19:58:08 +02:00
|
|
|
}
|
|
|
|
Some(HttpProtocol::H2(ref mut h2)) => {
|
2018-07-11 08:56:35 +02:00
|
|
|
self.node.as_mut().map(|n| h2.settings().head().insert(n))
|
2018-05-17 19:58:08 +02:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
Some(HttpProtocol::Unknown(ref mut settings, _, _, _)) => {
|
2018-07-11 08:56:35 +02:00
|
|
|
self.node.as_mut().map(|n| settings.head().insert(n))
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2018-03-03 20:16:55 +01:00
|
|
|
None => unreachable!(),
|
|
|
|
};
|
2018-01-04 07:43:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-12 06:48:36 +01:00
|
|
|
let kind = match self.proto {
|
2017-11-04 20:33:14 +01:00
|
|
|
Some(HttpProtocol::H1(ref mut h1)) => {
|
2018-01-12 07:06:06 +01:00
|
|
|
let result = h1.poll();
|
|
|
|
match result {
|
|
|
|
Ok(Async::Ready(())) | Err(_) => {
|
2018-04-29 07:55:47 +02:00
|
|
|
if let Some(n) = self.node.as_mut() {
|
|
|
|
n.remove()
|
|
|
|
};
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2018-01-12 07:06:06 +01:00
|
|
|
_ => (),
|
2017-12-29 01:25:47 +01:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
return result;
|
|
|
|
}
|
2017-12-29 01:25:47 +01:00
|
|
|
Some(HttpProtocol::H2(ref mut h2)) => {
|
|
|
|
let result = h2.poll();
|
|
|
|
match result {
|
2018-01-04 07:43:44 +01:00
|
|
|
Ok(Async::Ready(())) | Err(_) => {
|
2018-04-29 07:55:47 +02:00
|
|
|
if let Some(n) = self.node.as_mut() {
|
|
|
|
n.remove()
|
|
|
|
};
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2017-12-29 01:25:47 +01:00
|
|
|
_ => (),
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
return result;
|
|
|
|
}
|
2018-08-09 20:52:32 +02:00
|
|
|
Some(HttpProtocol::Unknown(_, _, ref mut io, ref mut buf)) => {
|
2018-06-22 05:01:20 +02:00
|
|
|
match io.read_available(buf) {
|
2018-06-22 07:30:40 +02:00
|
|
|
Ok(Async::Ready(true)) | Err(_) => {
|
2018-01-12 06:48:36 +01:00
|
|
|
debug!("Ignored premature client disconnection");
|
2018-04-29 07:55:47 +02:00
|
|
|
if let Some(n) = self.node.as_mut() {
|
|
|
|
n.remove()
|
|
|
|
};
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(());
|
|
|
|
}
|
2018-01-12 06:48:36 +01:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf.len() >= 14 {
|
|
|
|
if buf[..14] == HTTP2_PREFACE[..] {
|
|
|
|
ProtocolKind::Http2
|
|
|
|
} else {
|
|
|
|
ProtocolKind::Http1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2017-11-04 20:33:14 +01:00
|
|
|
None => unreachable!(),
|
2018-01-12 06:48:36 +01:00
|
|
|
};
|
2017-11-04 20:33:14 +01:00
|
|
|
|
2018-01-12 07:06:06 +01:00
|
|
|
// upgrade to specific http protocol
|
|
|
|
if let Some(HttpProtocol::Unknown(settings, addr, io, buf)) = self.proto.take() {
|
|
|
|
match kind {
|
|
|
|
ProtocolKind::Http1 => {
|
2018-05-17 19:58:08 +02:00
|
|
|
self.proto =
|
|
|
|
Some(HttpProtocol::H1(h1::Http1::new(settings, io, addr, buf)));
|
2018-04-14 01:02:01 +02:00
|
|
|
return self.poll();
|
|
|
|
}
|
2018-01-12 07:06:06 +01:00
|
|
|
ProtocolKind::Http2 => {
|
2018-04-14 01:02:01 +02:00
|
|
|
self.proto = Some(HttpProtocol::H2(h2::Http2::new(
|
|
|
|
settings,
|
|
|
|
io,
|
|
|
|
addr,
|
|
|
|
buf.freeze(),
|
|
|
|
)));
|
|
|
|
return self.poll();
|
|
|
|
}
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 07:06:06 +01:00
|
|
|
unreachable!()
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-04 07:43:44 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
pub(crate) struct Node<T> {
|
2018-07-11 08:56:35 +02:00
|
|
|
next: Option<*mut Node<T>>,
|
|
|
|
prev: Option<*mut Node<T>>,
|
2018-01-04 07:43:44 +01:00
|
|
|
element: *mut T,
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<T> Node<T> {
|
2018-03-03 20:16:55 +01:00
|
|
|
fn new(el: *mut T) -> Self {
|
2018-01-04 07:43:44 +01:00
|
|
|
Node {
|
|
|
|
next: None,
|
|
|
|
prev: None,
|
2018-03-03 20:16:55 +01:00
|
|
|
element: el,
|
2018-01-04 07:43:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 08:56:35 +02:00
|
|
|
fn insert<I>(&mut self, next: &mut Node<I>) {
|
2018-01-04 07:43:44 +01:00
|
|
|
unsafe {
|
2018-07-11 08:56:35 +02:00
|
|
|
let next: *mut Node<T> = next as *const _ as *mut _;
|
2018-04-29 18:09:08 +02:00
|
|
|
|
2018-07-11 08:56:35 +02:00
|
|
|
if let Some(ref mut next2) = self.next {
|
|
|
|
let n = next2.as_mut().unwrap();
|
|
|
|
n.prev = Some(next);
|
|
|
|
}
|
|
|
|
self.next = Some(next);
|
2018-01-04 07:43:44 +01:00
|
|
|
|
2018-07-11 08:56:35 +02:00
|
|
|
let next: &mut Node<T> = &mut *next;
|
|
|
|
next.prev = Some(self as *mut _);
|
2018-01-04 07:43:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 19:06:13 +01:00
|
|
|
fn remove(&mut self) {
|
2018-01-04 07:43:44 +01:00
|
|
|
unsafe {
|
2018-03-03 20:16:55 +01:00
|
|
|
self.element = ptr::null_mut();
|
2018-03-03 19:06:13 +01:00
|
|
|
let next = self.next.take();
|
2018-03-03 20:16:55 +01:00
|
|
|
let mut prev = self.prev.take();
|
2018-03-03 19:06:13 +01:00
|
|
|
|
|
|
|
if let Some(ref mut prev) = prev {
|
2018-03-03 20:16:55 +01:00
|
|
|
prev.as_mut().unwrap().next = next;
|
2018-01-04 07:43:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Node<()> {
|
|
|
|
pub(crate) fn head() -> Self {
|
|
|
|
Node {
|
|
|
|
next: None,
|
|
|
|
prev: None,
|
|
|
|
element: ptr::null_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
pub(crate) fn traverse<T, H>(&self)
|
|
|
|
where
|
|
|
|
T: IoStream,
|
|
|
|
H: HttpHandler + 'static,
|
|
|
|
{
|
2018-01-04 07:43:44 +01:00
|
|
|
let mut next = self.next.as_ref();
|
|
|
|
loop {
|
|
|
|
if let Some(n) = next {
|
|
|
|
unsafe {
|
2018-04-29 18:09:08 +02:00
|
|
|
let n: &Node<()> = &*(n.as_ref().unwrap() as *const _);
|
2018-01-04 07:43:44 +01:00
|
|
|
next = n.next.as_ref();
|
|
|
|
|
|
|
|
if !n.element.is_null() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let ch: &mut HttpChannel<T, H> =
|
2018-04-29 18:09:08 +02:00
|
|
|
&mut *(&mut *(n.element as *mut _) as *mut () as *mut _);
|
2018-01-04 07:43:44 +01:00
|
|
|
ch.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-14 01:02:01 +02:00
|
|
|
return;
|
2018-01-04 07:43:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 08:41:55 +01:00
|
|
|
|
2018-01-04 08:59:12 +01:00
|
|
|
/// Wrapper for `AsyncRead + AsyncWrite` types
|
2018-04-14 01:02:01 +02:00
|
|
|
pub(crate) struct WrapperStream<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
{
|
|
|
|
io: T,
|
2018-01-04 08:41:55 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<T> WrapperStream<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
{
|
2018-01-04 08:41:55 +01:00
|
|
|
pub fn new(io: T) -> Self {
|
2018-04-29 18:09:08 +02:00
|
|
|
WrapperStream { io }
|
2018-01-04 08:41:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<T> IoStream for WrapperStream<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
{
|
2018-01-04 08:41:55 +01:00
|
|
|
#[inline]
|
|
|
|
fn shutdown(&mut self, _: Shutdown) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn set_nodelay(&mut self, _: bool) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn set_linger(&mut self, _: Option<time::Duration>) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<T> io::Read for WrapperStream<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
{
|
2018-01-04 08:41:55 +01:00
|
|
|
#[inline]
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.io.read(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<T> io::Write for WrapperStream<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
{
|
2018-01-04 08:41:55 +01:00
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.io.write(buf)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
self.io.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<T> AsyncRead for WrapperStream<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
{
|
2018-01-12 07:06:06 +01:00
|
|
|
#[inline]
|
2018-01-04 08:41:55 +01:00
|
|
|
fn read_buf<B: BufMut>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
|
|
|
|
self.io.read_buf(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<T> AsyncWrite for WrapperStream<T>
|
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + 'static,
|
|
|
|
{
|
2018-01-12 07:06:06 +01:00
|
|
|
#[inline]
|
2018-01-04 08:41:55 +01:00
|
|
|
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
|
|
|
self.io.shutdown()
|
|
|
|
}
|
2018-01-12 07:06:06 +01:00
|
|
|
#[inline]
|
2018-01-04 08:41:55 +01:00
|
|
|
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
|
|
|
|
self.io.write_buf(buf)
|
|
|
|
}
|
|
|
|
}
|