Skip to content

Commit

Permalink
fix(server): coerce responses with HTTP2 version to HTTP/1.1 when pro…
Browse files Browse the repository at this point in the history
…tocol is 1.x
  • Loading branch information
seanmonstar committed Aug 10, 2018
1 parent 853266d commit 195fbb2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ impl Http1Transaction for Server {
match msg.head.version {
Version::HTTP_10 => extend(dst, b"HTTP/1.0 "),
Version::HTTP_11 => extend(dst, b"HTTP/1.1 "),
Version::HTTP_2 => {
warn!("response with HTTP2 version coerced to HTTP/1.1");
extend(dst, b"HTTP/1.1 ");
},
_ => unreachable!(),
}

Expand Down
28 changes: 28 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,25 @@ fn streaming_body() {
rt.block_on(fut.join(rx)).unwrap();
}

#[test]
fn http1_response_with_http2_version() {
let server = serve();
let addr_str = format!("http://{}", server.addr());

let mut rt = Runtime::new().expect("runtime new");

server
.reply()
.version(hyper::Version::HTTP_2);

rt.block_on(hyper::rt::lazy(move || {
let client = Client::new();
let uri = addr_str.parse().expect("server addr should parse");

client.get(uri)
})).unwrap();
}

#[test]
fn try_h2() {
let server = serve();
Expand Down Expand Up @@ -1641,6 +1660,11 @@ impl<'a> ReplyBuilder<'a> {
self
}

fn version(self, version: hyper::Version) -> Self {
self.tx.send(Reply::Version(version)).unwrap();
self
}

fn header<V: AsRef<str>>(self, name: &str, value: V) -> Self {
let name = HeaderName::from_bytes(name.as_bytes()).expect("header name");
let value = HeaderValue::from_str(value.as_ref()).expect("header value");
Expand Down Expand Up @@ -1681,6 +1705,7 @@ struct TestService {
#[derive(Debug)]
enum Reply {
Status(hyper::StatusCode),
Version(hyper::Version),
Header(HeaderName, HeaderValue),
Body(hyper::Body),
End,
Expand Down Expand Up @@ -1718,6 +1743,9 @@ impl TestService {
Reply::Status(s) => {
*res.status_mut() = s;
},
Reply::Version(v) => {
*res.version_mut() = v;
},
Reply::Header(name, value) => {
res.headers_mut().insert(name, value);
},
Expand Down

0 comments on commit 195fbb2

Please sign in to comment.