Skip to content

Commit c79d62c

Browse files
committed
proposed change to parseAccept API
1 parent 4988feb commit c79d62c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

examples/accept/accept.zig

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ var gpa = std.heap.GeneralPurposeAllocator(.{
66
}){};
77

88
fn on_request_verbose(r: zap.Request) void {
9-
const allocator = gpa.allocator();
9+
// use a local buffer for the parsed accept headers
10+
var accept_buffer: [1024]u8 = undefined;
11+
var fba = std.heap.FixedBufferAllocator.init(&accept_buffer);
12+
const accept_allocator = fba.allocator();
13+
1014
const content_type: zap.ContentType = content_type: {
11-
var accept_list = std.ArrayList(zap.Request.AcceptItem).init(allocator);
15+
var accept_list = r.parseAcceptHeaders(accept_allocator) catch break :content_type .HTML;
1216
defer accept_list.deinit();
13-
r.parseAccept(&accept_list) catch break :content_type .HTML;
17+
1418
for (accept_list.items) |accept| {
1519
break :content_type accept.toContentType() orelse continue;
1620
}

src/request.zig

+8-1
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,14 @@ pub const AcceptItem = struct {
600600
}
601601
};
602602

603+
/// List holding access headers parsed by parseAcceptHeaders()
604+
const AcceptHeaderList = std.ArrayList(AcceptItem);
605+
603606
/// Parses `Accept:` http header into `list`, ordered from highest q factor to lowest
604-
pub fn parseAccept(self: *const Self, list: *std.ArrayList(AcceptItem)) !void {
607+
pub fn parseAcceptHeaders(self: *const Self, allocator: std.mem.Allocator) !AcceptHeaderList {
608+
var list = AcceptHeaderList.init(allocator);
609+
errdefer list.deinit();
610+
605611
const accept_str = self.getHeaderCommon(.accept) orelse return error.NoAccept;
606612

607613
var tok_iter = std.mem.tokenize(u8, accept_str, ", ");
@@ -638,6 +644,7 @@ pub fn parseAccept(self: *const Self, list: *std.ArrayList(AcceptItem)) !void {
638644
try list.append(new_item);
639645
}
640646
}
647+
return list;
641648
}
642649

643650
/// Set a response cookie

0 commit comments

Comments
 (0)