agate

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

commit eaac36c8d61a72595a0cc76aeed53ddb1e05102e
parent 2213138f509fc59303001d07f2f3e92f0419e41b
Author: Matt Brubeck <mbrubeck@limpet.net>
Date:   Wed, 20 Dec 2023 17:23:01 -0800

Minor code cleanup

Diffstat:
Msrc/main.rs | 44+++++++++++++++++++-------------------------
1 file changed, 19 insertions(+), 25 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -535,9 +535,7 @@ where // ISOC-RFC 3986, we could use BufRead::read_line here, but that does // not allow us to cap the number of read bytes at 1024+2. let result = loop { - let bytes_read = if let Ok(read) = self.stream.read(buf).await { - read - } else { + let Ok(bytes_read) = self.stream.read(buf).await else { break Err((BAD_REQUEST, "Request ended unexpectedly")); }; len += bytes_read; @@ -575,25 +573,24 @@ where } // correct host - if let Some(domain) = url.domain() { - // because the gemini scheme is not special enough for WHATWG, normalize - // it ourselves - let host = Host::parse( - &percent_decode_str(domain) - .decode_utf8() - .or(Err((BAD_REQUEST, "Invalid URL")))?, - ) - .or(Err((BAD_REQUEST, "Invalid URL")))?; - // TODO: simplify when <https://github.com/servo/rust-url/issues/586> resolved - url.set_host(Some(&host.to_string())) - .expect("invalid domain?"); - // do not use "contains" here since it requires the same type and does - // not allow to check for Host<&str> if the vec contains Hostname<String> - if !ARGS.hostnames.is_empty() && !ARGS.hostnames.iter().any(|h| h == &host) { - return Err((PROXY_REQUEST_REFUSED, "Proxy request refused")); - } - } else { + let Some(domain) = url.domain() else { return Err((BAD_REQUEST, "URL does not contain a domain")); + }; + // because the gemini scheme is not special enough for WHATWG, normalize + // it ourselves + let host = Host::parse( + &percent_decode_str(domain) + .decode_utf8() + .or(Err((BAD_REQUEST, "Invalid URL")))?, + ) + .or(Err((BAD_REQUEST, "Invalid URL")))?; + // TODO: simplify when <https://github.com/servo/rust-url/issues/586> resolved + url.set_host(Some(&host.to_string())) + .expect("invalid domain?"); + // do not use "contains" here since it requires the same type and does + // not allow to check for Host<&str> if the vec contains Hostname<String> + if !ARGS.hostnames.is_empty() && !ARGS.hostnames.iter().any(|h| h == &host) { + return Err((PROXY_REQUEST_REFUSED, "Proxy request refused")); } // correct port @@ -737,10 +734,7 @@ where .add(b'}'); // check if directory listing is enabled by getting preamble - let preamble = if let Ok(txt) = std::fs::read_to_string(path.join(".directory-listing-ok")) - { - txt - } else { + let Ok(preamble) = std::fs::read_to_string(path.join(".directory-listing-ok")) else { self.send_header(NOT_FOUND, "Directory index disabled.") .await?; return Ok(());