Skip to content

Commit

Permalink
Add optional cookie value filter (#93)
Browse files Browse the repository at this point in the history
Add opt cookie filter that maps extracted value

This is an optional cookie filter that applies a function to the
exracted value if present. This is useful to do some transformation
on the cookie value before passing it down the filter chain.
  • Loading branch information
algermissen authored and seanmonstar committed Sep 18, 2018
1 parent 043e044 commit 54f5d40
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/target
**/*.rs.bk
Cargo.lock
.idea/
warp.iml
15 changes: 15 additions & 0 deletions src/filters/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ pub fn optional(name: &'static str) -> impl Filter<Extract=One<Option<String>>,
})
}

/// Creates a `Filter` that looks for an optional cookie by name and maps the value.
///
/// If found, extracts the value of the cookie and applies the provided function to it,
/// otherwise continues the request, extracting `None`.
pub fn optional_value<U, F>(name: &'static str, func: F)
-> impl Filter<Extract=One<Option<U>>, Error=Never> + Copy
where
F: Fn(&str) -> U + Copy,
U: Send,
{
header::optional_value(&::http::header::COOKIE, move |val| {
find_cookie(name, val).map(|v| func(v.as_ref()))
})
}

//TODO: probably shouldn't extract a `String`, but rather a `Cookie`.
//That would allow use to change from cloning a `String` to just shallow cloning
//the `Bytes` of the header value...
Expand Down

0 comments on commit 54f5d40

Please sign in to comment.