From 81ac56dd5578da03c2bacefcae615e263f12cdb2 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 30 Mar 2025 04:17:51 -0700 Subject: [PATCH] add get() function for single cookie value --- cookies.zig | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cookies.zig b/cookies.zig index f12f3f056caf756a41eee71928a175786bc4480e..9794439f7bc815e1226879f1e37a27c9e23cc383 100644 --- a/cookies.zig +++ b/cookies.zig @@ -19,6 +19,19 @@ pub fn parse(alloc: std.mem.Allocator, cookie_str: ?string) !Jar { return map; } +pub fn get(cookie_str: ?string, name: string) ?string { + const h = cookie_str orelse return null; + var iter = std.mem.split(u8, h, "; "); + while (iter.next()) |item| { + const i = std.mem.indexOfScalar(u8, item, '='); + if (i == null) continue; + const k = item[0..i.?]; + const v = item[i.? + 1 ..]; + if (std.mem.eql(u8, k, name)) return v; + } + return null; +} + pub fn delete_string(comptime name: string) string { return name ++ "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"; } -- 2.54.0