Something went wrong. Try again.
xburner is an experimental HTTP and gRPC load generator written in Zig.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586const std = @import("std");
pub const OwnedHeader = struct { name: []u8, value: []u8,};
pub fn clearOwnedHeaders(allocator: std.mem.Allocator, headers: []OwnedHeader) void { for (headers) |header| { allocator.free(header.name); allocator.free(header.value); } if (headers.len != 0) allocator.free(headers);}
pub const CookieJar = struct { allocator: std.mem.Allocator, cookies: std.StringHashMap([]u8),
pub fn init(allocator: std.mem.Allocator) CookieJar { return .{ .allocator = allocator, .cookies = std.StringHashMap([]u8).init(allocator), }; }
pub fn deinit(self: *CookieJar) void { var iterator = self.cookies.iterator(); while (iterator.next()) |entry| { self.allocator.free(entry.key_ptr.*); self.allocator.free(entry.value_ptr.*); } self.cookies.deinit(); self.* = undefined; }
pub fn clear(self: *CookieJar) void { var iterator = self.cookies.iterator(); while (iterator.next()) |entry| { self.allocator.free(entry.key_ptr.*); self.allocator.free(entry.value_ptr.*); } self.cookies.clearRetainingCapacity(); }
pub fn set(self: *CookieJar, name: []const u8, value: []const u8) !void { if (self.cookies.fetchRemove(name)) |removed| { self.allocator.free(removed.key); self.allocator.free(removed.value); } try self.cookies.put(try self.allocator.dupe(u8, name), try self.allocator.dupe(u8, value)); }
pub fn headerValue(self: *const CookieJar, allocator: std.mem.Allocator) !?[]u8 { if (self.cookies.count() == 0) return null;
var out = std.ArrayList(u8).empty; defer out.deinit(allocator);
var iterator = self.cookies.iterator(); var first = true; while (iterator.next()) |entry| { if (!first) try out.appendSlice(allocator, "; "); first = false; try out.appendSlice(allocator, entry.key_ptr.*); try out.append(allocator, '='); try out.appendSlice(allocator, entry.value_ptr.*); }
return try out.toOwnedSlice(allocator); }
pub fn updateFromHeaders(self: *CookieJar, headers: []const OwnedHeader) !void { for (headers) |header| { if (!std.ascii.eqlIgnoreCase(header.name, "set-cookie")) continue; var cookie_iter = std.mem.splitScalar(u8, header.value, ';'); const cookie_pair = std.mem.trim(u8, cookie_iter.first(), " \t"); const eq_index = std.mem.indexOfScalar(u8, cookie_pair, '=') orelse continue; const name = std.mem.trim(u8, cookie_pair[0..eq_index], " \t"); const value = std.mem.trim(u8, cookie_pair[eq_index + 1 ..], " \t"); if (name.len == 0) continue; try self.set(name, value); } }};