diff --git a/examples/hello2/hello2.zig b/examples/hello2/hello2.zig index a4821a2..75afaf0 100644 --- a/examples/hello2/hello2.zig +++ b/examples/hello2/hello2.zig @@ -7,6 +7,12 @@ fn on_request(r: zap.SimpleRequest) void { const qm = if (r.query) |_| "?" else ""; const qq = r.query orelse ""; + // curl -H "special-header: hello" http://localhost:3000 + if (r.getHeader("special-header")) |clstr| { + std.debug.print(">> Special Header: {s}\n", .{clstr}); + } else { + std.debug.print(">> Special Header: \n", .{}); + } std.debug.print(">> {s} {s}{s}{s}\n", .{ m, p, qm, qq }); if (r.body) |the_body| { @@ -41,6 +47,6 @@ pub fn main() !void { // start worker threads zap.start(.{ .threads = 2, - .workers = 2, + .workers = 1, }); } diff --git a/src/fio.zig b/src/fio.zig index 98b63f0..6e68c55 100644 --- a/src/fio.zig +++ b/src/fio.zig @@ -76,6 +76,7 @@ pub extern fn http_send_body(h: [*c]http_s, data: ?*anyopaque, length: usize) c_ pub extern fn fiobj_hash_new() FIOBJ; pub extern fn fiobj_hash_set(hash: FIOBJ, key: FIOBJ, obj: FIOBJ) c_int; +pub extern fn fiobj_hash_get(hash: FIOBJ, key: FIOBJ) FIOBJ; pub extern fn fiobj_ary_push(ary: FIOBJ, obj: FIOBJ) void; pub extern fn fiobj_float_new(num: f64) FIOBJ; pub extern fn fiobj_num_new_bignum(num: isize) FIOBJ; diff --git a/src/util.zig b/src/util.zig index 6e4b5aa..01fc9cc 100644 --- a/src/util.zig +++ b/src/util.zig @@ -4,6 +4,8 @@ const fio = @import("fio.zig"); pub fn fio2str(o: fio.FIOBJ) ?[]const u8 { if (o == 0) return null; const x: fio.fio_str_info_s = fio.fiobj_obj2cstr(o); + if (x.data == 0) + return null; // TODO: should we return an error? Actually, looking at fiobj_obj2cstr, this is unreachable return std.mem.span(x.data); } diff --git a/src/zap.zig b/src/zap.zig index f6505f3..3d62f70 100644 --- a/src/zap.zig +++ b/src/zap.zig @@ -123,6 +123,12 @@ pub const SimpleRequest = struct { if (ret == -1) return error.HttpSetContentType; } + pub fn getHeader(self: *const Self, name: []const u8) ?[]const u8 { + const hname = fio.fiobj_str_new(util.toCharPtr(name), name.len); + defer fio.fiobj_free_wrapped(hname); + return util.fio2str(fio.fiobj_hash_get(self.h.*.headers, hname)); + } + pub fn setHeader(self: *const Self, name: []const u8, value: []const u8) HttpError!void { const hname: fio.fio_str_info_s = .{ .data = util.toCharPtr(name),