diff --git a/examples/accept/accept.zig b/examples/accept/accept.zig index fc9f766..8e29350 100644 --- a/examples/accept/accept.zig +++ b/examples/accept/accept.zig @@ -21,6 +21,21 @@ fn on_request_verbose(r: zap.Request) !void { break :content_type .HTML; }; + // just for fun: print ALL headers + var maybe_headers: ?zap.Request.HttpParamStrKVList = blk: { + const h = r.headersToOwnedList(gpa.allocator()) catch |err| { + std.debug.print("Error getting headers: {}\n", .{err}); + break :blk null; + }; + break :blk h; + }; + if (maybe_headers) |*headers| { + defer headers.deinit(); + for (headers.items) |header| { + std.debug.print("Header {s} = {s}\n", .{ header.key, header.value }); + } + } + try r.setContentType(content_type); switch (content_type) { .TEXT => { diff --git a/src/request.zig b/src/request.zig index 09c25f4..74a84ef 100644 --- a/src/request.zig +++ b/src/request.zig @@ -30,6 +30,7 @@ pub const HttpParamStrKV = struct { pub const HttpParamStrKVList = struct { items: []HttpParamStrKV, allocator: Allocator, + pub fn deinit(self: *HttpParamStrKVList) void { for (self.items) |item| { self.allocator.free(item.key); @@ -537,6 +538,19 @@ pub fn setHeader(self: *const Request, name: []const u8, value: []const u8) Http return error.HttpSetHeader; } +pub fn headersToOwnedList(self: *const Request, a: Allocator) !HttpParamStrKVList { + var headers = std.ArrayList(HttpParamStrKV).init(a); + var context: CallbackContext_StrKV = .{ + .params = &headers, + .allocator = a, + }; + const howmany = fio.fiobj_each1(self.h.*.headers, 0, CallbackContext_StrKV.callback, &context); + if (howmany != headers.items.len) { + return error.HttpIterHeaders; + } + return .{ .items = try headers.toOwnedSlice(), .allocator = a }; +} + /// Set status by numeric value. pub fn setStatusNumeric(self: *const Request, status: usize) void { self.h.*.status = status;