Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http2): allow HTTP/2 requests by ALPN when http2_only is unset #2527

Merged
merged 1 commit into from May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/client/client.rs
Expand Up @@ -167,11 +167,7 @@ where
)));
}
}
other_h2 @ Version::HTTP_2 => {
if self.config.ver != Ver::Http2 {
return ResponseFuture::error_version(other_h2);
}
}
Version::HTTP_2 => (),
// completely unsupported HTTP version (like HTTP/0.9)!
other => return ResponseFuture::error_version(other),
};
Expand Down Expand Up @@ -227,6 +223,13 @@ where
let mut pooled = self.connection_for(pool_key).await?;

if pooled.is_http1() {
if req.version() == Version::HTTP_2 {
warn!("Connection is HTTP/1, but request requires HTTP/2");
return Err(ClientError::Normal(
crate::Error::new_user_unsupported_version(),
));
}

if self.config.set_host {
let uri = req.uri().clone();
req.headers_mut().entry(HOST).or_insert_with(|| {
Expand Down
12 changes: 11 additions & 1 deletion tests/client.rs
Expand Up @@ -2039,9 +2039,19 @@ mod dispatch_impl {
// so the unwrapped responses futures show it still worked.
assert_eq!(connects.load(Ordering::SeqCst), 3);

let res4 = client.get(url);
let res4 = client.get(url.clone());
rt.block_on(res4).unwrap();

// HTTP/2 request allowed
let res5 = client.request(
Request::builder()
.uri(url)
.version(hyper::Version::HTTP_2)
.body(Default::default())
.unwrap(),
);
rt.block_on(res5).unwrap();

assert_eq!(
connects.load(Ordering::SeqCst),
3,
Expand Down