|
1 |
| -//! HTTP extensions. |
| 1 | +//! Extensions for HTTP messages in Hyper. |
| 2 | +//! |
| 3 | +//! This module provides types and utilities that extend the capabilities of HTTP requests and responses |
| 4 | +//! in Hyper. Extensions are additional pieces of information or features that can be attached to HTTP |
| 5 | +//! messages via the [`http::Extensions`] map, which is |
| 6 | +//! accessible through methods like [`http::Request::extensions`] and [`http::Response::extensions`]. |
| 7 | +//! |
| 8 | +//! # What are extensions? |
| 9 | +//! |
| 10 | +//! Extensions allow Hyper to associate extra metadata or behaviors with HTTP messages, beyond the standard |
| 11 | +//! headers and body. These can be used by advanced users and library authors to access protocol-specific |
| 12 | +//! features, track original header casing, handle informational responses, and more. |
| 13 | +//! |
| 14 | +//! # How to access extensions |
| 15 | +//! |
| 16 | +//! Extensions are stored in the `Extensions` map of a request or response. You can access them using: |
| 17 | +//! |
| 18 | +//! ```rust |
| 19 | +//! # let response = http::Response::new(()); |
| 20 | +//! if let Some(ext) = response.extensions().get::<hyper::ext::ReasonPhrase>() { |
| 21 | +//! // use the extension |
| 22 | +//! } |
| 23 | +//! ``` |
| 24 | +//! |
| 25 | +//! # Extension Groups |
| 26 | +//! |
| 27 | +//! The extensions in this module can be grouped as follows: |
| 28 | +//! |
| 29 | +//! - **HTTP/1 Reason Phrase**: [`ReasonPhrase`] — Access non-canonical reason phrases in HTTP/1 responses. |
| 30 | +//! - **Informational Responses**: [`on_informational`] — Register callbacks for 1xx HTTP/1 responses on the client. |
| 31 | +//! - **Header Case Tracking**: Internal types for tracking the original casing and order of headers as received. |
| 32 | +//! - **HTTP/2 Protocol Extensions**: [`Protocol`] — Access the `:protocol` pseudo-header for Extended CONNECT in HTTP/2. |
| 33 | +//! |
| 34 | +//! Some extensions are only available for specific protocols (HTTP/1 or HTTP/2) or use cases (client, server, FFI). |
| 35 | +//! |
| 36 | +//! See the documentation on each item for details about its usage and requirements. |
2 | 37 |
|
3 | 38 | #[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
|
4 | 39 | use bytes::Bytes;
|
@@ -29,10 +64,24 @@ pub(crate) use informational::OnInformational;
|
29 | 64 | pub(crate) use informational::{on_informational_raw, OnInformationalCallback};
|
30 | 65 |
|
31 | 66 | #[cfg(feature = "http2")]
|
32 |
| -/// Represents the `:protocol` pseudo-header used by |
33 |
| -/// the [Extended CONNECT Protocol]. |
| 67 | +/// Extension type representing the `:protocol` pseudo-header in HTTP/2. |
34 | 68 | ///
|
35 |
| -/// [Extended CONNECT Protocol]: https://datatracker.ietf.org/doc/html/rfc8441#section-4 |
| 69 | +/// The `Protocol` extension allows access to the value of the `:protocol` pseudo-header |
| 70 | +/// used by the [Extended CONNECT Protocol](https://datatracker.ietf.org/doc/html/rfc8441#section-4). |
| 71 | +/// This extension is only sent on HTTP/2 CONNECT requests, most commonly with the value `websocket`. |
| 72 | +/// |
| 73 | +/// # Example |
| 74 | +/// |
| 75 | +/// ```rust |
| 76 | +/// use hyper::ext::Protocol; |
| 77 | +/// use http::{Request, Method, Version}; |
| 78 | +/// |
| 79 | +/// let mut req = Request::new(()); |
| 80 | +/// *req.method_mut() = Method::CONNECT; |
| 81 | +/// *req.version_mut() = Version::HTTP_2; |
| 82 | +/// req.extensions_mut().insert(Protocol::from_static("websocket")); |
| 83 | +/// // Now the request will include the `:protocol` pseudo-header with value "websocket" |
| 84 | +/// ``` |
36 | 85 | #[derive(Clone, Eq, PartialEq)]
|
37 | 86 | pub struct Protocol {
|
38 | 87 | inner: h2::ext::Protocol,
|
|
0 commit comments