From 151b10775f78b4516a05fa240df2f45c61d6cb4a Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 20 Oct 2023 16:09:53 +0200 Subject: [PATCH] refactor: properly check boolean env vars Co-authored-by: Jeidnx --- src/main.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index b095743..4eadbd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ async fn main() -> std::io::Result<()> { // get socket/port from env // backwards compat when only UDS is set - if env::var("UDS").is_ok() { + if get_env_bool("UDS") { let socket_path = env::var("BIND_UNIX").unwrap_or_else(|_| "./socket/actix.sock".to_string()); server.bind_uds(socket_path)? @@ -68,7 +68,7 @@ static CLIENT: Lazy = Lazy::new(|| { builder }; - if env::var("IPV4_ONLY").is_ok() { + if get_env_bool("IPV4_ONLY") { builder .local_address(Some("0.0.0.0".parse().unwrap())) .build() @@ -116,6 +116,13 @@ fn is_header_allowed(header: &str) -> bool { ) } +fn get_env_bool(key: &str) -> bool { + match env::var(key) { + Ok(val) => val.to_lowercase() == "true" || val == "1", + Err(_) => false, + } +} + async fn index(req: HttpRequest) -> Result> { if req.method() == Method::OPTIONS { let mut response = HttpResponse::Ok(); @@ -138,7 +145,7 @@ async fn index(req: HttpRequest) -> Result> { } #[cfg(any(feature = "webp", feature = "avif"))] - let disallow_image_transcoding = env::var("DISALLOW_IMAGE_TRANSCODING").is_ok(); + let disallow_image_transcoding = get_env_bool("DISALLOW_IMAGE_TRANSCODING"); let rewrite = query.get("rewrite") != Some("false");