authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2023-02-27 20:19:26 -08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-02-27 20:19:26 -08:00
log8739b57f429f6f7cca1073acab338160f0853f73
treed6704fb2397823cbace076d441213f0802e4733e
parentcc8b23b4e946c456312836ed7c133eb5fd9ee779
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Create cookies.zig


1 files changed, 28 insertions(+), 0 deletions(-)

cookies.zig created+28
...@@ -0,0 +1,28 @@
1const std = @import("std");
2const string = []const u8;
3const http = @import("apple_pie");
4
5pub const Jar = std.StringHashMap(string);
6
7pub fn parse(alloc: std.mem.Allocator, headers: http.Request.Headers) !Jar {
8 var map = Jar.init(alloc);
9 // extra check caused by https://github.com/Luukdegram/apple_pie/issues/70
10 const h = headers.get("Cookie") orelse headers.get("cookie");
11 if (h == null) return map;
12
13 var iter = std.mem.split(u8, h.?, ";");
14 while (iter.next()) |item| {
15 const i = std.mem.indexOfScalar(u8, item, '=');
16 if (i == null) continue;
17 const k = item[0..i.?];
18 const v = item[i.? + 1 ..];
19
20 if (map.contains(k)) continue;
21 try map.put(k, v);
22 }
23 return map;
24}
25
26pub fn delete(response: *http.Response, comptime name: string) !void {
27 try response.headers.put("Set-Cookie", name ++ "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT");
28}