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
5 changes: 5 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ impl Error {

/// Returns true if the error was caused by a timeout.
pub fn is_timeout(&self) -> bool {
#[cfg(any(all(feature = "http1", feature = "server"), feature = "ffi"))]
return matches!(self.inner.kind, Kind::HeaderTimeout)
|| self.find_source::<TimedOut>().is_some();

#[cfg(not(any(all(feature = "http1", feature = "server"), feature = "ffi")))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the feature flag combinations are tricky. Here's what I'd suggest:

#[cfg(all(feature = "http1", feature = "server"))]
if matches!(self.inner.kind, Kind::HeaderTimeout) {
    return true;
}

self.find_source::<TimedOut>().is_some()

self.find_source::<TimedOut>().is_some()
}

Expand Down
34 changes: 34 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,40 @@ fn http1_trailer_recv_fields() {
);
}

#[tokio::test]
async fn http1_timeout_error() {
let (listener, addr) = setup_tcp_listener();

let j = tokio::spawn(async move {
let (socket, _) = listener.accept().await.expect("accept");
let socket = TokioIo::new(socket);

if let Err(e) = http1::Builder::new()
.timer(TokioTimer)
.header_read_timeout(Duration::from_secs(1))
.serve_connection(socket, HelloWorld)
.await
{
Some(e.is_timeout())
} else {
None
}
});

let tcp = TokioIo::new(connect_async(addr).await);
let (_client, conn) = hyper::client::conn::http1::Builder::new()
.handshake::<_, Empty<Bytes>>(tcp)
.await
.expect("http handshake");

tokio::spawn(async move {
conn.await.expect_err("client conn fail");
});

TokioTimer.sleep(Duration::from_secs(2)).await;
assert_eq!(j.await.unwrap(), Some(true));
}

// -------------------------------------------------
// the Server that is used to run all the tests with
// -------------------------------------------------
Expand Down
Loading