Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,9 @@ where
// to do only speak HTTP/1.0 as well.
head.version = Version::HTTP_10;
}
Version::HTTP_11 => {
// A 101 hands off the connection to another protocol, so disabled
// HTTP keep-alive must not add a close option to the upgrade response
Version::HTTP_11 if !T::is_switching_protocols(head) => {
if let KA::Disabled = self.state.keep_alive.status() {
match head.headers.entry(CONNECTION) {
Entry::Occupied(mut entry) => {
Expand Down
4 changes: 4 additions & 0 deletions src/proto/h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pub(crate) trait Http1Transaction {

fn on_error(err: &crate::Error) -> Option<MessageHead<Self::Outgoing>>;

fn is_switching_protocols(_head: &MessageHead<Self::Outgoing>) -> bool {
false
}

fn is_client() -> bool {
!Self::is_server()
}
Expand Down
4 changes: 4 additions & 0 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,10 @@ impl Http1Transaction for Server {
Some(msg)
}

fn is_switching_protocols(head: &MessageHead<Self::Outgoing>) -> bool {
head.subject == StatusCode::SWITCHING_PROTOCOLS
}

fn is_server() -> bool {
true
}
Expand Down
12 changes: 9 additions & 3 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,18 +1819,23 @@ async fn upgrades_new() {
b"\
GET / HTTP/1.1\r\n\
Upgrade: foobar\r\n\
Connection: upgrade\r\n\
Connection: close, Upgrade\r\n\
\r\n\
eagerly optimistic\
",
)
.expect("write 1");
let mut buf = [0; 256];
tcp.read(&mut buf).expect("read 1");
let response = read_until(&mut tcp, |buf| buf.ends_with(b"\r\n\r\n")).expect("read 1");

let response = s(&buf);
let response = s(&response);
assert!(response.starts_with("HTTP/1.1 101 Switching Protocols\r\n"));
assert!(!has_header(response, "content-length"));
assert!(
response.contains("\r\nconnection: upgrade\r\n"),
"{response}"
);
assert!(!response.contains("close"), "{response}");
let _ = read_101_tx.send(());

let n = tcp.read(&mut buf).expect("read 2");
Expand All @@ -1846,6 +1851,7 @@ async fn upgrades_new() {
Response::builder()
.status(101)
.header("upgrade", "foobar")
.header("connection", "upgrade")
.body(Empty::<Bytes>::new())
.unwrap(),
)
Expand Down
Loading