| 1 |
use std::error::Error as StdError; |
| 2 |
#[cfg(feature = "runtime")] |
| 3 |
use std::time::Duration; |
| 4 |
|
| 5 |
use bytes::Bytes; |
| 6 |
use futures_channel::{mpsc, oneshot}; |
| 7 |
use futures_util::future::{self, Either, FutureExt as _, TryFutureExt as _}; |
| 8 |
use futures_util::stream::StreamExt as _; |
| 9 |
use h2::client::{Builder, SendRequest}; |
| 10 |
use h2::SendStream; |
| 11 |
use http::{Method, StatusCode}; |
| 12 |
use tokio::io::{AsyncRead, AsyncWrite}; |
| 13 |
use tracing::{debug, trace, warn}; |
| 14 |
|
| 15 |
use super::{ping, H2Upgraded, PipeToSendStream, SendBuf}; |
| 16 |
use crate::body::HttpBody; |
| 17 |
use crate::client::dispatch::Callback; |
| 18 |
use crate::common::{exec::Exec, task, Future, Never, Pin, Poll}; |
| 19 |
use crate::ext::Protocol; |
| 20 |
use crate::headers; |
| 21 |
use crate::proto::h2::UpgradedSendStream; |
| 22 |
use crate::proto::Dispatched; |
| 23 |
use crate::upgrade::Upgraded; |
| 24 |
use crate::{Body, Request, Response}; |
| 25 |
use h2::client::ResponseFuture; |
| 26 |
|
| 27 |
type ClientRx<B> = crate::client::dispatch::Receiver<Request<B>, Response<Body>>; |
| 28 |
|
| 29 |
///// An mpsc channel is used to help notify the `Connection` task when *all* |
| 30 |
///// other handles to it have been dropped, so that it can shutdown. |
| 31 |
type ConnDropRef = mpsc::Sender<Never>; |
| 32 |
|
| 33 |
///// A oneshot channel watches the `Connection` task, and when it completes, |
| 34 |
///// the "dispatch" task will be notified and can shutdown sooner. |
| 35 |
type ConnEof = oneshot::Receiver<Never>; |
| 36 |
|
| 37 |
// Our defaults are chosen for the "majority" case, which usually are not |
| 38 |
// resource constrained, and so the spec default of 64kb can be too limiting |
| 39 |
// for performance. |
| 40 |
const DEFAULT_CONN_WINDOW: u32 = 1024 * 1024 * 5; // 5mb |
| 41 |
const DEFAULT_STREAM_WINDOW: u32 = 1024 * 1024 * 2; // 2mb |
| 42 |
const DEFAULT_MAX_FRAME_SIZE: u32 = 1024 * 16; // 16kb |
| 43 |
const DEFAULT_MAX_SEND_BUF_SIZE: usize = 1024 * 1024; // 1mb |
| 44 |
|
| 45 |
#[derive(Clone, Debug)] |
| 46 |
pub(crate) struct Config { |
| 47 |
pub(crate) adaptive_window: bool, |
| 48 |
pub(crate) initial_conn_window_size: u32, |
| 49 |
pub(crate) initial_stream_window_size: u32, |
| 50 |
pub(crate) max_frame_size: u32, |
| 51 |
#[cfg(feature = "runtime")] |
| 52 |
pub(crate) keep_alive_interval: Option<Duration>, |
| 53 |
#[cfg(feature = "runtime")] |
| 54 |
pub(crate) keep_alive_timeout: Duration, |
| 55 |
#[cfg(feature = "runtime")] |
| 56 |
pub(crate) keep_alive_while_idle: bool, |
| 57 |
pub(crate) max_concurrent_reset_streams: Option<usize>, |
| 58 |
pub(crate) max_send_buffer_size: usize, |
| 59 |
} |
| 60 |
|
| 61 |
impl Default for Config { |
| 62 |
fn default() -> Config { |
| 63 |
Config { |
| 64 |
adaptive_window: false, |
| 65 |
initial_conn_window_size: DEFAULT_CONN_WINDOW, |
| 66 |
initial_stream_window_size: DEFAULT_STREAM_WINDOW, |
| 67 |
max_frame_size: DEFAULT_MAX_FRAME_SIZE, |
| 68 |
#[cfg(feature = "runtime")] |
| 69 |
keep_alive_interval: None, |
| 70 |
#[cfg(feature = "runtime")] |
| 71 |
keep_alive_timeout: Duration::from_secs(20), |
| 72 |
#[cfg(feature = "runtime")] |
| 73 |
keep_alive_while_idle: false, |
| 74 |
max_concurrent_reset_streams: None, |
| 75 |
max_send_buffer_size: DEFAULT_MAX_SEND_BUF_SIZE, |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
fn new_builder(config: &Config) -> Builder { |
| 81 |
let mut builder = Builder::default(); |
| 82 |
builder |
| 83 |
.initial_window_size(config.initial_stream_window_size) |
| 84 |
.initial_connection_window_size(config.initial_conn_window_size) |
| 85 |
.max_frame_size(config.max_frame_size) |
| 86 |
.max_send_buffer_size(config.max_send_buffer_size) |
| 87 |
.enable_push(false); |
| 88 |
if let Some(max) = config.max_concurrent_reset_streams { |
| 89 |
builder.max_concurrent_reset_streams(max); |
| 90 |
} |
| 91 |
builder |
| 92 |
} |
| 93 |
|
| 94 |
fn new_ping_config(config: &Config) -> ping::Config { |
| 95 |
ping::Config { |
| 96 |
bdp_initial_window: if config.adaptive_window { |
| 97 |
Some(config.initial_stream_window_size) |
| 98 |
} else { |
| 99 |
None |
| 100 |
}, |
| 101 |
#[cfg(feature = "runtime")] |
| 102 |
keep_alive_interval: config.keep_alive_interval, |
| 103 |
#[cfg(feature = "runtime")] |
| 104 |
keep_alive_timeout: config.keep_alive_timeout, |
| 105 |
#[cfg(feature = "runtime")] |
| 106 |
keep_alive_while_idle: config.keep_alive_while_idle, |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
pub(crate) async fn handshake<T, B>( |
| 111 |
io: T, |
| 112 |
req_rx: ClientRx<B>, |
| 113 |
config: &Config, |
| 114 |
exec: Exec, |
| 115 |
) -> crate::Result<ClientTask<B>> |
| 116 |
where |
| 117 |
T: AsyncRead + AsyncWrite + Send + Unpin + 'static, |
| 118 |
B: HttpBody, |
| 119 |
B::Data: Send + 'static, |
| 120 |
{ |
| 121 |
let (h2_tx, mut conn) = new_builder(config) |
| 122 |
.handshake::<_, SendBuf<B::Data>>(io) |
| 123 |
.await |
| 124 |
.map_err(crate::Error::new_h2)?; |
| 125 |
|
| 126 |
// An mpsc channel is used entirely to detect when the |
| 127 |
// 'Client' has been dropped. This is to get around a bug |
| 128 |
// in h2 where dropping all SendRequests won't notify a |
| 129 |
// parked Connection. |
| 130 |
let (conn_drop_ref, rx) = mpsc::channel(1); |
| 131 |
let (cancel_tx, conn_eof) = oneshot::channel(); |
| 132 |
|
| 133 |
let conn_drop_rx = rx.into_future().map(|(item, _rx)| { |
| 134 |
if let Some(never) = item { |
| 135 |
match never {} |
| 136 |
} |
| 137 |
}); |
| 138 |
|
| 139 |
let ping_config = new_ping_config(&config); |
| 140 |
|
| 141 |
let (conn, ping) = if ping_config.is_enabled() { |
| 142 |
let pp = conn.ping_pong().expect("conn.ping_pong"); |
| 143 |
let (recorder, mut ponger) = ping::channel(pp, ping_config); |
| 144 |
|
| 145 |
let conn = future::poll_fn(move |cx| { |
| 146 |
match ponger.poll(cx) { |
| 147 |
Poll::Ready(ping::Ponged::SizeUpdate(wnd)) => { |
| 148 |
conn.set_target_window_size(wnd); |
| 149 |
conn.set_initial_window_size(wnd)?; |
| 150 |
} |
| 151 |
#[cfg(feature = "runtime")] |
| 152 |
Poll::Ready(ping::Ponged::KeepAliveTimedOut) => { |
| 153 |
debug!("connection keep-alive timed out"); |
| 154 |
return Poll::Ready(Ok(())); |
| 155 |
} |
| 156 |
Poll::Pending => {} |
| 157 |
} |
| 158 |
|
| 159 |
Pin::new(&mut conn).poll(cx) |
| 160 |
}); |
| 161 |
(Either::Left(conn), recorder) |
| 162 |
} else { |
| 163 |
(Either::Right(conn), ping::disabled()) |
| 164 |
}; |
| 165 |
let conn = conn.map_err(|e| debug!("connection error: {}", e)); |
| 166 |
|
| 167 |
exec.execute(conn_task(conn, conn_drop_rx, cancel_tx)); |
| 168 |
|
| 169 |
Ok(ClientTask { |
| 170 |
ping, |
| 171 |
conn_drop_ref, |
| 172 |
conn_eof, |
| 173 |
executor: exec, |
| 174 |
h2_tx, |
| 175 |
req_rx, |
| 176 |
fut_ctx: None, |
| 177 |
}) |
| 178 |
} |
| 179 |
|
| 180 |
async fn conn_task<C, D>(conn: C, drop_rx: D, cancel_tx: oneshot::Sender<Never>) |
| 181 |
where |
| 182 |
C: Future + Unpin, |
| 183 |
D: Future<Output = ()> + Unpin, |
| 184 |
{ |
| 185 |
match future::select(conn, drop_rx).await { |
| 186 |
Either::Left(_) => { |
| 187 |
// ok or err, the `conn` has finished |
| 188 |
} |
| 189 |
Either::Right(((), conn)) => { |
| 190 |
// mpsc has been dropped, hopefully polling |
| 191 |
// the connection some more should start shutdown |
| 192 |
// and then close |
| 193 |
trace!("send_request dropped, starting conn shutdown"); |
| 194 |
drop(cancel_tx); |
| 195 |
let _ = conn.await; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
struct FutCtx<B> |
| 201 |
where |
| 202 |
B: HttpBody, |
| 203 |
{ |
| 204 |
is_connect: bool, |
| 205 |
eos: bool, |
| 206 |
fut: ResponseFuture, |
| 207 |
body_tx: SendStream<SendBuf<B::Data>>, |
| 208 |
body: B, |
| 209 |
cb: Callback<Request<B>, Response<Body>>, |
| 210 |
} |
| 211 |
|
| 212 |
impl<B: HttpBody> Unpin for FutCtx<B> {} |
| 213 |
|
| 214 |
pub(crate) struct ClientTask<B> |
| 215 |
where |
| 216 |
B: HttpBody, |
| 217 |
{ |
| 218 |
ping: ping::Recorder, |
| 219 |
conn_drop_ref: ConnDropRef, |
| 220 |
conn_eof: ConnEof, |
| 221 |
executor: Exec, |
| 222 |
h2_tx: SendRequest<SendBuf<B::Data>>, |
| 223 |
req_rx: ClientRx<B>, |
| 224 |
fut_ctx: Option<FutCtx<B>>, |
| 225 |
} |
| 226 |
|
| 227 |
impl<B> ClientTask<B> |
| 228 |
where |
| 229 |
B: HttpBody + 'static, |
| 230 |
{ |
| 231 |
pub(crate) fn is_extended_connect_protocol_enabled(&self) -> bool { |
| 232 |
self.h2_tx.is_extended_connect_protocol_enabled() |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
impl<B> ClientTask<B> |
| 237 |
where |
| 238 |
B: HttpBody + Send + 'static, |
| 239 |
B::Data: Send, |
| 240 |
B::Error: Into<Box<dyn StdError + Send + Sync>>, |
| 241 |
{ |
| 242 |
fn poll_pipe(&mut self, f: FutCtx<B>, cx: &mut task::Context<'_>) { |
| 243 |
let ping = self.ping.clone(); |
| 244 |
let send_stream = if !f.is_connect { |
| 245 |
if !f.eos { |
| 246 |
let mut pipe = Box::pin(PipeToSendStream::new(f.body, f.body_tx)).map(|res| { |
| 247 |
if let Err(e) = res { |
| 248 |
debug!("client request body error: {}", e); |
| 249 |
} |
| 250 |
}); |
| 251 |
|
| 252 |
// eagerly see if the body pipe is ready and |
| 253 |
// can thus skip allocating in the executor |
| 254 |
match Pin::new(&mut pipe).poll(cx) { |
| 255 |
Poll::Ready(_) => (), |
| 256 |
Poll::Pending => { |
| 257 |
let conn_drop_ref = self.conn_drop_ref.clone(); |
| 258 |
// keep the ping recorder's knowledge of an |
| 259 |
// "open stream" alive while this body is |
| 260 |
// still sending... |
| 261 |
let ping = ping.clone(); |
| 262 |
let pipe = pipe.map(move |x| { |
| 263 |
drop(conn_drop_ref); |
| 264 |
drop(ping); |
| 265 |
x |
| 266 |
}); |
| 267 |
// Clear send task |
| 268 |
self.executor.execute(pipe); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
None |
| 274 |
} else { |
| 275 |
Some(f.body_tx) |
| 276 |
}; |
| 277 |
|
| 278 |
let fut = f.fut.map(move |result| match result { |
| 279 |
Ok(res) => { |
| 280 |
// record that we got the response headers |
| 281 |
ping.record_non_data(); |
| 282 |
|
| 283 |
let content_length = headers::content_length_parse_all(res.headers()); |
| 284 |
if let (Some(mut send_stream), StatusCode::OK) = (send_stream, res.status()) { |
| 285 |
if content_length.map_or(false, |len| len != 0) { |
| 286 |
warn!("h2 connect response with non-zero body not supported"); |
| 287 |
|
| 288 |
send_stream.send_reset(h2::Reason::INTERNAL_ERROR); |
| 289 |
return Err(( |
| 290 |
crate::Error::new_h2(h2::Reason::INTERNAL_ERROR.into()), |
| 291 |
None, |
| 292 |
)); |
| 293 |
} |
| 294 |
let (parts, recv_stream) = res.into_parts(); |
| 295 |
let mut res = Response::from_parts(parts, Body::empty()); |
| 296 |
|
| 297 |
let (pending, on_upgrade) = crate::upgrade::pending(); |
| 298 |
let io = H2Upgraded { |
| 299 |
ping, |
| 300 |
send_stream: unsafe { UpgradedSendStream::new(send_stream) }, |
| 301 |
recv_stream, |
| 302 |
buf: Bytes::new(), |
| 303 |
}; |
| 304 |
let upgraded = Upgraded::new(io, Bytes::new()); |
| 305 |
|
| 306 |
pending.fulfill(upgraded); |
| 307 |
res.extensions_mut().insert(on_upgrade); |
| 308 |
|
| 309 |
Ok(res) |
| 310 |
} else { |
| 311 |
let res = res.map(|stream| { |
| 312 |
let ping = ping.for_stream(&stream); |
| 313 |
crate::Body::h2(stream, content_length.into(), ping) |
| 314 |
}); |
| 315 |
Ok(res) |
| 316 |
} |
| 317 |
} |
| 318 |
Err(err) => { |
| 319 |
ping.ensure_not_timed_out().map_err(|e| (e, None))?; |
| 320 |
|
| 321 |
debug!("client response error: {}", err); |
| 322 |
Err((crate::Error::new_h2(err), None)) |
| 323 |
} |
| 324 |
}); |
| 325 |
self.executor.execute(f.cb.send_when(fut)); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
impl<B> Future for ClientTask<B> |
| 330 |
where |
| 331 |
B: HttpBody + Send + 'static, |
| 332 |
B::Data: Send, |
| 333 |
B::Error: Into<Box<dyn StdError + Send + Sync>>, |
| 334 |
{ |
| 335 |
type Output = crate::Result<Dispatched>; |
| 336 |
|
| 337 |
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> { |
| 338 |
loop { |
| 339 |
match ready!(self.h2_tx.poll_ready(cx)) { |
| 340 |
Ok(()) => (), |
| 341 |
Err(err) => { |
| 342 |
self.ping.ensure_not_timed_out()?; |
| 343 |
return if err.reason() == Some(::h2::Reason::NO_ERROR) { |
| 344 |
trace!("connection gracefully shutdown"); |
| 345 |
Poll::Ready(Ok(Dispatched::Shutdown)) |
| 346 |
} else { |
| 347 |
Poll::Ready(Err(crate::Error::new_h2(err))) |
| 348 |
}; |
| 349 |
} |
| 350 |
}; |
| 351 |
|
| 352 |
match self.fut_ctx.take() { |
| 353 |
// If we were waiting on pending open |
| 354 |
// continue where we left off. |
| 355 |
Some(f) => { |
| 356 |
self.poll_pipe(f, cx); |
| 357 |
continue; |
| 358 |
} |
| 359 |
None => (), |
| 360 |
} |
| 361 |
|
| 362 |
match self.req_rx.poll_recv(cx) { |
| 363 |
Poll::Ready(Some((req, cb))) => { |
| 364 |
// check that future hasn't been canceled already |
| 365 |
if cb.is_canceled() { |
| 366 |
trace!("request callback is canceled"); |
| 367 |
continue; |
| 368 |
} |
| 369 |
let (head, body) = req.into_parts(); |
| 370 |
let mut req = ::http::Request::from_parts(head, ()); |
| 371 |
super::strip_connection_headers(req.headers_mut(), true); |
| 372 |
if let Some(len) = body.size_hint().exact() { |
| 373 |
if len != 0 || headers::method_has_defined_payload_semantics(req.method()) { |
| 374 |
headers::set_content_length_if_missing(req.headers_mut(), len); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
let is_connect = req.method() == Method::CONNECT; |
| 379 |
let eos = body.is_end_stream(); |
| 380 |
|
| 381 |
if is_connect { |
| 382 |
if headers::content_length_parse_all(req.headers()) |
| 383 |
.map_or(false, |len| len != 0) |
| 384 |
{ |
| 385 |
warn!("h2 connect request with non-zero body not supported"); |
| 386 |
cb.send(Err(( |
| 387 |
crate::Error::new_h2(h2::Reason::INTERNAL_ERROR.into()), |
| 388 |
None, |
| 389 |
))); |
| 390 |
continue; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
if let Some(protocol) = req.extensions_mut().remove::<Protocol>() { |
| 395 |
req.extensions_mut().insert(protocol.into_inner()); |
| 396 |
} |
| 397 |
|
| 398 |
let (fut, body_tx) = match self.h2_tx.send_request(req, !is_connect && eos) { |
| 399 |
Ok(ok) => ok, |
| 400 |
Err(err) => { |
| 401 |
debug!("client send request error: {}", err); |
| 402 |
cb.send(Err((crate::Error::new_h2(err), None))); |
| 403 |
continue; |
| 404 |
} |
| 405 |
}; |
| 406 |
|
| 407 |
let f = FutCtx { |
| 408 |
is_connect, |
| 409 |
eos, |
| 410 |
fut, |
| 411 |
body_tx, |
| 412 |
body, |
| 413 |
cb, |
| 414 |
}; |
| 415 |
|
| 416 |
// Check poll_ready() again. |
| 417 |
// If the call to send_request() resulted in the new stream being pending open |
| 418 |
// we have to wait for the open to complete before accepting new requests. |
| 419 |
match self.h2_tx.poll_ready(cx) { |
| 420 |
Poll::Pending => { |
| 421 |
// Save Context |
| 422 |
self.fut_ctx = Some(f); |
| 423 |
return Poll::Pending; |
| 424 |
} |
| 425 |
Poll::Ready(Ok(())) => (), |
| 426 |
Poll::Ready(Err(err)) => { |
| 427 |
f.cb.send(Err((crate::Error::new_h2(err), None))); |
| 428 |
continue; |
| 429 |
} |
| 430 |
} |
| 431 |
self.poll_pipe(f, cx); |
| 432 |
continue; |
| 433 |
} |
| 434 |
|
| 435 |
Poll::Ready(None) => { |
| 436 |
trace!("client::dispatch::Sender dropped"); |
| 437 |
return Poll::Ready(Ok(Dispatched::Shutdown)); |
| 438 |
} |
| 439 |
|
| 440 |
Poll::Pending => match ready!(Pin::new(&mut self.conn_eof).poll(cx)) { |
| 441 |
Ok(never) => match never {}, |
| 442 |
Err(_conn_is_eof) => { |
| 443 |
trace!("connection task is closed, closing dispatch task"); |
| 444 |
return Poll::Ready(Ok(Dispatched::Shutdown)); |
| 445 |
} |
| 446 |
}, |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
} |