mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
proposed change to parseAccept API
This commit is contained in:
parent
4988feb314
commit
c79d62ca2c
2 changed files with 15 additions and 4 deletions
|
@ -6,11 +6,15 @@ var gpa = std.heap.GeneralPurposeAllocator(.{
|
||||||
}){};
|
}){};
|
||||||
|
|
||||||
fn on_request_verbose(r: zap.Request) void {
|
fn on_request_verbose(r: zap.Request) void {
|
||||||
const allocator = gpa.allocator();
|
// use a local buffer for the parsed accept headers
|
||||||
|
var accept_buffer: [1024]u8 = undefined;
|
||||||
|
var fba = std.heap.FixedBufferAllocator.init(&accept_buffer);
|
||||||
|
const accept_allocator = fba.allocator();
|
||||||
|
|
||||||
const content_type: zap.ContentType = content_type: {
|
const content_type: zap.ContentType = content_type: {
|
||||||
var accept_list = std.ArrayList(zap.Request.AcceptItem).init(allocator);
|
var accept_list = r.parseAcceptHeaders(accept_allocator) catch break :content_type .HTML;
|
||||||
defer accept_list.deinit();
|
defer accept_list.deinit();
|
||||||
r.parseAccept(&accept_list) catch break :content_type .HTML;
|
|
||||||
for (accept_list.items) |accept| {
|
for (accept_list.items) |accept| {
|
||||||
break :content_type accept.toContentType() orelse continue;
|
break :content_type accept.toContentType() orelse continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -600,8 +600,14 @@ pub const AcceptItem = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// List holding access headers parsed by parseAcceptHeaders()
|
||||||
|
const AcceptHeaderList = std.ArrayList(AcceptItem);
|
||||||
|
|
||||||
/// Parses `Accept:` http header into `list`, ordered from highest q factor to lowest
|
/// Parses `Accept:` http header into `list`, ordered from highest q factor to lowest
|
||||||
pub fn parseAccept(self: *const Self, list: *std.ArrayList(AcceptItem)) !void {
|
pub fn parseAcceptHeaders(self: *const Self, allocator: std.mem.Allocator) !AcceptHeaderList {
|
||||||
|
var list = AcceptHeaderList.init(allocator);
|
||||||
|
errdefer list.deinit();
|
||||||
|
|
||||||
const accept_str = self.getHeaderCommon(.accept) orelse return error.NoAccept;
|
const accept_str = self.getHeaderCommon(.accept) orelse return error.NoAccept;
|
||||||
|
|
||||||
var tok_iter = std.mem.tokenize(u8, accept_str, ", ");
|
var tok_iter = std.mem.tokenize(u8, accept_str, ", ");
|
||||||
|
@ -638,6 +644,7 @@ pub fn parseAccept(self: *const Self, list: *std.ArrayList(AcceptItem)) !void {
|
||||||
try list.append(new_item);
|
try list.append(new_item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a response cookie
|
/// Set a response cookie
|
||||||
|
|
Loading…
Add table
Reference in a new issue