Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/body/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enum Kind {
H2 {
content_length: DecodedLength,
data_done: bool,
is_client: bool,
ping: ping::Recorder,
recv: h2::RecvStream,
},
Expand Down Expand Up @@ -112,6 +113,7 @@ impl Incoming {
recv: h2::RecvStream,
mut content_length: DecodedLength,
ping: ping::Recorder,
is_client: bool,
) -> Self {
// If the stream is already EOS, then the "unknown length" is clearly
// actually ZERO.
Expand All @@ -121,6 +123,7 @@ impl Incoming {

Incoming::new(Kind::H2 {
data_done: false,
is_client,
ping,
content_length,
recv,
Expand Down Expand Up @@ -178,6 +181,7 @@ impl Body for Incoming {
#[cfg(all(feature = "http2", any(feature = "client", feature = "server")))]
Kind::H2 {
data_done,
is_client,
ping,
recv: h2,
content_length: len,
Expand All @@ -191,10 +195,13 @@ impl Body for Incoming {
return Poll::Ready(Some(Ok(Frame::data(bytes))));
}
Some(Err(e)) => {
if let Some(h2::Reason::NO_ERROR) = e.reason() {
// As mentioned in RFC 7540 Section 8.1, a RST_STREAM with NO_ERROR
// indicates an early response, and should cause the body reading
// to stop, but not fail it:
if *is_client && e.reason() == Some(h2::Reason::NO_ERROR) {
// As mentioned in RFC 9113 Section 8.1, a server sending
// RST_STREAM with NO_ERROR indicates an early response, and
// should cause the client's response body reading to stop
// without failing. On the server side, a client resetting its
// own request with NO_ERROR before END_STREAM is an incomplete
// body and must surface as an error.
return Poll::Ready(None);
} else {
return Poll::Ready(Some(Err(crate::Error::new_body(e))));
Expand All @@ -214,10 +221,8 @@ impl Body for Incoming {
Poll::Ready(Ok(t.map(Frame::trailers)).transpose())
}
Err(e) => {
if let Some(h2::Reason::NO_ERROR) = e.reason() {
// Same as above, a RST_STREAM with NO_ERROR indicates an early
// response, and should cause reading the trailers to stop, but
// not fail it:
if *is_client && e.reason() == Some(h2::Reason::NO_ERROR) {
// Same as above: only suppress NO_ERROR on the client side.
Poll::Ready(None)
} else {
Poll::Ready(Some(Err(crate::Error::new_h2(e))))
Expand All @@ -244,6 +249,7 @@ impl Body for Incoming {
recv: h2,
content_length: _,
data_done: _,
is_client: _,
ping: _,
} => h2.is_end_stream(),
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -275,6 +281,7 @@ impl Body for Incoming {
Kind::H2 {
content_length,
data_done: _,
is_client: _,
ping: _,
recv: _,
} => opt_len(content_length),
Expand Down
2 changes: 1 addition & 1 deletion src/proto/h2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ where
} else {
let res = res.map(|stream| {
let ping = ping.for_stream(&stream);
IncomingBody::h2(stream, content_length.into(), ping)
IncomingBody::h2(stream, content_length.into(), ping, true)
});
Poll::Ready(Ok(res))
}
Expand Down
2 changes: 1 addition & 1 deletion src/proto/h2/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ where
(
Request::from_parts(
parts,
IncomingBody::h2(stream, content_length.into(), ping),
IncomingBody::h2(stream, content_length.into(), ping, false),
),
None,
)
Expand Down
55 changes: 55 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,61 @@ async fn h2_connect_shutdown_while_send_backpressured() {
client_handle.await.unwrap();
}

#[tokio::test]
async fn h2_server_rst_stream_no_error_mid_body() {
for with_content_length in [true, false] {
let (listener, addr) = setup_tcp_listener();
let conn = connect_async(addr).await;

let (h2, connection) = h2::client::handshake(conn).await.unwrap();
tokio::spawn(async move {
let _ = connection.await;
});
let mut h2 = h2.ready().await.unwrap();

let (body_res_tx, body_res_rx) = oneshot::channel::<Result<Bytes, hyper::Error>>();
let body_res_tx = Arc::new(Mutex::new(Some(body_res_tx)));

let client_handle = tokio::spawn(async move {
let mut req_builder = Request::post("http://localhost/");
if with_content_length {
req_builder = req_builder.header("content-length", "10");
}
let request = req_builder.body(()).unwrap();
let (_response, mut send_stream) = h2.send_request(request, false).unwrap();
send_stream
.send_data(Bytes::from_static(b"abc"), false)
.unwrap();
send_stream.send_reset(h2::Reason::NO_ERROR);
drop(send_stream);

let res = body_res_rx.await.unwrap();
assert!(
res.is_err(),
"server should receive error on client RST_STREAM(NO_ERROR) mid-body (content_length={with_content_length}), got {res:?}"
);
});

let svc = service_fn(move |req: Request<IncomingBody>| {
let body_res_tx = body_res_tx.clone();
async move {
let collected = req.into_body().collect().await.map(|c| c.to_bytes());
if let Some(tx) = body_res_tx.lock().unwrap().take() {
let _ = tx.send(collected);
}
Ok::<_, hyper::Error>(Response::new(Empty::<Bytes>::new()))
}
});

let socket = TokioIo::new(listener.accept().await.unwrap().0);
let _ = hyper::server::conn::http2::Builder::new(TokioExecutor)
.serve_connection(socket, svc)
.await;

client_handle.await.unwrap();
}
}

#[tokio::test]
async fn h2_connect_reset_during_backpressure() {
let (listener, addr) = setup_tcp_listener();
Expand Down
Loading