Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set error to Payload before dispatcher disconnect #3068

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions actix-http/CHANGES.md
Expand Up @@ -7,6 +7,9 @@
- Add `body::to_body_limit()` function.
- Add `body::BodyLimitExceeded` error type.

### Fixed
- Fix truncated body ending without error when connection closed abnormally. [#3067]

### Changed

- Minimum supported Rust version (MSRV) is now 1.65 due to transitive `time` dependency.
Expand Down
1 change: 1 addition & 0 deletions actix-http/src/h1/dispatcher.rs
Expand Up @@ -1115,6 +1115,7 @@ where
let inner = inner.as_mut().project();
inner.flags.insert(Flags::READ_DISCONNECT);
if let Some(mut payload) = inner.payload.take() {
payload.set_error(io::Error::from(io::ErrorKind::UnexpectedEof).into());
payload.feed_eof();
}
};
Expand Down
54 changes: 54 additions & 0 deletions actix-http/tests/test_server.rs
Expand Up @@ -439,6 +439,60 @@ async fn content_length() {
srv.stop().await;
}

#[actix_rt::test]
async fn content_length_truncated() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};

let mut srv = test_server(|| {
HttpService::build()
.h1(|mut req: Request| async move {
let expected_length: usize = req.uri().path()[1..].parse().unwrap();
let mut payload = req.take_payload();

let mut length = 0;
let mut seen_error = false;
while let Some(chunk) = payload.next().await {
match chunk {
Ok(b) => length += b.len(),
Err(_) => {
seen_error = true;
break;
}
}
}
if seen_error {
return Result::<_, Infallible>::Ok(Response::bad_request());
}

assert_eq!(length, expected_length, "length must match when no error");
Result::<_, Infallible>::Ok(Response::ok())
})
.tcp()
})
.await;

let addr = srv.addr();
let mut buf = [0; 12];

let mut conn = TcpStream::connect(&addr).await.unwrap();
conn.write_all(b"POST /10000 HTTP/1.1\r\nContent-Length: 10000\r\n\r\ndata_truncated")
.await
.unwrap();
conn.shutdown().await.unwrap();
conn.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"HTTP/1.1 400");

let mut conn = TcpStream::connect(&addr).await.unwrap();
conn.write_all(b"POST /4 HTTP/1.1\r\nContent-Length: 4\r\n\r\ndata")
.await
.unwrap();
conn.shutdown().await.unwrap();
conn.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"HTTP/1.1 200");

srv.stop().await;
}

#[actix_rt::test]
async fn h1_headers() {
let data = STR.repeat(10);
Expand Down