From 698a244a2b75c5962642ad8d4775357d6ff33df6 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Sat, 16 Sep 2023 01:41:43 +0200 Subject: [PATCH] refactor: prefer optional slice references for several vec data args --- src/custom_headers.rs | 10 +++------- src/handler.rs | 32 ++++++++++++++++++++++---------- src/redirects.rs | 4 ++-- src/rewrites.rs | 4 ++-- src/virtual_hosts.rs | 8 ++++---- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/custom_headers.rs b/src/custom_headers.rs index 4106053..b9a608f 100644 --- a/src/custom_headers.rs +++ b/src/custom_headers.rs @@ -11,15 +11,11 @@ use hyper::{Body, Response}; use crate::settings::Headers; /// Append custom HTTP headers to current response. -pub fn append_headers( - uri: &str, - headers_opts_vec: &Option>, - resp: &mut Response, -) { - if let Some(headers_vec) = headers_opts_vec { +pub fn append_headers(uri_path: &str, headers_opts: Option<&[Headers]>, resp: &mut Response) { + if let Some(headers_vec) = headers_opts { for headers_entry in headers_vec.iter() { // Match header glob pattern against request uri - if headers_entry.source.is_match(uri) { + if headers_entry.source.is_match(uri_path) { // Add/update headers if uri matches for (name, value) in &headers_entry.headers { resp.headers_mut().insert(name, value.to_owned()); diff --git a/src/handler.rs b/src/handler.rs index 9add7e4..b07ed4e 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -232,9 +232,10 @@ impl RequestHandler { // Advanced options if let Some(advanced) = &self.opts.advanced_opts { // Redirects - if let Some(redirects) = - redirects::get_redirection(uri_path.clone().as_str(), &advanced.redirects) - { + if let Some(redirects) = redirects::get_redirection( + uri_path.clone().as_str(), + advanced.redirects.as_deref(), + ) { // Redirects: Handle replacements (placeholders) if let Some(regex_caps) = redirects.source.captures(uri_path.as_str()) { let caps_range = 0..regex_caps.len(); @@ -290,9 +291,10 @@ impl RequestHandler { } // Rewrites - if let Some(rewrite) = - rewrites::rewrite_uri_path(uri_path.clone().as_str(), &advanced.rewrites) - { + if let Some(rewrite) = rewrites::rewrite_uri_path( + uri_path.clone().as_str(), + advanced.rewrites.as_deref(), + ) { // Rewrites: Handle replacements (placeholders) if let Some(regex_caps) = rewrite.source.captures(uri_path.as_str()) { let caps_range = 0..regex_caps.len(); @@ -348,8 +350,10 @@ impl RequestHandler { } } - // If the "Host" header matches any virtual_host, change the root dir - if let Some(root) = virtual_hosts::get_real_root(&advanced.virtual_hosts, headers) { + // If the "Host" header matches any virtual_host, change the root directory + if let Some(root) = + virtual_hosts::get_real_root(headers, advanced.virtual_hosts.as_deref()) + { base_path = root; } } @@ -425,7 +429,11 @@ impl RequestHandler { // Add/update custom headers if let Some(advanced) = &self.opts.advanced_opts { - custom_headers::append_headers(uri_path, &advanced.headers, &mut resp) + custom_headers::append_headers( + uri_path, + advanced.headers.as_deref(), + &mut resp, + ) } Ok(resp) @@ -491,7 +499,11 @@ impl RequestHandler { // Add/update custom headers if let Some(advanced) = &self.opts.advanced_opts { - custom_headers::append_headers(uri_path, &advanced.headers, &mut resp) + custom_headers::append_headers( + uri_path, + advanced.headers.as_deref(), + &mut resp, + ) } return Ok(resp); diff --git a/src/redirects.rs b/src/redirects.rs index 76a9729..38f5435 100644 --- a/src/redirects.rs +++ b/src/redirects.rs @@ -12,9 +12,9 @@ use crate::settings::Redirects; /// matches against the provided redirect's array. pub fn get_redirection<'a>( uri_path: &'a str, - redirects_opts_vec: &'a Option>, + redirects_opts: Option<&'a [Redirects]>, ) -> Option<&'a Redirects> { - if let Some(redirects_vec) = redirects_opts_vec { + if let Some(redirects_vec) = redirects_opts { for redirect_entry in redirects_vec.iter() { // Match source glob pattern against the request uri path if redirect_entry.source.is_match(uri_path) { diff --git a/src/rewrites.rs b/src/rewrites.rs index ebff85a..e825f18 100644 --- a/src/rewrites.rs +++ b/src/rewrites.rs @@ -12,9 +12,9 @@ use crate::settings::Rewrites; /// matches against the provided rewrites array. pub fn rewrite_uri_path<'a>( uri_path: &'a str, - rewrites_opts_vec: &'a Option>, + rewrites_opts: Option<&'a [Rewrites]>, ) -> Option<&'a Rewrites> { - if let Some(rewrites_vec) = rewrites_opts_vec { + if let Some(rewrites_vec) = rewrites_opts { for rewrites_entry in rewrites_vec.iter() { // Match source glob pattern against request uri path if rewrites_entry.source.is_match(uri_path) { diff --git a/src/virtual_hosts.rs b/src/virtual_hosts.rs index ef74c64..dd36f56 100644 --- a/src/virtual_hosts.rs +++ b/src/virtual_hosts.rs @@ -3,7 +3,7 @@ // See https://static-web-server.net/ for more information // Copyright (C) 2019-present Jose Quintana -//! Module that allows to rewrite request URLs with pattern matching support. +//! Module that allows to determine a virtual hostname. //! use hyper::{header::HOST, HeaderMap}; @@ -11,12 +11,12 @@ use std::path::PathBuf; use crate::settings::VirtualHosts; -/// It returns different root dir if the "Host" header matches a virtual hostname. +/// It returns different root directory if the "Host" header matches a virtual hostname. pub fn get_real_root<'a>( - vhosts_vec: &'a Option>, headers: &HeaderMap, + vhosts_opts: Option<&'a [VirtualHosts]>, ) -> Option<&'a PathBuf> { - if let Some(vhosts) = vhosts_vec { + if let Some(vhosts) = vhosts_opts { if let Ok(host_str) = headers.get(HOST)?.to_str() { for vhost in vhosts { if vhost.host == host_str { -- libgit2 1.7.2