Update lib to std-future
This commit is contained in:
committed by
Sean McArthur
parent
782f1f712c
commit
c8fefd49f1
364
src/server.rs
364
src/server.rs
@@ -64,50 +64,45 @@
|
||||
//! will use the HTTP/2.0 protocol without prior negotiation.
|
||||
//!
|
||||
//! ```rust
|
||||
//! use futures::{Future, Stream};
|
||||
//! # use futures::future::ok;
|
||||
//! #![feature(async_await)]
|
||||
//! use futures::StreamExt;
|
||||
//! use h2::server;
|
||||
//! use http::{Response, StatusCode};
|
||||
//! use tokio::net::TcpListener;
|
||||
//!
|
||||
//! pub fn main () {
|
||||
//! #[tokio::main]
|
||||
//! pub async fn main () {
|
||||
//! let addr = "127.0.0.1:5928".parse().unwrap();
|
||||
//! let listener = TcpListener::bind(&addr,).unwrap();
|
||||
//!
|
||||
//! tokio::run({
|
||||
//! // Accept all incoming TCP connections.
|
||||
//! listener.incoming().for_each(move |socket| {
|
||||
//! // Spawn a new task to process each connection.
|
||||
//! tokio::spawn({
|
||||
//! // Start the HTTP/2.0 connection handshake
|
||||
//! server::handshake(socket)
|
||||
//! .and_then(|h2| {
|
||||
//! // Accept all inbound HTTP/2.0 streams sent over the
|
||||
//! // connection.
|
||||
//! h2.for_each(|(request, mut respond)| {
|
||||
//! println!("Received request: {:?}", request);
|
||||
//! // Accept all incoming TCP connections.
|
||||
//! let mut incoming = listener.incoming();
|
||||
//! # futures::future::select(Box::pin(async {
|
||||
//! while let Some(socket) = incoming.next().await {
|
||||
//! // Spawn a new task to process each connection.
|
||||
//! tokio::spawn(async {
|
||||
//! // Start the HTTP/2.0 connection handshake
|
||||
//! let mut h2 = server::handshake(socket.unwrap()).await.unwrap();
|
||||
//! // Accept all inbound HTTP/2.0 streams sent over the
|
||||
//! // connection.
|
||||
//! while let Some(request) = h2.next().await {
|
||||
//! let (request, mut respond) = request.unwrap();
|
||||
//! println!("Received request: {:?}", request);
|
||||
//!
|
||||
//! // Build a response with no body
|
||||
//! let response = Response::builder()
|
||||
//! .status(StatusCode::OK)
|
||||
//! .body(())
|
||||
//! .unwrap();
|
||||
//! // Build a response with no body
|
||||
//! let response = Response::builder()
|
||||
//! .status(StatusCode::OK)
|
||||
//! .body(())
|
||||
//! .unwrap();
|
||||
//!
|
||||
//! // Send the response back to the client
|
||||
//! respond.send_response(response, true)
|
||||
//! .unwrap();
|
||||
//! // Send the response back to the client
|
||||
//! respond.send_response(response, true)
|
||||
//! .unwrap();
|
||||
//! }
|
||||
//!
|
||||
//! Ok(())
|
||||
//! })
|
||||
//! })
|
||||
//! .map_err(|e| panic!("unexpected error = {:?}", e))
|
||||
//! });
|
||||
//!
|
||||
//! Ok(())
|
||||
//! })
|
||||
//! .map_err(|e| panic!("failed to run HTTP/2.0 server: {:?}", e))
|
||||
//! # .select(ok(())).map(|_|()).map_err(|_|())
|
||||
//! });
|
||||
//! });
|
||||
//! }
|
||||
//! # }), Box::pin(async {})).await;
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
@@ -124,17 +119,20 @@
|
||||
//! [`SendStream`]: ../struct.SendStream.html
|
||||
//! [`TcpListener`]: https://docs.rs/tokio-core/0.1/tokio_core/net/struct.TcpListener.html
|
||||
|
||||
use crate::{SendStream, RecvStream, ReleaseCapacity, PingPong};
|
||||
use crate::codec::{Codec, RecvError};
|
||||
use crate::frame::{self, Pseudo, Reason, Settings, StreamId};
|
||||
use crate::proto::{self, Config, Prioritized};
|
||||
use crate::{PingPong, RecvStream, ReleaseCapacity, SendStream};
|
||||
|
||||
use bytes::{Buf, Bytes, IntoBuf};
|
||||
use futures::{self, Async, Future, Poll, try_ready};
|
||||
use futures::ready;
|
||||
use http::{HeaderMap, Request, Response};
|
||||
use std::{convert, fmt, io, mem};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use std::time::Duration;
|
||||
use tokio_io::{AsyncRead, AsyncWrite, try_nb};
|
||||
use std::{convert, fmt, io, mem};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
/// In progress HTTP/2.0 connection handshake future.
|
||||
///
|
||||
@@ -155,7 +153,7 @@ pub struct Handshake<T, B: IntoBuf = Bytes> {
|
||||
/// The config to pass to Connection::new after handshake succeeds.
|
||||
builder: Builder,
|
||||
/// The current state of the handshake.
|
||||
state: Handshaking<T, B>
|
||||
state: Handshaking<T, B>,
|
||||
}
|
||||
|
||||
/// Accepts inbound HTTP/2.0 streams on a connection.
|
||||
@@ -179,21 +177,19 @@ pub struct Handshake<T, B: IntoBuf = Bytes> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use futures::{Future, Stream};
|
||||
/// #![feature(async_await)]
|
||||
/// # use futures::StreamExt;
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T) {
|
||||
/// server::handshake(my_io)
|
||||
/// .and_then(|server| {
|
||||
/// server.for_each(|(request, respond)| {
|
||||
/// // Process the request and send the response back to the client
|
||||
/// // using `respond`.
|
||||
/// # Ok(())
|
||||
/// })
|
||||
/// })
|
||||
/// # .wait().unwrap();
|
||||
/// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T) {
|
||||
/// let mut server = server::handshake(my_io).await.unwrap();
|
||||
/// while let Some(request) = server.next().await {
|
||||
/// let (request, respond) = request.unwrap();
|
||||
/// // Process the request and send the response back to the client
|
||||
/// // using `respond`.
|
||||
/// }
|
||||
/// # }
|
||||
/// #
|
||||
/// # pub fn main() {}
|
||||
@@ -224,7 +220,7 @@ pub struct Connection<T, B: IntoBuf> {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -318,26 +314,23 @@ const PREFACE: [u8; 24] = *b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(async_await)]
|
||||
/// # use tokio_io::*;
|
||||
/// # use futures::*;
|
||||
/// # use h2::server;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # {
|
||||
/// server::handshake(my_io)
|
||||
/// .and_then(|connection| {
|
||||
/// // The HTTP/2.0 handshake has completed, now use `connection` to
|
||||
/// // accept inbound HTTP/2.0 streams.
|
||||
/// # Ok(())
|
||||
/// })
|
||||
/// # .wait().unwrap();
|
||||
/// let connection = server::handshake(my_io).await.unwrap();
|
||||
/// // The HTTP/2.0 handshake has completed, now use `connection` to
|
||||
/// // accept inbound HTTP/2.0 streams.
|
||||
/// # }
|
||||
/// #
|
||||
/// # pub fn main() {}
|
||||
/// ```
|
||||
pub fn handshake<T>(io: T) -> Handshake<T, Bytes>
|
||||
where T: AsyncRead + AsyncWrite,
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
Builder::new().handshake(io)
|
||||
}
|
||||
@@ -346,8 +339,9 @@ where T: AsyncRead + AsyncWrite,
|
||||
|
||||
impl<T, B> Connection<T, B>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
B: IntoBuf,
|
||||
T: AsyncRead + AsyncWrite + Unpin,
|
||||
B: IntoBuf + Unpin,
|
||||
B::Buf: Unpin,
|
||||
{
|
||||
fn handshake2(io: T, builder: Builder) -> Handshake<T, B> {
|
||||
// Create the codec.
|
||||
@@ -407,11 +401,14 @@ where
|
||||
/// [`poll`]: struct.Connection.html#method.poll
|
||||
/// [`RecvStream`]: ../struct.RecvStream.html
|
||||
/// [`SendStream`]: ../struct.SendStream.html
|
||||
pub fn poll_close(&mut self) -> Poll<(), crate::Error> {
|
||||
self.connection.poll().map_err(Into::into)
|
||||
pub fn poll_close(&mut self, cx: &mut Context) -> Poll<Result<(), crate::Error>> {
|
||||
self.connection.poll(cx).map_err(Into::into)
|
||||
}
|
||||
|
||||
#[deprecated(note="use abrupt_shutdown or graceful_shutdown instead", since="0.1.4")]
|
||||
#[deprecated(
|
||||
note = "use abrupt_shutdown or graceful_shutdown instead",
|
||||
since = "0.1.4"
|
||||
)]
|
||||
#[doc(hidden)]
|
||||
pub fn close_connection(&mut self) {
|
||||
self.graceful_shutdown();
|
||||
@@ -453,31 +450,28 @@ where
|
||||
///
|
||||
/// This may only be called once. Calling multiple times will return `None`.
|
||||
pub fn ping_pong(&mut self) -> Option<PingPong> {
|
||||
self.connection
|
||||
.take_user_pings()
|
||||
.map(PingPong::new)
|
||||
self.connection.take_user_pings().map(PingPong::new)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B> futures::Stream for Connection<T, B>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
B: IntoBuf,
|
||||
B::Buf: 'static,
|
||||
T: AsyncRead + AsyncWrite + Unpin,
|
||||
B: IntoBuf + Unpin,
|
||||
B::Buf: Unpin + 'static,
|
||||
{
|
||||
type Item = (Request<RecvStream>, SendResponse<B>);
|
||||
type Error = crate::Error;
|
||||
type Item = Result<(Request<RecvStream>, SendResponse<B>), crate::Error>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, crate::Error> {
|
||||
// Always try to advance the internal state. Getting NotReady also is
|
||||
// needed to allow this function to return NotReady.
|
||||
match self.poll_close()? {
|
||||
Async::Ready(_) => {
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
// Always try to advance the internal state. Getting Pending also is
|
||||
// needed to allow this function to return Pending.
|
||||
match self.poll_close(cx)? {
|
||||
Poll::Ready(_) => {
|
||||
// If the socket is closed, don't return anything
|
||||
// TODO: drop any pending streams
|
||||
return Ok(None.into());
|
||||
},
|
||||
_ => {},
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if let Some(inner) = self.connection.next_incoming() {
|
||||
@@ -488,10 +482,10 @@ where
|
||||
let request = Request::from_parts(head, body);
|
||||
let respond = SendResponse { inner };
|
||||
|
||||
return Ok(Some((request, respond)).into());
|
||||
return Poll::Ready(Some(Ok((request, respond))));
|
||||
}
|
||||
|
||||
Ok(Async::NotReady)
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
|
||||
@@ -522,7 +516,7 @@ impl Builder {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -561,7 +555,7 @@ impl Builder {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -595,7 +589,7 @@ impl Builder {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -628,7 +622,7 @@ impl Builder {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -667,7 +661,7 @@ impl Builder {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -715,7 +709,7 @@ impl Builder {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -761,7 +755,7 @@ impl Builder {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -808,7 +802,7 @@ impl Builder {
|
||||
/// # use h2::server::*;
|
||||
/// # use std::time::Duration;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -850,7 +844,7 @@ impl Builder {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -870,7 +864,7 @@ impl Builder {
|
||||
/// # use tokio_io::*;
|
||||
/// # use h2::server::*;
|
||||
/// #
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite>(my_io: T)
|
||||
/// # fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
|
||||
/// # -> Handshake<T, &'static [u8]>
|
||||
/// # {
|
||||
/// // `server_fut` is a future representing the completion of the HTTP/2.0
|
||||
@@ -884,9 +878,9 @@ impl Builder {
|
||||
/// ```
|
||||
pub fn handshake<T, B>(&self, io: T) -> Handshake<T, B>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
B: IntoBuf,
|
||||
B::Buf: 'static,
|
||||
T: AsyncRead + AsyncWrite + Unpin,
|
||||
B: IntoBuf + Unpin,
|
||||
B::Buf: Unpin + 'static,
|
||||
{
|
||||
Connection::handshake2(io, self.clone())
|
||||
}
|
||||
@@ -949,7 +943,7 @@ impl<B: IntoBuf> SendResponse<B> {
|
||||
|
||||
/// Polls to be notified when the client resets this stream.
|
||||
///
|
||||
/// If stream is still open, this returns `Ok(Async::NotReady)`, and
|
||||
/// If stream is still open, this returns `Poll::Pending`, and
|
||||
/// registers the task to be notified if a `RST_STREAM` is received.
|
||||
///
|
||||
/// If a `RST_STREAM` frame is received for this stream, calling this
|
||||
@@ -959,8 +953,8 @@ impl<B: IntoBuf> SendResponse<B> {
|
||||
///
|
||||
/// Calling this method after having called `send_response` will return
|
||||
/// a user error.
|
||||
pub fn poll_reset(&mut self) -> Poll<Reason, crate::Error> {
|
||||
self.inner.poll_reset(proto::PollReset::AwaitingHeaders)
|
||||
pub fn poll_reset(&mut self, cx: &mut Context) -> Poll<Result<Reason, crate::Error>> {
|
||||
self.inner.poll_reset(cx, proto::PollReset::AwaitingHeaders)
|
||||
}
|
||||
|
||||
/// Returns the stream ID of the response stream.
|
||||
@@ -979,26 +973,23 @@ impl<B: IntoBuf> SendResponse<B> {
|
||||
|
||||
impl<T, B: Buf> Flush<T, B> {
|
||||
fn new(codec: Codec<T, B>) -> Self {
|
||||
Flush {
|
||||
codec: Some(codec),
|
||||
}
|
||||
Flush { codec: Some(codec) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B> Future for Flush<T, B>
|
||||
where
|
||||
T: AsyncWrite,
|
||||
B: Buf,
|
||||
T: AsyncWrite + Unpin,
|
||||
B: Buf + Unpin,
|
||||
{
|
||||
type Item = Codec<T, B>;
|
||||
type Error = crate::Error;
|
||||
type Output = Result<Codec<T, B>, crate::Error>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
// Flush the codec
|
||||
try_ready!(self.codec.as_mut().unwrap().flush());
|
||||
ready!(self.codec.as_mut().unwrap().flush(cx))?;
|
||||
|
||||
// Return the codec
|
||||
Ok(Async::Ready(self.codec.take().unwrap()))
|
||||
Poll::Ready(Ok(self.codec.take().unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1017,49 +1008,50 @@ impl<T, B: Buf> ReadPreface<T, B> {
|
||||
|
||||
impl<T, B> Future for ReadPreface<T, B>
|
||||
where
|
||||
T: AsyncRead,
|
||||
B: Buf,
|
||||
T: AsyncRead + Unpin,
|
||||
B: Buf + Unpin,
|
||||
{
|
||||
type Item = Codec<T, B>;
|
||||
type Error = crate::Error;
|
||||
type Output = Result<Codec<T, B>, crate::Error>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut buf = [0; 24];
|
||||
let mut rem = PREFACE.len() - self.pos;
|
||||
|
||||
while rem > 0 {
|
||||
let n = try_nb!(self.inner_mut().read(&mut buf[..rem]));
|
||||
let n = ready!(Pin::new(self.inner_mut()).poll_read(cx, &mut buf[..rem]))?;
|
||||
if n == 0 {
|
||||
return Err(io::Error::new(
|
||||
return Poll::Ready(Err(io::Error::new(
|
||||
io::ErrorKind::ConnectionReset,
|
||||
"connection closed unexpectedly",
|
||||
).into());
|
||||
)
|
||||
.into()));
|
||||
}
|
||||
|
||||
if PREFACE[self.pos..self.pos + n] != buf[..n] {
|
||||
proto_err!(conn: "read_preface: invalid preface");
|
||||
// TODO: Should this just write the GO_AWAY frame directly?
|
||||
return Err(Reason::PROTOCOL_ERROR.into());
|
||||
return Poll::Ready(Err(Reason::PROTOCOL_ERROR.into()));
|
||||
}
|
||||
|
||||
self.pos += n;
|
||||
rem -= n; // TODO test
|
||||
}
|
||||
|
||||
Ok(Async::Ready(self.codec.take().unwrap()))
|
||||
Poll::Ready(Ok(self.codec.take().unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
// ===== impl Handshake =====
|
||||
|
||||
impl<T, B: IntoBuf> Future for Handshake<T, B>
|
||||
where T: AsyncRead + AsyncWrite,
|
||||
B: IntoBuf,
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + Unpin,
|
||||
B: IntoBuf + Unpin,
|
||||
B::Buf: Unpin,
|
||||
{
|
||||
type Item = Connection<T, B>;
|
||||
type Error = crate::Error;
|
||||
type Output = Result<Connection<T, B>, crate::Error>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
log::trace!("Handshake::poll(); state={:?};", self.state);
|
||||
use crate::server::Handshaking::*;
|
||||
|
||||
@@ -1067,12 +1059,12 @@ impl<T, B: IntoBuf> Future for Handshake<T, B>
|
||||
// We're currently flushing a pending SETTINGS frame. Poll the
|
||||
// flush future, and, if it's completed, advance our state to wait
|
||||
// for the client preface.
|
||||
let codec = match flush.poll()? {
|
||||
Async::NotReady => {
|
||||
log::trace!("Handshake::poll(); flush.poll()=NotReady");
|
||||
return Ok(Async::NotReady);
|
||||
},
|
||||
Async::Ready(flushed) => {
|
||||
let codec = match Pin::new(flush).poll(cx)? {
|
||||
Poll::Pending => {
|
||||
log::trace!("Handshake::poll(); flush.poll()=Pending");
|
||||
return Poll::Pending;
|
||||
}
|
||||
Poll::Ready(flushed) => {
|
||||
log::trace!("Handshake::poll(); flush.poll()=Ready");
|
||||
flushed
|
||||
}
|
||||
@@ -1089,38 +1081,41 @@ impl<T, B: IntoBuf> Future for Handshake<T, B>
|
||||
// We're now waiting for the client preface. Poll the `ReadPreface`
|
||||
// future. If it has completed, we will create a `Connection` handle
|
||||
// for the connection.
|
||||
read.poll()
|
||||
// Actually creating the `Connection` has to occur outside of this
|
||||
// `if let` block, because we've borrowed `self` mutably in order
|
||||
// to poll the state and won't be able to borrow the SETTINGS frame
|
||||
// as well until we release the borrow for `poll()`.
|
||||
Pin::new(read).poll(cx)
|
||||
// Actually creating the `Connection` has to occur outside of this
|
||||
// `if let` block, because we've borrowed `self` mutably in order
|
||||
// to poll the state and won't be able to borrow the SETTINGS frame
|
||||
// as well until we release the borrow for `poll()`.
|
||||
} else {
|
||||
unreachable!("Handshake::poll() state was not advanced completely!")
|
||||
};
|
||||
let server = poll?.map(|codec| {
|
||||
let connection = proto::Connection::new(codec, Config {
|
||||
next_stream_id: 2.into(),
|
||||
// Server does not need to locally initiate any streams
|
||||
initial_max_send_streams: 0,
|
||||
reset_stream_duration: self.builder.reset_stream_duration,
|
||||
reset_stream_max: self.builder.reset_stream_max,
|
||||
settings: self.builder.settings.clone(),
|
||||
});
|
||||
poll?.map(|codec| {
|
||||
let connection = proto::Connection::new(
|
||||
codec,
|
||||
Config {
|
||||
next_stream_id: 2.into(),
|
||||
// Server does not need to locally initiate any streams
|
||||
initial_max_send_streams: 0,
|
||||
reset_stream_duration: self.builder.reset_stream_duration,
|
||||
reset_stream_max: self.builder.reset_stream_max,
|
||||
settings: self.builder.settings.clone(),
|
||||
},
|
||||
);
|
||||
|
||||
log::trace!("Handshake::poll(); connection established!");
|
||||
let mut c = Connection { connection };
|
||||
if let Some(sz) = self.builder.initial_target_connection_window_size {
|
||||
c.set_target_window_size(sz);
|
||||
}
|
||||
c
|
||||
});
|
||||
Ok(server)
|
||||
Ok(c)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B> fmt::Debug for Handshake<T, B>
|
||||
where T: AsyncRead + AsyncWrite + fmt::Debug,
|
||||
B: fmt::Debug + IntoBuf,
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + fmt::Debug,
|
||||
B: fmt::Debug + IntoBuf,
|
||||
{
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(fmt, "server::Handshake")
|
||||
@@ -1131,16 +1126,14 @@ impl Peer {
|
||||
pub fn convert_send_message(
|
||||
id: StreamId,
|
||||
response: Response<()>,
|
||||
end_of_stream: bool) -> frame::Headers
|
||||
{
|
||||
end_of_stream: bool,
|
||||
) -> frame::Headers {
|
||||
use http::response::Parts;
|
||||
|
||||
// Extract the components of the HTTP request
|
||||
let (
|
||||
Parts {
|
||||
status,
|
||||
headers,
|
||||
..
|
||||
status, headers, ..
|
||||
},
|
||||
_,
|
||||
) = response.into_parts();
|
||||
@@ -1172,7 +1165,9 @@ impl proto::Peer for Peer {
|
||||
}
|
||||
|
||||
fn convert_poll_message(
|
||||
pseudo: Pseudo, fields: HeaderMap, stream_id: StreamId
|
||||
pseudo: Pseudo,
|
||||
fields: HeaderMap,
|
||||
stream_id: StreamId,
|
||||
) -> Result<Self::Poll, RecvError> {
|
||||
use http::{uri, Version};
|
||||
|
||||
@@ -1205,23 +1200,29 @@ impl proto::Peer for Peer {
|
||||
// Convert the URI
|
||||
let mut parts = uri::Parts::default();
|
||||
|
||||
|
||||
// A request translated from HTTP/1 must not include the :authority
|
||||
// header
|
||||
if let Some(authority) = pseudo.authority {
|
||||
let maybe_authority = uri::Authority::from_shared(authority.clone().into_inner());
|
||||
parts.authority = Some(maybe_authority.or_else(|why| malformed!(
|
||||
"malformed headers: malformed authority ({:?}): {}", authority, why,
|
||||
))?);
|
||||
|
||||
parts.authority = Some(maybe_authority.or_else(|why| {
|
||||
malformed!(
|
||||
"malformed headers: malformed authority ({:?}): {}",
|
||||
authority,
|
||||
why,
|
||||
)
|
||||
})?);
|
||||
}
|
||||
|
||||
// A :scheme is always required.
|
||||
if let Some(scheme) = pseudo.scheme {
|
||||
let maybe_scheme = uri::Scheme::from_shared(scheme.clone().into_inner());
|
||||
let scheme = maybe_scheme.or_else(|why| malformed!(
|
||||
"malformed headers: malformed scheme ({:?}): {}", scheme, why,
|
||||
))?;
|
||||
let scheme = maybe_scheme.or_else(|why| {
|
||||
malformed!(
|
||||
"malformed headers: malformed scheme ({:?}): {}",
|
||||
scheme,
|
||||
why,
|
||||
)
|
||||
})?;
|
||||
|
||||
// It's not possible to build an `Uri` from a scheme and path. So,
|
||||
// after validating is was a valid scheme, we just have to drop it
|
||||
@@ -1240,9 +1241,9 @@ impl proto::Peer for Peer {
|
||||
}
|
||||
|
||||
let maybe_path = uri::PathAndQuery::from_shared(path.clone().into_inner());
|
||||
parts.path_and_query = Some(maybe_path.or_else(|why| malformed!(
|
||||
"malformed headers: malformed path ({:?}): {}", path, why,
|
||||
))?);
|
||||
parts.path_and_query = Some(maybe_path.or_else(|why| {
|
||||
malformed!("malformed headers: malformed path ({:?}): {}", path, why,)
|
||||
})?);
|
||||
}
|
||||
|
||||
b.uri(parts);
|
||||
@@ -1257,7 +1258,7 @@ impl proto::Peer for Peer {
|
||||
id: stream_id,
|
||||
reason: Reason::PROTOCOL_ERROR,
|
||||
});
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
*request.headers_mut() = fields;
|
||||
@@ -1270,18 +1271,15 @@ impl proto::Peer for Peer {
|
||||
|
||||
impl<T, B> fmt::Debug for Handshaking<T, B>
|
||||
where
|
||||
B: IntoBuf
|
||||
B: IntoBuf,
|
||||
{
|
||||
#[inline] fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
match *self {
|
||||
Handshaking::Flushing(_) =>
|
||||
write!(f, "Handshaking::Flushing(_)"),
|
||||
Handshaking::ReadingPreface(_) =>
|
||||
write!(f, "Handshaking::ReadingPreface(_)"),
|
||||
Handshaking::Empty =>
|
||||
write!(f, "Handshaking::Empty"),
|
||||
Handshaking::Flushing(_) => write!(f, "Handshaking::Flushing(_)"),
|
||||
Handshaking::ReadingPreface(_) => write!(f, "Handshaking::ReadingPreface(_)"),
|
||||
Handshaking::Empty => write!(f, "Handshaking::Empty"),
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1290,18 +1288,19 @@ where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
B: IntoBuf,
|
||||
{
|
||||
#[inline] fn from(flush: Flush<T, Prioritized<B::Buf>>) -> Self {
|
||||
#[inline]
|
||||
fn from(flush: Flush<T, Prioritized<B::Buf>>) -> Self {
|
||||
Handshaking::Flushing(flush)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B> convert::From<ReadPreface<T, Prioritized<B::Buf>>> for
|
||||
Handshaking<T, B>
|
||||
impl<T, B> convert::From<ReadPreface<T, Prioritized<B::Buf>>> for Handshaking<T, B>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
B: IntoBuf,
|
||||
{
|
||||
#[inline] fn from(read: ReadPreface<T, Prioritized<B::Buf>>) -> Self {
|
||||
#[inline]
|
||||
fn from(read: ReadPreface<T, Prioritized<B::Buf>>) -> Self {
|
||||
Handshaking::ReadingPreface(read)
|
||||
}
|
||||
}
|
||||
@@ -1311,7 +1310,8 @@ where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
B: IntoBuf,
|
||||
{
|
||||
#[inline] fn from(codec: Codec<T, Prioritized<B::Buf>>) -> Self {
|
||||
#[inline]
|
||||
fn from(codec: Codec<T, Prioritized<B::Buf>>) -> Self {
|
||||
Handshaking::from(Flush::new(codec))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user