h2/client.rs
1//! Client implementation of the HTTP/2 protocol.
2//!
3//! # Getting started
4//!
5//! Running an HTTP/2 client requires the caller to establish the underlying
6//! connection as well as get the connection to a state that is ready to begin
7//! the HTTP/2 handshake. See [here](../index.html#handshake) for more
8//! details.
9//!
10//! This could be as basic as using Tokio's [`TcpStream`] to connect to a remote
11//! host, but usually it means using either ALPN or HTTP/1.1 protocol upgrades.
12//!
13//! Once a connection is obtained, it is passed to [`handshake`], which will
14//! begin the [HTTP/2 handshake]. This returns a future that completes once
15//! the handshake process is performed and HTTP/2 streams may be initialized.
16//!
17//! [`handshake`] uses default configuration values. There are a number of
18//! settings that can be changed by using [`Builder`] instead.
19//!
20//! Once the handshake future completes, the caller is provided with a
21//! [`Connection`] instance and a [`SendRequest`] instance. The [`Connection`]
22//! instance is used to drive the connection (see [Managing the connection]).
23//! The [`SendRequest`] instance is used to initialize new streams (see [Making
24//! requests]).
25//!
26//! # Making requests
27//!
28//! Requests are made using the [`SendRequest`] handle provided by the handshake
29//! future. Once a request is submitted, an HTTP/2 stream is initialized and
30//! the request is sent to the server.
31//!
32//! A request body and request trailers are sent using [`SendRequest`] and the
33//! server's response is returned once the [`ResponseFuture`] future completes.
34//! Both the [`SendStream`] and [`ResponseFuture`] instances are returned by
35//! [`SendRequest::send_request`] and are tied to the HTTP/2 stream
36//! initialized by the sent request.
37//!
38//! The [`SendRequest::poll_ready`] function returns `Ready` when a new HTTP/2
39//! stream can be created, i.e. as long as the current number of active streams
40//! is below [`MAX_CONCURRENT_STREAMS`]. If a new stream cannot be created, the
41//! caller will be notified once an existing stream closes, freeing capacity for
42//! the caller. The caller should use [`SendRequest::poll_ready`] to check for
43//! capacity before sending a request to the server.
44//!
45//! [`SendRequest`] enforces the [`MAX_CONCURRENT_STREAMS`] setting. The user
46//! must not send a request if `poll_ready` does not return `Ready`. Attempting
47//! to do so will result in an [`Error`] being returned.
48//!
49//! # Managing the connection
50//!
51//! The [`Connection`] instance is used to manage connection state. The caller
52//! is required to call [`Connection::poll`] in order to advance state.
53//! [`SendRequest::send_request`] and other functions have no effect unless
54//! [`Connection::poll`] is called.
55//!
56//! The [`Connection`] instance should only be dropped once [`Connection::poll`]
57//! returns `Ready`. At this point, the underlying socket has been closed and no
58//! further work needs to be done.
59//!
60//! The easiest way to ensure that the [`Connection`] instance gets polled is to
61//! submit the [`Connection`] instance to an [executor]. The executor will then
62//! manage polling the connection until the connection is complete.
63//! Alternatively, the caller can call `poll` manually.
64//!
65//! # Example
66//!
67//! ```rust, no_run
68//!
69//! use h2::client;
70//!
71//! use http::{Request, Method};
72//! use std::error::Error;
73//! use tokio::net::TcpStream;
74//!
75//! #[tokio::main]
76//! pub async fn main() -> Result<(), Box<dyn Error>> {
77//! // Establish TCP connection to the server.
78//! let tcp = TcpStream::connect("127.0.0.1:5928").await?;
79//! let (h2, connection) = client::handshake(tcp).await?;
80//! tokio::spawn(async move {
81//! connection.await.unwrap();
82//! });
83//!
84//! let mut h2 = h2.ready().await?;
85//! // Prepare the HTTP request to send to the server.
86//! let request = Request::builder()
87//! .method(Method::GET)
88//! .uri("https://www.example.com/")
89//! .body(())
90//! .unwrap();
91//!
92//! // Send the request. The second tuple item allows the caller
93//! // to stream a request body.
94//! let (response, _) = h2.send_request(request, true).unwrap();
95//!
96//! let (head, mut body) = response.await?.into_parts();
97//!
98//! println!("Received response: {:?}", head);
99//!
100//! // The `flow_control` handle allows the caller to manage
101//! // flow control.
102//! //
103//! // Whenever data is received, the caller is responsible for
104//! // releasing capacity back to the server once it has freed
105//! // the data from memory.
106//! let mut flow_control = body.flow_control().clone();
107//!
108//! while let Some(chunk) = body.data().await {
109//! let chunk = chunk?;
110//! println!("RX: {:?}", chunk);
111//!
112//! // Let the server send more data.
113//! let _ = flow_control.release_capacity(chunk.len());
114//! }
115//!
116//! Ok(())
117//! }
118//! ```
119//!
120//! [`TcpStream`]: https://docs.rs/tokio-core/0.1/tokio_core/net/struct.TcpStream.html
121//! [`handshake`]: fn.handshake.html
122//! [executor]: https://docs.rs/futures/0.1/futures/future/trait.Executor.html
123//! [`SendRequest`]: struct.SendRequest.html
124//! [`SendStream`]: ../struct.SendStream.html
125//! [Making requests]: #making-requests
126//! [Managing the connection]: #managing-the-connection
127//! [`Connection`]: struct.Connection.html
128//! [`Connection::poll`]: struct.Connection.html#method.poll
129//! [`SendRequest::send_request`]: struct.SendRequest.html#method.send_request
130//! [`MAX_CONCURRENT_STREAMS`]: http://httpwg.org/specs/rfc7540.html#SettingValues
131//! [`SendRequest`]: struct.SendRequest.html
132//! [`ResponseFuture`]: struct.ResponseFuture.html
133//! [`SendRequest::poll_ready`]: struct.SendRequest.html#method.poll_ready
134//! [HTTP/2 handshake]: http://httpwg.org/specs/rfc7540.html#ConnectionHeader
135//! [`Builder`]: struct.Builder.html
136//! [`Error`]: ../struct.Error.html
137
138use crate::codec::{Codec, SendError, UserError};
139use crate::ext::Protocol;
140use crate::frame::{Headers, Pseudo, Reason, Settings, StreamId};
141use crate::proto::{self, Error};
142use crate::{FlowControl, PingPong, RecvStream, SendStream};
143
144use bytes::{Buf, Bytes};
145use http::{uri, HeaderMap, Method, Request, Response, Version};
146use std::fmt;
147use std::future::Future;
148use std::pin::Pin;
149use std::task::{Context, Poll};
150use std::time::Duration;
151use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
152use tracing::Instrument;
153
154/// Initializes new HTTP/2 streams on a connection by sending a request.
155///
156/// This type does no work itself. Instead, it is a handle to the inner
157/// connection state held by [`Connection`]. If the associated connection
158/// instance is dropped, all `SendRequest` functions will return [`Error`].
159///
160/// [`SendRequest`] instances are able to move to and operate on separate tasks
161/// / threads than their associated [`Connection`] instance. Internally, there
162/// is a buffer used to stage requests before they get written to the
163/// connection. There is no guarantee that requests get written to the
164/// connection in FIFO order as HTTP/2 prioritization logic can play a role.
165///
166/// [`SendRequest`] implements [`Clone`], enabling the creation of many
167/// instances that are backed by a single connection.
168///
169/// See [module] level documentation for more details.
170///
171/// [module]: index.html
172/// [`Connection`]: struct.Connection.html
173/// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html
174/// [`Error`]: ../struct.Error.html
175pub struct SendRequest<B: Buf> {
176 inner: proto::Streams<B, Peer>,
177 pending: Option<proto::OpaqueStreamRef>,
178}
179
180/// Returns a `SendRequest` instance once it is ready to send at least one
181/// request.
182#[derive(Debug)]
183pub struct ReadySendRequest<B: Buf> {
184 inner: Option<SendRequest<B>>,
185}
186
187/// Manages all state associated with an HTTP/2 client connection.
188///
189/// A `Connection` is backed by an I/O resource (usually a TCP socket) and
190/// implements the HTTP/2 client logic for that connection. It is responsible
191/// for driving the internal state forward, performing the work requested of the
192/// associated handles ([`SendRequest`], [`ResponseFuture`], [`SendStream`],
193/// [`RecvStream`]).
194///
195/// `Connection` values are created by calling [`handshake`]. Once a
196/// `Connection` value is obtained, the caller must repeatedly call [`poll`]
197/// until `Ready` is returned. The easiest way to do this is to submit the
198/// `Connection` instance to an [executor].
199///
200/// [module]: index.html
201/// [`handshake`]: fn.handshake.html
202/// [`SendRequest`]: struct.SendRequest.html
203/// [`ResponseFuture`]: struct.ResponseFuture.html
204/// [`SendStream`]: ../struct.SendStream.html
205/// [`RecvStream`]: ../struct.RecvStream.html
206/// [`poll`]: #method.poll
207/// [executor]: https://docs.rs/futures/0.1/futures/future/trait.Executor.html
208///
209/// # Examples
210///
211/// ```
212/// # use tokio::io::{AsyncRead, AsyncWrite};
213/// # use h2::client;
214/// # use h2::client::*;
215/// #
216/// # async fn doc<T>(my_io: T) -> Result<(), h2::Error>
217/// # where T: AsyncRead + AsyncWrite + Send + Unpin + 'static,
218/// # {
219/// let (send_request, connection) = client::handshake(my_io).await?;
220/// // Submit the connection handle to an executor.
221/// tokio::spawn(async { connection.await.expect("connection failed"); });
222///
223/// // Now, use `send_request` to initialize HTTP/2 streams.
224/// // ...
225/// # Ok(())
226/// # }
227/// #
228/// # pub fn main() {}
229/// ```
230#[must_use = "futures do nothing unless polled"]
231pub struct Connection<T, B: Buf = Bytes> {
232 inner: proto::Connection<T, Peer, B>,
233}
234
235/// A future of an HTTP response.
236#[derive(Debug)]
237#[must_use = "futures do nothing unless polled"]
238pub struct ResponseFuture {
239 inner: proto::OpaqueStreamRef,
240 push_promise_consumed: bool,
241}
242
243/// A future of a pushed HTTP response.
244///
245/// We have to differentiate between pushed and non pushed because of the spec
246/// <https://httpwg.org/specs/rfc7540.html#PUSH_PROMISE>
247/// > PUSH_PROMISE frames MUST only be sent on a peer-initiated stream
248/// > that is in either the "open" or "half-closed (remote)" state.
249#[derive(Debug)]
250#[must_use = "futures do nothing unless polled"]
251pub struct PushedResponseFuture {
252 inner: ResponseFuture,
253}
254
255/// A pushed response and corresponding request headers
256#[derive(Debug)]
257pub struct PushPromise {
258 /// The request headers
259 request: Request<()>,
260
261 /// The pushed response
262 response: PushedResponseFuture,
263}
264
265/// A stream of pushed responses and corresponding promised requests
266#[derive(Debug)]
267#[must_use = "streams do nothing unless polled"]
268pub struct PushPromises {
269 inner: proto::OpaqueStreamRef,
270}
271
272/// Builds client connections with custom configuration values.
273///
274/// Methods can be chained in order to set the configuration values.
275///
276/// The client is constructed by calling [`handshake`] and passing the I/O
277/// handle that will back the HTTP/2 server.
278///
279/// New instances of `Builder` are obtained via [`Builder::new`].
280///
281/// See function level documentation for details on the various client
282/// configuration settings.
283///
284/// [`Builder::new`]: struct.Builder.html#method.new
285/// [`handshake`]: struct.Builder.html#method.handshake
286///
287/// # Examples
288///
289/// ```
290/// # use tokio::io::{AsyncRead, AsyncWrite};
291/// # use h2::client::*;
292/// # use bytes::Bytes;
293/// #
294/// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
295/// -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
296/// # {
297/// // `client_fut` is a future representing the completion of the HTTP/2
298/// // handshake.
299/// let client_fut = Builder::new()
300/// .initial_window_size(1_000_000)
301/// .max_concurrent_streams(1000)
302/// .handshake(my_io);
303/// # client_fut.await
304/// # }
305/// #
306/// # pub fn main() {}
307/// ```
308#[derive(Clone, Debug)]
309pub struct Builder {
310 /// Time to keep locally reset streams around before reaping.
311 reset_stream_duration: Duration,
312
313 /// Initial maximum number of locally initiated (send) streams.
314 /// After receiving a SETTINGS frame from the remote peer,
315 /// the connection will overwrite this value with the
316 /// MAX_CONCURRENT_STREAMS specified in the frame.
317 /// If no value is advertised by the remote peer in the initial SETTINGS
318 /// frame, it will be set to usize::MAX.
319 initial_max_send_streams: usize,
320
321 /// Initial target window size for new connections.
322 initial_target_connection_window_size: Option<u32>,
323
324 /// Maximum amount of bytes to "buffer" for writing per stream.
325 max_send_buffer_size: usize,
326
327 /// Maximum number of locally reset streams to keep at a time.
328 reset_stream_max: usize,
329
330 /// Maximum number of remotely reset streams to allow in the pending
331 /// accept queue.
332 pending_accept_reset_stream_max: usize,
333
334 /// Initial `Settings` frame to send as part of the handshake.
335 settings: Settings,
336
337 /// The stream ID of the first (lowest) stream. Subsequent streams will use
338 /// monotonically increasing stream IDs.
339 stream_id: StreamId,
340
341 /// Maximum number of locally reset streams due to protocol error across
342 /// the lifetime of the connection.
343 ///
344 /// When this gets exceeded, we issue GOAWAYs.
345 local_max_error_reset_streams: Option<usize>,
346
347 /// connection-level budget for DATA framing overhead.
348 ///
349 /// When this gets exhausted, we issue a GOAWAY with `ENHANCE_YOUR_CALM`.
350 data_frame_budget: proto::DataFrameBudget,
351}
352
353#[derive(Debug)]
354pub(crate) struct Peer;
355
356// ===== impl SendRequest =====
357
358impl<B> SendRequest<B>
359where
360 B: Buf,
361{
362 /// Returns `Ready` when the connection can initialize a new HTTP/2
363 /// stream.
364 ///
365 /// This function must return `Ready` before `send_request` is called. When
366 /// `Poll::Pending` is returned, the task will be notified once the readiness
367 /// state changes.
368 ///
369 /// See [module] level docs for more details.
370 ///
371 /// [module]: index.html
372 pub fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), crate::Error>> {
373 ready!(self.inner.poll_pending_open(cx, self.pending.as_ref()))?;
374 self.pending = None;
375 Poll::Ready(Ok(()))
376 }
377
378 /// Consumes `self`, returning a future that returns `self` back once it is
379 /// ready to send a request.
380 ///
381 /// This function should be called before calling `send_request`.
382 ///
383 /// This is a functional combinator for [`poll_ready`]. The returned future
384 /// will call `SendStream::poll_ready` until `Ready`, then returns `self` to
385 /// the caller.
386 ///
387 /// # Examples
388 ///
389 /// ```rust
390 /// # use h2::client::*;
391 /// # use http::*;
392 /// # async fn doc(send_request: SendRequest<&'static [u8]>)
393 /// # {
394 /// // First, wait until the `send_request` handle is ready to send a new
395 /// // request
396 /// let mut send_request = send_request.ready().await.unwrap();
397 /// // Use `send_request` here.
398 /// # }
399 /// # pub fn main() {}
400 /// ```
401 ///
402 /// See [module] level docs for more details.
403 ///
404 /// [`poll_ready`]: #method.poll_ready
405 /// [module]: index.html
406 pub fn ready(self) -> ReadySendRequest<B> {
407 ReadySendRequest { inner: Some(self) }
408 }
409
410 /// Sends a HTTP/2 request to the server.
411 ///
412 /// `send_request` initializes a new HTTP/2 stream on the associated
413 /// connection, then sends the given request using this new stream. Only the
414 /// request head is sent.
415 ///
416 /// On success, a [`ResponseFuture`] instance and [`SendStream`] instance
417 /// are returned. The [`ResponseFuture`] instance is used to get the
418 /// server's response and the [`SendStream`] instance is used to send a
419 /// request body or trailers to the server over the same HTTP/2 stream.
420 ///
421 /// To send a request body or trailers, set `end_of_stream` to `false`.
422 /// Then, use the returned [`SendStream`] instance to stream request body
423 /// chunks or send trailers. If `end_of_stream` is **not** set to `false`
424 /// then attempting to call [`SendStream::send_data`] or
425 /// [`SendStream::send_trailers`] will result in an error.
426 ///
427 /// If no request body or trailers are to be sent, set `end_of_stream` to
428 /// `true` and drop the returned [`SendStream`] instance.
429 ///
430 /// # A note on HTTP versions
431 ///
432 /// The provided `Request` will be encoded differently depending on the
433 /// value of its version field. If the version is set to 2.0, then the
434 /// request is encoded as per the specification recommends.
435 ///
436 /// If the version is set to a lower value, then the request is encoded to
437 /// preserve the characteristics of HTTP 1.1 and lower. Specifically, host
438 /// headers are permitted and the `:authority` pseudo header is not
439 /// included.
440 ///
441 /// The caller should always set the request's version field to 2.0 unless
442 /// specifically transmitting an HTTP 1.1 request over 2.0.
443 ///
444 /// # Examples
445 ///
446 /// Sending a request with no body
447 ///
448 /// ```rust
449 /// # use h2::client::*;
450 /// # use http::*;
451 /// # async fn doc(send_request: SendRequest<&'static [u8]>)
452 /// # {
453 /// // First, wait until the `send_request` handle is ready to send a new
454 /// // request
455 /// let mut send_request = send_request.ready().await.unwrap();
456 /// // Prepare the HTTP request to send to the server.
457 /// let request = Request::get("https://www.example.com/")
458 /// .body(())
459 /// .unwrap();
460 ///
461 /// // Send the request to the server. Since we are not sending a
462 /// // body or trailers, we can drop the `SendStream` instance.
463 /// let (response, _) = send_request.send_request(request, true).unwrap();
464 /// let response = response.await.unwrap();
465 /// // Process the response
466 /// # }
467 /// # pub fn main() {}
468 /// ```
469 ///
470 /// Sending a request with a body and trailers
471 ///
472 /// ```rust
473 /// # use h2::client::*;
474 /// # use http::*;
475 /// # async fn doc(send_request: SendRequest<&'static [u8]>)
476 /// # {
477 /// // First, wait until the `send_request` handle is ready to send a new
478 /// // request
479 /// let mut send_request = send_request.ready().await.unwrap();
480 ///
481 /// // Prepare the HTTP request to send to the server.
482 /// let request = Request::get("https://www.example.com/")
483 /// .body(())
484 /// .unwrap();
485 ///
486 /// // Send the request to the server. If we are not sending a
487 /// // body or trailers, we can drop the `SendStream` instance.
488 /// let (response, mut send_stream) = send_request
489 /// .send_request(request, false).unwrap();
490 ///
491 /// // At this point, one option would be to wait for send capacity.
492 /// // Doing so would allow us to not hold data in memory that
493 /// // cannot be sent. However, this is not a requirement, so this
494 /// // example will skip that step. See `SendStream` documentation
495 /// // for more details.
496 /// send_stream.send_data(b"hello", false).unwrap();
497 /// send_stream.send_data(b"world", false).unwrap();
498 ///
499 /// // Send the trailers.
500 /// let mut trailers = HeaderMap::new();
501 /// trailers.insert(
502 /// header::HeaderName::from_bytes(b"my-trailer").unwrap(),
503 /// header::HeaderValue::from_bytes(b"hello").unwrap());
504 ///
505 /// send_stream.send_trailers(trailers).unwrap();
506 ///
507 /// let response = response.await.unwrap();
508 /// // Process the response
509 /// # }
510 /// # pub fn main() {}
511 /// ```
512 ///
513 /// [`ResponseFuture`]: struct.ResponseFuture.html
514 /// [`SendStream`]: ../struct.SendStream.html
515 /// [`SendStream::send_data`]: ../struct.SendStream.html#method.send_data
516 /// [`SendStream::send_trailers`]: ../struct.SendStream.html#method.send_trailers
517 pub fn send_request(
518 &mut self,
519 request: Request<()>,
520 end_of_stream: bool,
521 ) -> Result<(ResponseFuture, SendStream<B>), crate::Error> {
522 self.inner
523 .send_request(request, end_of_stream, self.pending.as_ref())
524 .map_err(Into::into)
525 .map(|(stream, is_full)| {
526 if stream.is_pending_open() && is_full {
527 // Only prevent sending another request when the request queue
528 // is not full.
529 self.pending = Some(stream.clone_to_opaque());
530 }
531
532 let response = ResponseFuture {
533 inner: stream.clone_to_opaque(),
534 push_promise_consumed: false,
535 };
536
537 let stream = SendStream::new(stream);
538
539 (response, stream)
540 })
541 }
542
543 /// Returns whether the [extended CONNECT protocol][1] is enabled or not.
544 ///
545 /// This setting is configured by the server peer by sending the
546 /// [`SETTINGS_ENABLE_CONNECT_PROTOCOL` parameter][2] in a `SETTINGS` frame.
547 /// This method returns the currently acknowledged value received from the
548 /// remote.
549 ///
550 /// [1]: https://datatracker.ietf.org/doc/html/rfc8441#section-4
551 /// [2]: https://datatracker.ietf.org/doc/html/rfc8441#section-3
552 pub fn is_extended_connect_protocol_enabled(&self) -> bool {
553 self.inner.is_extended_connect_protocol_enabled()
554 }
555
556 /// Returns the current max send streams
557 pub fn current_max_send_streams(&self) -> usize {
558 self.inner.current_max_send_streams()
559 }
560
561 /// Returns the current max recv streams
562 pub fn current_max_recv_streams(&self) -> usize {
563 self.inner.current_max_recv_streams()
564 }
565}
566
567impl<B> fmt::Debug for SendRequest<B>
568where
569 B: Buf,
570{
571 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
572 fmt.debug_struct("SendRequest").finish()
573 }
574}
575
576impl<B> Clone for SendRequest<B>
577where
578 B: Buf,
579{
580 fn clone(&self) -> Self {
581 SendRequest {
582 inner: self.inner.clone(),
583 pending: None,
584 }
585 }
586}
587
588#[cfg(feature = "unstable")]
589impl<B> SendRequest<B>
590where
591 B: Buf,
592{
593 /// Returns the number of active streams.
594 ///
595 /// An active stream is a stream that has not yet transitioned to a closed
596 /// state.
597 pub fn num_active_streams(&self) -> usize {
598 self.inner.num_active_streams()
599 }
600
601 /// Returns the number of streams that are held in memory.
602 ///
603 /// A wired stream is a stream that is either active or is closed but must
604 /// stay in memory for some reason. For example, there are still outstanding
605 /// userspace handles pointing to the slot.
606 pub fn num_wired_streams(&self) -> usize {
607 self.inner.num_wired_streams()
608 }
609}
610
611// ===== impl ReadySendRequest =====
612
613impl<B> Future for ReadySendRequest<B>
614where
615 B: Buf,
616{
617 type Output = Result<SendRequest<B>, crate::Error>;
618
619 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
620 match &mut self.inner {
621 Some(send_request) => {
622 ready!(send_request.poll_ready(cx))?;
623 }
624 None => panic!("called `poll` after future completed"),
625 }
626
627 Poll::Ready(Ok(self.inner.take().unwrap()))
628 }
629}
630
631// ===== impl Builder =====
632
633impl Builder {
634 /// Returns a new client builder instance initialized with default
635 /// configuration values.
636 ///
637 /// Configuration methods can be chained on the return value.
638 ///
639 /// # Examples
640 ///
641 /// ```
642 /// # use tokio::io::{AsyncRead, AsyncWrite};
643 /// # use h2::client::*;
644 /// # use bytes::Bytes;
645 /// #
646 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
647 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
648 /// # {
649 /// // `client_fut` is a future representing the completion of the HTTP/2
650 /// // handshake.
651 /// let client_fut = Builder::new()
652 /// .initial_window_size(1_000_000)
653 /// .max_concurrent_streams(1000)
654 /// .handshake(my_io);
655 /// # client_fut.await
656 /// # }
657 /// #
658 /// # pub fn main() {}
659 /// ```
660 pub fn new() -> Builder {
661 Builder {
662 max_send_buffer_size: proto::DEFAULT_MAX_SEND_BUFFER_SIZE,
663 reset_stream_duration: Duration::from_secs(proto::DEFAULT_RESET_STREAM_SECS),
664 reset_stream_max: proto::DEFAULT_RESET_STREAM_MAX,
665 pending_accept_reset_stream_max: proto::DEFAULT_REMOTE_RESET_STREAM_MAX,
666 initial_target_connection_window_size: None,
667 initial_max_send_streams: usize::MAX,
668 settings: Default::default(),
669 stream_id: 1.into(),
670 local_max_error_reset_streams: Some(proto::DEFAULT_LOCAL_RESET_COUNT_MAX),
671 data_frame_budget: proto::DataFrameBudget::Auto,
672 }
673 }
674
675 /// Indicates the initial window size (in octets) for stream-level
676 /// flow control for received data.
677 ///
678 /// The initial window of a stream is used as part of flow control. For more
679 /// details, see [`FlowControl`].
680 ///
681 /// The default value is 65,535.
682 ///
683 /// [`FlowControl`]: ../struct.FlowControl.html
684 ///
685 /// # Examples
686 ///
687 /// ```
688 /// # use tokio::io::{AsyncRead, AsyncWrite};
689 /// # use h2::client::*;
690 /// # use bytes::Bytes;
691 /// #
692 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
693 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
694 /// # {
695 /// // `client_fut` is a future representing the completion of the HTTP/2
696 /// // handshake.
697 /// let client_fut = Builder::new()
698 /// .initial_window_size(1_000_000)
699 /// .handshake(my_io);
700 /// # client_fut.await
701 /// # }
702 /// #
703 /// # pub fn main() {}
704 /// ```
705 pub fn initial_window_size(&mut self, size: u32) -> &mut Self {
706 self.settings.set_initial_window_size(Some(size));
707 self
708 }
709
710 /// Indicates the initial window size (in octets) for connection-level flow control
711 /// for received data.
712 ///
713 /// The initial window of a connection is used as part of flow control. For more details,
714 /// see [`FlowControl`].
715 ///
716 /// The default value is 65,535.
717 ///
718 /// [`FlowControl`]: ../struct.FlowControl.html
719 ///
720 /// # Examples
721 ///
722 /// ```
723 /// # use tokio::io::{AsyncRead, AsyncWrite};
724 /// # use h2::client::*;
725 /// # use bytes::Bytes;
726 /// #
727 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
728 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
729 /// # {
730 /// // `client_fut` is a future representing the completion of the HTTP/2
731 /// // handshake.
732 /// let client_fut = Builder::new()
733 /// .initial_connection_window_size(1_000_000)
734 /// .handshake(my_io);
735 /// # client_fut.await
736 /// # }
737 /// #
738 /// # pub fn main() {}
739 /// ```
740 pub fn initial_connection_window_size(&mut self, size: u32) -> &mut Self {
741 self.initial_target_connection_window_size = Some(size);
742 self
743 }
744
745 /// Indicates the size (in octets) of the largest HTTP/2 frame payload that the
746 /// configured client is able to accept.
747 ///
748 /// The sender may send data frames that are **smaller** than this value,
749 /// but any data larger than `max` will be broken up into multiple `DATA`
750 /// frames.
751 ///
752 /// The value **must** be between 16,384 and 16,777,215. The default value is 16,384.
753 ///
754 /// # Examples
755 ///
756 /// ```
757 /// # use tokio::io::{AsyncRead, AsyncWrite};
758 /// # use h2::client::*;
759 /// # use bytes::Bytes;
760 /// #
761 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
762 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
763 /// # {
764 /// // `client_fut` is a future representing the completion of the HTTP/2
765 /// // handshake.
766 /// let client_fut = Builder::new()
767 /// .max_frame_size(1_000_000)
768 /// .handshake(my_io);
769 /// # client_fut.await
770 /// # }
771 /// #
772 /// # pub fn main() {}
773 /// ```
774 ///
775 /// # Panics
776 ///
777 /// This function panics if `max` is not within the legal range specified
778 /// above.
779 pub fn max_frame_size(&mut self, max: u32) -> &mut Self {
780 self.settings.set_max_frame_size(Some(max));
781 self
782 }
783
784 /// Sets the max size of received header frames.
785 ///
786 /// This advisory setting informs a peer of the maximum size of header list
787 /// that the sender is prepared to accept, in octets. The value is based on
788 /// the uncompressed size of header fields, including the length of the name
789 /// and value in octets plus an overhead of 32 octets for each header field.
790 ///
791 /// This setting is also used to limit the maximum amount of data that is
792 /// buffered to decode HEADERS frames.
793 ///
794 /// # Examples
795 ///
796 /// ```
797 /// # use tokio::io::{AsyncRead, AsyncWrite};
798 /// # use h2::client::*;
799 /// # use bytes::Bytes;
800 /// #
801 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
802 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
803 /// # {
804 /// // `client_fut` is a future representing the completion of the HTTP/2
805 /// // handshake.
806 /// let client_fut = Builder::new()
807 /// .max_header_list_size(16 * 1024)
808 /// .handshake(my_io);
809 /// # client_fut.await
810 /// # }
811 /// #
812 /// # pub fn main() {}
813 /// ```
814 pub fn max_header_list_size(&mut self, max: u32) -> &mut Self {
815 self.settings.set_max_header_list_size(Some(max));
816 self
817 }
818
819 /// Sets the maximum number of concurrent streams.
820 ///
821 /// The maximum concurrent streams setting only controls the maximum number
822 /// of streams that can be initiated by the remote peer. In other words,
823 /// when this setting is set to 100, this does not limit the number of
824 /// concurrent streams that can be created by the caller.
825 ///
826 /// It is recommended that this value be no smaller than 100, so as to not
827 /// unnecessarily limit parallelism. However, any value is legal, including
828 /// 0. If `max` is set to 0, then the remote will not be permitted to
829 /// initiate streams.
830 ///
831 /// Although the HTTP/2 specification excludes streams in the reserved state
832 /// from this limit, `h2` counts reserved push streams against this setting
833 /// to protect resources. This includes push promises whose response streams
834 /// have not yet started.
835 ///
836 /// Also note that if the remote *does* exceed the value set here, it is not
837 /// a protocol level error. Instead, the `h2` library will immediately reset
838 /// the stream.
839 ///
840 /// See [Section 5.1.2] in the HTTP/2 spec for more details.
841 ///
842 /// [Section 5.1.2]: https://http2.github.io/http2-spec/#rfc.section.5.1.2
843 ///
844 /// # Examples
845 ///
846 /// ```
847 /// # use tokio::io::{AsyncRead, AsyncWrite};
848 /// # use h2::client::*;
849 /// # use bytes::Bytes;
850 /// #
851 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
852 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
853 /// # {
854 /// // `client_fut` is a future representing the completion of the HTTP/2
855 /// // handshake.
856 /// let client_fut = Builder::new()
857 /// .max_concurrent_streams(1000)
858 /// .handshake(my_io);
859 /// # client_fut.await
860 /// # }
861 /// #
862 /// # pub fn main() {}
863 /// ```
864 pub fn max_concurrent_streams(&mut self, max: u32) -> &mut Self {
865 self.settings.set_max_concurrent_streams(Some(max));
866 self
867 }
868
869 /// Sets the initial maximum of locally initiated (send) streams.
870 ///
871 /// The initial settings will be overwritten by the remote peer when
872 /// the SETTINGS frame is received. The new value will be set to the
873 /// `max_concurrent_streams()` from the frame. If no value is advertised in
874 /// the initial SETTINGS frame from the remote peer as part of
875 /// [HTTP/2 Connection Preface], `usize::MAX` will be set.
876 ///
877 /// This setting prevents the caller from exceeding this number of
878 /// streams that are counted towards the concurrency limit.
879 ///
880 /// Sending streams past the limit returned by the peer will be treated
881 /// as a stream error of type PROTOCOL_ERROR or REFUSED_STREAM.
882 ///
883 /// See [Section 5.1.2] in the HTTP/2 spec for more details.
884 ///
885 /// The default value is `usize::MAX`.
886 ///
887 /// [HTTP/2 Connection Preface]: https://httpwg.org/specs/rfc9113.html#preface
888 /// [Section 5.1.2]: https://httpwg.org/specs/rfc9113.html#rfc.section.5.1.2
889 ///
890 /// # Examples
891 ///
892 /// ```
893 /// # use tokio::io::{AsyncRead, AsyncWrite};
894 /// # use h2::client::*;
895 /// # use bytes::Bytes;
896 /// #
897 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
898 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
899 /// # {
900 /// // `client_fut` is a future representing the completion of the HTTP/2
901 /// // handshake.
902 /// let client_fut = Builder::new()
903 /// .initial_max_send_streams(1000)
904 /// .handshake(my_io);
905 /// # client_fut.await
906 /// # }
907 /// #
908 /// # pub fn main() {}
909 /// ```
910 pub fn initial_max_send_streams(&mut self, initial: usize) -> &mut Self {
911 self.initial_max_send_streams = initial;
912 self
913 }
914
915 /// Sets the maximum number of concurrent locally reset streams.
916 ///
917 /// When a stream is explicitly reset, the HTTP/2 specification requires
918 /// that any further frames received for that stream must be ignored for
919 /// "some time".
920 ///
921 /// In order to satisfy the specification, internal state must be maintained
922 /// to implement the behavior. This state grows linearly with the number of
923 /// streams that are locally reset.
924 ///
925 /// The `max_concurrent_reset_streams` setting configures sets an upper
926 /// bound on the amount of state that is maintained. When this max value is
927 /// reached, the oldest reset stream is purged from memory.
928 ///
929 /// Once the stream has been fully purged from memory, any additional frames
930 /// received for that stream will result in a connection level protocol
931 /// error, forcing the connection to terminate.
932 ///
933 /// The default value is currently 50.
934 ///
935 /// # Examples
936 ///
937 /// ```
938 /// # use tokio::io::{AsyncRead, AsyncWrite};
939 /// # use h2::client::*;
940 /// # use bytes::Bytes;
941 /// #
942 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
943 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
944 /// # {
945 /// // `client_fut` is a future representing the completion of the HTTP/2
946 /// // handshake.
947 /// let client_fut = Builder::new()
948 /// .max_concurrent_reset_streams(1000)
949 /// .handshake(my_io);
950 /// # client_fut.await
951 /// # }
952 /// #
953 /// # pub fn main() {}
954 /// ```
955 pub fn max_concurrent_reset_streams(&mut self, max: usize) -> &mut Self {
956 self.reset_stream_max = max;
957 self
958 }
959
960 /// Sets the duration to remember locally reset streams.
961 ///
962 /// When a stream is explicitly reset, the HTTP/2 specification requires
963 /// that any further frames received for that stream must be ignored for
964 /// "some time".
965 ///
966 /// In order to satisfy the specification, internal state must be maintained
967 /// to implement the behavior. This state grows linearly with the number of
968 /// streams that are locally reset.
969 ///
970 /// The `reset_stream_duration` setting configures the max amount of time
971 /// this state will be maintained in memory. Once the duration elapses, the
972 /// stream state is purged from memory.
973 ///
974 /// Once the stream has been fully purged from memory, any additional frames
975 /// received for that stream will result in a connection level protocol
976 /// error, forcing the connection to terminate.
977 ///
978 /// The default value is currently 1 second.
979 ///
980 /// # Examples
981 ///
982 /// ```
983 /// # use tokio::io::{AsyncRead, AsyncWrite};
984 /// # use h2::client::*;
985 /// # use std::time::Duration;
986 /// # use bytes::Bytes;
987 /// #
988 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
989 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
990 /// # {
991 /// // `client_fut` is a future representing the completion of the HTTP/2
992 /// // handshake.
993 /// let client_fut = Builder::new()
994 /// .reset_stream_duration(Duration::from_secs(10))
995 /// .handshake(my_io);
996 /// # client_fut.await
997 /// # }
998 /// #
999 /// # pub fn main() {}
1000 /// ```
1001 pub fn reset_stream_duration(&mut self, dur: Duration) -> &mut Self {
1002 self.reset_stream_duration = dur;
1003 self
1004 }
1005
1006 /// Sets the maximum number of local resets due to protocol errors made by the remote end.
1007 ///
1008 /// Invalid frames and many other protocol errors will lead to resets being generated for those streams.
1009 /// Too many of these often indicate a malicious client, and there are attacks which can abuse this to DOS servers.
1010 /// This limit protects against these DOS attacks by limiting the amount of resets we can be forced to generate.
1011 ///
1012 /// When the number of local resets exceeds this threshold, the client will close the connection.
1013 ///
1014 /// If you really want to disable this, supply [`Option::None`] here.
1015 /// Disabling this is not recommended and may expose you to DOS attacks.
1016 ///
1017 /// The default value is currently 1024, but could change.
1018 pub fn max_local_error_reset_streams(&mut self, max: Option<usize>) -> &mut Self {
1019 self.local_max_error_reset_streams = max;
1020 self
1021 }
1022
1023 /// Sets the maximum number of pending-accept remotely-reset streams.
1024 ///
1025 /// Streams that have been received by the peer, but not accepted by the
1026 /// user, can also receive a RST_STREAM. This is a legitimate pattern: one
1027 /// could send a request and then shortly after, realize it is not needed,
1028 /// sending a CANCEL.
1029 ///
1030 /// However, since those streams are now "closed", they don't count towards
1031 /// the max concurrent streams. So, they will sit in the accept queue,
1032 /// using memory.
1033 ///
1034 /// When the number of remotely-reset streams sitting in the pending-accept
1035 /// queue reaches this maximum value, a connection error with the code of
1036 /// `ENHANCE_YOUR_CALM` will be sent to the peer, and returned by the
1037 /// `Future`.
1038 ///
1039 /// The default value is currently 20, but could change.
1040 ///
1041 /// # Examples
1042 ///
1043 /// ```
1044 /// # use tokio::io::{AsyncRead, AsyncWrite};
1045 /// # use h2::client::*;
1046 /// # use bytes::Bytes;
1047 /// #
1048 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
1049 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
1050 /// # {
1051 /// // `client_fut` is a future representing the completion of the HTTP/2
1052 /// // handshake.
1053 /// let client_fut = Builder::new()
1054 /// .max_pending_accept_reset_streams(100)
1055 /// .handshake(my_io);
1056 /// # client_fut.await
1057 /// # }
1058 /// #
1059 /// # pub fn main() {}
1060 /// ```
1061 pub fn max_pending_accept_reset_streams(&mut self, max: usize) -> &mut Self {
1062 self.pending_accept_reset_stream_max = max;
1063 self
1064 }
1065
1066 /// Sets the maximum send buffer size per stream.
1067 ///
1068 /// Once a stream has buffered up to (or over) the maximum, the stream's
1069 /// flow control will not "poll" additional capacity. Once bytes for the
1070 /// stream have been written to the connection, the send buffer capacity
1071 /// will be freed up again.
1072 ///
1073 /// The default is currently ~400KB, but may change.
1074 ///
1075 /// # Panics
1076 ///
1077 /// This function panics if `max` is larger than `u32::MAX`.
1078 pub fn max_send_buffer_size(&mut self, max: usize) -> &mut Self {
1079 assert!(max <= u32::MAX as usize);
1080 self.max_send_buffer_size = max;
1081 self
1082 }
1083
1084 /// Enables or disables server push promises.
1085 ///
1086 /// This value is included in the initial SETTINGS handshake.
1087 /// Setting this value to value to
1088 /// false in the initial SETTINGS handshake guarantees that the remote server
1089 /// will never send a push promise.
1090 ///
1091 /// This setting can be changed during the life of a single HTTP/2
1092 /// connection by sending another settings frame updating the value.
1093 ///
1094 /// Default value: `true`.
1095 ///
1096 /// # Examples
1097 ///
1098 /// ```
1099 /// # use tokio::io::{AsyncRead, AsyncWrite};
1100 /// # use h2::client::*;
1101 /// # use std::time::Duration;
1102 /// # use bytes::Bytes;
1103 /// #
1104 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
1105 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
1106 /// # {
1107 /// // `client_fut` is a future representing the completion of the HTTP/2
1108 /// // handshake.
1109 /// let client_fut = Builder::new()
1110 /// .enable_push(false)
1111 /// .handshake(my_io);
1112 /// # client_fut.await
1113 /// # }
1114 /// #
1115 /// # pub fn main() {}
1116 /// ```
1117 pub fn enable_push(&mut self, enabled: bool) -> &mut Self {
1118 self.settings.set_enable_push(enabled);
1119 self
1120 }
1121
1122 /// Sets the header table size.
1123 ///
1124 /// This setting informs the peer of the maximum size of the header compression
1125 /// table used to encode header blocks, in octets. The encoder may select any value
1126 /// equal to or less than the header table size specified by the sender.
1127 ///
1128 /// The default value is 4,096.
1129 ///
1130 /// # Examples
1131 ///
1132 /// ```
1133 /// # use tokio::io::{AsyncRead, AsyncWrite};
1134 /// # use h2::client::*;
1135 /// # use bytes::Bytes;
1136 /// #
1137 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
1138 /// # -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
1139 /// # {
1140 /// // `client_fut` is a future representing the completion of the HTTP/2
1141 /// // handshake.
1142 /// let client_fut = Builder::new()
1143 /// .header_table_size(1_000_000)
1144 /// .handshake(my_io);
1145 /// # client_fut.await
1146 /// # }
1147 /// #
1148 /// # pub fn main() {}
1149 /// ```
1150 pub fn header_table_size(&mut self, size: u32) -> &mut Self {
1151 self.settings.set_header_table_size(Some(size));
1152 self
1153 }
1154
1155 /// Sets a connection-level budget for limiting memory overhead from
1156 /// received small DATA frames.
1157 ///
1158 /// HTTP/2 flow control accounts for DATA payload bytes, but not the
1159 /// additional memory required to buffer each DATA frame. An excessive
1160 /// number of small frames may therefore consume disproportionate memory.
1161 ///
1162 /// Small DATA frames consume this budget. The budget is restored when
1163 /// buffered frames are consumed by the application, while sufficiently
1164 /// large frames may also restore budget. Empty DATA frames are limited
1165 /// separately and do not consume this budget.
1166 ///
1167 /// When this budget is exhausted, the connection is closed with
1168 /// `ENHANCE_YOUR_CALM`.
1169 ///
1170 /// By default, the budget is half the target connection window, with a
1171 /// minimum of 25,600 bytes. Changing the target window at runtime also
1172 /// updates the permitted framing overhead.
1173 pub fn data_frame_budget(&mut self, budget: usize) -> &mut Self {
1174 self.data_frame_budget = proto::DataFrameBudget::Configured(budget);
1175 self
1176 }
1177
1178 /// Sets the first stream ID to something other than 1.
1179 #[cfg(feature = "unstable")]
1180 pub fn initial_stream_id(&mut self, stream_id: u32) -> &mut Self {
1181 self.stream_id = stream_id.into();
1182 assert!(
1183 self.stream_id.is_client_initiated(),
1184 "stream id must be odd"
1185 );
1186 self
1187 }
1188
1189 /// Creates a new configured HTTP/2 client backed by `io`.
1190 ///
1191 /// It is expected that `io` already be in an appropriate state to commence
1192 /// the [HTTP/2 handshake]. The handshake is completed once both the connection
1193 /// preface and the initial settings frame is sent by the client.
1194 ///
1195 /// The handshake future does not wait for the initial settings frame from the
1196 /// server.
1197 ///
1198 /// Returns a future which resolves to the [`Connection`] / [`SendRequest`]
1199 /// tuple once the HTTP/2 handshake has been completed.
1200 ///
1201 /// This function also allows the caller to configure the send payload data
1202 /// type. See [Outbound data type] for more details.
1203 ///
1204 /// [HTTP/2 handshake]: http://httpwg.org/specs/rfc7540.html#ConnectionHeader
1205 /// [`Connection`]: struct.Connection.html
1206 /// [`SendRequest`]: struct.SendRequest.html
1207 /// [Outbound data type]: ../index.html#outbound-data-type.
1208 ///
1209 /// # Examples
1210 ///
1211 /// Basic usage:
1212 ///
1213 /// ```
1214 /// # use tokio::io::{AsyncRead, AsyncWrite};
1215 /// # use h2::client::*;
1216 /// # use bytes::Bytes;
1217 /// #
1218 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
1219 /// -> Result<((SendRequest<Bytes>, Connection<T, Bytes>)), h2::Error>
1220 /// # {
1221 /// // `client_fut` is a future representing the completion of the HTTP/2
1222 /// // handshake.
1223 /// let client_fut = Builder::new()
1224 /// .handshake(my_io);
1225 /// # client_fut.await
1226 /// # }
1227 /// #
1228 /// # pub fn main() {}
1229 /// ```
1230 ///
1231 /// Configures the send-payload data type. In this case, the outbound data
1232 /// type will be `&'static [u8]`.
1233 ///
1234 /// ```
1235 /// # use tokio::io::{AsyncRead, AsyncWrite};
1236 /// # use h2::client::*;
1237 /// #
1238 /// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T)
1239 /// # -> Result<((SendRequest<&'static [u8]>, Connection<T, &'static [u8]>)), h2::Error>
1240 /// # {
1241 /// // `client_fut` is a future representing the completion of the HTTP/2
1242 /// // handshake.
1243 /// let client_fut = Builder::new()
1244 /// .handshake::<_, &'static [u8]>(my_io);
1245 /// # client_fut.await
1246 /// # }
1247 /// #
1248 /// # pub fn main() {}
1249 /// ```
1250 pub fn handshake<T, B>(
1251 &self,
1252 io: T,
1253 ) -> impl Future<Output = Result<(SendRequest<B>, Connection<T, B>), crate::Error>>
1254 where
1255 T: AsyncRead + AsyncWrite + Unpin,
1256 B: Buf,
1257 {
1258 Connection::handshake2(io, self.clone())
1259 }
1260}
1261
1262impl Default for Builder {
1263 fn default() -> Builder {
1264 Builder::new()
1265 }
1266}
1267
1268/// Creates a new configured HTTP/2 client with default configuration
1269/// values backed by `io`.
1270///
1271/// It is expected that `io` already be in an appropriate state to commence
1272/// the [HTTP/2 handshake]. See [Handshake] for more details.
1273///
1274/// Returns a future which resolves to the [`Connection`] / [`SendRequest`]
1275/// tuple once the HTTP/2 handshake has been completed. The returned
1276/// [`Connection`] instance will be using default configuration values. Use
1277/// [`Builder`] to customize the configuration values used by a [`Connection`]
1278/// instance.
1279///
1280/// [HTTP/2 handshake]: http://httpwg.org/specs/rfc7540.html#ConnectionHeader
1281/// [Handshake]: ../index.html#handshake
1282/// [`Connection`]: struct.Connection.html
1283/// [`SendRequest`]: struct.SendRequest.html
1284///
1285/// # Examples
1286///
1287/// ```
1288/// # use tokio::io::{AsyncRead, AsyncWrite};
1289/// # use h2::client;
1290/// # use h2::client::*;
1291/// #
1292/// # async fn doc<T: AsyncRead + AsyncWrite + Unpin>(my_io: T) -> Result<(), h2::Error>
1293/// # {
1294/// let (send_request, connection) = client::handshake(my_io).await?;
1295/// // The HTTP/2 handshake has completed, now start polling
1296/// // `connection` and use `send_request` to send requests to the
1297/// // server.
1298/// # Ok(())
1299/// # }
1300/// #
1301/// # pub fn main() {}
1302/// ```
1303pub async fn handshake<T>(io: T) -> Result<(SendRequest<Bytes>, Connection<T, Bytes>), crate::Error>
1304where
1305 T: AsyncRead + AsyncWrite + Unpin,
1306{
1307 let builder = Builder::new();
1308 builder
1309 .handshake(io)
1310 .instrument(tracing::trace_span!("client_handshake"))
1311 .await
1312}
1313
1314// ===== impl Connection =====
1315
1316async fn bind_connection<T>(io: &mut T) -> Result<(), crate::Error>
1317where
1318 T: AsyncRead + AsyncWrite + Unpin,
1319{
1320 tracing::debug!("binding client connection");
1321
1322 let msg: &'static [u8] = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
1323 io.write_all(msg).await.map_err(crate::Error::from_io)?;
1324
1325 tracing::debug!("client connection bound");
1326
1327 Ok(())
1328}
1329
1330impl<T, B> Connection<T, B>
1331where
1332 T: AsyncRead + AsyncWrite + Unpin,
1333 B: Buf,
1334{
1335 async fn handshake2(
1336 mut io: T,
1337 builder: Builder,
1338 ) -> Result<(SendRequest<B>, Connection<T, B>), crate::Error> {
1339 bind_connection(&mut io).await?;
1340
1341 // Create the codec
1342 let mut codec = Codec::new(io);
1343
1344 if let Some(max) = builder.settings.max_frame_size() {
1345 codec.set_max_recv_frame_size(max as usize);
1346 }
1347
1348 if let Some(max) = builder.settings.max_header_list_size() {
1349 codec.set_max_recv_header_list_size(max as usize);
1350 }
1351
1352 // Send initial settings frame
1353 codec
1354 .buffer(builder.settings.clone().into())
1355 .expect("invalid SETTINGS frame");
1356
1357 let inner = proto::Connection::new(
1358 codec,
1359 proto::Config {
1360 next_stream_id: builder.stream_id,
1361 initial_max_send_streams: builder.initial_max_send_streams,
1362 max_send_buffer_size: builder.max_send_buffer_size,
1363 reset_stream_duration: builder.reset_stream_duration,
1364 reset_stream_max: builder.reset_stream_max,
1365 remote_reset_stream_max: builder.pending_accept_reset_stream_max,
1366 local_error_reset_streams_max: builder.local_max_error_reset_streams,
1367 settings: builder.settings,
1368 data_frame_budget: builder.data_frame_budget,
1369 },
1370 );
1371 let send_request = SendRequest {
1372 inner: inner.streams().clone(),
1373 pending: None,
1374 };
1375
1376 let mut connection = Connection { inner };
1377 if let Some(sz) = builder.initial_target_connection_window_size {
1378 connection.set_target_window_size(sz);
1379 }
1380
1381 Ok((send_request, connection))
1382 }
1383
1384 /// Sets the target window size for the whole connection.
1385 ///
1386 /// If `size` is greater than the current value, then a `WINDOW_UPDATE`
1387 /// frame will be immediately sent to the remote, increasing the connection
1388 /// level window by `size - current_value`.
1389 ///
1390 /// If `size` is less than the current value, nothing will happen
1391 /// immediately. However, as window capacity is released by
1392 /// [`FlowControl`] instances, no `WINDOW_UPDATE` frames will be sent
1393 /// out until the number of "in flight" bytes drops below `size`.
1394 ///
1395 /// The default value is 65,535. The automatic small-DATA-frame budget is
1396 /// updated to match this target.
1397 ///
1398 /// See [`FlowControl`] documentation for more details.
1399 ///
1400 /// [`FlowControl`]: ../struct.FlowControl.html
1401 /// [library level]: ../index.html#flow-control
1402 pub fn set_target_window_size(&mut self, size: u32) {
1403 assert!(size <= proto::MAX_WINDOW_SIZE);
1404 self.inner.set_target_window_size(size);
1405 }
1406
1407 /// Set a new `INITIAL_WINDOW_SIZE` setting (in octets) for stream-level
1408 /// flow control for received data.
1409 ///
1410 /// The `SETTINGS` will be sent to the remote, and only applied once the
1411 /// remote acknowledges the change.
1412 ///
1413 /// This can be used to increase or decrease the window size for existing
1414 /// streams.
1415 ///
1416 /// # Errors
1417 ///
1418 /// Returns an error if a previous call is still pending acknowledgement
1419 /// from the remote endpoint.
1420 pub fn set_initial_window_size(&mut self, size: u32) -> Result<(), crate::Error> {
1421 assert!(size <= proto::MAX_WINDOW_SIZE);
1422 self.inner.set_initial_window_size(size)?;
1423 Ok(())
1424 }
1425
1426 /// Takes a `PingPong` instance from the connection.
1427 ///
1428 /// # Note
1429 ///
1430 /// This may only be called once. Calling multiple times will return `None`.
1431 pub fn ping_pong(&mut self) -> Option<PingPong> {
1432 self.inner.take_user_pings().map(PingPong::new)
1433 }
1434
1435 /// Returns the maximum number of concurrent streams that may be initiated
1436 /// by this client.
1437 ///
1438 /// This limit is configured by the server peer by sending the
1439 /// [`SETTINGS_MAX_CONCURRENT_STREAMS` parameter][1] in a `SETTINGS` frame.
1440 /// This method returns the currently acknowledged value received from the
1441 /// remote.
1442 ///
1443 /// [1]: https://tools.ietf.org/html/rfc7540#section-5.1.2
1444 pub fn max_concurrent_send_streams(&self) -> usize {
1445 self.inner.max_send_streams()
1446 }
1447 /// Returns the maximum number of concurrent streams that may be initiated
1448 /// by the server on this connection.
1449 ///
1450 /// This returns the value of the [`SETTINGS_MAX_CONCURRENT_STREAMS`
1451 /// parameter][1] sent in a `SETTINGS` frame that has been
1452 /// acknowledged by the remote peer. The value to be sent is configured by
1453 /// the [`Builder::max_concurrent_streams`][2] method before handshaking
1454 /// with the remote peer.
1455 ///
1456 /// [1]: https://tools.ietf.org/html/rfc7540#section-5.1.2
1457 /// [2]: ../struct.Builder.html#method.max_concurrent_streams
1458 pub fn max_concurrent_recv_streams(&self) -> usize {
1459 self.inner.max_recv_streams()
1460 }
1461}
1462
1463impl<T, B> Future for Connection<T, B>
1464where
1465 T: AsyncRead + AsyncWrite + Unpin,
1466 B: Buf,
1467{
1468 type Output = Result<(), crate::Error>;
1469
1470 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1471 // Snapshot before checking for shutdown: the last reference may be
1472 // dropped after the shutdown check, before inner.poll registers a
1473 // waker. A snapshot taken after that drop would be false and skip the
1474 // self-wake below, leaving the connection pending without closing.
1475 let had_streams_or_refs = self.inner.has_streams_or_other_references();
1476 self.inner.maybe_close_connection_if_no_streams();
1477 let result = self.inner.poll(cx).map_err(Into::into);
1478 // if we had streams/refs, and don't anymore, wake up one more time to
1479 // ensure proper shutdown
1480 if result.is_pending()
1481 && had_streams_or_refs
1482 && !self.inner.has_streams_or_other_references()
1483 {
1484 tracing::trace!("last stream closed during poll, wake again");
1485 cx.waker().wake_by_ref();
1486 }
1487 result
1488 }
1489}
1490
1491impl<T, B> fmt::Debug for Connection<T, B>
1492where
1493 T: AsyncRead + AsyncWrite,
1494 T: fmt::Debug,
1495 B: fmt::Debug + Buf,
1496{
1497 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1498 fmt::Debug::fmt(&self.inner, fmt)
1499 }
1500}
1501
1502// ===== impl ResponseFuture =====
1503
1504impl Future for ResponseFuture {
1505 type Output = Result<Response<RecvStream>, crate::Error>;
1506
1507 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1508 let (parts, _) = ready!(self.inner.poll_response(cx))?.into_parts();
1509 let body = RecvStream::new(FlowControl::new(self.inner.clone()));
1510
1511 Poll::Ready(Ok(Response::from_parts(parts, body)))
1512 }
1513}
1514
1515impl ResponseFuture {
1516 /// Returns the stream ID of the response stream.
1517 ///
1518 /// # Panics
1519 ///
1520 /// If the lock on the stream store has been poisoned.
1521 pub fn stream_id(&self) -> crate::StreamId {
1522 crate::StreamId::from_internal(self.inner.stream_id())
1523 }
1524
1525 /// Polls for informational responses (1xx status codes).
1526 ///
1527 /// This method should be called before polling the main response future
1528 /// to check for any informational responses that have been received.
1529 ///
1530 /// Returns `Poll::Ready(Some(response))` if an informational response is available,
1531 /// `Poll::Ready(None)` if no more informational responses are expected,
1532 /// or `Poll::Pending` if no informational response is currently available.
1533 pub fn poll_informational(
1534 &mut self,
1535 cx: &mut Context<'_>,
1536 ) -> Poll<Option<Result<Response<()>, crate::Error>>> {
1537 self.inner.poll_informational(cx).map_err(Into::into)
1538 }
1539
1540 /// Returns a stream of PushPromises
1541 ///
1542 /// # Panics
1543 ///
1544 /// If this method has been called before
1545 /// or the stream was itself was pushed
1546 pub fn push_promises(&mut self) -> PushPromises {
1547 if self.push_promise_consumed {
1548 panic!("Reference to push promises stream taken!");
1549 }
1550 self.push_promise_consumed = true;
1551 PushPromises {
1552 inner: self.inner.clone(),
1553 }
1554 }
1555}
1556
1557// ===== impl PushPromises =====
1558
1559impl PushPromises {
1560 /// Get the next `PushPromise`.
1561 pub async fn push_promise(&mut self) -> Option<Result<PushPromise, crate::Error>> {
1562 crate::poll_fn(move |cx| self.poll_push_promise(cx)).await
1563 }
1564
1565 #[doc(hidden)]
1566 pub fn poll_push_promise(
1567 &mut self,
1568 cx: &mut Context<'_>,
1569 ) -> Poll<Option<Result<PushPromise, crate::Error>>> {
1570 match self.inner.poll_pushed(cx) {
1571 Poll::Ready(Some(Ok((request, response)))) => {
1572 let response = PushedResponseFuture {
1573 inner: ResponseFuture {
1574 inner: response,
1575 push_promise_consumed: false,
1576 },
1577 };
1578 Poll::Ready(Some(Ok(PushPromise { request, response })))
1579 }
1580 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e.into()))),
1581 Poll::Ready(None) => Poll::Ready(None),
1582 Poll::Pending => Poll::Pending,
1583 }
1584 }
1585}
1586
1587#[cfg(feature = "stream")]
1588impl futures_core::Stream for PushPromises {
1589 type Item = Result<PushPromise, crate::Error>;
1590
1591 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
1592 self.poll_push_promise(cx)
1593 }
1594}
1595
1596// ===== impl PushPromise =====
1597
1598impl PushPromise {
1599 /// Returns a reference to the push promise's request headers.
1600 pub fn request(&self) -> &Request<()> {
1601 &self.request
1602 }
1603
1604 /// Returns a mutable reference to the push promise's request headers.
1605 pub fn request_mut(&mut self) -> &mut Request<()> {
1606 &mut self.request
1607 }
1608
1609 /// Consumes `self`, returning the push promise's request headers and
1610 /// response future.
1611 pub fn into_parts(self) -> (Request<()>, PushedResponseFuture) {
1612 (self.request, self.response)
1613 }
1614}
1615
1616// ===== impl PushedResponseFuture =====
1617
1618impl Future for PushedResponseFuture {
1619 type Output = Result<Response<RecvStream>, crate::Error>;
1620
1621 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1622 Pin::new(&mut self.inner).poll(cx)
1623 }
1624}
1625
1626impl PushedResponseFuture {
1627 /// Returns the stream ID of the response stream.
1628 ///
1629 /// # Panics
1630 ///
1631 /// If the lock on the stream store has been poisoned.
1632 pub fn stream_id(&self) -> crate::StreamId {
1633 self.inner.stream_id()
1634 }
1635}
1636
1637// ===== impl Peer =====
1638
1639impl Peer {
1640 pub fn convert_send_message(
1641 id: StreamId,
1642 request: Request<()>,
1643 protocol: Option<Protocol>,
1644 end_of_stream: bool,
1645 ) -> Result<Headers, SendError> {
1646 use http::request::Parts;
1647
1648 let (
1649 Parts {
1650 method,
1651 uri,
1652 headers,
1653 version,
1654 ..
1655 },
1656 _,
1657 ) = request.into_parts();
1658
1659 let is_connect = method == Method::CONNECT;
1660
1661 // Build the set pseudo header set. All requests will include `method`
1662 // and `path`.
1663 let mut pseudo = Pseudo::request(method, uri, protocol);
1664
1665 if pseudo.scheme.is_none() {
1666 // If the scheme is not set, then there are a two options.
1667 //
1668 // 1) Authority is not set. In this case, a request was issued with
1669 // a relative URI. This is permitted **only** when forwarding
1670 // HTTP 1.x requests. If the HTTP version is set to 2.0, then
1671 // this is an error.
1672 //
1673 // 2) Authority is set, then the HTTP method *must* be CONNECT.
1674 //
1675 // It is not possible to have a scheme but not an authority set (the
1676 // `http` crate does not allow it).
1677 //
1678 if pseudo.authority.is_none() {
1679 if version == Version::HTTP_2 {
1680 return Err(UserError::MissingUriSchemeAndAuthority.into());
1681 } else {
1682 // This is acceptable as per the above comment. However,
1683 // HTTP/2 requires that a scheme is set. Since we are
1684 // forwarding an HTTP 1.1 request, the scheme is set to
1685 // "http".
1686 pseudo.set_scheme(uri::Scheme::HTTP);
1687 }
1688 } else if !is_connect {
1689 // TODO: Error
1690 }
1691 }
1692
1693 // Create the HEADERS frame
1694 let mut frame = Headers::new(id, pseudo, headers);
1695
1696 if end_of_stream {
1697 frame.set_end_stream()
1698 }
1699
1700 Ok(frame)
1701 }
1702}
1703
1704impl proto::Peer for Peer {
1705 type Poll = Response<()>;
1706
1707 const NAME: &'static str = "Client";
1708
1709 fn r#dyn() -> proto::DynPeer {
1710 proto::DynPeer::Client
1711 }
1712
1713 /*
1714 fn is_server() -> bool {
1715 false
1716 }
1717 */
1718
1719 fn convert_poll_message(
1720 pseudo: Pseudo,
1721 fields: HeaderMap,
1722 stream_id: StreamId,
1723 ) -> Result<Self::Poll, Error> {
1724 let mut b = Response::builder();
1725
1726 b = b.version(Version::HTTP_2);
1727
1728 if let Some(status) = pseudo.status {
1729 b = b.status(status);
1730 } else {
1731 // Every response must include :status (RFC 9113, section 8.3.2).
1732 // https://www.rfc-editor.org/rfc/rfc9113.html#section-8.3.2
1733 proto_err!(stream: "missing :status; stream={:?}", stream_id);
1734 return Err(Error::library_reset(stream_id, Reason::PROTOCOL_ERROR));
1735 }
1736
1737 let mut response = match b.body(()) {
1738 Ok(response) => response,
1739 Err(_) => {
1740 // TODO: Should there be more specialized handling for different
1741 // kinds of errors
1742 return Err(Error::library_reset(stream_id, Reason::PROTOCOL_ERROR));
1743 }
1744 };
1745
1746 *response.headers_mut() = fields;
1747
1748 Ok(response)
1749 }
1750}