diff --git a/src/main.rs b/src/main.rs index 7e64bf2..b716efa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -142,9 +142,12 @@ fn gemini( // EOF if l == 0 { println!("[{}] [info] [{peer}] Response: {read} bytes", now()); - stream.flush().unwrap(); - stream.shutdown().unwrap(); - println!("[{}] [info] [{peer}] Connection closed by server.", now()); + match close(stream) { + Ok(()) => { + println!("[{}] [info] [{peer}] Connection closed by server.", now()) + } + Err(e) => println!("[{}] [warning] [{peer}] {e}", now()), + } break; } }, @@ -158,7 +161,7 @@ fn gemini( .into_bytes(), stream, |result| match result { - Ok(()) => println!("[{}] [error] [{peer}] {e}", now()), + Ok(()) => println!("[{}] [warning] [{peer}] {e}", now()), Err(e) => println!("[{}] [error] [{peer}] {e}", now()), }, ), @@ -383,13 +386,18 @@ fn titan( } } +fn close(stream: &mut TlsStream) -> Result<()> { + stream.flush()?; + // close connection gracefully + // https://geminiprotocol.net/docs/protocol-specification.gmi#closing-connections + stream.shutdown()?; + Ok(()) +} + fn send(data: &[u8], stream: &mut TlsStream, callback: impl FnOnce(Result<()>)) { callback((|| { stream.write_all(data)?; - stream.flush()?; - // close connection gracefully - // https://geminiprotocol.net/docs/protocol-specification.gmi#closing-connections - stream.shutdown()?; + close(stream)?; Ok(()) })()); }