|
| 1 | +//! HTTP/1 client connections |
| 2 | +
|
| 3 | +use std::error::Error as StdError; |
| 4 | +use std::fmt; |
| 5 | +use std::sync::Arc; |
| 6 | + |
| 7 | +use http::{Request, Response}; |
| 8 | +use httparse::ParserConfig; |
| 9 | +use tokio::io::{AsyncRead, AsyncWrite}; |
| 10 | + |
| 11 | +use crate::Body; |
| 12 | +use crate::body::HttpBody; |
| 13 | +use crate::common::{ |
| 14 | + exec::{BoxSendFuture, Exec}, |
| 15 | + task, Future, Pin, Poll, |
| 16 | +}; |
| 17 | +use crate::upgrade::Upgraded; |
| 18 | +use crate::proto; |
| 19 | +use crate::rt::Executor; |
| 20 | +use super::super::dispatch; |
| 21 | + |
| 22 | +type Dispatcher<T, B> = |
| 23 | + proto::dispatch::Dispatcher<proto::dispatch::Client<B>, B, T, proto::h1::ClientTransaction>; |
| 24 | + |
| 25 | +/// The sender side of an established connection. |
| 26 | +pub struct SendRequest<B> { |
| 27 | + dispatch: dispatch::Sender<Request<B>, Response<Body>>, |
| 28 | +} |
| 29 | + |
| 30 | +/// A future that processes all HTTP state for the IO object. |
| 31 | +/// |
| 32 | +/// In most cases, this should just be spawned into an executor, so that it |
| 33 | +/// can process incoming and outgoing messages, notice hangups, and the like. |
| 34 | +#[must_use = "futures do nothing unless polled"] |
| 35 | +pub struct Connection<T, B> |
| 36 | +where |
| 37 | + T: AsyncRead + AsyncWrite + Send + 'static, |
| 38 | + B: HttpBody + 'static, |
| 39 | +{ |
| 40 | + inner: Option<Dispatcher<T, B>>, |
| 41 | +} |
| 42 | + |
| 43 | +/// A builder to configure an HTTP connection. |
| 44 | +/// |
| 45 | +/// After setting options, the builder is used to create a handshake future. |
| 46 | +#[derive(Clone, Debug)] |
| 47 | +pub struct Builder { |
| 48 | + pub(super) exec: Exec, |
| 49 | + h09_responses: bool, |
| 50 | + h1_parser_config: ParserConfig, |
| 51 | + h1_writev: Option<bool>, |
| 52 | + h1_title_case_headers: bool, |
| 53 | + h1_preserve_header_case: bool, |
| 54 | + #[cfg(feature = "ffi")] |
| 55 | + h1_preserve_header_order: bool, |
| 56 | + h1_read_buf_exact_size: Option<usize>, |
| 57 | + h1_max_buf_size: Option<usize>, |
| 58 | +} |
| 59 | + |
| 60 | +/// Returns a handshake future over some IO. |
| 61 | +/// |
| 62 | +/// This is a shortcut for `Builder::new().handshake(io)`. |
| 63 | +/// See [`client::conn`](crate::client::conn) for more. |
| 64 | +pub async fn handshake<T>( |
| 65 | + io: T, |
| 66 | +) -> crate::Result<(SendRequest<crate::Body>, Connection<T, crate::Body>)> |
| 67 | +where |
| 68 | + T: AsyncRead + AsyncWrite + Unpin + Send + 'static, |
| 69 | +{ |
| 70 | + Builder::new().handshake(io).await |
| 71 | +} |
| 72 | + |
| 73 | +// ===== impl SendRequest |
| 74 | + |
| 75 | +impl<B> SendRequest<B> { |
| 76 | + /// Polls to determine whether this sender can be used yet for a request. |
| 77 | + /// |
| 78 | + /// If the associated connection is closed, this returns an Error. |
| 79 | + pub fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<crate::Result<()>> { |
| 80 | + self.dispatch.poll_ready(cx) |
| 81 | + } |
| 82 | + |
| 83 | + /* |
| 84 | + pub(super) async fn when_ready(self) -> crate::Result<Self> { |
| 85 | + let mut me = Some(self); |
| 86 | + future::poll_fn(move |cx| { |
| 87 | + ready!(me.as_mut().unwrap().poll_ready(cx))?; |
| 88 | + Poll::Ready(Ok(me.take().unwrap())) |
| 89 | + }) |
| 90 | + .await |
| 91 | + } |
| 92 | +
|
| 93 | + pub(super) fn is_ready(&self) -> bool { |
| 94 | + self.dispatch.is_ready() |
| 95 | + } |
| 96 | +
|
| 97 | + pub(super) fn is_closed(&self) -> bool { |
| 98 | + self.dispatch.is_closed() |
| 99 | + } |
| 100 | + */ |
| 101 | +} |
| 102 | + |
| 103 | +impl<B> SendRequest<B> |
| 104 | +where |
| 105 | + B: HttpBody + 'static, |
| 106 | +{ |
| 107 | + /// Sends a `Request` on the associated connection. |
| 108 | + /// |
| 109 | + /// Returns a future that if successful, yields the `Response`. |
| 110 | + /// |
| 111 | + /// # Note |
| 112 | + /// |
| 113 | + /// There are some key differences in what automatic things the `Client` |
| 114 | + /// does for you that will not be done here: |
| 115 | + /// |
| 116 | + /// - `Client` requires absolute-form `Uri`s, since the scheme and |
| 117 | + /// authority are needed to connect. They aren't required here. |
| 118 | + /// - Since the `Client` requires absolute-form `Uri`s, it can add |
| 119 | + /// the `Host` header based on it. You must add a `Host` header yourself |
| 120 | + /// before calling this method. |
| 121 | + /// - Since absolute-form `Uri`s are not required, if received, they will |
| 122 | + /// be serialized as-is. |
| 123 | + /// |
| 124 | + /// # Example |
| 125 | + /// |
| 126 | + /// ``` |
| 127 | + /// # use http::header::HOST; |
| 128 | + /// # use hyper::client::conn::SendRequest; |
| 129 | + /// # use hyper::Body; |
| 130 | + /// use hyper::Request; |
| 131 | + /// |
| 132 | + /// # async fn doc(mut tx: SendRequest<Body>) -> hyper::Result<()> { |
| 133 | + /// // build a Request |
| 134 | + /// let req = Request::builder() |
| 135 | + /// .uri("/foo/bar") |
| 136 | + /// .header(HOST, "hyper.rs") |
| 137 | + /// .body(Body::empty()) |
| 138 | + /// .unwrap(); |
| 139 | + /// |
| 140 | + /// // send it and await a Response |
| 141 | + /// let res = tx.send_request(req).await?; |
| 142 | + /// // assert the Response |
| 143 | + /// assert!(res.status().is_success()); |
| 144 | + /// # Ok(()) |
| 145 | + /// # } |
| 146 | + /// # fn main() {} |
| 147 | + /// ``` |
| 148 | + pub fn send_request(&mut self, req: Request<B>) -> impl Future<Output = crate::Result<Response<Body>>> { |
| 149 | + let sent = self.dispatch.send(req); |
| 150 | + |
| 151 | + async move { |
| 152 | + match sent { |
| 153 | + Ok(rx) => match rx.await { |
| 154 | + Ok(Ok(resp)) => Ok(resp), |
| 155 | + Ok(Err(err)) => Err(err), |
| 156 | + // this is definite bug if it happens, but it shouldn't happen! |
| 157 | + Err(_canceled) => panic!("dispatch dropped without returning error"), |
| 158 | + } |
| 159 | + Err(_req) => { |
| 160 | + tracing::debug!("connection was not ready"); |
| 161 | + |
| 162 | + Err(crate::Error::new_canceled().with("connection was not ready")) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /* |
| 169 | + pub(super) fn send_request_retryable( |
| 170 | + &mut self, |
| 171 | + req: Request<B>, |
| 172 | + ) -> impl Future<Output = Result<Response<Body>, (crate::Error, Option<Request<B>>)>> + Unpin |
| 173 | + where |
| 174 | + B: Send, |
| 175 | + { |
| 176 | + match self.dispatch.try_send(req) { |
| 177 | + Ok(rx) => { |
| 178 | + Either::Left(rx.then(move |res| { |
| 179 | + match res { |
| 180 | + Ok(Ok(res)) => future::ok(res), |
| 181 | + Ok(Err(err)) => future::err(err), |
| 182 | + // this is definite bug if it happens, but it shouldn't happen! |
| 183 | + Err(_) => panic!("dispatch dropped without returning error"), |
| 184 | + } |
| 185 | + })) |
| 186 | + } |
| 187 | + Err(req) => { |
| 188 | + tracing::debug!("connection was not ready"); |
| 189 | + let err = crate::Error::new_canceled().with("connection was not ready"); |
| 190 | + Either::Right(future::err((err, Some(req)))) |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + */ |
| 195 | +} |
| 196 | + |
| 197 | +impl<B> fmt::Debug for SendRequest<B> { |
| 198 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 199 | + f.debug_struct("SendRequest").finish() |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +// ===== impl Connection |
| 204 | + |
| 205 | +impl<T, B> fmt::Debug for Connection<T, B> |
| 206 | +where |
| 207 | + T: AsyncRead + AsyncWrite + fmt::Debug + Send + 'static, |
| 208 | + B: HttpBody + 'static, |
| 209 | +{ |
| 210 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 211 | + f.debug_struct("Connection").finish() |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +impl<T, B> Future for Connection<T, B> |
| 216 | +where |
| 217 | + T: AsyncRead + AsyncWrite + Unpin + Send + 'static, |
| 218 | + B: HttpBody + Send + 'static, |
| 219 | + B::Data: Send, |
| 220 | + B::Error: Into<Box<dyn StdError + Send + Sync>>, |
| 221 | +{ |
| 222 | + type Output = crate::Result<()>; |
| 223 | + |
| 224 | + fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> { |
| 225 | + match ready!(Pin::new(self.inner.as_mut().unwrap()).poll(cx))? { |
| 226 | + proto::Dispatched::Shutdown => Poll::Ready(Ok(())), |
| 227 | + proto::Dispatched::Upgrade(pending) => match self.inner.take() { |
| 228 | + Some(h1) => { |
| 229 | + let (io, buf, _) = h1.into_inner(); |
| 230 | + pending.fulfill(Upgraded::new(io, buf)); |
| 231 | + Poll::Ready(Ok(())) |
| 232 | + } |
| 233 | + _ => { |
| 234 | + drop(pending); |
| 235 | + unreachable!("Upgraded twice"); |
| 236 | + } |
| 237 | + }, |
| 238 | + } |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +// ===== impl Builder |
| 243 | + |
| 244 | +impl Builder { |
| 245 | + /// Creates a new connection builder. |
| 246 | + #[inline] |
| 247 | + pub fn new() -> Builder { |
| 248 | + Builder { |
| 249 | + exec: Exec::Default, |
| 250 | + h09_responses: false, |
| 251 | + h1_writev: None, |
| 252 | + h1_read_buf_exact_size: None, |
| 253 | + h1_parser_config: Default::default(), |
| 254 | + h1_title_case_headers: false, |
| 255 | + h1_preserve_header_case: false, |
| 256 | + #[cfg(feature = "ffi")] |
| 257 | + h1_preserve_header_order: false, |
| 258 | + h1_max_buf_size: None, |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + /// Provide an executor to execute background HTTP2 tasks. |
| 263 | + pub fn executor<E>(&mut self, exec: E) -> &mut Builder |
| 264 | + where |
| 265 | + E: Executor<BoxSendFuture> + Send + Sync + 'static, |
| 266 | + { |
| 267 | + self.exec = Exec::Executor(Arc::new(exec)); |
| 268 | + self |
| 269 | + } |
| 270 | + |
| 271 | + /// Set whether HTTP/0.9 responses should be tolerated. |
| 272 | + /// |
| 273 | + /// Default is false. |
| 274 | + pub fn http09_responses(&mut self, enabled: bool) -> &mut Builder { |
| 275 | + self.h09_responses = enabled; |
| 276 | + self |
| 277 | + } |
| 278 | + |
| 279 | + /// Set whether HTTP/1 connections will accept spaces between header names |
| 280 | + /// and the colon that follow them in responses. |
| 281 | + /// |
| 282 | + /// You probably don't need this, here is what [RFC 7230 Section 3.2.4.] has |
| 283 | + /// to say about it: |
| 284 | + /// |
| 285 | + /// > No whitespace is allowed between the header field-name and colon. In |
| 286 | + /// > the past, differences in the handling of such whitespace have led to |
| 287 | + /// > security vulnerabilities in request routing and response handling. A |
| 288 | + /// > server MUST reject any received request message that contains |
| 289 | + /// > whitespace between a header field-name and colon with a response code |
| 290 | + /// > of 400 (Bad Request). A proxy MUST remove any such whitespace from a |
| 291 | + /// > response message before forwarding the message downstream. |
| 292 | + /// |
| 293 | + /// Note that this setting does not affect HTTP/2. |
| 294 | + /// |
| 295 | + /// Default is false. |
| 296 | + /// |
| 297 | + /// [RFC 7230 Section 3.2.4.]: https://tools.ietf.org/html/rfc7230#section-3.2.4 |
| 298 | + pub fn http1_allow_spaces_after_header_name_in_responses( |
| 299 | + &mut self, |
| 300 | + enabled: bool, |
| 301 | + ) -> &mut Builder { |
| 302 | + self.h1_parser_config |
| 303 | + .allow_spaces_after_header_name_in_responses(enabled); |
| 304 | + self |
| 305 | + } |
| 306 | + |
| 307 | + /// Set whether HTTP/1 connections will accept obsolete line folding for |
| 308 | + /// header values. |
| 309 | + /// |
| 310 | + /// Newline codepoints (`\r` and `\n`) will be transformed to spaces when |
| 311 | + /// parsing. |
| 312 | + /// |
| 313 | + /// You probably don't need this, here is what [RFC 7230 Section 3.2.4.] has |
| 314 | + /// to say about it: |
| 315 | + /// |
| 316 | + /// > A server that receives an obs-fold in a request message that is not |
| 317 | + /// > within a message/http container MUST either reject the message by |
| 318 | + /// > sending a 400 (Bad Request), preferably with a representation |
| 319 | + /// > explaining that obsolete line folding is unacceptable, or replace |
| 320 | + /// > each received obs-fold with one or more SP octets prior to |
| 321 | + /// > interpreting the field value or forwarding the message downstream. |
| 322 | + /// |
| 323 | + /// > A proxy or gateway that receives an obs-fold in a response message |
| 324 | + /// > that is not within a message/http container MUST either discard the |
| 325 | + /// > message and replace it with a 502 (Bad Gateway) response, preferably |
| 326 | + /// > with a representation explaining that unacceptable line folding was |
| 327 | + /// > received, or replace each received obs-fold with one or more SP |
| 328 | + /// > octets prior to interpreting the field value or forwarding the |
| 329 | + /// > message downstream. |
| 330 | + /// |
| 331 | + /// > A user agent that receives an obs-fold in a response message that is |
| 332 | + /// > not within a message/http container MUST replace each received |
| 333 | + /// > obs-fold with one or more SP octets prior to interpreting the field |
| 334 | + /// > value. |
| 335 | + /// |
| 336 | + /// Note that this setting does not affect HTTP/2. |
| 337 | + /// |
| 338 | + /// Default is false. |
| 339 | + /// |
| 340 | + /// [RFC 7230 Section 3.2.4.]: https://tools.ietf.org/html/rfc7230#section-3.2.4 |
| 341 | + pub fn http1_allow_obsolete_multiline_headers_in_responses( |
| 342 | + &mut self, |
| 343 | + enabled: bool, |
| 344 | + ) -> &mut Builder { |
| 345 | + self.h1_parser_config |
| 346 | + .allow_obsolete_multiline_headers_in_responses(enabled); |
| 347 | + self |
| 348 | + } |
| 349 | + |
| 350 | + /// Set whether HTTP/1 connections should try to use vectored writes, |
| 351 | + /// or always flatten into a single buffer. |
| 352 | + /// |
| 353 | + /// Note that setting this to false may mean more copies of body data, |
| 354 | + /// but may also improve performance when an IO transport doesn't |
| 355 | + /// support vectored writes well, such as most TLS implementations. |
| 356 | + /// |
| 357 | + /// Setting this to true will force hyper to use queued strategy |
| 358 | + /// which may eliminate unnecessary cloning on some TLS backends |
| 359 | + /// |
| 360 | + /// Default is `auto`. In this mode hyper will try to guess which |
| 361 | + /// mode to use |
| 362 | + pub fn http1_writev(&mut self, enabled: bool) -> &mut Builder { |
| 363 | + self.h1_writev = Some(enabled); |
| 364 | + self |
| 365 | + } |
| 366 | + |
| 367 | + /// Set whether HTTP/1 connections will write header names as title case at |
| 368 | + /// the socket level. |
| 369 | + /// |
| 370 | + /// Note that this setting does not affect HTTP/2. |
| 371 | + /// |
| 372 | + /// Default is false. |
| 373 | + pub fn http1_title_case_headers(&mut self, enabled: bool) -> &mut Builder { |
| 374 | + self.h1_title_case_headers = enabled; |
| 375 | + self |
| 376 | + } |
| 377 | + |
| 378 | + /// Set whether to support preserving original header cases. |
| 379 | + /// |
| 380 | + /// Currently, this will record the original cases received, and store them |
| 381 | + /// in a private extension on the `Response`. It will also look for and use |
| 382 | + /// such an extension in any provided `Request`. |
| 383 | + /// |
| 384 | + /// Since the relevant extension is still private, there is no way to |
| 385 | + /// interact with the original cases. The only effect this can have now is |
| 386 | + /// to forward the cases in a proxy-like fashion. |
| 387 | + /// |
| 388 | + /// Note that this setting does not affect HTTP/2. |
| 389 | + /// |
| 390 | + /// Default is false. |
| 391 | + pub fn http1_preserve_header_case(&mut self, enabled: bool) -> &mut Builder { |
| 392 | + self.h1_preserve_header_case = enabled; |
| 393 | + self |
| 394 | + } |
| 395 | + |
| 396 | + /// Set whether to support preserving original header order. |
| 397 | + /// |
| 398 | + /// Currently, this will record the order in which headers are received, and store this |
| 399 | + /// ordering in a private extension on the `Response`. It will also look for and use |
| 400 | + /// such an extension in any provided `Request`. |
| 401 | + /// |
| 402 | + /// Note that this setting does not affect HTTP/2. |
| 403 | + /// |
| 404 | + /// Default is false. |
| 405 | + #[cfg(feature = "ffi")] |
| 406 | + pub fn http1_preserve_header_order(&mut self, enabled: bool) -> &mut Builder { |
| 407 | + self.h1_preserve_header_order = enabled; |
| 408 | + self |
| 409 | + } |
| 410 | + |
| 411 | + /// Sets the exact size of the read buffer to *always* use. |
| 412 | + /// |
| 413 | + /// Note that setting this option unsets the `http1_max_buf_size` option. |
| 414 | + /// |
| 415 | + /// Default is an adaptive read buffer. |
| 416 | + pub fn http1_read_buf_exact_size(&mut self, sz: Option<usize>) -> &mut Builder { |
| 417 | + self.h1_read_buf_exact_size = sz; |
| 418 | + self.h1_max_buf_size = None; |
| 419 | + self |
| 420 | + } |
| 421 | + |
| 422 | + /// Set the maximum buffer size for the connection. |
| 423 | + /// |
| 424 | + /// Default is ~400kb. |
| 425 | + /// |
| 426 | + /// Note that setting this option unsets the `http1_read_exact_buf_size` option. |
| 427 | + /// |
| 428 | + /// # Panics |
| 429 | + /// |
| 430 | + /// The minimum value allowed is 8192. This method panics if the passed `max` is less than the minimum. |
| 431 | + #[cfg(feature = "http1")] |
| 432 | + #[cfg_attr(docsrs, doc(cfg(feature = "http1")))] |
| 433 | + pub fn http1_max_buf_size(&mut self, max: usize) -> &mut Self { |
| 434 | + assert!( |
| 435 | + max >= proto::h1::MINIMUM_MAX_BUFFER_SIZE, |
| 436 | + "the max_buf_size cannot be smaller than the minimum that h1 specifies." |
| 437 | + ); |
| 438 | + |
| 439 | + self.h1_max_buf_size = Some(max); |
| 440 | + self.h1_read_buf_exact_size = None; |
| 441 | + self |
| 442 | + } |
| 443 | + |
| 444 | + /// Constructs a connection with the configured options and IO. |
| 445 | + /// See [`client::conn`](crate::client::conn) for more. |
| 446 | + /// |
| 447 | + /// Note, if [`Connection`] is not `await`-ed, [`SendRequest`] will |
| 448 | + /// do nothing. |
| 449 | + pub fn handshake<T, B>( |
| 450 | + &self, |
| 451 | + io: T, |
| 452 | + ) -> impl Future<Output = crate::Result<(SendRequest<B>, Connection<T, B>)>> |
| 453 | + where |
| 454 | + T: AsyncRead + AsyncWrite + Unpin + Send + 'static, |
| 455 | + B: HttpBody + 'static, |
| 456 | + B::Data: Send, |
| 457 | + B::Error: Into<Box<dyn StdError + Send + Sync>>, |
| 458 | + { |
| 459 | + let opts = self.clone(); |
| 460 | + |
| 461 | + async move { |
| 462 | + tracing::trace!("client handshake HTTP/1"); |
| 463 | + |
| 464 | + let (tx, rx) = dispatch::channel(); |
| 465 | + let mut conn = proto::Conn::new(io); |
| 466 | + conn.set_h1_parser_config(opts.h1_parser_config); |
| 467 | + if let Some(writev) = opts.h1_writev { |
| 468 | + if writev { |
| 469 | + conn.set_write_strategy_queue(); |
| 470 | + } else { |
| 471 | + conn.set_write_strategy_flatten(); |
| 472 | + } |
| 473 | + } |
| 474 | + if opts.h1_title_case_headers { |
| 475 | + conn.set_title_case_headers(); |
| 476 | + } |
| 477 | + if opts.h1_preserve_header_case { |
| 478 | + conn.set_preserve_header_case(); |
| 479 | + } |
| 480 | + #[cfg(feature = "ffi")] |
| 481 | + if opts.h1_preserve_header_order { |
| 482 | + conn.set_preserve_header_order(); |
| 483 | + } |
| 484 | + if opts.h09_responses { |
| 485 | + conn.set_h09_responses(); |
| 486 | + } |
| 487 | + |
| 488 | + if let Some(sz) = opts.h1_read_buf_exact_size { |
| 489 | + conn.set_read_buf_exact_size(sz); |
| 490 | + } |
| 491 | + if let Some(max) = opts.h1_max_buf_size { |
| 492 | + conn.set_max_buf_size(max); |
| 493 | + } |
| 494 | + let cd = proto::h1::dispatch::Client::new(rx); |
| 495 | + let proto = proto::h1::Dispatcher::new(cd, conn); |
| 496 | + |
| 497 | + Ok(( |
| 498 | + SendRequest { dispatch: tx }, |
| 499 | + Connection { inner: Some(proto) }, |
| 500 | + )) |
| 501 | + } |
| 502 | + } |
| 503 | +} |
| 504 | + |
0 commit comments