From 8739b57f429f6f7cca1073acab338160f0853f73 Mon Sep 17 00:00:00 2001 From: Meghan Date: Mon, 27 Feb 2023 20:19:26 -0800 Subject: [PATCH] Create cookies.zig --- cookies.zig | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cookies.zig diff --git a/cookies.zig b/cookies.zig new file mode 100644 index 0000000000000000000000000000000000000000..f986ae6c638b67a168cac93d1f4ab0da993f7723 --- /dev/null +++ b/cookies.zig @@ -0,0 +1,28 @@ +const std = @import("std"); +const string = []const u8; +const http = @import("apple_pie"); + +pub const Jar = std.StringHashMap(string); + +pub fn parse(alloc: std.mem.Allocator, headers: http.Request.Headers) !Jar { + var map = Jar.init(alloc); + // extra check caused by https://github.com/Luukdegram/apple_pie/issues/70 + const h = headers.get("Cookie") orelse headers.get("cookie"); + if (h == null) return map; + + 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 (map.contains(k)) continue; + try map.put(k, v); + } + return map; +} + +pub fn delete(response: *http.Response, comptime name: string) !void { + try response.headers.put("Set-Cookie", name ++ "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"); +} -- 2.54.0