Skip to content

Commit 36c6a65

Browse files
committed
fix(http1): send 'connection: close' during graceful shutdown
1 parent 7de0237 commit 36c6a65

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/proto/h1/conn.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::{Decoder, Encode, EncodedBuf, Encoder, Http1Transaction, ParseContext
2121
use crate::body::DecodedLength;
2222
#[cfg(feature = "server")]
2323
use crate::common::time::Time;
24-
use crate::headers::connection_keep_alive;
24+
use crate::headers;
2525
use crate::proto::{BodyLength, MessageHead};
2626
#[cfg(feature = "server")]
2727
use crate::rt::Sleep;
@@ -657,7 +657,7 @@ where
657657
let outgoing_is_keep_alive = head
658658
.headers
659659
.get(CONNECTION)
660-
.map_or(false, connection_keep_alive);
660+
.map_or(false, headers::connection_keep_alive);
661661

662662
if !outgoing_is_keep_alive {
663663
match head.version {
@@ -680,12 +680,22 @@ where
680680
// If we know the remote speaks an older version, we try to fix up any messages
681681
// to work with our older peer.
682682
fn enforce_version(&mut self, head: &mut MessageHead<T::Outgoing>) {
683-
if let Version::HTTP_10 = self.state.version {
684-
// Fixes response or connection when keep-alive header is not present
685-
self.fix_keep_alive(head);
686-
// If the remote only knows HTTP/1.0, we should force ourselves
687-
// to do only speak HTTP/1.0 as well.
688-
head.version = Version::HTTP_10;
683+
match self.state.version {
684+
Version::HTTP_10 => {
685+
// Fixes response or connection when keep-alive header is not present
686+
self.fix_keep_alive(head);
687+
// If the remote only knows HTTP/1.0, we should force ourselves
688+
// to do only speak HTTP/1.0 as well.
689+
head.version = Version::HTTP_10;
690+
},
691+
Version::HTTP_11 => {
692+
if let KA::Disabled = self.state.keep_alive.status() {
693+
head.headers
694+
.insert(CONNECTION, HeaderValue::from_static("close"));
695+
}
696+
697+
},
698+
_ => (),
689699
}
690700
// If the remote speaks HTTP/1.1, then it *should* be fine with
691701
// both HTTP/1.0 and HTTP/1.1 from us. So again, we just let

tests/server.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,12 @@ async fn disable_keep_alive_mid_request() {
12081208
"should receive OK response, but buf: {:?}",
12091209
buf,
12101210
);
1211+
let sbuf = s(&buf);
1212+
assert!(
1213+
sbuf.contains("connection: close\r\n"),
1214+
"response should have sent close: {:?}",
1215+
sbuf,
1216+
);
12111217
});
12121218

12131219
let (socket, _) = listener.accept().await.unwrap();

0 commit comments

Comments
 (0)