mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
Merge pull request #197 from jetzig-framework/cookie-delete
Cookie deletion
This commit is contained in:
commit
58fa7ae397
@ -107,10 +107,12 @@ pub fn deinit(self: *Cookies) void {
|
|||||||
self.arena.deinit();
|
self.arena.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch a cookie by name.
|
||||||
pub fn get(self: *Cookies, key: []const u8) ?*Cookie {
|
pub fn get(self: *Cookies, key: []const u8) ?*Cookie {
|
||||||
return self.cookies.get(key);
|
return self.cookies.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Put a cookie into the cookie store.
|
||||||
pub fn put(self: *Cookies, cookie: Cookie) !void {
|
pub fn put(self: *Cookies, cookie: Cookie) !void {
|
||||||
self.modified = true;
|
self.modified = true;
|
||||||
|
|
||||||
@ -127,6 +129,18 @@ pub fn put(self: *Cookies, cookie: Cookie) !void {
|
|||||||
try self.cookies.put(key, ptr);
|
try self.cookies.put(key, ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Overwrite a cookie with an empty string and expiry of 0. The browser should then no longer
|
||||||
|
/// send the specified cookie value.
|
||||||
|
///
|
||||||
|
/// > Notice that servers can delete cookies by sending the user agent a new cookie with an
|
||||||
|
/// > Expires attribute with a value in the past.
|
||||||
|
/// - https://www.rfc-editor.org/rfc/rfc6265.html
|
||||||
|
pub fn delete(self: *Cookies, key: []const u8) !void {
|
||||||
|
self.modified = true;
|
||||||
|
|
||||||
|
try self.put(.{ .name = key, .value = "", .expires = 0 });
|
||||||
|
}
|
||||||
|
|
||||||
pub const HeaderIterator = struct {
|
pub const HeaderIterator = struct {
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
cookies_iterator: std.StringArrayHashMap(*Cookie).Iterator,
|
cookies_iterator: std.StringArrayHashMap(*Cookie).Iterator,
|
||||||
@ -434,3 +448,17 @@ test "default flags" {
|
|||||||
try std.testing.expect(cookie.expires == null);
|
try std.testing.expect(cookie.expires == null);
|
||||||
try std.testing.expect(cookie.max_age == null);
|
try std.testing.expect(cookie.max_age == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "delete" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
var cookies = Cookies.init(allocator, "foo=bar;");
|
||||||
|
defer cookies.deinit();
|
||||||
|
|
||||||
|
try cookies.parse();
|
||||||
|
|
||||||
|
try cookies.delete("foo");
|
||||||
|
const cookie = cookies.get("foo").?;
|
||||||
|
|
||||||
|
try std.testing.expectEqualStrings(cookie.value, "");
|
||||||
|
try std.testing.expectEqual(cookie.expires.?, 0);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user