|
|
@ -1,6 +1,8 @@ |
|
|
|
use crate::error::unexpected::{self, Mappable}; |
|
|
|
use crate::error::unexpected::{self, Mappable}; |
|
|
|
use crate::origin; |
|
|
|
use crate::origin; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::borrow; |
|
|
|
|
|
|
|
|
|
|
|
use hex::ToHex; |
|
|
|
use hex::ToHex; |
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
use sha2::{Digest, Sha256}; |
|
|
|
use sha2::{Digest, Sha256}; |
|
|
@ -11,6 +13,9 @@ use sha2::{Digest, Sha256}; |
|
|
|
pub struct Settings { |
|
|
|
pub struct Settings { |
|
|
|
#[serde(flatten)] |
|
|
|
#[serde(flatten)] |
|
|
|
pub origin_descr: origin::Descr, |
|
|
|
pub origin_descr: origin::Descr, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub remove_path_prefix: Option<String>, |
|
|
|
|
|
|
|
pub add_path_prefix: Option<String>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Settings { |
|
|
|
impl Settings { |
|
|
@ -19,4 +24,104 @@ impl Settings { |
|
|
|
serde_json::to_writer(&mut h, self).or_unexpected()?; |
|
|
|
serde_json::to_writer(&mut h, self).or_unexpected()?; |
|
|
|
Ok(h.finalize().encode_hex::<String>()) |
|
|
|
Ok(h.finalize().encode_hex::<String>()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn remove_path_prefix<'path, 'prefix>( |
|
|
|
|
|
|
|
path: borrow::Cow<'path, str>, |
|
|
|
|
|
|
|
prefix: &'prefix str, |
|
|
|
|
|
|
|
) -> borrow::Cow<'path, str> { |
|
|
|
|
|
|
|
if prefix.len() == 0 { |
|
|
|
|
|
|
|
return path; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut prefix = prefix.trim_end_matches('/'); |
|
|
|
|
|
|
|
prefix = prefix.trim_start_matches('/'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut stripped_path = path.trim_start_matches('/'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !stripped_path.starts_with(prefix) { |
|
|
|
|
|
|
|
return path; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stripped_path = stripped_path.strip_prefix(prefix).unwrap(); |
|
|
|
|
|
|
|
if stripped_path.len() == 0 { |
|
|
|
|
|
|
|
return borrow::Cow::Borrowed("/"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
borrow::Cow::Owned(stripped_path.to_string()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn add_path_prefix<'path, 'prefix>( |
|
|
|
|
|
|
|
path: borrow::Cow<'path, str>, |
|
|
|
|
|
|
|
prefix: &'prefix str, |
|
|
|
|
|
|
|
) -> borrow::Cow<'path, str> { |
|
|
|
|
|
|
|
if prefix.len() == 0 { |
|
|
|
|
|
|
|
return path; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut prefix = prefix.trim_end_matches('/'); |
|
|
|
|
|
|
|
prefix = prefix.trim_start_matches('/'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut prefixed_path = String::with_capacity(1 + prefix.len() + path.len()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prefixed_path.push('/'); |
|
|
|
|
|
|
|
prefixed_path.push_str(prefix); |
|
|
|
|
|
|
|
prefixed_path.push_str(path.as_ref()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
borrow::Cow::Owned(prefixed_path) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn process_path<'a>(&self, path: &'a str) -> borrow::Cow<'a, str> { |
|
|
|
|
|
|
|
let mut path = borrow::Cow::Borrowed(path); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(ref prefix) = self.remove_path_prefix { |
|
|
|
|
|
|
|
path = Self::remove_path_prefix(path, prefix); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(ref prefix) = self.add_path_prefix { |
|
|
|
|
|
|
|
path = Self::add_path_prefix(path, prefix); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
path |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
|
|
|
|
mod tests { |
|
|
|
|
|
|
|
use super::*; |
|
|
|
|
|
|
|
use std::borrow; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
|
|
|
fn remove_path_prefix() { |
|
|
|
|
|
|
|
let assert_remove = |want: &str, path: &str, prefix: &str| { |
|
|
|
|
|
|
|
assert_eq!( |
|
|
|
|
|
|
|
want, |
|
|
|
|
|
|
|
Settings::remove_path_prefix(borrow::Cow::Borrowed(path), prefix).as_ref(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_remove("/bar", "/foo/bar", "/foo"); |
|
|
|
|
|
|
|
assert_remove("/foo/bar", "/foo/bar", "/baz"); |
|
|
|
|
|
|
|
assert_remove("/bar", "/foo/bar", "/foo/"); |
|
|
|
|
|
|
|
assert_remove("/bar", "/foo/bar", "/foo///"); |
|
|
|
|
|
|
|
assert_remove("/", "/", "/"); |
|
|
|
|
|
|
|
assert_remove("/", "/foo/bar/", "/foo/bar"); |
|
|
|
|
|
|
|
assert_remove("/", "/foo/bar", "/foo/bar"); |
|
|
|
|
|
|
|
assert_remove("/", "/foo/bar", "/foo/bar///"); |
|
|
|
|
|
|
|
assert_remove("/bar", "/bar", ""); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
|
|
|
fn add_path_prefix() { |
|
|
|
|
|
|
|
let assert_add = |want: &str, path: &str, prefix: &str| { |
|
|
|
|
|
|
|
assert_eq!( |
|
|
|
|
|
|
|
want, |
|
|
|
|
|
|
|
Settings::add_path_prefix(borrow::Cow::Borrowed(path), prefix).as_ref(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_add("/foo/bar", "/bar", "/foo"); |
|
|
|
|
|
|
|
assert_add("/foo/", "/", "/foo"); |
|
|
|
|
|
|
|
assert_add("/foo/", "/", "/foo///"); |
|
|
|
|
|
|
|
assert_add("/bar", "/bar", ""); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|