Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ atomic-waker = { version = "1.1.2", optional = true }
futures-channel = { version = "0.3", optional = true }
futures-core = { version = "0.3.31", optional = true }
futures-util = { version = "0.3", default-features = false, features = ["alloc"], optional = true }
h2 = { version = "0.4.14", optional = true }
h2 = { version = "0.4.16", optional = true }
http-body-util = { version = "0.1", optional = true }
httparse = { version = "1.9", optional = true }
httpdate = { version = "1.0", optional = true }
Expand Down
20 changes: 2 additions & 18 deletions src/body/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,7 @@ 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:
return Poll::Ready(None);
} else {
return Poll::Ready(Some(Err(crate::Error::new_body(e))));
}
return Poll::Ready(Some(Err(crate::Error::new_body(e))));
}
None => {
*data_done = true;
Expand All @@ -213,16 +206,7 @@ impl Body for Incoming {
ping.record_non_data();
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:
Poll::Ready(None)
} else {
Poll::Ready(Some(Err(crate::Error::new_h2(e))))
}
}
Err(e) => Poll::Ready(Some(Err(crate::Error::new_h2(e)))),
}
}

Expand Down
59 changes: 59 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3024,6 +3024,65 @@ mod conn {
assert_eq!(trailers.get("grpc").unwrap(), "0");
}

#[tokio::test]
async fn h2_response_body_reset_without_end_stream() {
tokio::time::timeout(Duration::from_secs(5), async {
for content_length in [None, Some("10"), Some("3")] {
let (client_io, server_io) = tokio::io::duplex(4096);
let (reset_tx, reset_rx) = oneshot::channel();
let server = tokio::spawn(async move {
let mut h2 = h2::server::handshake(server_io).await.unwrap();
let (_request, mut respond) = h2.accept().await.unwrap().unwrap();
let mut response = Response::builder();
if let Some(length) = content_length {
response = response.header("content-length", length);
}
let mut stream = respond
.send_response(response.body(()).unwrap(), false)
.unwrap();
stream.send_data(Bytes::from_static(b"abc"), false).unwrap();

let reset = async move {
reset_rx.await.unwrap();
stream.send_reset(h2::Reason::NO_ERROR);
};
let ((), result) = future::join(reset, poll_fn(|cx| h2.poll_closed(cx))).await;
result.unwrap();
});
let (mut client, connection) = conn::http2::Builder::new(TokioExecutor)
.handshake(TokioIo::new(client_io))
.await
.unwrap();
let connection = tokio::spawn(connection);
let request = Request::get("https://localhost/")
.body(Empty::<Bytes>::new())
.unwrap();
let mut body = client.send_request(request).await.unwrap().into_body();
// Read the DATA before sending the reset so it reaches the body.
assert_eq!(
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"abc"
);
reset_tx.send(()).unwrap();

let err = match body.collect().await {
Err(err) => err,
Ok(_) => panic!("reset ended the response body cleanly with Content-Length {content_length:?}"),
};
let source = err.source().unwrap();
assert_eq!(
source.downcast_ref::<h2::Error>().unwrap().reason(),
Some(h2::Reason::NO_ERROR)
);
drop(client);
connection.await.unwrap().unwrap();
server.await.unwrap();
}
})
.await
.expect("response body reset test timed out");
}

#[tokio::test]
async fn http2_head_response_size_hint() {
tokio::time::timeout(Duration::from_secs(5), async {
Expand Down
53 changes: 53 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,59 @@ use tokio::pin;

mod support;

#[tokio::test]
async fn h2_request_body_reset_without_end_stream() {
tokio::time::timeout(Duration::from_secs(5), async {
for content_length in [None, Some("10"), Some("3")] {
let (client_io, server_io) = tokio::io::duplex(4096);
let (body_tx, mut body_rx) = tokio::sync::mpsc::channel(1);
let service = service_fn(move |req: Request<IncomingBody>| {
body_tx.try_send(req.into_body()).unwrap();
future::ready(Ok::<_, hyper::Error>(Response::new(Empty::<Bytes>::new())))
});
let server = tokio::spawn(
http2::Builder::new(TokioExecutor)
.serve_connection(TokioIo::new(server_io), service),
);

let (mut client, connection) = h2::client::handshake(client_io).await.unwrap();
let connection = tokio::spawn(connection);
let mut request = Request::post("https://localhost/");
if let Some(length) = content_length {
request = request.header("content-length", length);
}
let (_response, mut stream) = client
.send_request(request.body(()).unwrap(), false)
.unwrap();
stream.send_data(Bytes::from_static(b"abc"), false).unwrap();

let mut body = body_rx.recv().await.unwrap();
// Read the DATA before sending the reset so it reaches the body.
assert_eq!(
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"abc"
);
stream.send_reset(h2::Reason::NO_ERROR);

let err = match body.collect().await {
Err(err) => err,
Ok(_) => panic!(
"reset ended the request body cleanly with Content-Length {content_length:?}"
),
};
let source = std::error::Error::source(&err).unwrap();
assert_eq!(
source.downcast_ref::<h2::Error>().unwrap().reason(),
Some(h2::Reason::NO_ERROR)
);
connection.abort();
server.abort();
}
})
.await
.expect("request body reset test timed out");
}

#[test]
fn get_should_ignore_body() {
let server = serve();
Expand Down
Loading