Skip to content

Commit 7565f80

Browse files
committed
suppress tls connection closed error
1 parent fc18bf1 commit 7565f80

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

gemini/src/client.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,30 @@ pub struct ConnectionAsyncRead<T: AsyncRead> {
8989
pub connection: gio::SocketConnection,
9090
pub readable: T,
9191
}
92-
92+
fn suppress_tls_connection_closed_error(
93+
res: Result<usize, std::io::Error>,
94+
) -> Result<usize, std::io::Error> {
95+
res.or_else(|e| {
96+
if e.kind() == std::io::ErrorKind::Other {
97+
let inner = e.into_inner().map(|e| e.downcast()).unwrap();
98+
inner
99+
.and_then(|gio_err: Box<glib::error::Error>| {
100+
match gio_err.kind::<gio::TlsError>() {
101+
// map the error to an equivalent read of 0 bytes, which will signal the end of the
102+
// connection
103+
Some(gio::TlsError::Eof) => {
104+
debug!("suppressed gio tls eof error");
105+
Ok(0)
106+
}
107+
_ => Err(gio_err.into()),
108+
}
109+
})
110+
.or_else(|e| Err(std::io::Error::other(e)))
111+
} else {
112+
Err(e)
113+
}
114+
})
115+
}
93116
impl<T: AsyncRead + std::marker::Unpin> AsyncRead for ConnectionAsyncRead<T> {
94117
// Required method
95118
fn poll_read(
@@ -100,7 +123,8 @@ impl<T: AsyncRead + std::marker::Unpin> AsyncRead for ConnectionAsyncRead<T> {
100123
let readable = &mut self.as_mut().readable;
101124
futures::pin_mut!(readable);
102125
let readable: std::pin::Pin<_> = readable.as_mut();
103-
AsyncRead::poll_read(readable, cx, buf)
126+
let res = AsyncRead::poll_read(readable, cx, buf);
127+
res.map(|ready| suppress_tls_connection_closed_error(ready))
104128
}
105129
}
106130

0 commit comments

Comments
 (0)