Skip to content

Commit

Permalink
feat(http2): Make HTTP/2 support an optional feature
Browse files Browse the repository at this point in the history
cc #2251

BREAKING CHANGE: This puts all HTTP/2 methods and support behind an
  `http2` cargo feature, which will not be enabled by default. To use
  HTTP/2, add `features = ["http2"]` to the hyper dependency in your
  `Cargo.toml`.
  • Loading branch information
seanmonstar committed Nov 10, 2020
1 parent 5438e9b commit b819b42
Show file tree
Hide file tree
Showing 19 changed files with 394 additions and 111 deletions.
27 changes: 21 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ http = "0.2"
http-body = "0.3.1"
httpdate = "0.3"
httparse = "1.0"
h2 = { git = "https://github.com/hyperium/h2" }
h2 = { git = "https://github.com/hyperium/h2", optional = true }
itoa = "0.4.1"
tracing = { version = "0.1", default-features = false, features = ["log", "std"] }
pin-project = "1.0"
Expand All @@ -56,6 +56,7 @@ tokio = { version = "0.3", features = [
"fs",
"macros",
"io-std",
"io-util",
"rt",
"rt-multi-thread", # so examples can use #[tokio::main]
"sync",
Expand All @@ -73,7 +74,16 @@ pnet = "0.25.0"
[features]
default = [
"runtime",
"stream"
"stream",

#"http1",
"http2",
]
full = [
#"http1",
"http2",
"stream",
"runtime",
]
runtime = [
"tcp",
Expand All @@ -86,6 +96,10 @@ tcp = [
"tokio/time",
]

# HTTP versions
#http1 = []
http2 = ["h2"]

# `impl Stream` for things
stream = []

Expand All @@ -94,10 +108,11 @@ nightly = []
__internal_happy_eyeballs_tests = []

[package.metadata.docs.rs]
features = [
"runtime",
"stream",
]
features = ["full"]
rustdoc-args = ["--cfg", "docsrs"]

[package.metadata.playground]
features = ["full"]

[profile.release]
codegen-units = 1
Expand Down
32 changes: 22 additions & 10 deletions src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use http_body::{Body as HttpBody, SizeHint};
#[cfg(feature = "stream")]
use crate::common::sync_wrapper::SyncWrapper;
use crate::common::{task, watch, Future, Never, Pin, Poll};
#[cfg(feature = "http2")]
use crate::proto::h2::ping;
use crate::proto::DecodedLength;
use crate::upgrade::OnUpgrade;
Expand All @@ -39,6 +40,7 @@ enum Kind {
want_tx: watch::Sender,
rx: mpsc::Receiver<Result<Bytes, crate::Error>>,
},
#[cfg(feature = "http2")]
H2 {
ping: ping::Recorder,
content_length: DecodedLength,
Expand Down Expand Up @@ -186,6 +188,7 @@ impl Body {
Body { kind, extra: None }
}

#[cfg(feature = "http2")]
pub(crate) fn h2(
recv: h2::RecvStream,
content_length: DecodedLength,
Expand Down Expand Up @@ -273,6 +276,7 @@ impl Body {
None => Poll::Ready(None),
}
}
#[cfg(feature = "http2")]
Kind::H2 {
ref ping,
recv: ref mut h2,
Expand Down Expand Up @@ -325,10 +329,11 @@ impl HttpBody for Body {
}

fn poll_trailers(
mut self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
#[cfg_attr(not(feature = "http2"), allow(unused_mut))] mut self: Pin<&mut Self>,
#[cfg_attr(not(feature = "http2"), allow(unused))] cx: &mut task::Context<'_>,
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
match self.kind {
#[cfg(feature = "http2")]
Kind::H2 {
recv: ref mut h2,
ref ping,
Expand All @@ -348,27 +353,34 @@ impl HttpBody for Body {
match self.kind {
Kind::Once(ref val) => val.is_none(),
Kind::Chan { content_length, .. } => content_length == DecodedLength::ZERO,
#[cfg(feature = "http2")]
Kind::H2 { recv: ref h2, .. } => h2.is_end_stream(),
#[cfg(feature = "stream")]
Kind::Wrapped(..) => false,
}
}

fn size_hint(&self) -> SizeHint {
match self.kind {
Kind::Once(Some(ref val)) => SizeHint::with_exact(val.len() as u64),
Kind::Once(None) => SizeHint::with_exact(0),
#[cfg(feature = "stream")]
Kind::Wrapped(..) => SizeHint::default(),
Kind::Chan { content_length, .. } | Kind::H2 { content_length, .. } => {
macro_rules! opt_len {
($content_length:expr) => {{
let mut hint = SizeHint::default();

if let Some(content_length) = content_length.into_opt() {
if let Some(content_length) = $content_length.into_opt() {
hint.set_exact(content_length);
}

hint
}
}};
}

match self.kind {
Kind::Once(Some(ref val)) => SizeHint::with_exact(val.len() as u64),
Kind::Once(None) => SizeHint::with_exact(0),
#[cfg(feature = "stream")]
Kind::Wrapped(..) => SizeHint::default(),
Kind::Chan { content_length, .. } => opt_len!(content_length),
#[cfg(feature = "http2")]
Kind::H2 { content_length, .. } => opt_len!(content_length),
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/cfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
macro_rules! cfg_http2 {
($($item:item)*) => {
$(
#[cfg(feature = "http2")]
//#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
$item
)*
}
}
Loading

0 comments on commit b819b42

Please sign in to comment.