Skip to content

Commit 6c68d1d

Browse files
committed
Fixed a bug in the cookie component where removing a cookie from a subdirectory would not work
fixes #368 Thanks to @E8y2FqZE for reporting the issue.
1 parent 64ffe50 commit 6c68d1d

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- new `tooltip` property in the button component.
77
- New `search_value` property in the shell component.
88
- Fixed a display issue in the hero component when the button text is long and the viewport is narrow.
9+
- Fixed a bug in the cookie component where removing a cookie from a subdirectory would not work.
910

1011
## 0.22.0 (2024-05-29)
1112
- **Important Security Fix:** The behavior of `SET $x` has been modified to match `SELECT $x`.

examples/read-and-set-http-cookies/index.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ SELECT 'username' as name,
1111
'try leaving this page and coming back, the value should be saved in a cookie' as description;
1212

1313
select 'text' as component;
14-
select 'log out' as contents, 'logout.sql' as link;
14+
select 'log out' as contents, 'logout.sql' as link;
15+
16+
select 'text' as component;
17+
select 'View the cookie from a subdirectory' as contents, 'subdirectory/read_cookies.sql' as link;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Cookies can be specific to a certain path in the website
2+
-- This page demonstrates that a gobal cookie can be removed from a subdirectory
3+
select 'cookie' as component, 'username' as name, true as remove, '/' as path;
4+
5+
SELECT 'text' as component;
6+
SELECT 'The value of your username cookie was: ' ||
7+
COALESCE(sqlpage.cookie('username'), 'NULL') ||
8+
'. It has now been removed. You can reload this page.' as contents;
9+

src/render.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ impl<'a, W: std::io::Write> HeaderContext<'a, W> {
114114
.with_context(|| "cookie name must be a string")?;
115115
let mut cookie = actix_web::cookie::Cookie::named(name);
116116

117+
let path = obj.get("path").and_then(JsonValue::as_str);
118+
if let Some(path) = path {
119+
cookie.set_path(path);
120+
} else {
121+
cookie.set_path("/");
122+
}
123+
let domain = obj.get("domain").and_then(JsonValue::as_str);
124+
if let Some(domain) = domain {
125+
cookie.set_domain(domain);
126+
}
127+
117128
let remove = obj.get("remove");
118129
if remove == Some(&json!(true)) || remove == Some(&json!(1)) {
119130
cookie.make_removal();
@@ -138,16 +149,6 @@ impl<'a, W: std::io::Write> HeaderContext<'a, W> {
138149
});
139150
let secure = obj.get("secure");
140151
cookie.set_secure(secure != Some(&json!(false)) && secure != Some(&json!(0)));
141-
let path = obj.get("path").and_then(JsonValue::as_str);
142-
if let Some(path) = path {
143-
cookie.set_path(path);
144-
} else {
145-
cookie.set_path("/");
146-
}
147-
let domain = obj.get("domain").and_then(JsonValue::as_str);
148-
if let Some(domain) = domain {
149-
cookie.set_domain(domain);
150-
}
151152
let expires = obj.get("expires");
152153
if let Some(expires) = expires {
153154
cookie.set_expires(actix_web::cookie::Expiration::DateTime(match expires {

0 commit comments

Comments
 (0)