update certificate match function

This commit is contained in:
yggverse 2024-11-16 19:19:15 +02:00
parent 7ff9efb166
commit e60a00c500
2 changed files with 24 additions and 12 deletions

View File

@ -37,14 +37,14 @@ impl Identity {
} }
} }
/// Get `pem` record match `request` according to /// Get `pem` record match `request`
/// [Gemini protocol specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates) /// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates)
/// * this function work with memory cache not database /// * this function work with memory cache collected (not database)
pub fn gemini(&self, request: &str) -> Option<String> { pub fn gemini(&self, request: &str) -> Option<String> {
// @TODO apply protocol rules to certificate selection if let Some(id) = self.gemini.auth.memory.match_priority(request) {
for profile_identity_gemini_id in self.gemini.auth.memory.starts_with(request) { match self.gemini.memory.get(id) {
if let Ok(pem) = self.gemini.memory.get(profile_identity_gemini_id) { Ok(pem) => return Some(pem),
return Some(pem); Err(_) => todo!(),
} }
} }
None None

View File

@ -39,14 +39,26 @@ impl Memory {
self.index.borrow_mut().clear() self.index.borrow_mut().clear()
} */ } */
/// Search for `profile_identity_gemini_id` by `url` starts with given substring /// Get `profile_identity_gemini_id` vector match given `request`
pub fn starts_with(&self, prefix: &str) -> Vec<i64> { /// * [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<i64> {
let mut result = Vec::new(); 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() { for (url, &profile_identity_gemini_id) in self.index.borrow().iter() {
if url.starts_with(prefix) { if request.starts_with(url) {
result.push(profile_identity_gemini_id) 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,
}
} }
} }