Skip to content

Commit

Permalink
Update 'cookie' to 0.18.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed Sep 29, 2023
1 parent f41474d commit 5d31ad4
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 88 deletions.
2 changes: 1 addition & 1 deletion core/codegen/src/bang/uri_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ impl Arg {
fn unnamed(&self) -> &ArgExpr {
match self {
Arg::Unnamed(expr) => expr,
_ => panic!("Called Arg::unnamed() on an Arg::named!"),
_ => panic!("Called Arg::unnamed() on an Arg::Named!"),
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pear = "0.2.3"
pin-project-lite = "0.2"
memchr = "2"
stable-pattern = "0.1"
cookie = { version = "0.17.0", features = ["percent-encode"] }
cookie = { version = "=0.18.0-rc.0", features = ["percent-encode"] }
state = "0.6"
futures = { version = "0.3", default-features = false }

Expand Down
73 changes: 41 additions & 32 deletions core/lib/src/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ pub use self::cookie::{Cookie, SameSite, Iter};
///
/// #[get("/message")]
/// fn message(jar: &CookieJar<'_>) {
/// jar.add(Cookie::new("message", "hello!"));
/// jar.add(Cookie::new("other", "bye!"));
/// jar.add(("message", "hello!"));
/// jar.add(Cookie::build(("session", "bye!")).expires(None));
///
/// // `get()` does not reflect changes.
/// assert!(jar.get("other").is_none());
/// # assert_eq!(jar.get("message").map(|c| c.value()), Some("hi"));
/// assert!(jar.get("session").is_none());
/// assert_eq!(jar.get("message").map(|c| c.value()), Some("hi"));
///
/// // `get_pending()` does.
/// let other_pending = jar.get_pending("other");
/// let session_pending = jar.get_pending("session");
/// let message_pending = jar.get_pending("message");
/// assert_eq!(other_pending.as_ref().map(|c| c.value()), Some("bye!"));
/// assert_eq!(session_pending.as_ref().map(|c| c.value()), Some("bye!"));
/// assert_eq!(message_pending.as_ref().map(|c| c.value()), Some("hello!"));
/// # jar.remove(Cookie::named("message"));
/// # jar.remove("message");
/// # assert_eq!(jar.get("message").map(|c| c.value()), Some("hi"));
/// # assert!(jar.get_pending("message").is_none());
/// }
/// # fn main() {
/// # use rocket::local::blocking::Client;
/// # let client = Client::debug_with(routes![message]).unwrap();
/// # let response = client.get("/message")
/// # .cookie(Cookie::new("message", "hi"))
/// # .cookie(("message", "hi"))
/// # .dispatch();
/// #
/// # assert!(response.status().class().is_success());
Expand Down Expand Up @@ -202,7 +202,7 @@ impl<'a> CookieJar<'a> {
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// use rocket::http::{Cookie, CookieJar};
/// use rocket::http::CookieJar;
///
/// #[get("/")]
/// fn handler(jar: &CookieJar<'_>) {
Expand All @@ -226,7 +226,7 @@ impl<'a> CookieJar<'a> {
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// use rocket::http::{Cookie, CookieJar};
/// use rocket::http::CookieJar;
///
/// #[get("/")]
/// fn handler(jar: &CookieJar<'_>) {
Expand All @@ -252,7 +252,7 @@ impl<'a> CookieJar<'a> {
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// use rocket::http::{Cookie, CookieJar};
/// use rocket::http::CookieJar;
///
/// #[get("/")]
/// fn handler(jar: &CookieJar<'_>) {
Expand Down Expand Up @@ -297,17 +297,18 @@ impl<'a> CookieJar<'a> {
///
/// #[get("/")]
/// fn handler(jar: &CookieJar<'_>) {
/// jar.add(Cookie::new("first", "value"));
/// jar.add(("first", "value"));
///
/// let cookie = Cookie::build("other", "value_two")
/// let cookie = Cookie::build(("other", "value_two"))
/// .path("/")
/// .secure(true)
/// .same_site(SameSite::Lax);
///
/// jar.add(cookie.finish());
/// jar.add(cookie);
/// }
/// ```
pub fn add(&self, mut cookie: Cookie<'static>) {
pub fn add<C: Into<Cookie<'static>>>(&self, cookie: C) {
let mut cookie = cookie.into();
Self::set_defaults(self.config, &mut cookie);
self.ops.lock().push(Op::Add(cookie, false));
}
Expand All @@ -334,30 +335,32 @@ impl<'a> CookieJar<'a> {
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// use rocket::http::{Cookie, CookieJar};
/// use rocket::http::CookieJar;
///
/// #[get("/")]
/// fn handler(jar: &CookieJar<'_>) {
/// jar.add_private(Cookie::new("name", "value"));
/// jar.add_private(("name", "value"));
/// }
/// ```
#[cfg(feature = "secrets")]
#[cfg_attr(nightly, doc(cfg(feature = "secrets")))]
pub fn add_private(&self, mut cookie: Cookie<'static>) {
pub fn add_private<C: Into<Cookie<'static>>>(&self, cookie: C) {
let mut cookie = cookie.into();
Self::set_private_defaults(self.config, &mut cookie);
self.ops.lock().push(Op::Add(cookie, true));
}

/// Removes `cookie` from this collection and generates a "removal" cookies
/// to send to the client on response. For correctness, `cookie` must
/// contain the same `path` and `domain` as the cookie that was initially
/// set. Failure to provide the initial `path` and `domain` will result in
/// cookies that are not properly removed. For convenience, if a path is not
/// set on `cookie`, the `"/"` path will automatically be set.
/// to send to the client on response. A "removal" cookie is a cookie that
/// has the same name as the original cookie but has an empty value, a
/// max-age of 0, and an expiration date far in the past.
///
/// **Note: For correctness, `cookie` must contain the same `path` and
/// `domain` as the cookie that was initially set. Failure to provide the
/// initial `path` and `domain` will result in cookies that are not properly
/// removed. For convenience, if a path is not set on `cookie`, the `"/"`
/// path will automatically be set.**
///
/// A "removal" cookie is a cookie that has the same name as the original
/// cookie but has an empty value, a max-age of 0, and an expiration date
/// far in the past.
///
/// # Example
///
Expand All @@ -367,10 +370,15 @@ impl<'a> CookieJar<'a> {
///
/// #[get("/")]
/// fn handler(jar: &CookieJar<'_>) {
/// jar.remove(Cookie::named("name"));
/// // Rocket will set `path` to `/`.
/// jar.remove("name");
///
/// // Use a custom-built cookie to set a custom path.
/// jar.remove(Cookie::build("name").path("/login"));
/// }
/// ```
pub fn remove(&self, mut cookie: Cookie<'static>) {
pub fn remove<C: Into<Cookie<'static>>>(&self, cookie: C) {
let mut cookie = cookie.into();
if cookie.path().is_none() {
cookie.set_path("/");
}
Expand All @@ -388,16 +396,17 @@ impl<'a> CookieJar<'a> {
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// use rocket::http::{Cookie, CookieJar};
/// use rocket::http::CookieJar;
///
/// #[get("/")]
/// fn handler(jar: &CookieJar<'_>) {
/// jar.remove_private(Cookie::named("name"));
/// jar.remove_private("name");
/// }
/// ```
#[cfg(feature = "secrets")]
#[cfg_attr(nightly, doc(cfg(feature = "secrets")))]
pub fn remove_private(&self, mut cookie: Cookie<'static>) {
pub fn remove_private<C: Into<Cookie<'static>>>(&self, cookie: C) {
let mut cookie = cookie.into();
if cookie.path().is_none() {
cookie.set_path("/");
}
Expand All @@ -415,7 +424,7 @@ impl<'a> CookieJar<'a> {
///
/// ```rust
/// # #[macro_use] extern crate rocket;
/// use rocket::http::{Cookie, CookieJar};
/// use rocket::http::CookieJar;
///
/// #[get("/")]
/// fn handler(jar: &CookieJar<'_>) {
Expand Down
4 changes: 2 additions & 2 deletions core/lib/src/local/asynchronous/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::{Client, LocalResponse};
/// let req = client.post("/")
/// .header(ContentType::JSON)
/// .remote("127.0.0.1:8000".parse().unwrap())
/// .cookie(Cookie::new("name", "value"))
/// .cookie(("name", "value"))
/// .body(r#"{ "value": 42 }"#);
///
/// let response = req.dispatch().await;
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'c> LocalRequest<'c> {
for cookie in response.cookies().iter() {
if let Some(expires) = cookie.expires_datetime() {
if expires <= current_time {
jar.force_remove(cookie);
jar.force_remove(cookie.name());
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/src/local/blocking/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::{Client, LocalResponse};
/// let req = client.post("/")
/// .header(ContentType::JSON)
/// .remote("127.0.0.1:8000".parse().unwrap())
/// .cookie(Cookie::new("name", "value"))
/// .cookie(("name", "value"))
/// .body(r#"{ "value": 42 }"#);
///
/// let response = req.dispatch();
Expand Down
26 changes: 16 additions & 10 deletions core/lib/src/local/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,15 @@ macro_rules! pub_request_impl {
/// # Client::_test(|_, request, _| {
/// let request: LocalRequest = request;
/// let req = request
/// .cookie(Cookie::new("username", "sb"))
/// .cookie(Cookie::new("user_id", "12"));
/// .cookie(("username", "sb"))
/// .cookie(("user_id", "12"));
/// # });
/// ```
#[inline]
pub fn cookie(mut self, cookie: crate::http::Cookie<'_>) -> Self {
self._request_mut().cookies_mut().add_original(cookie.into_owned());
pub fn cookie<'a, C>(mut self, cookie: C) -> Self
where C: Into<crate::http::Cookie<'a>>
{
self._request_mut().cookies_mut().add_original(cookie.into().into_owned());
self
}

Expand All @@ -151,15 +153,17 @@ macro_rules! pub_request_impl {
///
/// # Client::_test(|_, request, _| {
/// let request: LocalRequest = request;
/// let cookies = vec![Cookie::new("a", "b"), Cookie::new("c", "d")];
/// let cookies = vec![("a", "b"), ("c", "d")];
/// let req = request.cookies(cookies);
/// # });
/// ```
#[inline]
pub fn cookies<'a, C>(mut self, cookies: C) -> Self
where C: IntoIterator<Item = crate::http::Cookie<'a>>
pub fn cookies<'a, C, I>(mut self, cookies: I) -> Self
where C: Into<crate::http::Cookie<'a>>,
I: IntoIterator<Item = C>
{
for cookie in cookies {
let cookie: crate::http::Cookie<'_> = cookie.into();
self._request_mut().cookies_mut().add_original(cookie.into_owned());
}

Expand All @@ -180,14 +184,16 @@ macro_rules! pub_request_impl {
///
/// # Client::_test(|_, request, _| {
/// let request: LocalRequest = request;
/// let req = request.private_cookie(Cookie::new("user_id", "sb"));
/// let req = request.private_cookie(("user_id", "sb"));
/// # });
/// ```
#[cfg(feature = "secrets")]
#[cfg_attr(nightly, doc(cfg(feature = "secrets")))]
#[inline]
pub fn private_cookie(mut self, cookie: crate::http::Cookie<'static>) -> Self {
self._request_mut().cookies_mut().add_original_private(cookie);
pub fn private_cookie<C>(mut self, cookie: C) -> Self
where C: Into<crate::http::Cookie<'static>>
{
self._request_mut().cookies_mut().add_original_private(cookie.into());
self
}

Expand Down
4 changes: 2 additions & 2 deletions core/lib/src/request/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ impl<'r> Request<'r> {
/// # let c = rocket::local::blocking::Client::debug_with(vec![]).unwrap();
/// # let request = c.get("/");
/// # let req = request.inner();
/// req.cookies().add(Cookie::new("key", "val"));
/// req.cookies().add(Cookie::new("ans", format!("life: {}", 38 + 4)));
/// req.cookies().add(("key", "val"));
/// req.cookies().add(("ans", format!("life: {}", 38 + 4)));
///
/// assert_eq!(req.cookies().get_pending("key").unwrap().value(), "val");
/// assert_eq!(req.cookies().get_pending("ans").unwrap().value(), "life: 42");
Expand Down
6 changes: 3 additions & 3 deletions core/lib/src/response/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ impl<R> Flash<R> {
let content = format!("{}{}{}{}",
self.kind.len(), FLASH_COOKIE_DELIM, self.kind, self.message);

Cookie::build(FLASH_COOKIE_NAME, content)
Cookie::build((FLASH_COOKIE_NAME, content))
.max_age(Duration::minutes(5))
.finish()
.build()
}
}

Expand Down Expand Up @@ -211,7 +211,7 @@ impl<'r> FlashMessage<'r> {
fn clear_cookie_if_needed(&self) {
// Remove the cookie if it hasn't already been removed.
if !self.consumed.swap(true, Ordering::Relaxed) {
self.inner.remove(Cookie::named(FLASH_COOKIE_NAME));
self.inner.remove(FLASH_COOKIE_NAME);
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/lib/tests/catcher-cookies-1213.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#[macro_use] extern crate rocket;

use rocket::request::Request;
use rocket::http::{Cookie, CookieJar};
use rocket::http::CookieJar;

#[catch(404)]
fn not_found(request: &Request) -> &'static str {
request.cookies().add(Cookie::new("not_found", "404"));
request.cookies().add(("not_found", "404"));
"404 - Not Found"
}

#[get("/")]
fn index(cookies: &CookieJar<'_>) -> &'static str {
cookies.add(Cookie::new("index", "hi"));
cookies.add(("index", "hi"));
"Hello, world!"
}

Expand All @@ -26,7 +26,7 @@ mod tests {
.mount("/", routes![index])
.register("/", catchers![not_found])
.attach(AdHoc::on_request("Add Cookie", |req, _| Box::pin(async move {
req.cookies().add(Cookie::new("fairing", "woo"));
req.cookies().add(("fairing", "woo"));
})));

let client = Client::debug(rocket).unwrap();
Expand Down
14 changes: 7 additions & 7 deletions core/lib/tests/cookies-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn cookie_add_private(jar: &CookieJar<'_>) {
jar.add(cookie_a.clone());
let mut cookie_b = Cookie::new("b", "v2");
jar.add_private(cookie_b.clone());
jar.add(Cookie::new("c", "v3"));
jar.add(("c", "v3"));

// private: CookieJar::set_defaults(&mut cookie_a);
cookie_a.set_path("/");
Expand Down Expand Up @@ -89,9 +89,9 @@ mod cookies_private_tests {
let client = Client::debug(rocket()).unwrap();
let response = client
.get("/")
.cookie(Cookie::new("a", "Cookie"))
.private_cookie(Cookie::new("b", " tastes "))
.cookie(Cookie::new("c", "good!"))
.cookie(("a", "Cookie"))
.private_cookie(("b", " tastes "))
.cookie(("c", "good!"))
.dispatch();

assert_eq!(response.into_string().unwrap(), "Cookie tastes good!");
Expand All @@ -103,9 +103,9 @@ mod cookies_private_tests {
let client = Client::debug(rocket()).unwrap();
let response = client
.get("/oh-no")
.cookie(Cookie::new("a", "Cookie"))
.private_cookie(Cookie::new("b", " tastes "))
.cookie(Cookie::new("c", "good!"))
.cookie(("a", "Cookie"))
.private_cookie(("b", " tastes "))
.cookie(("c", "good!"))
.dispatch();

assert_ne!(response.into_string().unwrap(), "Cookie tastes good!");
Expand Down
Loading

0 comments on commit 5d31ad4

Please sign in to comment.