Skip to content

Commit ef1054a

Browse files
committed
feat(upgrade): add upgrade feature
1 parent 8ebe25c commit ef1054a

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

Cargo.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ futures-util = { version = "0.3", default-features = false }
2626
http = "0.2"
2727
http-body = "=1.0.0-rc.2"
2828
pin-project-lite = "0.2.4"
29-
tokio = { version = "1.13", features = ["sync"] }
3029

3130
# Optional
3231

@@ -36,6 +35,7 @@ httparse = { version = "1.8", optional = true }
3635
httpdate = { version = "1.0", optional = true }
3736
itoa = { version = "1", optional = true }
3837
libc = { version = "0.2", optional = true }
38+
tokio = { version = "1", features = ["sync"], optional = true }
3939
tracing = { version = "0.1", default-features = false, features = ["std"], optional = true }
4040
want = { version = "0.3", optional = true }
4141

@@ -74,13 +74,16 @@ full = [
7474
]
7575

7676
# HTTP versions
77-
http1 = ["dep:httparse", "dep:itoa"]
78-
http2 = ["dep:h2"]
77+
http1 = ["upgrade", "dep:httparse", "dep:itoa"]
78+
http2 = ["upgrade", "dep:h2"]
7979

8080
# Client/Server
8181
client = ["dep:want"]
8282
server = ["dep:httpdate"]
8383

84+
# HTTP Upgrades
85+
upgrade = ["dep:tokio"]
86+
8487
# C-API support (currently unstable (no semver))
8588
ffi = ["dep:libc", "dep:http-body-util"]
8689

src/common/io/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))]
22
mod compat;
3+
#[cfg(feature = "upgrade")]
34
mod rewind;
45

56
#[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))]
67
pub(crate) use self::compat::{compat, Compat};
8+
#[cfg(feature = "upgrade")]
79
pub(crate) use self::rewind::Rewind;

src/error.rs

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub(super) enum Kind {
4848
#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
4949
UnexpectedMessage,
5050
/// A pending item was dropped before ever being processed.
51+
#[cfg(feature = "upgrade")]
5152
Canceled,
5253
/// Indicates a channel (client or body sender) is closed.
5354
ChannelClosed,
@@ -140,6 +141,7 @@ pub(super) enum User {
140141
UnsupportedStatusCode,
141142

142143
/// User tried polling for an upgrade that doesn't exist.
144+
#[cfg(feature = "upgrade")]
143145
NoUpgrade,
144146

145147
/// User polled for an upgrade, but low-level API is not using upgrades.
@@ -186,6 +188,7 @@ impl Error {
186188
}
187189

188190
/// Returns true if this was about a `Request` that was canceled.
191+
#[cfg(feature = "upgrade")]
189192
pub fn is_canceled(&self) -> bool {
190193
matches!(self.inner.kind, Kind::Canceled)
191194
}
@@ -216,6 +219,7 @@ impl Error {
216219
}
217220
}
218221

222+
#[cfg(feature = "upgrade")]
219223
pub(super) fn with<C: Into<Cause>>(mut self, cause: C) -> Error {
220224
self.inner.cause = Some(cause.into());
221225
self
@@ -248,6 +252,7 @@ impl Error {
248252
.unwrap_or(h2::Reason::INTERNAL_ERROR)
249253
}
250254

255+
#[cfg(feature = "upgrade")]
251256
pub(super) fn new_canceled() -> Error {
252257
Error::new(Kind::Canceled)
253258
}
@@ -304,6 +309,7 @@ impl Error {
304309
Error::new(Kind::User(User::BodyWriteAborted))
305310
}
306311

312+
#[cfg(feature = "upgrade")]
307313
fn new_user(user: User) -> Error {
308314
Error::new(Kind::User(user))
309315
}
@@ -325,6 +331,7 @@ impl Error {
325331
Error::new_user(User::UnsupportedStatusCode)
326332
}
327333

334+
#[cfg(feature = "upgrade")]
328335
pub(super) fn new_user_no_upgrade() -> Error {
329336
Error::new_user(User::NoUpgrade)
330337
}
@@ -408,6 +415,7 @@ impl Error {
408415
#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
409416
Kind::UnexpectedMessage => "received unexpected message from connection",
410417
Kind::ChannelClosed => "channel closed",
418+
#[cfg(feature = "upgrade")]
411419
Kind::Canceled => "operation was canceled",
412420
#[cfg(all(feature = "http1", feature = "server"))]
413421
Kind::HeaderTimeout => "read header from client timeout",
@@ -450,6 +458,7 @@ impl Error {
450458
Kind::User(User::UnsupportedStatusCode) => {
451459
"response has 1xx status code, not supported by server"
452460
}
461+
#[cfg(feature = "upgrade")]
453462
Kind::User(User::NoUpgrade) => "no upgrade available",
454463
#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
455464
Kind::User(User::ManualUpgrade) => "upgrade expected but low level API in use",

src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
//! - `http2`: Enables HTTP/2 support.
5050
//! - `client`: Enables the HTTP `client`.
5151
//! - `server`: Enables the HTTP `server`.
52+
//! - `upgrade`: Enables [HTTP Upgrades].
5253
//!
5354
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
55+
//! [Http Upgrades]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism
5456
//!
5557
//! # Unstable Features
5658
//! hyper includes a set of unstable optional features that can be enabled through the use of a
@@ -93,7 +95,6 @@ pub mod ext;
9395
mod mock;
9496
pub mod rt;
9597
pub mod service;
96-
pub mod upgrade;
9798

9899
#[cfg(feature = "ffi")]
99100
#[cfg_attr(docsrs, doc(cfg(all(feature = "ffi", hyper_unstable_ffi))))]
@@ -115,3 +116,9 @@ cfg_feature! {
115116

116117
pub mod server;
117118
}
119+
120+
cfg_feature! {
121+
#![feature = "upgrade"]
122+
123+
pub mod upgrade;
124+
}

src/rt/io.rs

+3
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ impl<'data> ReadBuf<'data> {
200200
}
201201

202202
#[inline]
203+
#[cfg(feature = "upgrade")]
203204
fn remaining(&self) -> usize {
204205
self.capacity() - self.filled
205206
}
@@ -244,11 +245,13 @@ impl<'data> ReadBufCursor<'data> {
244245
}
245246

246247
#[inline]
248+
#[cfg(feature = "upgrade")]
247249
pub(crate) fn remaining(&self) -> usize {
248250
self.buf.remaining()
249251
}
250252

251253
#[inline]
254+
#[cfg(feature = "upgrade")]
252255
pub(crate) fn put_slice(&mut self, buf: &[u8]) {
253256
assert!(
254257
self.buf.remaining() >= buf.len(),

0 commit comments

Comments
 (0)