1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00

add zap.Request.headersToOwnedList()

This commit is contained in:
Rene Schallner 2025-03-21 19:28:50 +01:00
parent dcd07b7025
commit f4e42f2557
2 changed files with 29 additions and 0 deletions

View file

@ -21,6 +21,21 @@ fn on_request_verbose(r: zap.Request) !void {
break :content_type .HTML; 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); try r.setContentType(content_type);
switch (content_type) { switch (content_type) {
.TEXT => { .TEXT => {

View file

@ -30,6 +30,7 @@ pub const HttpParamStrKV = struct {
pub const HttpParamStrKVList = struct { pub const HttpParamStrKVList = struct {
items: []HttpParamStrKV, items: []HttpParamStrKV,
allocator: Allocator, allocator: Allocator,
pub fn deinit(self: *HttpParamStrKVList) void { pub fn deinit(self: *HttpParamStrKVList) void {
for (self.items) |item| { for (self.items) |item| {
self.allocator.free(item.key); 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; 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. /// Set status by numeric value.
pub fn setStatusNumeric(self: *const Request, status: usize) void { pub fn setStatusNumeric(self: *const Request, status: usize) void {
self.h.*.status = status; self.h.*.status = status;