From be9677a1e782d33c4402772e0fc4ef0a4c49d507 Mon Sep 17 00:00:00 2001 From: Geoffry Song Date: Wed, 19 May 2021 10:01:20 -0700 Subject: [PATCH] feat(http2): allow HTTP/2 requests by ALPN when http2_only is unset (#2527) --- src/client/client.rs | 13 ++++++++----- tests/client.rs | 12 +++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/client/client.rs b/src/client/client.rs index 90d94e12ed..3b6a0d9f31 100644 --- a/src/client/client.rs +++ b/src/client/client.rs @@ -170,11 +170,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), }; @@ -230,6 +226,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(|| { diff --git a/tests/client.rs b/tests/client.rs index 52747a30c6..978f79a1d1 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -2116,9 +2116,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,