Skip to content

Commit

Permalink
perf: Remove unnecessary allocation and construction of a HashMap in …
Browse files Browse the repository at this point in the history
…Signature::parse
  • Loading branch information
mennovf committed Aug 28, 2024
1 parent 8cbf4a2 commit bcbbe84
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/resources/webhook_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,21 +565,23 @@ struct Signature<'r> {
#[cfg(feature = "webhook-events")]
impl<'r> Signature<'r> {
fn parse(raw: &'r str) -> Result<Signature<'r>, WebhookError> {
let headers: HashMap<&str, &str> = raw
.split(',')
.map(|header| {
let mut key_and_value = header.split('=');
let key = key_and_value.next();
let value = key_and_value.next();
(key, value)
})
.filter_map(|(key, value)| match (key, value) {
(Some(key), Some(value)) => Some((key, value)),
_ => None,
})
.collect();
let t = headers.get("t").ok_or(WebhookError::BadSignature)?;
let v1 = headers.get("v1").ok_or(WebhookError::BadSignature)?;
let mut t = None;
let mut v1 = None;
for header in raw.split(',') {
let Some((key, value)) = header.split_once('=') else {
continue;
};

match key {
"t" => t = Some(value),
"v1" => v1 = Some(value),
_ => (),
}
}
let (Some(t), Some(v1)) = (t, v1) else {
return Err(WebhookError::BadSignature);
};

Ok(Signature { t: t.parse::<i64>().map_err(WebhookError::BadHeader)?, v1 })
}
}
Expand Down

0 comments on commit bcbbe84

Please sign in to comment.