Skip to content

Commit a12db28

Browse files
committed
docs(upgrade): add module documentation for HTTP upgrades
1 parent 7f5e853 commit a12db28

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/upgrade.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
//! HTTP Upgrades
22
//!
3+
//! This module deals with managing [HTTP Upgrades][mdn] in hyper. Since
4+
//! several concepts in HTTP allow for first talking HTTP, and then converting
5+
//! to a different protocol, this module conflates them into a single API.
6+
//! Those include:
7+
//!
8+
//! - HTTP/1.1 Upgrades
9+
//! - HTTP `CONNECT`
10+
//!
11+
//! You are responsible for any other pre-requisites to establish an upgrade,
12+
//! such as sending the appropriate headers, methods, and status codes. You can
13+
//! then use [`on`][] to grab a `Future` which will resolve to the upgraded
14+
//! connection object, or an error if the upgrade fails.
15+
//!
16+
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism
17+
//!
18+
//! # Client
19+
//!
20+
//! Sending an HTTP ugprade from the [`client`](super::client) involves setting
21+
//! either the appropriate method, if wanting to `CONNECT`, or headers such as
22+
//! `Upgrade` and `Connection`, on the `http::Request`. Once receiving the
23+
//! `http::Response` back, you must check for the specific information that the
24+
//! upgrade is agreed upon by the server (such as a `101` status code), and then
25+
//! get the `Future` from the `Response`.
26+
//!
27+
//! # Server
28+
//!
29+
//! Receiving upgrade requests in a server requires you to check the relevant
30+
//! headers in a `Request`, and if an upgrade should be done, you then send the
31+
//! corresponding headers in a response. To then wait for hyper to finish the
32+
//! upgrade, you call `on()` with the `Request`, and then can spawn a task
33+
//! awaiting it.
34+
//!
35+
//! # Example
36+
//!
337
//! See [this example][example] showing how upgrades work with both
438
//! Clients and Servers.
539
//!
@@ -60,6 +94,13 @@ pub struct Parts<T> {
6094
}
6195

6296
/// Gets a pending HTTP upgrade from this message.
97+
///
98+
/// This can be called on the following types:
99+
///
100+
/// - `http::Request<B>`
101+
/// - `http::Response<B>`
102+
/// - `&mut http::Request<B>`
103+
/// - `&mut http::Response<B>`
63104
pub fn on<T: sealed::CanUpgrade>(msg: T) -> OnUpgrade {
64105
msg.on_upgrade()
65106
}

0 commit comments

Comments
 (0)