Skip to content

Commit e5cc615

Browse files
committed
Don't send Content-Length on HTTP/1.1 100 Continue
Backported from hyperium#2216.
1 parent a00cc20 commit e5cc615

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/proto/h1/role.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,13 @@ impl Http1Transaction for Server {
550550
}
551551
}
552552
None | Some(BodyLength::Known(0)) => {
553-
if msg.head.subject != StatusCode::NOT_MODIFIED {
553+
if Server::can_have_content_length(msg.req_method, msg.head.subject) {
554554
extend(dst, b"content-length: 0\r\n");
555555
}
556556
Encoder::length(0)
557557
}
558558
Some(BodyLength::Known(len)) => {
559-
if msg.head.subject == StatusCode::NOT_MODIFIED {
559+
if !Server::can_have_content_length(msg.req_method, msg.head.subject) {
560560
Encoder::length(0)
561561
} else {
562562
extend(dst, b"content-length: ");
@@ -625,13 +625,22 @@ impl Server {
625625
if method == &Some(Method::HEAD) || method == &Some(Method::CONNECT) && status.is_success()
626626
{
627627
false
628+
} else if status.is_informational() {
629+
false
630+
} else {
631+
match status {
632+
StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false,
633+
_ => true,
634+
}
635+
}
636+
}
637+
638+
fn can_have_content_length(method: &Option<Method>, status: StatusCode) -> bool {
639+
if status.is_informational() || method == &Some(Method::CONNECT) && status.is_success() {
640+
false
628641
} else {
629642
match status {
630-
// TODO: support for 1xx codes needs improvement everywhere
631-
// would be 100...199 => false
632-
StatusCode::SWITCHING_PROTOCOLS
633-
| StatusCode::NO_CONTENT
634-
| StatusCode::NOT_MODIFIED => false,
643+
StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false,
635644
_ => true,
636645
}
637646
}

tests/server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,9 @@ async fn upgrades_new() {
13271327
let mut buf = [0; 256];
13281328
tcp.read(&mut buf).expect("read 1");
13291329

1330-
let expected = "HTTP/1.1 101 Switching Protocols\r\n";
1331-
assert_eq!(s(&buf[..expected.len()]), expected);
1330+
let response = s(&buf);
1331+
assert!(response.starts_with("HTTP/1.1 101 Switching Protocols\r\n"));
1332+
assert!(!has_header(&response, "content-length"));
13321333
let _ = read_101_tx.send(());
13331334

13341335
let n = tcp.read(&mut buf).expect("read 2");

0 commit comments

Comments
 (0)