From 341948bcc56f2b9e1e7fe235c4549a1bb80788a3 Mon Sep 17 00:00:00 2001 From: tecc Date: Tue, 25 Apr 2023 01:16:00 +0200 Subject: [PATCH] change(763-owned-cookie-names): Make cookie names owned instead of static strings --- src/filters/cookie.rs | 20 +++++++++++--------- src/reject.rs | 6 +++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/filters/cookie.rs b/src/filters/cookie.rs index 53695e85c..efa2f3334 100644 --- a/src/filters/cookie.rs +++ b/src/filters/cookie.rs @@ -12,15 +12,16 @@ use std::str::FromStr; /// Creates a `Filter` that requires a cookie by name. /// /// If found, extracts the value of the cookie, otherwise rejects. -pub fn cookie(name: &'static str) -> impl Filter, Error = Rejection> + Copy +pub fn cookie(name: impl ToString) -> impl Filter, Error = Rejection> + Clone where - T: FromStr + Send + 'static, + T: FromStr + Send, { + let name = name.to_string(); header::header2().and_then(move |cookie: Cookie| { let cookie = cookie - .get(name) - .ok_or_else(|| crate::reject::missing_cookie(name)) - .and_then(|s| T::from_str(s).map_err(|_| crate::reject::missing_cookie(name))); + .get(name.as_str()) + .ok_or_else(|| crate::reject::missing_cookie(name.clone())) + .and_then(|s| T::from_str(s).map_err(|_| crate::reject::missing_cookie(name.clone()))); future::ready(cookie) }) } @@ -30,13 +31,14 @@ where /// If found, extracts the value of the cookie, otherwise continues /// the request, extracting `None`. pub fn optional( - name: &'static str, -) -> impl Filter>, Error = Infallible> + Copy + name: impl ToString, +) -> impl Filter>, Error = Infallible> + Clone where - T: FromStr + Send + 'static, + T: FromStr + Send, { + let name = name.to_string(); header::optional2().map(move |opt: Option| { - let cookie = opt.and_then(|cookie| cookie.get(name).map(|x| T::from_str(x))); + let cookie = opt.and_then(|cookie| cookie.get(name.as_str()).map(|x| T::from_str(x))); match cookie { Some(Ok(t)) => Some(t), Some(Err(_)) => None, diff --git a/src/reject.rs b/src/reject.rs index fd0918841..b6f275c48 100644 --- a/src/reject.rs +++ b/src/reject.rs @@ -104,7 +104,7 @@ pub(crate) fn invalid_header(name: &'static str) -> Rejection { // 400 Bad Request #[inline] -pub(crate) fn missing_cookie(name: &'static str) -> Rejection { +pub(crate) fn missing_cookie(name: String) -> Rejection { known(MissingCookie { name }) } @@ -579,13 +579,13 @@ impl StdError for InvalidHeader {} /// Missing cookie #[derive(Debug)] pub struct MissingCookie { - name: &'static str, + name: String, } impl MissingCookie { /// Retrieve the name of the cookie that was missing pub fn name(&self) -> &str { - self.name + self.name.as_str() } }