From 22487ceddfd3b8d0641601874e0f19143da38699 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sat, 4 Jan 2025 18:22:42 +0100 Subject: [PATCH] move Redirect::compute_target to standalone function in web_server.rs --- src/model/bucket_table.rs | 25 ------------------------- src/web/web_server.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/model/bucket_table.rs b/src/model/bucket_table.rs index eba89e9b..b6daab75 100644 --- a/src/model/bucket_table.rs +++ b/src/model/bucket_table.rs @@ -85,31 +85,6 @@ mod v08 { pub replace_key: Option, } - impl Redirect { - pub fn compute_target(&self, suffix: Option<&str>) -> String { - let mut res = String::new(); - if let Some(hostname) = &self.hostname { - if let Some(protocol) = &self.protocol { - res.push_str(protocol); - res.push_str("://"); - } else { - res.push_str("//"); - } - res.push_str(hostname); - } - res.push('/'); - if let Some(replace_key_prefix) = &self.replace_key_prefix { - res.push_str(replace_key_prefix); - if let Some(suffix) = suffix { - res.push_str(suffix) - } - } else if let Some(replace_key) = &self.replace_key { - res.push_str(replace_key) - } - res - } - } - #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] pub struct CorsRule { pub id: Option, diff --git a/src/web/web_server.rs b/src/web/web_server.rs index c497270a..bd543bb0 100644 --- a/src/web/web_server.rs +++ b/src/web/web_server.rs @@ -28,7 +28,7 @@ use garage_api::s3::error::{ }; use garage_api::s3::get::{handle_get_without_ctx, handle_head_without_ctx}; -use garage_model::bucket_table::RoutingRule; +use garage_model::bucket_table::{self, RoutingRule}; use garage_model::garage::Garage; use garage_table::*; @@ -518,7 +518,7 @@ fn path_to_keys( } else { None }; - let mut target = routing_rule.redirect.compute_target(suffix); + let mut target = compute_redirect_target(&routing_rule.redirect, suffix); let query_alternative_key = status_code == StatusCode::OK || status_code == StatusCode::NOT_FOUND; let redirect_on_error = @@ -560,7 +560,7 @@ fn path_to_keys( } } } else { - let target = routing_rule.redirect.compute_target(None); + let target = compute_redirect_target(&routing_rule.redirect, None); return Ok(RoutingResult::Redirect { url: target, code: status_code, @@ -601,6 +601,29 @@ const PATH_ENCODING_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTRO .add(b'{') .add(b'}'); +fn compute_redirect_target(redirect: &bucket_table::Redirect, suffix: Option<&str>) -> String { + let mut res = String::new(); + if let Some(hostname) = &redirect.hostname { + if let Some(protocol) = &redirect.protocol { + res.push_str(protocol); + res.push_str("://"); + } else { + res.push_str("//"); + } + res.push_str(hostname); + } + res.push('/'); + if let Some(replace_key_prefix) = &redirect.replace_key_prefix { + res.push_str(replace_key_prefix); + if let Some(suffix) = suffix { + res.push_str(suffix) + } + } else if let Some(replace_key) = &redirect.replace_key { + res.push_str(replace_key) + } + res +} + #[cfg(test)] mod tests { use super::*;