diff --git a/src/main.rs b/src/main.rs index 966aba1..6b910d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,20 +48,18 @@ fn args() -> Option { async fn handle_request(stream: TcpStream) -> Result { // Perform handshake. static TLS: Lazy = Lazy::new(|| acceptor().unwrap()); - let mut stream = TLS.accept(stream).await?; + let stream = &mut TLS.accept(stream).await?; - match parse_request(&mut stream).await { - Ok(url) => { - eprintln!("Got request for {:?}", url); - if let Err(e) = send_response(&url, &mut stream).await { - respond(&mut stream, "51", &["Not found, sorry."]).await?; - return Err(e) - } - } + let url = match parse_request(stream).await { + Ok(url) => url, Err(e) => { - respond(&mut stream, "59", &["Invalid request."]).await?; + respond(stream, "59", &["Invalid request."]).await?; return Err(e) } + }; + if let Err(e) = send_response(url, stream).await { + respond(stream, "51", &["Not found, sorry."]).await?; + return Err(e) } Ok(()) } @@ -80,7 +78,7 @@ fn acceptor() -> Result { } /// Return the URL requested by the client. -async fn parse_request(mut stream: R) -> Result { +async fn parse_request(stream: &mut R) -> Result { // Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we // can use a fixed-sized buffer on the stack, avoiding allocations and // copying, and stopping bad clients from making us use too much memory. @@ -112,11 +110,12 @@ async fn parse_request(mut stream: R) -> Result { if url.scheme() != "gemini" { Err("unsupported URL scheme")? } + eprintln!("Got request for {:?}", url); Ok(url) } /// Send the client the file located at the requested URL. -async fn send_response(url: &Url, mut stream: W) -> Result { +async fn send_response(url: Url, stream: &mut W) -> Result { let mut path = std::path::PathBuf::from(&ARGS.content_dir); if let Some(segments) = url.path_segments() { path.extend(segments); @@ -126,7 +125,7 @@ async fn send_response(url: &Url, mut stream: W) -> Result { path.push("index.gmi"); } else { // Send a redirect when the URL for a directory has no trailing slash. - return respond(&mut stream, "31", &[url.as_str(), "/"]).await; + return respond(stream, "31", &[url.as_str(), "/"]).await; } } @@ -135,18 +134,18 @@ async fn send_response(url: &Url, mut stream: W) -> Result { // Send header. if path.extension() == Some(OsStr::new("gmi")) { - respond(&mut stream, "20", &["text/gemini"]).await?; + respond(stream, "20", &["text/gemini"]).await?; } else { let mime = mime_guess::from_path(&path).first_or_octet_stream(); - respond(&mut stream, "20", &[mime.essence_str()]).await?; + respond(stream, "20", &[mime.essence_str()]).await?; } // Send body. - async_std::io::copy(&mut file, &mut stream).await?; + async_std::io::copy(&mut file, stream).await?; Ok(()) } -async fn respond(mut stream: W, status: &str, meta: &[&str]) -> Result { +async fn respond(stream: &mut W, status: &str, meta: &[&str]) -> Result { stream.write_all(status.as_bytes()).await?; stream.write_all(b" ").await?; for m in meta {