Allow responses of HEAD requests to have empty DATA frames (#490)

This commit is contained in:
Yuchen Wu
2020-10-22 14:36:41 -07:00
committed by GitHub
parent cb2c7ac72b
commit 0ba7d13ae5
2 changed files with 49 additions and 1 deletions

View File

@@ -286,7 +286,11 @@ impl Stream {
Some(val) => *rem = val, Some(val) => *rem = val,
None => return Err(()), None => return Err(()),
}, },
ContentLength::Head => return Err(()), ContentLength::Head => {
if len != 0 {
return Err(());
}
}
_ => {} _ => {}
} }

View File

@@ -1171,6 +1171,50 @@ async fn malformed_response_headers_dont_unlink_stream() {
join(srv, client).await; join(srv, client).await;
} }
#[tokio::test]
async fn allow_empty_data_for_head() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();
let srv = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);
srv.recv_frame(
frames::headers(1)
.request("HEAD", "https://example.com/")
.eos(),
)
.await;
srv.send_frame(
frames::headers(1)
.response(200)
.field("content-length", 100),
)
.await;
srv.send_frame(frames::data(1, "").eos()).await;
};
let h2 = async move {
let (mut client, h2) = client::Builder::new()
.handshake::<_, Bytes>(io)
.await
.unwrap();
tokio::spawn(async {
h2.await.expect("connection failed");
});
let request = Request::builder()
.method(Method::HEAD)
.uri("https://example.com/")
.body(())
.unwrap();
let (response, _) = client.send_request(request, true).unwrap();
let (_, mut body) = response.await.unwrap().into_parts();
assert_eq!(body.data().await.unwrap().unwrap(), "");
};
join(srv, h2).await;
}
const SETTINGS: &'static [u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0]; const SETTINGS: &'static [u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0];
const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0]; const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0];