From e60a00c5000cb05f4d77d38fde8acd75b0f48694 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 16 Nov 2024 19:19:15 +0200 Subject: [PATCH] update certificate match function --- src/profile/identity.rs | 14 +++++++------- src/profile/identity/gemini/auth/memory.rs | 22 +++++++++++++++++----- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/profile/identity.rs b/src/profile/identity.rs index ffc80b5f..c3073ecf 100644 --- a/src/profile/identity.rs +++ b/src/profile/identity.rs @@ -37,14 +37,14 @@ impl Identity { } } - /// Get `pem` record match `request` according to - /// [Gemini protocol specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates) - /// * this function work with memory cache not database + /// Get `pem` record match `request` + /// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates) + /// * this function work with memory cache collected (not database) pub fn gemini(&self, request: &str) -> Option { - // @TODO apply protocol rules to certificate selection - for profile_identity_gemini_id in self.gemini.auth.memory.starts_with(request) { - if let Ok(pem) = self.gemini.memory.get(profile_identity_gemini_id) { - return Some(pem); + if let Some(id) = self.gemini.auth.memory.match_priority(request) { + match self.gemini.memory.get(id) { + Ok(pem) => return Some(pem), + Err(_) => todo!(), } } None diff --git a/src/profile/identity/gemini/auth/memory.rs b/src/profile/identity/gemini/auth/memory.rs index d4d09396..0c635e75 100644 --- a/src/profile/identity/gemini/auth/memory.rs +++ b/src/profile/identity/gemini/auth/memory.rs @@ -39,14 +39,26 @@ impl Memory { self.index.borrow_mut().clear() } */ - /// Search for `profile_identity_gemini_id` by `url` starts with given substring - pub fn starts_with(&self, prefix: &str) -> Vec { + /// Get `profile_identity_gemini_id` vector match given `request` + /// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates) + /// * contain unspecified length priority implementation @TODO + pub fn match_priority(&self, request: &str) -> Option { let mut result = Vec::new(); + + // Get all records starts with URL cached, collect length for priority for (url, &profile_identity_gemini_id) in self.index.borrow().iter() { - if url.starts_with(prefix) { - result.push(profile_identity_gemini_id) + if request.starts_with(url) { + result.push((profile_identity_gemini_id, url.len())) } } - result + + // Sort by length desc @TODO + result.sort_by(|a, b| b.1.cmp(&a.1)); + + // Get first match ID + match result.get(0) { + Some(value) => Some(value.0), + None => None, + } } }