1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00

added support for SimpleRequest.getHeader()

This commit is contained in:
Rene Schallner 2023-04-14 23:32:56 +02:00
parent cdf7363fcd
commit dcf1ea4750
4 changed files with 16 additions and 1 deletions

View file

@ -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: <unknown>\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,
});
}

View file

@ -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;

View file

@ -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);
}

View file

@ -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),