mirror of
https://github.com/zigzap/zap.git
synced 2025-10-21 15:44:10 +00:00
Use custom method enum
This commit is contained in:
parent
92fdb6605c
commit
37f7b8b4aa
5 changed files with 45 additions and 38 deletions
|
@ -7,7 +7,7 @@ const User = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
fn on_request(r: zap.Request) void {
|
fn on_request(r: zap.Request) void {
|
||||||
if (r.method == null or r.method.? == .GET) return;
|
if (r.method.? != .GET) return;
|
||||||
|
|
||||||
// /user/n
|
// /user/n
|
||||||
if (r.path) |the_path| {
|
if (r.path) |the_path| {
|
||||||
|
|
|
@ -60,18 +60,15 @@ fn nop(self: *Endpoint, r: Request) void {
|
||||||
|
|
||||||
/// The global request handler for this Endpoint, called by the listener.
|
/// The global request handler for this Endpoint, called by the listener.
|
||||||
pub fn onRequest(self: *Endpoint, r: zap.Request) void {
|
pub fn onRequest(self: *Endpoint, r: zap.Request) void {
|
||||||
const Method = std.http.Method;
|
switch (r.method) {
|
||||||
return if (r.method) |m| {
|
.GET => self.settings.get.?(self, r),
|
||||||
switch (m) {
|
.POST => self.settings.post.?(self, r),
|
||||||
Method.GET => self.settings.get.?(self, r),
|
.PUT => self.settings.put.?(self, r),
|
||||||
Method.POST => self.settings.post.?(self, r),
|
.DELETE => self.settings.delete.?(self, r),
|
||||||
Method.PUT => self.settings.put.?(self, r),
|
.PATCH => self.settings.patch.?(self, r),
|
||||||
Method.DELETE => self.settings.delete.?(self, r),
|
.OPTIONS => self.settings.options.?(self, r),
|
||||||
Method.PATCH => self.settings.patch.?(self, r),
|
else => return,
|
||||||
Method.OPTIONS => self.settings.options.?(self, r),
|
}
|
||||||
else => return,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrap an endpoint with an Authenticator -> new Endpoint of type Endpoint
|
/// Wrap an endpoint with an Authenticator -> new Endpoint of type Endpoint
|
||||||
|
|
31
src/http.zig
31
src/http.zig
|
@ -1,3 +1,5 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
/// HTTP Status codes according to `rfc7231`
|
/// HTTP Status codes according to `rfc7231`
|
||||||
/// https://tools.ietf.org/html/rfc7231#section-6
|
/// https://tools.ietf.org/html/rfc7231#section-6
|
||||||
/// (taken from https://github.com/Luukdegram/apple_pie/blob/master/src/response.zig)
|
/// (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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -266,7 +266,7 @@ pub const CookieArgs = struct {
|
||||||
path: ?[]const u8,
|
path: ?[]const u8,
|
||||||
query: ?[]const u8,
|
query: ?[]const u8,
|
||||||
body: ?[]const u8,
|
body: ?[]const u8,
|
||||||
method: ?std.http.Method,
|
method: http.Method,
|
||||||
method_str: ?[]const u8,
|
method_str: ?[]const u8,
|
||||||
h: [*c]fio.http_s,
|
h: [*c]fio.http_s,
|
||||||
|
|
||||||
|
|
27
src/zap.zig
27
src/zap.zig
|
@ -184,27 +184,6 @@ pub const HttpListenerSettings = struct {
|
||||||
tls: ?Tls = null,
|
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
|
/// Http listener
|
||||||
pub const HttpListener = struct {
|
pub const HttpListener = struct {
|
||||||
settings: HttpListenerSettings,
|
settings: HttpListenerSettings,
|
||||||
|
@ -228,7 +207,7 @@ pub const HttpListener = struct {
|
||||||
.path = util.fio2str(r.*.path),
|
.path = util.fio2str(r.*.path),
|
||||||
.query = util.fio2str(r.*.query),
|
.query = util.fio2str(r.*.query),
|
||||||
.body = util.fio2str(r.*.body),
|
.body = util.fio2str(r.*.body),
|
||||||
.method = methodToEnum(util.fio2str(r.*.method)),
|
.method = http.methodToEnum(util.fio2str(r.*.method)),
|
||||||
.method_str = util.fio2str(r.*.method),
|
.method_str = util.fio2str(r.*.method),
|
||||||
.h = r,
|
.h = r,
|
||||||
._is_finished_request_global = false,
|
._is_finished_request_global = false,
|
||||||
|
@ -255,7 +234,7 @@ pub const HttpListener = struct {
|
||||||
.path = util.fio2str(r.*.path),
|
.path = util.fio2str(r.*.path),
|
||||||
.query = util.fio2str(r.*.query),
|
.query = util.fio2str(r.*.query),
|
||||||
.body = util.fio2str(r.*.body),
|
.body = util.fio2str(r.*.body),
|
||||||
.method = methodToEnum(util.fio2str(r.*.method)),
|
.method = http.methodToEnum(util.fio2str(r.*.method)),
|
||||||
.method_str = util.fio2str(r.*.method),
|
.method_str = util.fio2str(r.*.method),
|
||||||
.h = r,
|
.h = r,
|
||||||
._is_finished_request_global = false,
|
._is_finished_request_global = false,
|
||||||
|
@ -277,7 +256,7 @@ pub const HttpListener = struct {
|
||||||
.path = util.fio2str(r.*.path),
|
.path = util.fio2str(r.*.path),
|
||||||
.query = util.fio2str(r.*.query),
|
.query = util.fio2str(r.*.query),
|
||||||
.body = util.fio2str(r.*.body),
|
.body = util.fio2str(r.*.body),
|
||||||
.method = methodToEnum(util.fio2str(r.*.method)),
|
.method = http.methodToEnum(util.fio2str(r.*.method)),
|
||||||
.method_str = util.fio2str(r.*.method),
|
.method_str = util.fio2str(r.*.method),
|
||||||
.h = r,
|
.h = r,
|
||||||
._is_finished_request_global = false,
|
._is_finished_request_global = false,
|
||||||
|
|
Loading…
Add table
Reference in a new issue