Skip to content

Commit 8cbf952

Browse files
authoredMar 5, 2021
fix(server): skip automatic Content-Length headers when not allowed (#2216)
Closes #2215
1 parent f162ca2 commit 8cbf952

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed
 

‎src/proto/h1/role.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,13 @@ impl Http1Transaction for Server {
562562
}
563563
}
564564
None | Some(BodyLength::Known(0)) => {
565-
if msg.head.subject != StatusCode::NOT_MODIFIED {
565+
if Server::can_have_content_length(msg.req_method, msg.head.subject) {
566566
extend(dst, b"content-length: 0\r\n");
567567
}
568568
Encoder::length(0)
569569
}
570570
Some(BodyLength::Known(len)) => {
571-
if msg.head.subject == StatusCode::NOT_MODIFIED {
571+
if !Server::can_have_content_length(msg.req_method, msg.head.subject) {
572572
Encoder::length(0)
573573
} else {
574574
extend(dst, b"content-length: ");
@@ -638,13 +638,22 @@ impl Server {
638638
if method == &Some(Method::HEAD) || method == &Some(Method::CONNECT) && status.is_success()
639639
{
640640
false
641+
} else if status.is_informational() {
642+
false
643+
} else {
644+
match status {
645+
StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false,
646+
_ => true,
647+
}
648+
}
649+
}
650+
651+
fn can_have_content_length(method: &Option<Method>, status: StatusCode) -> bool {
652+
if status.is_informational() || method == &Some(Method::CONNECT) && status.is_success() {
653+
false
641654
} else {
642655
match status {
643-
// TODO: support for 1xx codes needs improvement everywhere
644-
// would be 100...199 => false
645-
StatusCode::SWITCHING_PROTOCOLS
646-
| StatusCode::NO_CONTENT
647-
| StatusCode::NOT_MODIFIED => false,
656+
StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false,
648657
_ => true,
649658
}
650659
}

‎tests/server.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,9 @@ async fn upgrades_new() {
13301330
let mut buf = [0; 256];
13311331
tcp.read(&mut buf).expect("read 1");
13321332

1333-
let expected = "HTTP/1.1 101 Switching Protocols\r\n";
1334-
assert_eq!(s(&buf[..expected.len()]), expected);
1333+
let response = s(&buf);
1334+
assert!(response.starts_with("HTTP/1.1 101 Switching Protocols\r\n"));
1335+
assert!(!has_header(&response, "content-length"));
13351336
let _ = read_101_tx.send(());
13361337

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

0 commit comments

Comments
 (0)
Please sign in to comment.