From 5ed5dc2c3a7f5c3bf4ca06da0caed9087733708d Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Thu, 18 Apr 2024 16:43:07 -0600 Subject: [PATCH 1/2] feat: add getHeaderCommon --- src/request.zig | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/request.zig b/src/request.zig index 75a3be7f..91f800be 100644 --- a/src/request.zig +++ b/src/request.zig @@ -439,6 +439,46 @@ pub fn getHeader(self: *const Self, name: []const u8) ?[]const u8 { return util.fio2str(fio.fiobj_hash_get(self.h.*.headers, hname)); } +pub const HttpHeaderCommon = enum(usize) { + /// Represents the HTTP Header "Accept". + accept = fio.HTTP_HEADER_ACCEPT, + /// Represents the HTTP Header "Cache-Control". + cache_control = fio.HTTP_HEADER_CACHE_CONTROL, + /// Represents the HTTP Header "Connection". + connection = fio.HTTP_HEADER_CONNECTION, + /// Represents the HTTP Header "Content-Encoding". + content_encoding = fio.HTTP_HEADER_CONTENT_ENCODING, + /// Represents the HTTP Header "Content-Length". + content_length = fio.HTTP_HEADER_CONTENT_LENGTH, + /// Represents the HTTP Header "Content-Range". + content_range = fio.HTTP_HEADER_CONTENT_RANGE, + /// Represents the HTTP Header "Content-Type". + content_type = fio.HTTP_HEADER_CONTENT_TYPE, + /// Represents the HTTP Header "Cookie". + cookie = fio.HTTP_HEADER_COOKIE, + /// Represents the HTTP Header "Date". + date = fio.HTTP_HEADER_DATE, + /// Represents the HTTP Header "Etag". + etag = fio.HTTP_HEADER_ETAG, + /// Represents the HTTP Header "Host". + host = fio.HTTP_HEADER_HOST, + /// Represents the HTTP Header "Last-Modified". + last_modifed = fio.HTTP_HEADER_LAST_MODIFIED, + /// Represents the HTTP Header "Origin". + origin = fio.HTTP_HEADER_ORIGIN, + /// Represents the HTTP Header "Set-Cookie". + set_cookie = fio.HTTP_HEADER_SET_COOKIE, + /// Represents the HTTP Header "Upgrade". + upgrade = fio.HTTP_HEADER_UPGRADE, +}; + +/// Returns the header value of a given common header key. Returned memory +/// should not be freed. +pub fn getHeaderCommon(self: *const Self, which: HttpHeaderCommon) ?[]const u8 { + const fiobj = zap.fio.fiobj_hash_get(self.h.*.headers, @intFromEnum(which)); + return zap.fio2str(fiobj); +} + /// Set header. pub fn setHeader(self: *const Self, name: []const u8, value: []const u8) HttpError!void { const hname: fio.fio_str_info_s = .{ From 38c12a0a6b4563d88a9d5e8282d44736fd97d133 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Thu, 18 Apr 2024 18:08:39 -0600 Subject: [PATCH 2/2] fix: use runtime values of HTTP_HEADER_* for getHeaderCommon --- src/request.zig | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/request.zig b/src/request.zig index 91f800be..244d0010 100644 --- a/src/request.zig +++ b/src/request.zig @@ -441,41 +441,58 @@ pub fn getHeader(self: *const Self, name: []const u8) ?[]const u8 { pub const HttpHeaderCommon = enum(usize) { /// Represents the HTTP Header "Accept". - accept = fio.HTTP_HEADER_ACCEPT, + accept, /// Represents the HTTP Header "Cache-Control". - cache_control = fio.HTTP_HEADER_CACHE_CONTROL, + cache_control, /// Represents the HTTP Header "Connection". - connection = fio.HTTP_HEADER_CONNECTION, + connection, /// Represents the HTTP Header "Content-Encoding". - content_encoding = fio.HTTP_HEADER_CONTENT_ENCODING, + content_encoding, /// Represents the HTTP Header "Content-Length". - content_length = fio.HTTP_HEADER_CONTENT_LENGTH, + content_length, /// Represents the HTTP Header "Content-Range". - content_range = fio.HTTP_HEADER_CONTENT_RANGE, + content_range, /// Represents the HTTP Header "Content-Type". - content_type = fio.HTTP_HEADER_CONTENT_TYPE, + content_type, /// Represents the HTTP Header "Cookie". - cookie = fio.HTTP_HEADER_COOKIE, + cookie, /// Represents the HTTP Header "Date". - date = fio.HTTP_HEADER_DATE, + date, /// Represents the HTTP Header "Etag". - etag = fio.HTTP_HEADER_ETAG, + etag, /// Represents the HTTP Header "Host". - host = fio.HTTP_HEADER_HOST, + host, /// Represents the HTTP Header "Last-Modified". - last_modifed = fio.HTTP_HEADER_LAST_MODIFIED, + last_modifed, /// Represents the HTTP Header "Origin". - origin = fio.HTTP_HEADER_ORIGIN, + origin, /// Represents the HTTP Header "Set-Cookie". - set_cookie = fio.HTTP_HEADER_SET_COOKIE, + set_cookie, /// Represents the HTTP Header "Upgrade". - upgrade = fio.HTTP_HEADER_UPGRADE, + upgrade, }; /// Returns the header value of a given common header key. Returned memory /// should not be freed. pub fn getHeaderCommon(self: *const Self, which: HttpHeaderCommon) ?[]const u8 { - const fiobj = zap.fio.fiobj_hash_get(self.h.*.headers, @intFromEnum(which)); + const field = switch (which) { + .accept => fio.HTTP_HEADER_ACCEPT, + .cache_control => fio.HTTP_HEADER_CACHE_CONTROL, + .connection => fio.HTTP_HEADER_CONNECTION, + .content_encoding => fio.HTTP_HEADER_CONTENT_ENCODING, + .content_length => fio.HTTP_HEADER_CONTENT_LENGTH, + .content_range => fio.HTTP_HEADER_CONTENT_RANGE, + .content_type => fio.HTTP_HEADER_CONTENT_TYPE, + .cookie => fio.HTTP_HEADER_COOKIE, + .date => fio.HTTP_HEADER_DATE, + .etag => fio.HTTP_HEADER_ETAG, + .host => fio.HTTP_HEADER_HOST, + .last_modified => fio.HTTP_HEADER_LAST_MODIFIED, + .origin => fio.HTTP_HEADER_ORIGIN, + .set_cookie => fio.HTTP_HEADER_SET_COOKIE, + .upgrade => fio.HTTP_HEADER_UPGRADE, + }; + const fiobj = zap.fio.fiobj_hash_get(self.h.*.headers, field); return zap.fio2str(fiobj); }