fix(server): skip automatic Content-Length headers when not allowed (#2216)
Closes #2215
This commit is contained in:
@@ -562,13 +562,13 @@ impl Http1Transaction for Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
None | Some(BodyLength::Known(0)) => {
|
None | Some(BodyLength::Known(0)) => {
|
||||||
if msg.head.subject != StatusCode::NOT_MODIFIED {
|
if Server::can_have_content_length(msg.req_method, msg.head.subject) {
|
||||||
extend(dst, b"content-length: 0\r\n");
|
extend(dst, b"content-length: 0\r\n");
|
||||||
}
|
}
|
||||||
Encoder::length(0)
|
Encoder::length(0)
|
||||||
}
|
}
|
||||||
Some(BodyLength::Known(len)) => {
|
Some(BodyLength::Known(len)) => {
|
||||||
if msg.head.subject == StatusCode::NOT_MODIFIED {
|
if !Server::can_have_content_length(msg.req_method, msg.head.subject) {
|
||||||
Encoder::length(0)
|
Encoder::length(0)
|
||||||
} else {
|
} else {
|
||||||
extend(dst, b"content-length: ");
|
extend(dst, b"content-length: ");
|
||||||
@@ -638,13 +638,22 @@ impl Server {
|
|||||||
if method == &Some(Method::HEAD) || method == &Some(Method::CONNECT) && status.is_success()
|
if method == &Some(Method::HEAD) || method == &Some(Method::CONNECT) && status.is_success()
|
||||||
{
|
{
|
||||||
false
|
false
|
||||||
|
} else if status.is_informational() {
|
||||||
|
false
|
||||||
} else {
|
} else {
|
||||||
match status {
|
match status {
|
||||||
// TODO: support for 1xx codes needs improvement everywhere
|
StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false,
|
||||||
// would be 100...199 => false
|
_ => true,
|
||||||
StatusCode::SWITCHING_PROTOCOLS
|
}
|
||||||
| StatusCode::NO_CONTENT
|
}
|
||||||
| StatusCode::NOT_MODIFIED => false,
|
}
|
||||||
|
|
||||||
|
fn can_have_content_length(method: &Option<Method>, status: StatusCode) -> bool {
|
||||||
|
if status.is_informational() || method == &Some(Method::CONNECT) && status.is_success() {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
match status {
|
||||||
|
StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false,
|
||||||
_ => true,
|
_ => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1330,8 +1330,9 @@ async fn upgrades_new() {
|
|||||||
let mut buf = [0; 256];
|
let mut buf = [0; 256];
|
||||||
tcp.read(&mut buf).expect("read 1");
|
tcp.read(&mut buf).expect("read 1");
|
||||||
|
|
||||||
let expected = "HTTP/1.1 101 Switching Protocols\r\n";
|
let response = s(&buf);
|
||||||
assert_eq!(s(&buf[..expected.len()]), expected);
|
assert!(response.starts_with("HTTP/1.1 101 Switching Protocols\r\n"));
|
||||||
|
assert!(!has_header(&response, "content-length"));
|
||||||
let _ = read_101_tx.send(());
|
let _ = read_101_tx.send(());
|
||||||
|
|
||||||
let n = tcp.read(&mut buf).expect("read 2");
|
let n = tcp.read(&mut buf).expect("read 2");
|
||||||
|
|||||||
Reference in New Issue
Block a user