Skip to content

Commit

Permalink
fix(server): skip automatic Content-Length header for HTTP 304 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs committed Apr 25, 2019
1 parent 4133181 commit a26e802
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,9 @@ impl Http1Transaction for Server {
},
None |
Some(BodyLength::Known(0)) => {
extend(dst, b"content-length: 0\r\n");
if msg.head.subject != StatusCode::NOT_MODIFIED {
extend(dst, b"content-length: 0\r\n");
}
Encoder::length(0)
},
Some(BodyLength::Known(len)) => {
Expand Down Expand Up @@ -1591,4 +1593,3 @@ mod tests {
})
}
}

19 changes: 18 additions & 1 deletion tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,24 @@ fn http2_service_poll_ready_error_sends_goaway() {
assert_eq!(h2_err.reason(), Some(h2::Reason::INADEQUATE_SECURITY));
}

#[test]
fn skips_content_length_for_304_responses() {
let server = serve();
server.reply()
.status(hyper::StatusCode::NOT_MODIFIED);
let mut req = connect(server.addr());
req.write_all(b"\
GET / HTTP/1.1\r\n\
Host: example.domain\r\n\
Connection: close\r\n\
\r\n\
").unwrap();

let mut response = String::new();
req.read_to_string(&mut response).unwrap();
assert!(!response.contains("content-length:"));
}

// -------------------------------------------------
// the Server that is used to run all the tests with
// -------------------------------------------------
Expand Down Expand Up @@ -2058,4 +2076,3 @@ impl Drop for Dropped {
self.0.store(true, Ordering::SeqCst);
}
}

0 comments on commit a26e802

Please sign in to comment.