mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
example comments
This commit is contained in:
parent
652f0e2277
commit
214cfc5dee
3 changed files with 13 additions and 8 deletions
|
@ -5,7 +5,7 @@ const Handler = struct {
|
||||||
var alloc: std.mem.Allocator = undefined;
|
var alloc: std.mem.Allocator = undefined;
|
||||||
|
|
||||||
pub fn on_request(r: zap.Request) void {
|
pub fn on_request(r: zap.Request) void {
|
||||||
// check for FORM parameters
|
// parse for FORM (body) parameters first
|
||||||
r.parseBody() catch |err| {
|
r.parseBody() catch |err| {
|
||||||
std.log.err("Parse Body error: {any}. Expected if body is empty", .{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| {
|
if (r.body) |body| {
|
||||||
std.log.info("Body length is {any}\n", .{body.len});
|
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();
|
r.parseQuery();
|
||||||
|
|
||||||
const param_count = r.getParamCount();
|
const param_count = r.getParamCount();
|
||||||
|
@ -56,8 +57,11 @@ const Handler = struct {
|
||||||
else => {
|
else => {
|
||||||
// might be a string param, we don't care
|
// might be a string param, we don't care
|
||||||
// let's just get it as string
|
// 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| {
|
if (r.getParamStr(Handler.alloc, kv.key.str, false)) |maybe_str| {
|
||||||
const value: []const u8 = if (maybe_str) |s| s.str else "(no value)";
|
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 });
|
std.log.debug(" {s} = {s}", .{ kv.key.str, value });
|
||||||
} else |err| {
|
} else |err| {
|
||||||
std.log.err("Error: {any}\n", .{err});
|
std.log.err("Error: {any}\n", .{err});
|
||||||
|
@ -70,7 +74,6 @@ const Handler = struct {
|
||||||
// check if we received a terminate=true parameter
|
// check if we received a terminate=true parameter
|
||||||
if (r.getParamStr(Handler.alloc, "terminate", false)) |maybe_str| {
|
if (r.getParamStr(Handler.alloc, "terminate", false)) |maybe_str| {
|
||||||
if (maybe_str) |*s| {
|
if (maybe_str) |*s| {
|
||||||
defer s.deinit();
|
|
||||||
std.log.info("?terminate={s}\n", .{s.str});
|
std.log.info("?terminate={s}\n", .{s.str});
|
||||||
if (std.mem.eql(u8, s.str, "true")) {
|
if (std.mem.eql(u8, s.str, "true")) {
|
||||||
zap.stop();
|
zap.stop();
|
||||||
|
|
|
@ -37,17 +37,19 @@ pub fn main() !void {
|
||||||
std.debug.print("\n=====================================================\n", .{});
|
std.debug.print("\n=====================================================\n", .{});
|
||||||
defer 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();
|
const cookie_count = r.getCookiesCount();
|
||||||
std.log.info("cookie_count: {}", .{cookie_count});
|
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;
|
var strCookies = r.cookiesToOwnedStrList(alloc, false) catch unreachable;
|
||||||
defer strCookies.deinit();
|
defer strCookies.deinit();
|
||||||
std.debug.print("\n", .{});
|
std.debug.print("\n", .{});
|
||||||
for (strCookies.items) |kv| {
|
for (strCookies.items) |kv| {
|
||||||
std.log.info("CookieStr `{s}` is `{s}`", .{ kv.key.str, kv.value.str });
|
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", .{});
|
std.debug.print("\n", .{});
|
||||||
|
@ -63,7 +65,7 @@ pub fn main() !void {
|
||||||
std.debug.print("\n", .{});
|
std.debug.print("\n", .{});
|
||||||
if (r.getCookieStr(alloc, "ZIG_ZAP", false)) |maybe_str| {
|
if (r.getCookieStr(alloc, "ZIG_ZAP", false)) |maybe_str| {
|
||||||
if (maybe_str) |*s| {
|
if (maybe_str) |*s| {
|
||||||
defer s.deinit();
|
defer s.deinit(); // unnecessary because always_alloc=false
|
||||||
|
|
||||||
std.log.info("Cookie ZIG_ZAP = {s}", .{s.str});
|
std.log.info("Cookie ZIG_ZAP = {s}", .{s.str});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,7 +3,7 @@ const zap = @import("zap");
|
||||||
const UserWeb = @import("userweb.zig");
|
const UserWeb = @import("userweb.zig");
|
||||||
const StopEndpoint = @import("stopendpoint.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 {
|
fn on_request(r: zap.Request) void {
|
||||||
if (r.path) |the_path| {
|
if (r.path) |the_path| {
|
||||||
std.debug.print("REQUESTED PATH: {s}\n", .{the_path});
|
std.debug.print("REQUESTED PATH: {s}\n", .{the_path});
|
||||||
|
@ -56,7 +56,7 @@ pub fn main() !void {
|
||||||
|
|
||||||
// and run
|
// and run
|
||||||
zap.start(.{
|
zap.start(.{
|
||||||
.threads = 2000,
|
.threads = 2,
|
||||||
// IMPORTANT! It is crucial to only have a single worker for this example to work!
|
// 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.
|
// Multiple workers would have multiple copies of the users hashmap.
|
||||||
//
|
//
|
||||||
|
|
Loading…
Add table
Reference in a new issue