From 19279327a73ffa70f4c5079fd149ab82f7649e15 Mon Sep 17 00:00:00 2001 From: Jorge Hermo Date: Sat, 2 Nov 2024 12:48:59 +0100 Subject: [PATCH] refactor: refactor prefixed headers parsing --- core/src/raw/http_util/header.rs | 17 +++++++++++++++++ core/src/raw/http_util/mod.rs | 1 + core/src/services/azblob/backend.rs | 18 ++++-------------- core/src/services/s3/backend.rs | 14 ++------------ 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/core/src/raw/http_util/header.rs b/core/src/raw/http_util/header.rs index 87748c96f4db..49dff81a0ff7 100644 --- a/core/src/raw/http_util/header.rs +++ b/core/src/raw/http_util/header.rs @@ -15,6 +15,8 @@ // specific language governing permissions and limitations // under the License. +use std::collections::HashMap; + use base64::engine::general_purpose; use base64::Engine; use chrono::DateTime; @@ -189,6 +191,21 @@ pub fn parse_into_metadata(path: &str, headers: &HeaderMap) -> Result Ok(m) } +/// Parse prefixed headers and return a map with the prefix of each header removed. +pub fn parse_prefixed_headers(headers: &HeaderMap, prefix: &str) -> HashMap { + headers + .iter() + .filter_map(|(name, value)| { + name.as_str().strip_prefix(prefix).and_then(|stripped_key| { + value + .to_str() + .ok() + .map(|parsed_value| (stripped_key.to_string(), parsed_value.to_string())) + }) + }) + .collect() +} + /// format content md5 header by given input. pub fn format_content_md5(bs: &[u8]) -> String { let mut hasher = md5::Md5::new(); diff --git a/core/src/raw/http_util/mod.rs b/core/src/raw/http_util/mod.rs index 226fb17b7d47..8999b6b807f0 100644 --- a/core/src/raw/http_util/mod.rs +++ b/core/src/raw/http_util/mod.rs @@ -49,6 +49,7 @@ pub use header::parse_header_to_str; pub use header::parse_into_metadata; pub use header::parse_last_modified; pub use header::parse_location; +pub use header::parse_prefixed_headers; mod uri; pub use uri::percent_decode_path; diff --git a/core/src/services/azblob/backend.rs b/core/src/services/azblob/backend.rs index 8da8f936ee35..90c57de584e1 100644 --- a/core/src/services/azblob/backend.rs +++ b/core/src/services/azblob/backend.rs @@ -34,6 +34,7 @@ use sha2::Digest; use sha2::Sha256; use super::core::constants; +use super::core::constants::X_MS_META_NAME_PREFIX; use super::error::parse_error; use super::lister::AzblobLister; use super::writer::AzblobWriter; @@ -550,23 +551,12 @@ impl Access for AzblobBackend { StatusCode::OK => { let headers = resp.headers(); let mut meta = parse_into_metadata(path, headers)?; - // TODO: Refactor in common with s3 metadata parsing - // do the same as in parse_into_metadata... but for user metadata - let user_meta: HashMap = headers - .iter() - .filter_map(|(name, _)| { - name.as_str() - .strip_prefix(constants::X_MS_META_NAME_PREFIX) - .and_then(|stripped_key| { - parse_header_to_str(headers, name) - .unwrap_or(None) - .map(|val| (stripped_key.to_string(), val.to_string())) - }) - }) - .collect(); + + let user_meta = parse_prefixed_headers(&headers, X_MS_META_NAME_PREFIX); if !user_meta.is_empty() { meta.with_user_metadata(user_meta); } + Ok(RpStat::new(meta)) } _ => Err(parse_error(resp)), diff --git a/core/src/services/s3/backend.rs b/core/src/services/s3/backend.rs index 57b3a2f2fa46..525add77d75e 100644 --- a/core/src/services/s3/backend.rs +++ b/core/src/services/s3/backend.rs @@ -26,6 +26,7 @@ use std::sync::Arc; use base64::prelude::BASE64_STANDARD; use base64::Engine; use bytes::Buf; +use constants::X_AMZ_META_PREFIX; use http::Response; use http::StatusCode; use log::debug; @@ -970,18 +971,7 @@ impl Access for S3Backend { let headers = resp.headers(); let mut meta = parse_into_metadata(path, headers)?; - let user_meta: HashMap = headers - .iter() - .filter_map(|(name, _)| { - name.as_str() - .strip_prefix(constants::X_AMZ_META_PREFIX) - .and_then(|stripped_key| { - parse_header_to_str(headers, name) - .unwrap_or(None) - .map(|val| (stripped_key.to_string(), val.to_string())) - }) - }) - .collect(); + let user_meta = parse_prefixed_headers(&headers, X_AMZ_META_PREFIX); if !user_meta.is_empty() { meta.with_user_metadata(user_meta); }