index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2023-09-15 23:41:43.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2023-09-15 23:41:43.0 +00:00:00
commit
698a244a2b75c5962642ad8d4775357d6ff33df6 [patch]
tree
f9abc03ddf603a2f56485eefab5fa4ae58ae5a68
parent
e551d67278bcd4889ad235f0c19f014a03b90678
download
698a244a2b75c5962642ad8d4775357d6ff33df6.tar.gz

refactor: prefer optional slice references for several vec data args



Diff

 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<Vec<Headers>>,
    resp: &mut Response<Body>,
) {
    if let Some(headers_vec) = headers_opts_vec {
pub fn append_headers(uri_path: &str, headers_opts: Option<&[Headers]>, resp: &mut Response<Body>) {
    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<Vec<Redirects>>,
    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<Vec<Rewrites>>,
    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 <joseluisq.net>

//! 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<Vec<VirtualHosts>>,
    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 {