mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
Use std.http.Method for Request.method
This commit is contained in:
parent
8a9d7369df
commit
39ee26f9a4
5 changed files with 43 additions and 21 deletions
|
@ -2,7 +2,7 @@ const std = @import("std");
|
||||||
const zap = @import("zap");
|
const zap = @import("zap");
|
||||||
|
|
||||||
fn on_request(r: zap.Request) void {
|
fn on_request(r: zap.Request) void {
|
||||||
const m = r.method orelse "";
|
const m = r.method_str orelse "";
|
||||||
const p = r.path orelse "/";
|
const p = r.path orelse "/";
|
||||||
const qm = if (r.query) |_| "?" else "";
|
const qm = if (r.query) |_| "?" else "";
|
||||||
const qq = r.query orelse "";
|
const qq = r.query orelse "";
|
||||||
|
|
|
@ -7,8 +7,7 @@ const User = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
fn on_request(r: zap.Request) void {
|
fn on_request(r: zap.Request) void {
|
||||||
if (!std.mem.eql(u8, r.method.?, "GET"))
|
if (r.method == null or r.method.? == .GET) return;
|
||||||
return;
|
|
||||||
|
|
||||||
// /user/n
|
// /user/n
|
||||||
if (r.path) |the_path| {
|
if (r.path) |the_path| {
|
||||||
|
|
|
@ -60,20 +60,18 @@ 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 {
|
||||||
if (r.method) |m| {
|
const Method = std.http.Method;
|
||||||
if (std.mem.eql(u8, m, "GET"))
|
return if (r.method) |m| {
|
||||||
return self.settings.get.?(self, r);
|
switch (m) {
|
||||||
if (std.mem.eql(u8, m, "POST"))
|
Method.GET => self.settings.get.?(self, r),
|
||||||
return self.settings.post.?(self, r);
|
Method.POST => self.settings.post.?(self, r),
|
||||||
if (std.mem.eql(u8, m, "PUT"))
|
Method.PUT => self.settings.put.?(self, r),
|
||||||
return self.settings.put.?(self, r);
|
Method.DELETE => self.settings.delete.?(self, r),
|
||||||
if (std.mem.eql(u8, m, "DELETE"))
|
Method.PATCH => self.settings.patch.?(self, r),
|
||||||
return self.settings.delete.?(self, r);
|
Method.OPTIONS => self.settings.options.?(self, r),
|
||||||
if (std.mem.eql(u8, m, "PATCH"))
|
else => return,
|
||||||
return self.settings.patch.?(self, r);
|
}
|
||||||
if (std.mem.eql(u8, m, "OPTIONS"))
|
};
|
||||||
return self.settings.options.?(self, r);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrap an endpoint with an Authenticator -> new Endpoint of type Endpoint
|
/// Wrap an endpoint with an Authenticator -> new Endpoint of type Endpoint
|
||||||
|
|
|
@ -266,7 +266,8 @@ pub const CookieArgs = struct {
|
||||||
path: ?[]const u8,
|
path: ?[]const u8,
|
||||||
query: ?[]const u8,
|
query: ?[]const u8,
|
||||||
body: ?[]const u8,
|
body: ?[]const u8,
|
||||||
method: ?[]const u8,
|
method: ?std.http.Method,
|
||||||
|
method_str: ?[]const u8,
|
||||||
h: [*c]fio.http_s,
|
h: [*c]fio.http_s,
|
||||||
|
|
||||||
/// NEVER touch this field!!!!
|
/// NEVER touch this field!!!!
|
||||||
|
|
30
src/zap.zig
30
src/zap.zig
|
@ -184,6 +184,27 @@ 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,
|
||||||
|
@ -207,7 +228,8 @@ 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 = util.fio2str(r.*.method),
|
.method = methodToEnum(util.fio2str(r.*.method)),
|
||||||
|
.method_str = util.fio2str(r.*.method),
|
||||||
.h = r,
|
.h = r,
|
||||||
._is_finished_request_global = false,
|
._is_finished_request_global = false,
|
||||||
._user_context = undefined,
|
._user_context = undefined,
|
||||||
|
@ -233,7 +255,8 @@ 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 = util.fio2str(r.*.method),
|
.method = methodToEnum(util.fio2str(r.*.method)),
|
||||||
|
.method_str = util.fio2str(r.*.method),
|
||||||
.h = r,
|
.h = r,
|
||||||
._is_finished_request_global = false,
|
._is_finished_request_global = false,
|
||||||
._user_context = undefined,
|
._user_context = undefined,
|
||||||
|
@ -254,7 +277,8 @@ 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 = util.fio2str(r.*.method),
|
.method = methodToEnum(util.fio2str(r.*.method)),
|
||||||
|
.method_str = util.fio2str(r.*.method),
|
||||||
.h = r,
|
.h = r,
|
||||||
._is_finished_request_global = false,
|
._is_finished_request_global = false,
|
||||||
._user_context = undefined,
|
._user_context = undefined,
|
||||||
|
|
Loading…
Add table
Reference in a new issue