From 92fdb6605c0856777f3cc651cb9f84c1b1e0a995 Mon Sep 17 00:00:00 2001 From: Froxcey Date: Sat, 27 Jan 2024 13:10:21 +0800 Subject: [PATCH 1/2] Use std.http.Method for Request.method --- examples/hello2/hello2.zig | 2 +- examples/hello_json/hello_json.zig | 3 +-- src/endpoint.zig | 26 ++++++++++++-------------- src/request.zig | 3 ++- src/zap.zig | 30 +++++++++++++++++++++++++++--- 5 files changed, 43 insertions(+), 21 deletions(-) diff --git a/examples/hello2/hello2.zig b/examples/hello2/hello2.zig index 5e7fa9a..52c70ec 100644 --- a/examples/hello2/hello2.zig +++ b/examples/hello2/hello2.zig @@ -2,7 +2,7 @@ const std = @import("std"); const zap = @import("zap"); fn on_request(r: zap.Request) void { - const m = r.method orelse ""; + const m = r.method_str orelse ""; const p = r.path orelse "/"; const qm = if (r.query) |_| "?" else ""; const qq = r.query orelse ""; diff --git a/examples/hello_json/hello_json.zig b/examples/hello_json/hello_json.zig index d5b2131..7e1be25 100644 --- a/examples/hello_json/hello_json.zig +++ b/examples/hello_json/hello_json.zig @@ -7,8 +7,7 @@ const User = struct { }; fn on_request(r: zap.Request) void { - if (!std.mem.eql(u8, r.method.?, "GET")) - return; + if (r.method == null or r.method.? == .GET) return; // /user/n if (r.path) |the_path| { diff --git a/src/endpoint.zig b/src/endpoint.zig index b8e02b3..5388aef 100644 --- a/src/endpoint.zig +++ b/src/endpoint.zig @@ -60,20 +60,18 @@ fn nop(self: *Endpoint, r: Request) void { /// The global request handler for this Endpoint, called by the listener. pub fn onRequest(self: *Endpoint, r: zap.Request) void { - if (r.method) |m| { - if (std.mem.eql(u8, m, "GET")) - return self.settings.get.?(self, r); - if (std.mem.eql(u8, m, "POST")) - return self.settings.post.?(self, r); - if (std.mem.eql(u8, m, "PUT")) - return self.settings.put.?(self, r); - if (std.mem.eql(u8, m, "DELETE")) - return self.settings.delete.?(self, r); - if (std.mem.eql(u8, m, "PATCH")) - return self.settings.patch.?(self, r); - if (std.mem.eql(u8, m, "OPTIONS")) - return self.settings.options.?(self, r); - } + const Method = std.http.Method; + return if (r.method) |m| { + switch (m) { + Method.GET => self.settings.get.?(self, r), + Method.POST => self.settings.post.?(self, r), + Method.PUT => self.settings.put.?(self, r), + Method.DELETE => self.settings.delete.?(self, r), + Method.PATCH => self.settings.patch.?(self, r), + Method.OPTIONS => self.settings.options.?(self, r), + else => return, + } + }; } /// Wrap an endpoint with an Authenticator -> new Endpoint of type Endpoint diff --git a/src/request.zig b/src/request.zig index c6b4fb7..48096fb 100644 --- a/src/request.zig +++ b/src/request.zig @@ -266,7 +266,8 @@ pub const CookieArgs = struct { path: ?[]const u8, query: ?[]const u8, body: ?[]const u8, -method: ?[]const u8, +method: ?std.http.Method, +method_str: ?[]const u8, h: [*c]fio.http_s, /// NEVER touch this field!!!! diff --git a/src/zap.zig b/src/zap.zig index 0414d68..bfd85ce 100644 --- a/src/zap.zig +++ b/src/zap.zig @@ -184,6 +184,27 @@ pub const HttpListenerSettings = struct { tls: ?Tls = null, }; +fn methodToEnum(method: ?[]const u8) ?std.http.Method { + const Method = std.http.Method; + { + if (method) |m| { + if (std.mem.eql(u8, m, "GET")) + return Method.GET; + if (std.mem.eql(u8, m, "POST")) + return Method.POST; + if (std.mem.eql(u8, m, "PUT")) + return Method.PUT; + if (std.mem.eql(u8, m, "DELETE")) + return Method.DELETE; + if (std.mem.eql(u8, m, "PATCH")) + return Method.PATCH; + if (std.mem.eql(u8, m, "OPTIONS")) + return Method.OPTIONS; + } + return null; + } +} + /// Http listener pub const HttpListener = struct { settings: HttpListenerSettings, @@ -207,7 +228,8 @@ pub const HttpListener = struct { .path = util.fio2str(r.*.path), .query = util.fio2str(r.*.query), .body = util.fio2str(r.*.body), - .method = util.fio2str(r.*.method), + .method = methodToEnum(util.fio2str(r.*.method)), + .method_str = util.fio2str(r.*.method), .h = r, ._is_finished_request_global = false, ._user_context = undefined, @@ -233,7 +255,8 @@ pub const HttpListener = struct { .path = util.fio2str(r.*.path), .query = util.fio2str(r.*.query), .body = util.fio2str(r.*.body), - .method = util.fio2str(r.*.method), + .method = methodToEnum(util.fio2str(r.*.method)), + .method_str = util.fio2str(r.*.method), .h = r, ._is_finished_request_global = false, ._user_context = undefined, @@ -254,7 +277,8 @@ pub const HttpListener = struct { .path = util.fio2str(r.*.path), .query = util.fio2str(r.*.query), .body = util.fio2str(r.*.body), - .method = util.fio2str(r.*.method), + .method = methodToEnum(util.fio2str(r.*.method)), + .method_str = util.fio2str(r.*.method), .h = r, ._is_finished_request_global = false, ._user_context = undefined, From 37f7b8b4aa596f88c5f1d26d823860cc10c8e9c7 Mon Sep 17 00:00:00 2001 From: Froxcey Date: Wed, 31 Jan 2024 21:15:27 +0800 Subject: [PATCH 2/2] Use custom method enum --- examples/hello_json/hello_json.zig | 2 +- src/endpoint.zig | 21 +++++++++----------- src/http.zig | 31 ++++++++++++++++++++++++++++++ src/request.zig | 2 +- src/zap.zig | 27 +++----------------------- 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/examples/hello_json/hello_json.zig b/examples/hello_json/hello_json.zig index 7e1be25..42640ce 100644 --- a/examples/hello_json/hello_json.zig +++ b/examples/hello_json/hello_json.zig @@ -7,7 +7,7 @@ const User = struct { }; fn on_request(r: zap.Request) void { - if (r.method == null or r.method.? == .GET) return; + if (r.method.? != .GET) return; // /user/n if (r.path) |the_path| { diff --git a/src/endpoint.zig b/src/endpoint.zig index 5388aef..3903809 100644 --- a/src/endpoint.zig +++ b/src/endpoint.zig @@ -60,18 +60,15 @@ fn nop(self: *Endpoint, r: Request) void { /// The global request handler for this Endpoint, called by the listener. pub fn onRequest(self: *Endpoint, r: zap.Request) void { - const Method = std.http.Method; - return if (r.method) |m| { - switch (m) { - Method.GET => self.settings.get.?(self, r), - Method.POST => self.settings.post.?(self, r), - Method.PUT => self.settings.put.?(self, r), - Method.DELETE => self.settings.delete.?(self, r), - Method.PATCH => self.settings.patch.?(self, r), - Method.OPTIONS => self.settings.options.?(self, r), - else => return, - } - }; + switch (r.method) { + .GET => self.settings.get.?(self, r), + .POST => self.settings.post.?(self, r), + .PUT => self.settings.put.?(self, r), + .DELETE => self.settings.delete.?(self, r), + .PATCH => self.settings.patch.?(self, r), + .OPTIONS => self.settings.options.?(self, r), + else => return, + } } /// Wrap an endpoint with an Authenticator -> new Endpoint of type Endpoint diff --git a/src/http.zig b/src/http.zig index 4978490..94e25d4 100644 --- a/src/http.zig +++ b/src/http.zig @@ -1,3 +1,5 @@ +const std = @import("std"); + /// HTTP Status codes according to `rfc7231` /// https://tools.ietf.org/html/rfc7231#section-6 /// (taken from https://github.com/Luukdegram/apple_pie/blob/master/src/response.zig) @@ -105,3 +107,32 @@ pub const StatusCode = enum(u16) { }; } }; + +pub const Method = enum { + GET, + POST, + PUT, + DELETE, + PATCH, + OPTIONS, + UNKNOWN, +}; +pub fn methodToEnum(method: ?[]const u8) Method { + { + if (method) |m| { + if (std.mem.eql(u8, m, "GET")) + return Method.GET; + if (std.mem.eql(u8, m, "POST")) + return Method.POST; + if (std.mem.eql(u8, m, "PUT")) + return Method.PUT; + if (std.mem.eql(u8, m, "DELETE")) + return Method.DELETE; + if (std.mem.eql(u8, m, "PATCH")) + return Method.PATCH; + if (std.mem.eql(u8, m, "OPTIONS")) + return Method.OPTIONS; + } + return Method.UNKNOWN; + } +} diff --git a/src/request.zig b/src/request.zig index 48096fb..cd98ddd 100644 --- a/src/request.zig +++ b/src/request.zig @@ -266,7 +266,7 @@ pub const CookieArgs = struct { path: ?[]const u8, query: ?[]const u8, body: ?[]const u8, -method: ?std.http.Method, +method: http.Method, method_str: ?[]const u8, h: [*c]fio.http_s, diff --git a/src/zap.zig b/src/zap.zig index bfd85ce..8ab0de7 100644 --- a/src/zap.zig +++ b/src/zap.zig @@ -184,27 +184,6 @@ pub const HttpListenerSettings = struct { tls: ?Tls = null, }; -fn methodToEnum(method: ?[]const u8) ?std.http.Method { - const Method = std.http.Method; - { - if (method) |m| { - if (std.mem.eql(u8, m, "GET")) - return Method.GET; - if (std.mem.eql(u8, m, "POST")) - return Method.POST; - if (std.mem.eql(u8, m, "PUT")) - return Method.PUT; - if (std.mem.eql(u8, m, "DELETE")) - return Method.DELETE; - if (std.mem.eql(u8, m, "PATCH")) - return Method.PATCH; - if (std.mem.eql(u8, m, "OPTIONS")) - return Method.OPTIONS; - } - return null; - } -} - /// Http listener pub const HttpListener = struct { settings: HttpListenerSettings, @@ -228,7 +207,7 @@ pub const HttpListener = struct { .path = util.fio2str(r.*.path), .query = util.fio2str(r.*.query), .body = util.fio2str(r.*.body), - .method = methodToEnum(util.fio2str(r.*.method)), + .method = http.methodToEnum(util.fio2str(r.*.method)), .method_str = util.fio2str(r.*.method), .h = r, ._is_finished_request_global = false, @@ -255,7 +234,7 @@ pub const HttpListener = struct { .path = util.fio2str(r.*.path), .query = util.fio2str(r.*.query), .body = util.fio2str(r.*.body), - .method = methodToEnum(util.fio2str(r.*.method)), + .method = http.methodToEnum(util.fio2str(r.*.method)), .method_str = util.fio2str(r.*.method), .h = r, ._is_finished_request_global = false, @@ -277,7 +256,7 @@ pub const HttpListener = struct { .path = util.fio2str(r.*.path), .query = util.fio2str(r.*.query), .body = util.fio2str(r.*.body), - .method = methodToEnum(util.fio2str(r.*.method)), + .method = http.methodToEnum(util.fio2str(r.*.method)), .method_str = util.fio2str(r.*.method), .h = r, ._is_finished_request_global = false,