feat(http2): Implement Client-side CONNECT support over HTTP/2 (#2523)
Closes #2508
This commit is contained in:
@@ -2,17 +2,21 @@ use std::error::Error as StdError;
|
||||
#[cfg(feature = "runtime")]
|
||||
use std::time::Duration;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures_channel::{mpsc, oneshot};
|
||||
use futures_util::future::{self, Either, FutureExt as _, TryFutureExt as _};
|
||||
use futures_util::stream::StreamExt as _;
|
||||
use h2::client::{Builder, SendRequest};
|
||||
use http::{Method, StatusCode};
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
use super::{decode_content_length, ping, PipeToSendStream, SendBuf};
|
||||
use super::{ping, H2Upgraded, PipeToSendStream, SendBuf};
|
||||
use crate::body::HttpBody;
|
||||
use crate::common::{exec::Exec, task, Future, Never, Pin, Poll};
|
||||
use crate::headers;
|
||||
use crate::proto::h2::UpgradedSendStream;
|
||||
use crate::proto::Dispatched;
|
||||
use crate::upgrade::Upgraded;
|
||||
use crate::{Body, Request, Response};
|
||||
|
||||
type ClientRx<B> = crate::client::dispatch::Receiver<Request<B>, Response<Body>>;
|
||||
@@ -233,8 +237,25 @@ where
|
||||
headers::set_content_length_if_missing(req.headers_mut(), len);
|
||||
}
|
||||
}
|
||||
|
||||
let is_connect = req.method() == Method::CONNECT;
|
||||
let eos = body.is_end_stream();
|
||||
let (fut, body_tx) = match self.h2_tx.send_request(req, eos) {
|
||||
let ping = self.ping.clone();
|
||||
|
||||
if is_connect {
|
||||
if headers::content_length_parse_all(req.headers())
|
||||
.map_or(false, |len| len != 0)
|
||||
{
|
||||
warn!("h2 connect request with non-zero body not supported");
|
||||
cb.send(Err((
|
||||
crate::Error::new_h2(h2::Reason::INTERNAL_ERROR.into()),
|
||||
None,
|
||||
)));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let (fut, body_tx) = match self.h2_tx.send_request(req, !is_connect && eos) {
|
||||
Ok(ok) => ok,
|
||||
Err(err) => {
|
||||
debug!("client send request error: {}", err);
|
||||
@@ -243,45 +264,81 @@ where
|
||||
}
|
||||
};
|
||||
|
||||
let ping = self.ping.clone();
|
||||
if !eos {
|
||||
let mut pipe = Box::pin(PipeToSendStream::new(body, body_tx)).map(|res| {
|
||||
if let Err(e) = res {
|
||||
debug!("client request body error: {}", e);
|
||||
}
|
||||
});
|
||||
|
||||
// eagerly see if the body pipe is ready and
|
||||
// can thus skip allocating in the executor
|
||||
match Pin::new(&mut pipe).poll(cx) {
|
||||
Poll::Ready(_) => (),
|
||||
Poll::Pending => {
|
||||
let conn_drop_ref = self.conn_drop_ref.clone();
|
||||
// keep the ping recorder's knowledge of an
|
||||
// "open stream" alive while this body is
|
||||
// still sending...
|
||||
let ping = ping.clone();
|
||||
let pipe = pipe.map(move |x| {
|
||||
drop(conn_drop_ref);
|
||||
drop(ping);
|
||||
x
|
||||
let send_stream = if !is_connect {
|
||||
if !eos {
|
||||
let mut pipe =
|
||||
Box::pin(PipeToSendStream::new(body, body_tx)).map(|res| {
|
||||
if let Err(e) = res {
|
||||
debug!("client request body error: {}", e);
|
||||
}
|
||||
});
|
||||
self.executor.execute(pipe);
|
||||
|
||||
// eagerly see if the body pipe is ready and
|
||||
// can thus skip allocating in the executor
|
||||
match Pin::new(&mut pipe).poll(cx) {
|
||||
Poll::Ready(_) => (),
|
||||
Poll::Pending => {
|
||||
let conn_drop_ref = self.conn_drop_ref.clone();
|
||||
// keep the ping recorder's knowledge of an
|
||||
// "open stream" alive while this body is
|
||||
// still sending...
|
||||
let ping = ping.clone();
|
||||
let pipe = pipe.map(move |x| {
|
||||
drop(conn_drop_ref);
|
||||
drop(ping);
|
||||
x
|
||||
});
|
||||
self.executor.execute(pipe);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
} else {
|
||||
Some(body_tx)
|
||||
};
|
||||
|
||||
let fut = fut.map(move |result| match result {
|
||||
Ok(res) => {
|
||||
// record that we got the response headers
|
||||
ping.record_non_data();
|
||||
|
||||
let content_length = decode_content_length(res.headers());
|
||||
let res = res.map(|stream| {
|
||||
let ping = ping.for_stream(&stream);
|
||||
crate::Body::h2(stream, content_length, ping)
|
||||
});
|
||||
Ok(res)
|
||||
let content_length = headers::content_length_parse_all(res.headers());
|
||||
if let (Some(mut send_stream), StatusCode::OK) =
|
||||
(send_stream, res.status())
|
||||
{
|
||||
if content_length.map_or(false, |len| len != 0) {
|
||||
warn!("h2 connect response with non-zero body not supported");
|
||||
|
||||
send_stream.send_reset(h2::Reason::INTERNAL_ERROR);
|
||||
return Err((
|
||||
crate::Error::new_h2(h2::Reason::INTERNAL_ERROR.into()),
|
||||
None,
|
||||
));
|
||||
}
|
||||
let (parts, recv_stream) = res.into_parts();
|
||||
let mut res = Response::from_parts(parts, Body::empty());
|
||||
|
||||
let (pending, on_upgrade) = crate::upgrade::pending();
|
||||
let io = H2Upgraded {
|
||||
ping,
|
||||
send_stream: unsafe { UpgradedSendStream::new(send_stream) },
|
||||
recv_stream,
|
||||
buf: Bytes::new(),
|
||||
};
|
||||
let upgraded = Upgraded::new(io, Bytes::new());
|
||||
|
||||
pending.fulfill(upgraded);
|
||||
res.extensions_mut().insert(on_upgrade);
|
||||
|
||||
Ok(res)
|
||||
} else {
|
||||
let res = res.map(|stream| {
|
||||
let ping = ping.for_stream(&stream);
|
||||
crate::Body::h2(stream, content_length.into(), ping)
|
||||
});
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
ping.ensure_not_timed_out().map_err(|e| (e, None))?;
|
||||
|
||||
Reference in New Issue
Block a user