agate

Simple gemini server for static files
git clone https://github.com/mbrubeck/agate.git
Log | Files | Refs | README

commit 74853799c71e2a30782f9e82cc155c686bfdc96f
parent 3353989e7e20ceb7ca5980aca4c72e4bad37a825
Author: Johann150 <johann.galle@protonmail.com>
Date:   Sun, 24 Jan 2021 20:45:36 +0100

handle errors in handle_request

Diffstat:
Msrc/main.rs | 38++++++++++++++++++++++----------------
1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -34,11 +34,7 @@ fn main() -> Result { log::info!("Listening on {:?}...", ARGS.addrs); loop { let (stream, _) = listener.accept().await?; - tokio::spawn(async { - if let Err(e) = handle_request(stream).await { - log::error!("{:?}", e); - } - }); + tokio::spawn(async { handle_request(stream).await }); } }) } @@ -123,8 +119,9 @@ struct RequestHandle { pub log_line: String, } -/// Handle a single client session (request + response). -async fn handle_request(stream: TcpStream) -> Result { +/// Handle a single client session (request + response) and any errors that +/// may occur while processing it. +async fn handle_request(stream: TcpStream) { let log_line = format!( "{} {}", stream.local_addr().unwrap(), @@ -139,19 +136,28 @@ async fn handle_request(stream: TcpStream) -> Result { } ); - let stream = TLS.accept(stream).await?; + let stream = match TLS.accept(stream).await { + Ok(stream) => stream, + Err(e) => { + log::warn!("{} error:{}", log_line, e); + return; + } + }; let mut handle = RequestHandle { stream, log_line }; - match parse_request(&mut handle).await { - Ok(url) => send_response(url, &mut handle).await?, - Err((status, msg)) => send_header(&mut handle, status, msg).await?, - } - handle.stream.shutdown().await?; - - log::info!("{}", handle.log_line); + let mut result = match parse_request(&mut handle).await { + Ok(url) => send_response(url, &mut handle).await, + Err((status, msg)) => send_header(&mut handle, status, msg).await, + }; - Ok(()) + if let Err(e) = result { + log::warn!("{} error:{}", handle.log_line, e); + } else if let Err(e) = handle.stream.shutdown().await { + log::warn!("{} error:{}", handle.log_line, e); + } else { + log::info!("{}", handle.log_line); + } } /// TLS configuration.