update connection close handler

This commit is contained in:
yggverse 2025-02-23 06:00:58 +02:00
parent e20969c28d
commit eb6c6afb12

View file

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