//! //! Part of the Zap examples. //! //! Build me with `zig build accept`. //! Run me with `zig build run-accept`. //! const std = @import("std"); const zap = @import("zap"); var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, }){}; fn on_request_verbose(r: zap.Request) !void { // 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: { var accept_list = r.parseAcceptHeaders(accept_allocator) catch break :content_type .HTML; defer accept_list.deinit(accept_allocator); for (accept_list.items) |accept| { break :content_type accept.toContentType() orelse continue; } 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 => { try r.sendBody("Hello from ZAP!!!"); }, .HTML => { try r.sendBody("