2018-01-04 08:41:55 +01:00
|
|
|
use std::{ptr, mem, time, io};
|
2017-12-08 18:24:05 +01:00
|
|
|
use std::rc::Rc;
|
2018-01-04 07:43:44 +01:00
|
|
|
use std::net::{SocketAddr, Shutdown};
|
2017-11-04 20:33:14 +01:00
|
|
|
|
2018-01-12 06:48:36 +01:00
|
|
|
use bytes::{Bytes, BytesMut, Buf, BufMut};
|
2017-11-04 20:33:14 +01:00
|
|
|
use futures::{Future, Poll, Async};
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
|
2018-01-12 06:48:36 +01:00
|
|
|
use super::{h1, h2, utils, HttpHandler, IoStream};
|
2018-01-12 03:35:05 +01:00
|
|
|
use super::settings::WorkerSettings;
|
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-01-12 03:35:05 +01:00
|
|
|
enum HttpProtocol<T: IoStream, H: '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
|
|
|
impl<T: IoStream, H: 'static> HttpProtocol<T, H> {
|
|
|
|
fn is_unknown(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
HttpProtocol::Unknown(_, _, _, _) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ProtocolKind {
|
|
|
|
Http1,
|
|
|
|
Http2,
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
|
|
|
|
2017-11-27 07:53:28 +01:00
|
|
|
#[doc(hidden)]
|
2018-01-12 06:48:36 +01: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-01-12 06:48:36 +01:00
|
|
|
impl<T, H> HttpChannel<T, H> where T: IoStream, H: HttpHandler + 'static
|
2017-11-04 20:33:14 +01:00
|
|
|
{
|
2018-01-12 05:11:34 +01:00
|
|
|
pub(crate) fn new(settings: Rc<WorkerSettings<H>>,
|
2018-01-04 07:43:44 +01:00
|
|
|
io: T, peer: Option<SocketAddr>, http2: bool) -> HttpChannel<T, H>
|
2017-12-08 06:52:46 +01:00
|
|
|
{
|
2018-01-12 05:11:34 +01:00
|
|
|
settings.add_channel();
|
2017-11-04 20:33:14 +01:00
|
|
|
if http2 {
|
|
|
|
HttpChannel {
|
2018-01-04 07:43:44 +01:00
|
|
|
node: None,
|
2017-11-04 20:33:14 +01:00
|
|
|
proto: Some(HttpProtocol::H2(
|
2018-01-12 05:11:34 +01:00
|
|
|
h2::Http2::new(settings, io, peer, Bytes::new()))) }
|
2017-11-04 20:33:14 +01:00
|
|
|
} else {
|
|
|
|
HttpChannel {
|
2018-01-04 07:43:44 +01:00
|
|
|
node: None,
|
2018-01-12 06:48:36 +01:00
|
|
|
proto: Some(HttpProtocol::Unknown(
|
|
|
|
settings, peer, io, BytesMut::with_capacity(4096))) }
|
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
|
|
|
}
|
|
|
|
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-01-12 07:06:06 +01: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-01-12 06:48:36 +01:00
|
|
|
if !self.proto.as_ref().map(|p| p.is_unknown()).unwrap_or(false) && self.node.is_none() {
|
2018-01-04 07:43:44 +01:00
|
|
|
self.node = Some(Node::new(self));
|
|
|
|
match self.proto {
|
2018-01-12 06:48:36 +01:00
|
|
|
Some(HttpProtocol::H1(ref mut h1)) =>
|
|
|
|
h1.settings().head().insert(self.node.as_ref().unwrap()),
|
|
|
|
Some(HttpProtocol::H2(ref mut h2)) =>
|
|
|
|
h2.settings().head().insert(self.node.as_ref().unwrap()),
|
|
|
|
_ => (),
|
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(_) => {
|
2017-12-29 01:25:47 +01:00
|
|
|
h1.settings().remove_channel();
|
2018-01-04 07:43:44 +01:00
|
|
|
self.node.as_ref().unwrap().remove();
|
2018-01-12 07:06:06 +01:00
|
|
|
},
|
|
|
|
_ => (),
|
2017-12-29 01:25:47 +01:00
|
|
|
}
|
2018-01-12 07:06:06 +01:00
|
|
|
return result
|
2018-01-12 06:48:36 +01:00
|
|
|
},
|
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(_) => {
|
|
|
|
h2.settings().remove_channel();
|
|
|
|
self.node.as_ref().unwrap().remove();
|
2018-01-12 07:06:06 +01:00
|
|
|
},
|
2017-12-29 01:25:47 +01:00
|
|
|
_ => (),
|
2017-11-04 20:33:14 +01:00
|
|
|
}
|
2017-12-29 01:25:47 +01:00
|
|
|
return result
|
2018-01-12 06:48:36 +01:00
|
|
|
},
|
2018-01-12 08:49:53 +01:00
|
|
|
Some(HttpProtocol::Unknown(ref mut settings, _, ref mut io, ref mut buf)) => {
|
2018-01-12 06:48:36 +01:00
|
|
|
match utils::read_from_io(io, buf) {
|
2018-01-12 08:49:53 +01:00
|
|
|
Ok(Async::Ready(0)) | Err(_) => {
|
2018-01-12 06:48:36 +01:00
|
|
|
debug!("Ignored premature client disconnection");
|
2018-01-12 08:49:53 +01:00
|
|
|
settings.remove_channel();
|
2018-01-12 06:48:36 +01:00
|
|
|
return Err(())
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf.len() >= 14 {
|
|
|
|
if buf[..14] == HTTP2_PREFACE[..] {
|
|
|
|
ProtocolKind::Http2
|
|
|
|
} else {
|
|
|
|
ProtocolKind::Http1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
},
|
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 => {
|
|
|
|
self.proto = Some(
|
|
|
|
HttpProtocol::H1(h1::Http1::new(settings, io, addr, buf)));
|
|
|
|
return self.poll()
|
|
|
|
},
|
|
|
|
ProtocolKind::Http2 => {
|
|
|
|
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
|
|
|
|
|
|
|
pub(crate) struct Node<T>
|
|
|
|
{
|
|
|
|
next: Option<*mut Node<()>>,
|
|
|
|
prev: Option<*mut Node<()>>,
|
|
|
|
element: *mut T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Node<T>
|
|
|
|
{
|
|
|
|
fn new(el: &mut T) -> Self {
|
|
|
|
Node {
|
|
|
|
next: None,
|
|
|
|
prev: None,
|
|
|
|
element: el as *mut _,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert<I>(&self, next: &Node<I>) {
|
|
|
|
#[allow(mutable_transmutes)]
|
|
|
|
unsafe {
|
|
|
|
if let Some(ref next2) = self.next {
|
|
|
|
let n: &mut Node<()> = mem::transmute(next2.as_ref().unwrap());
|
|
|
|
n.prev = Some(next as *const _ as *mut _);
|
|
|
|
}
|
|
|
|
let slf: &mut Node<T> = mem::transmute(self);
|
|
|
|
slf.next = Some(next as *const _ as *mut _);
|
|
|
|
|
|
|
|
let next: &mut Node<T> = mem::transmute(next);
|
|
|
|
next.prev = Some(slf as *const _ as *mut _);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(&self) {
|
|
|
|
#[allow(mutable_transmutes)]
|
|
|
|
unsafe {
|
|
|
|
if let Some(ref prev) = self.prev {
|
|
|
|
let p: &mut Node<()> = mem::transmute(prev.as_ref().unwrap());
|
|
|
|
let slf: &mut Node<T> = mem::transmute(self);
|
|
|
|
p.next = slf.next.take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Node<()> {
|
|
|
|
|
|
|
|
pub(crate) fn head() -> Self {
|
|
|
|
Node {
|
|
|
|
next: None,
|
|
|
|
prev: None,
|
|
|
|
element: ptr::null_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 08:41:55 +01: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 {
|
|
|
|
let n: &Node<()> = mem::transmute(n.as_ref().unwrap());
|
|
|
|
next = n.next.as_ref();
|
|
|
|
|
|
|
|
if !n.element.is_null() {
|
2018-01-04 08:41:55 +01:00
|
|
|
let ch: &mut HttpChannel<T, H> = mem::transmute(
|
2018-01-04 07:43:44 +01:00
|
|
|
&mut *(n.element as *mut _));
|
|
|
|
ch.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 08:41:55 +01:00
|
|
|
|
2018-01-04 08:59:12 +01:00
|
|
|
/// Wrapper for `AsyncRead + AsyncWrite` types
|
2018-01-04 08:41:55 +01:00
|
|
|
pub(crate) struct WrapperStream<T> where T: AsyncRead + AsyncWrite + 'static {
|
|
|
|
io: T,
|
|
|
|
}
|
|
|
|
|
2018-01-12 07:06:06 +01:00
|
|
|
impl<T> WrapperStream<T> where T: AsyncRead + AsyncWrite + 'static {
|
2018-01-04 08:41:55 +01:00
|
|
|
pub fn new(io: T) -> Self {
|
|
|
|
WrapperStream{io: io}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 07:06:06 +01: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-01-12 07:06:06 +01: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-01-12 07:06:06 +01: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-01-12 07:06:06 +01:00
|
|
|
impl<T> AsyncRead for WrapperStream<T> where T: AsyncRead + AsyncWrite + 'static {
|
|
|
|
#[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-01-12 07:06:06 +01:00
|
|
|
impl<T> AsyncWrite for WrapperStream<T> where T: AsyncRead + AsyncWrite + 'static {
|
|
|
|
#[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)
|
|
|
|
}
|
|
|
|
}
|