From 96a0cf0df3e5224bfc742d34f1ee90c37f447044 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 20 May 2024 06:42:22 +0200 Subject: [PATCH] fix(uriproxy): oops didnt strip base --- uriproxy/src/lib.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/uriproxy/src/lib.rs b/uriproxy/src/lib.rs index 41dc53f..13f9ae9 100644 --- a/uriproxy/src/lib.rs +++ b/uriproxy/src/lib.rs @@ -21,23 +21,28 @@ impl AsRef for UriClass { /// unpack uri in id if valid, otherwise compose full uri with "{base}/{entity}/{id}" pub fn uri(base: &str, entity: UriClass, id: &str) -> String { - if id.starts_with('~') { // ready-to-use base64-encoded id - if let Ok(bytes) = base64::prelude::BASE64_STANDARD.decode(id) { - if let Ok(uri) = std::str::from_utf8(&bytes) { - return uri.to_string(); + if let Some(bare_id) = get_nth_uri_element(id) { + if bare_id.starts_with('~') { + if let Ok(bytes) = base64::prelude::BASE64_STANDARD.decode(bare_id.replacen('~', "", 1)) { + if let Ok(uri) = std::str::from_utf8(&bytes) { + return uri.to_string(); + } } } } format!("{}/{}/{}", base, entity.as_ref(), id) } +fn get_nth_uri_element(uri: &str) -> Option { + uri // https://example.org/users/test/followers/page?offset=42 + .split('/') // ['https:', '', 'example.org', 'users', 'test', 'followers', 'page?offset=42' ] + .nth(4) // 'test' + .map(|x| x.to_string()) +} + /// decompose local id constructed by uri() fn pub fn decompose_id(full_id: &str) -> String { - full_id // https://example.org/users/test/followers/page?offset=42 - .split('/') // ['https:', '', 'example.org', 'users', 'test', 'followers', 'page?offset=42' ] - .nth(4) // 'test' - .unwrap_or("") - .to_string() + get_nth_uri_element(full_id).unwrap_or_default() } /// encode with base64 remote url and prefix it with ~