feat(http1): Add higher-level HTTP upgrade support to Client and Server (#1563)
- Adds `Body::on_upgrade()` that returns an `OnUpgrade` future.
- Adds `hyper::upgrade` module containing types for dealing with
upgrades.
- Adds `server::conn::Connection::with_upgrades()` method to enable
these upgrades when using lower-level API (because of a missing
`Send` bound on the transport generic).
- Client connections are automatically enabled.
- Optimizes request parsing, to make up for extra work to look for
upgrade requests.
- Returns a smaller `DecodedLength` type instead of the fatter
`Decoder`, which should also allow a couple fewer branches.
- Removes the `Decode::Ignore` wrapper enum, and instead ignoring
1xx responses is handled directly in the response parsing code.
Ref #1563
Closes #1395
This commit is contained in:
@@ -9,22 +9,26 @@
|
||||
//! higher-level [Server](super) API.
|
||||
|
||||
use std::fmt;
|
||||
use std::mem;
|
||||
#[cfg(feature = "runtime")] use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
#[cfg(feature = "runtime")] use std::time::Duration;
|
||||
|
||||
use super::rewind::Rewind;
|
||||
use bytes::Bytes;
|
||||
use futures::{Async, Future, Poll, Stream};
|
||||
use futures::future::{Either, Executor};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
#[cfg(feature = "runtime")] use tokio_reactor::Handle;
|
||||
|
||||
use common::Exec;
|
||||
use proto;
|
||||
use body::{Body, Payload};
|
||||
use service::{NewService, Service};
|
||||
use common::Exec;
|
||||
use common::io::Rewind;
|
||||
use error::{Kind, Parse};
|
||||
use proto;
|
||||
use service::{NewService, Service};
|
||||
use upgrade::Upgraded;
|
||||
|
||||
use self::upgrades::UpgradeableConnection;
|
||||
|
||||
#[cfg(feature = "runtime")] pub use super::tcp::AddrIncoming;
|
||||
|
||||
@@ -109,6 +113,8 @@ where
|
||||
fallback: bool,
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Deconstructed parts of a `Connection`.
|
||||
///
|
||||
/// This allows taking apart a `Connection` at a later time, in order to
|
||||
@@ -429,7 +435,7 @@ where
|
||||
loop {
|
||||
let polled = match *self.conn.as_mut().unwrap() {
|
||||
Either::A(ref mut h1) => h1.poll_without_shutdown(),
|
||||
Either::B(ref mut h2) => h2.poll(),
|
||||
Either::B(ref mut h2) => return h2.poll().map(|x| x.map(|_| ())),
|
||||
};
|
||||
match polled {
|
||||
Ok(x) => return Ok(x),
|
||||
@@ -466,6 +472,18 @@ where
|
||||
debug_assert!(self.conn.is_none());
|
||||
self.conn = Some(Either::B(h2));
|
||||
}
|
||||
|
||||
/// Enable this connection to support higher-level HTTP upgrades.
|
||||
///
|
||||
/// See [the `upgrade` module](::upgrade) for more.
|
||||
pub fn with_upgrades(self) -> UpgradeableConnection<I, S>
|
||||
where
|
||||
I: Send,
|
||||
{
|
||||
UpgradeableConnection {
|
||||
inner: self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, B, S> Future for Connection<I, S>
|
||||
@@ -482,7 +500,15 @@ where
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
loop {
|
||||
match self.conn.poll() {
|
||||
Ok(x) => return Ok(x.map(|o| o.unwrap_or_else(|| ()))),
|
||||
Ok(x) => return Ok(x.map(|opt| {
|
||||
if let Some(proto::Dispatched::Upgrade(pending)) = opt {
|
||||
// With no `Send` bound on `I`, we can't try to do
|
||||
// upgrades here. In case a user was trying to use
|
||||
// `Body::on_upgrade` with this API, send a special
|
||||
// error letting them know about that.
|
||||
pending.manual();
|
||||
}
|
||||
})),
|
||||
Err(e) => {
|
||||
debug!("error polling connection protocol: {}", e);
|
||||
match *e.kind() {
|
||||
@@ -507,7 +533,6 @@ where
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
// ===== impl Serve =====
|
||||
|
||||
impl<I, S> Serve<I, S> {
|
||||
@@ -614,7 +639,7 @@ where
|
||||
let fut = connecting
|
||||
.map_err(::Error::new_user_new_service)
|
||||
// flatten basically
|
||||
.and_then(|conn| conn)
|
||||
.and_then(|conn| conn.with_upgrades())
|
||||
.map_err(|err| debug!("conn error: {}", err));
|
||||
self.serve.protocol.exec.execute(fut);
|
||||
} else {
|
||||
@@ -623,3 +648,82 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod upgrades {
|
||||
use super::*;
|
||||
|
||||
// A future binding a connection with a Service with Upgrade support.
|
||||
//
|
||||
// This type is unnameable outside the crate, and so basically just an
|
||||
// `impl Future`, without requiring Rust 1.26.
|
||||
#[must_use = "futures do nothing unless polled"]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct UpgradeableConnection<T, S>
|
||||
where
|
||||
S: Service,
|
||||
{
|
||||
pub(super) inner: Connection<T, S>,
|
||||
}
|
||||
|
||||
impl<I, B, S> UpgradeableConnection<I, S>
|
||||
where
|
||||
S: Service<ReqBody=Body, ResBody=B> + 'static,
|
||||
S::Error: Into<Box<::std::error::Error + Send + Sync>>,
|
||||
S::Future: Send,
|
||||
I: AsyncRead + AsyncWrite + Send + 'static,
|
||||
B: Payload + 'static,
|
||||
{
|
||||
/// Start a graceful shutdown process for this connection.
|
||||
///
|
||||
/// This `Connection` should continue to be polled until shutdown
|
||||
/// can finish.
|
||||
pub fn graceful_shutdown(&mut self) {
|
||||
self.inner.graceful_shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, B, S> Future for UpgradeableConnection<I, S>
|
||||
where
|
||||
S: Service<ReqBody=Body, ResBody=B> + 'static,
|
||||
S::Error: Into<Box<::std::error::Error + Send + Sync>>,
|
||||
S::Future: Send,
|
||||
I: AsyncRead + AsyncWrite + Send + 'static,
|
||||
B: Payload + 'static,
|
||||
{
|
||||
type Item = ();
|
||||
type Error = ::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
loop {
|
||||
match self.inner.conn.poll() {
|
||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
||||
Ok(Async::Ready(Some(proto::Dispatched::Shutdown))) |
|
||||
Ok(Async::Ready(None)) => {
|
||||
return Ok(Async::Ready(()));
|
||||
},
|
||||
Ok(Async::Ready(Some(proto::Dispatched::Upgrade(pending)))) => {
|
||||
let h1 = match mem::replace(&mut self.inner.conn, None) {
|
||||
Some(Either::A(h1)) => h1,
|
||||
_ => unreachable!("Upgrade expects h1"),
|
||||
};
|
||||
|
||||
let (io, buf, _) = h1.into_inner();
|
||||
pending.fulfill(Upgraded::new(Box::new(io), buf));
|
||||
return Ok(Async::Ready(()));
|
||||
},
|
||||
Err(e) => {
|
||||
debug!("error polling connection protocol: {}", e);
|
||||
match *e.kind() {
|
||||
Kind::Parse(Parse::VersionH2) if self.inner.fallback => {
|
||||
self.inner.upgrade_h2();
|
||||
continue;
|
||||
}
|
||||
_ => return Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,6 @@
|
||||
|
||||
pub mod conn;
|
||||
#[cfg(feature = "runtime")] mod tcp;
|
||||
mod rewind;
|
||||
|
||||
use std::fmt;
|
||||
#[cfg(feature = "runtime")] use std::net::SocketAddr;
|
||||
|
||||
@@ -1,208 +0,0 @@
|
||||
use bytes::{Buf, BufMut, Bytes, IntoBuf};
|
||||
use futures::{Async, Poll};
|
||||
use std::io::{self, Read, Write};
|
||||
use std::cmp;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Rewind<T> {
|
||||
pre: Option<Bytes>,
|
||||
inner: T,
|
||||
}
|
||||
|
||||
impl<T> Rewind<T> {
|
||||
pub(super) fn new(tcp: T) -> Rewind<T> {
|
||||
Rewind {
|
||||
pre: None,
|
||||
inner: tcp,
|
||||
}
|
||||
}
|
||||
pub fn rewind(&mut self, bs: Bytes) {
|
||||
debug_assert!(self.pre.is_none());
|
||||
self.pre = Some(bs);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Read for Rewind<T>
|
||||
where
|
||||
T: Read,
|
||||
{
|
||||
#[inline]
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
if let Some(pre_bs) = self.pre.take() {
|
||||
// If there are no remaining bytes, let the bytes get dropped.
|
||||
if pre_bs.len() > 0 {
|
||||
let mut pre_reader = pre_bs.into_buf().reader();
|
||||
let read_cnt = pre_reader.read(buf)?;
|
||||
|
||||
let mut new_pre = pre_reader.into_inner().into_inner();
|
||||
new_pre.advance(read_cnt);
|
||||
|
||||
// Put back whats left
|
||||
if new_pre.len() > 0 {
|
||||
self.pre = Some(new_pre);
|
||||
}
|
||||
|
||||
return Ok(read_cnt);
|
||||
}
|
||||
}
|
||||
self.inner.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Write for Rewind<T>
|
||||
where
|
||||
T: Write,
|
||||
{
|
||||
#[inline]
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
self.inner.write(buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.inner.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AsyncRead for Rewind<T>
|
||||
where
|
||||
T: AsyncRead,
|
||||
{
|
||||
#[inline]
|
||||
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
|
||||
self.inner.prepare_uninitialized_buffer(buf)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_buf<B: BufMut>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
|
||||
if let Some(bs) = self.pre.take() {
|
||||
let pre_len = bs.len();
|
||||
// If there are no remaining bytes, let the bytes get dropped.
|
||||
if pre_len > 0 {
|
||||
let cnt = cmp::min(buf.remaining_mut(), pre_len);
|
||||
let pre_buf = bs.into_buf();
|
||||
let mut xfer = Buf::take(pre_buf, cnt);
|
||||
buf.put(&mut xfer);
|
||||
|
||||
let mut new_pre = xfer.into_inner().into_inner();
|
||||
new_pre.advance(cnt);
|
||||
|
||||
// Put back whats left
|
||||
if new_pre.len() > 0 {
|
||||
self.pre = Some(new_pre);
|
||||
}
|
||||
|
||||
return Ok(Async::Ready(cnt));
|
||||
}
|
||||
}
|
||||
self.inner.read_buf(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AsyncWrite for Rewind<T>
|
||||
where
|
||||
T: AsyncWrite,
|
||||
{
|
||||
#[inline]
|
||||
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
||||
AsyncWrite::shutdown(&mut self.inner)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
|
||||
self.inner.write_buf(buf)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
extern crate tokio_mockstream;
|
||||
use self::tokio_mockstream::MockStream;
|
||||
use std::io::Cursor;
|
||||
|
||||
// Test a partial rewind
|
||||
#[test]
|
||||
fn async_partial_rewind() {
|
||||
let bs = &mut [104, 101, 108, 108, 111];
|
||||
let o1 = &mut [0, 0];
|
||||
let o2 = &mut [0, 0, 0, 0, 0];
|
||||
|
||||
let mut stream = Rewind::new(MockStream::new(bs));
|
||||
let mut o1_cursor = Cursor::new(o1);
|
||||
// Read off some bytes, ensure we filled o1
|
||||
match stream.read_buf(&mut o1_cursor).unwrap() {
|
||||
Async::NotReady => panic!("should be ready"),
|
||||
Async::Ready(cnt) => assert_eq!(2, cnt),
|
||||
}
|
||||
|
||||
// Rewind the stream so that it is as if we never read in the first place.
|
||||
let read_buf = Bytes::from(&o1_cursor.into_inner()[..]);
|
||||
stream.rewind(read_buf);
|
||||
|
||||
// We poll 2x here since the first time we'll only get what is in the
|
||||
// prefix (the rewinded part) of the Rewind.\
|
||||
let mut o2_cursor = Cursor::new(o2);
|
||||
stream.read_buf(&mut o2_cursor).unwrap();
|
||||
stream.read_buf(&mut o2_cursor).unwrap();
|
||||
let o2_final = o2_cursor.into_inner();
|
||||
|
||||
// At this point we should have read everything that was in the MockStream
|
||||
assert_eq!(&o2_final, &bs);
|
||||
}
|
||||
// Test a full rewind
|
||||
#[test]
|
||||
fn async_full_rewind() {
|
||||
let bs = &mut [104, 101, 108, 108, 111];
|
||||
let o1 = &mut [0, 0, 0, 0, 0];
|
||||
let o2 = &mut [0, 0, 0, 0, 0];
|
||||
|
||||
let mut stream = Rewind::new(MockStream::new(bs));
|
||||
let mut o1_cursor = Cursor::new(o1);
|
||||
match stream.read_buf(&mut o1_cursor).unwrap() {
|
||||
Async::NotReady => panic!("should be ready"),
|
||||
Async::Ready(cnt) => assert_eq!(5, cnt),
|
||||
}
|
||||
|
||||
let read_buf = Bytes::from(&o1_cursor.into_inner()[..]);
|
||||
stream.rewind(read_buf);
|
||||
|
||||
let mut o2_cursor = Cursor::new(o2);
|
||||
stream.read_buf(&mut o2_cursor).unwrap();
|
||||
stream.read_buf(&mut o2_cursor).unwrap();
|
||||
let o2_final = o2_cursor.into_inner();
|
||||
|
||||
assert_eq!(&o2_final, &bs);
|
||||
}
|
||||
#[test]
|
||||
fn partial_rewind() {
|
||||
let bs = &mut [104, 101, 108, 108, 111];
|
||||
let o1 = &mut [0, 0];
|
||||
let o2 = &mut [0, 0, 0, 0, 0];
|
||||
|
||||
let mut stream = Rewind::new(MockStream::new(bs));
|
||||
stream.read(o1).unwrap();
|
||||
|
||||
let read_buf = Bytes::from(&o1[..]);
|
||||
stream.rewind(read_buf);
|
||||
let cnt = stream.read(o2).unwrap();
|
||||
stream.read(&mut o2[cnt..]).unwrap();
|
||||
assert_eq!(&o2, &bs);
|
||||
}
|
||||
#[test]
|
||||
fn full_rewind() {
|
||||
let bs = &mut [104, 101, 108, 108, 111];
|
||||
let o1 = &mut [0, 0, 0, 0, 0];
|
||||
let o2 = &mut [0, 0, 0, 0, 0];
|
||||
|
||||
let mut stream = Rewind::new(MockStream::new(bs));
|
||||
stream.read(o1).unwrap();
|
||||
|
||||
let read_buf = Bytes::from(&o1[..]);
|
||||
stream.rewind(read_buf);
|
||||
let cnt = stream.read(o2).unwrap();
|
||||
stream.read(&mut o2[cnt..]).unwrap();
|
||||
assert_eq!(&o2, &bs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user