Use if-let chains

This commit is contained in:
Matt Brubeck 2025-08-21 09:28:45 -07:00
parent cdc72e4f2d
commit 13ad2f363e

View file

@ -290,55 +290,55 @@ fn args() -> Result<Args> {
let hostname = Host::parse(&s)?; let hostname = Host::parse(&s)?;
// check if we have a certificate for that domain // check if we have a certificate for that domain
if let Host::Domain(ref domain) = hostname { if let Host::Domain(ref domain) = hostname
if !matches!(certs, Some(ref certs) if certs.has_domain(domain)) { && !matches!(certs, Some(ref certs) if certs.has_domain(domain))
log::info!("No certificate or key found for {s:?}, generating them."); {
log::info!("No certificate or key found for {s:?}, generating them.");
let mut cert_params = CertificateParams::new(vec![domain.clone()])?; let mut cert_params = CertificateParams::new(vec![domain.clone()])?;
cert_params cert_params
.distinguished_name .distinguished_name
.push(DnType::CommonName, domain); .push(DnType::CommonName, domain);
// <CertificateParams as Default>::default() already implements a // <CertificateParams as Default>::default() already implements a
// date in the far future from the time of writing: 4096-01-01 // date in the far future from the time of writing: 4096-01-01
let key_pair = if matches.opt_present("e") { let key_pair = if matches.opt_present("e") {
KeyPair::generate_for(&rcgen::PKCS_ED25519) KeyPair::generate_for(&rcgen::PKCS_ED25519)
} else { } else {
KeyPair::generate() KeyPair::generate()
}?; }?;
// generate the certificate with the configuration // generate the certificate with the configuration
let cert = cert_params.self_signed(&key_pair)?; let cert = cert_params.self_signed(&key_pair)?;
// make sure the certificate directory exists // make sure the certificate directory exists
fs::create_dir(certs_path.join(domain))?; fs::create_dir(certs_path.join(domain))?;
// write certificate data to disk // write certificate data to disk
let mut cert_file = File::create(certs_path.join(format!( let mut cert_file = File::create(certs_path.join(format!(
"{}/{}", "{}/{}",
domain, domain,
certificates::CERT_FILE_NAME certificates::CERT_FILE_NAME
)))?; )))?;
cert_file.write_all(cert.der())?; cert_file.write_all(cert.der())?;
// write key data to disk // write key data to disk
let key_file_path = let key_file_path =
certs_path.join(format!("{}/{}", domain, certificates::KEY_FILE_NAME)); certs_path.join(format!("{}/{}", domain, certificates::KEY_FILE_NAME));
let mut key_file = File::create(&key_file_path)?; let mut key_file = File::create(&key_file_path)?;
#[cfg(unix)] #[cfg(unix)]
{ {
// set permissions so only owner can read // set permissions so only owner can read
match key_file.set_permissions(std::fs::Permissions::from_mode(0o400)) { match key_file.set_permissions(std::fs::Permissions::from_mode(0o400)) {
Ok(_) => (), Ok(_) => (),
Err(_) => log::warn!( Err(_) => log::warn!(
"could not set permissions for new key file {}", "could not set permissions for new key file {}",
key_file_path.display() key_file_path.display()
), ),
}
} }
key_file.write_all(key_pair.serialized_der())?;
reload_certs = true;
} }
key_file.write_all(key_pair.serialized_der())?;
reload_certs = true;
} }
hostnames.push(hostname); hostnames.push(hostname);
@ -594,13 +594,13 @@ where
} }
// correct port // correct port
if let Some(expected_port) = self.local_port_check { if let Some(expected_port) = self.local_port_check
if let Some(port) = url.port() { && let Some(port) = url.port()
// Validate that the port in the URL is the same as for the stream this request {
// came in on. // Validate that the port in the URL is the same as for the stream this request
if port != expected_port { // came in on.
return Err((PROXY_REQUEST_REFUSED, "Proxy request refused")); if port != expected_port {
} return Err((PROXY_REQUEST_REFUSED, "Proxy request refused"));
} }
} }
Ok(url) Ok(url)
@ -659,24 +659,24 @@ where
} }
} }
if let Ok(metadata) = tokio::fs::metadata(&path).await { if let Ok(metadata) = tokio::fs::metadata(&path).await
if metadata.is_dir() { && metadata.is_dir()
if url.path().ends_with('/') || url.path().is_empty() { {
// if the path ends with a slash or the path is empty, the links will work the same if url.path().ends_with('/') || url.path().is_empty() {
// without a redirect // if the path ends with a slash or the path is empty, the links will work the same
// use `push` instead of `join` because the changed path is used later // without a redirect
path.push("index.gmi"); // use `push` instead of `join` because the changed path is used later
if !path.exists() { path.push("index.gmi");
path.pop(); if !path.exists() {
// try listing directory path.pop();
return self.list_directory(&path).await; // try listing directory
} return self.list_directory(&path).await;
} else {
// if client is not redirected, links may not work as expected without trailing slash
let mut url = url;
url.set_path(&format!("{}/", url.path()));
return self.send_header(REDIRECT_PERMANENT, url.as_str()).await;
} }
} else {
// if client is not redirected, links may not work as expected without trailing slash
let mut url = url;
url.set_path(&format!("{}/", url.path()));
return self.send_header(REDIRECT_PERMANENT, url.as_str()).await;
} }
} }