| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | |
| 4 | pub const Jar = std.StringHashMap(string); |
| 5 | |
| 6 | pub fn parse(alloc: std.mem.Allocator, cookie_str: ?string) !Jar { |
| 7 | var map = Jar.init(alloc); |
| 8 | const h = cookie_str orelse return map; |
| 9 | var iter = std.mem.splitSequence(u8, h, "; "); |
| 10 | while (iter.next()) |item| { |
| 11 | const i = std.mem.indexOfScalar(u8, item, '='); |
| 12 | if (i == null) continue; |
| 13 | const k = item[0..i.?]; |
| 14 | const v = item[i.? + 1 ..]; |
| 15 | |
| 16 | if (map.contains(k)) continue; |
| 17 | try map.put(k, v); |
| 18 | } |
| 19 | return map; |
| 20 | } |
| 21 | |
| 22 | pub fn get(cookie_str: ?string, name: string) ?string { |
| 23 | const h = cookie_str orelse return null; |
| 24 | var iter = std.mem.splitSequence(u8, h, "; "); |
| 25 | while (iter.next()) |item| { |
| 26 | const i = std.mem.indexOfScalar(u8, item, '='); |
| 27 | if (i == null) continue; |
| 28 | const k = item[0..i.?]; |
| 29 | const v = item[i.? + 1 ..]; |
| 30 | if (std.mem.eql(u8, k, name)) return v; |
| 31 | } |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | pub fn delete_string(comptime name: string) string { |
| 36 | return name ++ "=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT"; |
| 37 | } |