Skip to content

Commit

Permalink
fix(server): skip automatic Content-Length headers when not allowed
Browse files Browse the repository at this point in the history
Closes #2215
  • Loading branch information
psmit committed Jun 3, 2020
1 parent 2354a7e commit f7ceff8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/proto/h1/role.rs
Expand Up @@ -520,13 +520,13 @@ impl Http1Transaction for Server {
}
}
None | Some(BodyLength::Known(0)) => {
if msg.head.subject != StatusCode::NOT_MODIFIED {
if Server::can_have_body(msg.req_method, msg.head.subject) {
extend(dst, b"content-length: 0\r\n");
}
Encoder::length(0)
}
Some(BodyLength::Known(len)) => {
if msg.head.subject == StatusCode::NOT_MODIFIED {
if !Server::can_have_body(msg.req_method, msg.head.subject) {
Encoder::length(0)
} else {
extend(dst, b"content-length: ");
Expand Down Expand Up @@ -595,13 +595,11 @@ impl Server {
if method == &Some(Method::HEAD) || method == &Some(Method::CONNECT) && status.is_success()
{
false
} else if status.is_informational() {
false
} else {
match status {
// TODO: support for 1xx codes needs improvement everywhere
// would be 100...199 => false
StatusCode::SWITCHING_PROTOCOLS
| StatusCode::NO_CONTENT
| StatusCode::NOT_MODIFIED => false,
StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false,
_ => true,
}
}
Expand Down
8 changes: 5 additions & 3 deletions tests/server.rs
Expand Up @@ -498,7 +498,7 @@ fn head_response_doesnt_send_body() {
let mut response = String::new();
req.read_to_string(&mut response).unwrap();

assert!(response.contains("content-length: 11\r\n"));
assert!(!has_header(&response, "content-length"));

let mut lines = response.lines();
assert_eq!(lines.next(), Some("HTTP/1.1 200 OK"));
Expand Down Expand Up @@ -1327,13 +1327,15 @@ async fn upgrades_new() {
let mut buf = [0; 256];
tcp.read(&mut buf).expect("read 1");

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

let n = tcp.read(&mut buf).expect("read 2");
assert_eq!(s(&buf[..n]), "foo=bar");
tcp.write_all(b"bar=foo").expect("write 2");

});

let (upgrades_tx, upgrades_rx) = mpsc::channel();
Expand Down

0 comments on commit f7ceff8

Please sign in to comment.