Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cookie names owned strings instead of static strings #1036

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/filters/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(name: &'static str) -> impl Filter<Extract = One<T>, Error = Rejection> + Copy
pub fn cookie<T>(name: impl ToString) -> impl Filter<Extract = One<T>, 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)
})
}
Expand All @@ -30,13 +31,14 @@ where
/// If found, extracts the value of the cookie, otherwise continues
/// the request, extracting `None`.
pub fn optional<T>(
name: &'static str,
) -> impl Filter<Extract = One<Option<T>>, Error = Infallible> + Copy
name: impl ToString,
) -> impl Filter<Extract = One<Option<T>>, Error = Infallible> + Clone
where
T: FromStr + Send + 'static,
T: FromStr + Send,
{
let name = name.to_string();
header::optional2().map(move |opt: Option<Cookie>| {
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,
Expand Down
6 changes: 3 additions & 3 deletions src/reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}

Expand Down Expand Up @@ -588,13 +588,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()
}
}

Expand Down