Skip to content

feat: make HTTP/2 a cargo feature - #45

Merged
BKPepe merged 2 commits into
masterfrom
feat-http2-feature
Nov 15, 2026
Merged

BKPepe merged 2 commits into
masterfrom
feat-http2-feature

Conversation

@BKPepe

@BKPepe BKPepe commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

--http2 is opt-in at run time, but h2 was compiled into every build. This makes it the http2 cargo feature. It is in the default set, so release tarballs and cargo install stay as they are.

A build without it hides --http2 from --help and refuses it with this build has no HTTP/2 support, so --http2 cannot be used. h2 is then gone from the dependency tree, dev-dependencies included:

cargo tree -e features -i h2 h2
--no-default-features --features native-tls not in the tree
--no-default-features --features rustls-tls not in the tree
--no-default-features --features native-tls,http2 0.4.20, through hyper and hyper-util
default 0.4.20, through hyper, hyper-util and hyper-rustls

Size of native-tls builds from the same commit, Rust 1.96.1, aarch64-unknown-linux-gnu, with OpenWrt's release profile (opt-level z, LTO, codegen-units 1, panic unwind), stripped:

with http2 without
ELF 2036944 B 1774672 B (-12.9 %)
gzip -9 1003295 B 877386 B (-12.5 %)
crates in the normal dependency tree 140 133

The OpenWrt package builds with --no-default-features, so the first release with this change drops HTTP/2 there. A Config.in switch, off by default, will bring it back for those who want it.

tests/alpn.rs runs the binary against a TLS server in the test process and checks what the client offers in ALPN and which protocol it then speaks. Without --http2 neither backend sends ALPN and the client speaks HTTP/1.1. With it the client offers h2 and http/1.1, speaks HTTP/2 when h2 is agreed and falls back to HTTP/1.1 otherwise. CI now tests rustls-tls and native-tls each with and without http2, and the MSRV job checks all four.

Checked on Linux: fmt, clippy and cargo test for all four combinations, and cargo check with Rust 1.93.1 for all four. By hand against nginx with HTTP/2 enabled: builds without the feature refuse --http2 and speak HTTP/1.1, builds with it speak HTTP/2 with --http2 and HTTP/1.1 without.

@BKPepe
BKPepe force-pushed the feat-http2-feature branch from 984960d to ddbbca1 Compare November 10, 2026 10:56
--http2 has to be asked for, yet h2 was compiled into every build. It is
now the http2 feature, part of the default set, so the release tarballs
and cargo install do not change.

A build without it hides --http2 from --help and refuses it, instead of
quietly running the test over HTTP/1.1. In the shape OpenWrt builds, the
binary is 12.5 % smaller that way, 877 KB against 1003 KB with gzip on
aarch64, and links 133 crates instead of 140. The OpenWrt package builds
with --no-default-features, so it leaves HTTP/2 out.

CI tests rustls-tls and native-tls each with and without http2, and the
MSRV job checks all four.
The backend of the integration tests is plain HTTP/1.1, so nothing
checked ALPN or h2 against a real handshake. tests/alpn.rs runs the
binary against a rustls server in the test process and records what the
client offers, what is agreed and the first bytes it sends.

Without --http2 neither backend sends ALPN and the client speaks
HTTP/1.1. With it the client offers h2 and http/1.1, sends the HTTP/2
preface when h2 is agreed, and falls back to HTTP/1.1 when the server
picks http/1.1 or does not use ALPN. rustls is a dev-dependency, so the
test also runs with native-tls.

The comment in tls.rs said hyper-rustls sets ALPN from enable_http1().
It leaves ALPN empty then.
@BKPepe
BKPepe force-pushed the feat-http2-feature branch from ddbbca1 to 22c6d48 Compare November 15, 2026 02:02
@BKPepe
BKPepe merged commit 5c44cce into master Nov 15, 2026
21 checks passed
@BKPepe
BKPepe deleted the feat-http2-feature branch November 17, 2026 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant