diff --git a/examples/bindataformpost/bindataformpost.zig b/examples/bindataformpost/bindataformpost.zig index 0406c58..20b826a 100644 --- a/examples/bindataformpost/bindataformpost.zig +++ b/examples/bindataformpost/bindataformpost.zig @@ -5,7 +5,7 @@ const Handler = struct { var alloc: std.mem.Allocator = undefined; pub fn on_request(r: zap.Request) void { - // check for FORM parameters + // parse for FORM (body) parameters first r.parseBody() catch |err| { std.log.err("Parse Body error: {any}. Expected if body is empty", .{err}); }; @@ -13,7 +13,8 @@ const Handler = struct { if (r.body) |body| { std.log.info("Body length is {any}\n", .{body.len}); } - // check for query params (for ?terminate=true) + + // parse potential query params (for ?terminate=true) r.parseQuery(); const param_count = r.getParamCount(); @@ -56,8 +57,11 @@ const Handler = struct { else => { // might be a string param, we don't care // let's just get it as string + // always_alloc param = false -> the string will be a slice from the request buffer + // --> no deinit necessary if (r.getParamStr(Handler.alloc, kv.key.str, false)) |maybe_str| { const value: []const u8 = if (maybe_str) |s| s.str else "(no value)"; + // above, we didn't defer s.deinit because the string is just a slice from the request buffer std.log.debug(" {s} = {s}", .{ kv.key.str, value }); } else |err| { std.log.err("Error: {any}\n", .{err}); @@ -70,7 +74,6 @@ const Handler = struct { // check if we received a terminate=true parameter if (r.getParamStr(Handler.alloc, "terminate", false)) |maybe_str| { if (maybe_str) |*s| { - defer s.deinit(); std.log.info("?terminate={s}\n", .{s.str}); if (std.mem.eql(u8, s.str, "true")) { zap.stop(); diff --git a/examples/cookies/cookies.zig b/examples/cookies/cookies.zig index d80cafc..ed7268b 100644 --- a/examples/cookies/cookies.zig +++ b/examples/cookies/cookies.zig @@ -37,17 +37,19 @@ pub fn main() !void { std.debug.print("\n=====================================================\n", .{}); defer std.debug.print("=====================================================\n\n", .{}); - r.parseCookies(false); + r.parseCookies(false); // url_encoded = false const cookie_count = r.getCookiesCount(); std.log.info("cookie_count: {}", .{cookie_count}); - // iterate over all cookies as strings + // iterate over all cookies as strings (always_alloc=false) var strCookies = r.cookiesToOwnedStrList(alloc, false) catch unreachable; defer strCookies.deinit(); std.debug.print("\n", .{}); for (strCookies.items) |kv| { std.log.info("CookieStr `{s}` is `{s}`", .{ kv.key.str, kv.value.str }); + // we don't need to deinit kv.key and kv.value because we requested always_alloc=false + // so they are just slices into the request buffer } std.debug.print("\n", .{}); @@ -63,7 +65,7 @@ pub fn main() !void { std.debug.print("\n", .{}); if (r.getCookieStr(alloc, "ZIG_ZAP", false)) |maybe_str| { if (maybe_str) |*s| { - defer s.deinit(); + defer s.deinit(); // unnecessary because always_alloc=false std.log.info("Cookie ZIG_ZAP = {s}", .{s.str}); } else { diff --git a/examples/endpoint/main.zig b/examples/endpoint/main.zig index fcc63c3..cbb74b3 100644 --- a/examples/endpoint/main.zig +++ b/examples/endpoint/main.zig @@ -3,7 +3,7 @@ const zap = @import("zap"); const UserWeb = @import("userweb.zig"); const StopEndpoint = @import("stopendpoint.zig"); -// this is just to demo that we can catch arbitrary slugs +// this is just to demo that we can catch arbitrary slugs as fallback fn on_request(r: zap.Request) void { if (r.path) |the_path| { std.debug.print("REQUESTED PATH: {s}\n", .{the_path}); @@ -56,7 +56,7 @@ pub fn main() !void { // and run zap.start(.{ - .threads = 2000, + .threads = 2, // IMPORTANT! It is crucial to only have a single worker for this example to work! // Multiple workers would have multiple copies of the users hashmap. //