summaryrefslogtreecommitdiffstats
path: root/third_party/rust/warp/src/filters/cookie.rs
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/warp/src/filters/cookie.rs')
-rw-r--r--third_party/rust/warp/src/filters/cookie.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/third_party/rust/warp/src/filters/cookie.rs b/third_party/rust/warp/src/filters/cookie.rs
new file mode 100644
index 0000000000..53695e85c8
--- /dev/null
+++ b/third_party/rust/warp/src/filters/cookie.rs
@@ -0,0 +1,46 @@
+//! Cookie Filters
+
+use futures_util::future;
+use headers::Cookie;
+
+use super::header;
+use crate::filter::{Filter, One};
+use crate::reject::Rejection;
+use std::convert::Infallible;
+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
+where
+ T: FromStr + Send + 'static,
+{
+ 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)));
+ future::ready(cookie)
+ })
+}
+
+/// Creates a `Filter` that looks for an optional cookie by name.
+///
+/// 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
+where
+ T: FromStr + Send + 'static,
+{
+ header::optional2().map(move |opt: Option<Cookie>| {
+ let cookie = opt.and_then(|cookie| cookie.get(name).map(|x| T::from_str(x)));
+ match cookie {
+ Some(Ok(t)) => Some(t),
+ Some(Err(_)) => None,
+ None => None,
+ }
+ })
+}