mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
Merge pull request #72 from Chiissu/master
Use std.http.Method for Request.method
This commit is contained in:
commit
4254cc067e
6 changed files with 49 additions and 20 deletions
|
@ -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 "";
|
||||
|
|
|
@ -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.? != .GET) return;
|
||||
|
||||
// /user/n
|
||||
if (r.path) |the_path| {
|
||||
|
|
|
@ -60,19 +60,14 @@ 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);
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
31
src/http.zig
31
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -266,7 +266,8 @@ pub const CookieArgs = struct {
|
|||
path: ?[]const u8,
|
||||
query: ?[]const u8,
|
||||
body: ?[]const u8,
|
||||
method: ?[]const u8,
|
||||
method: http.Method,
|
||||
method_str: ?[]const u8,
|
||||
h: [*c]fio.http_s,
|
||||
|
||||
/// NEVER touch this field!!!!
|
||||
|
|
|
@ -207,7 +207,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 = http.methodToEnum(util.fio2str(r.*.method)),
|
||||
.method_str = util.fio2str(r.*.method),
|
||||
.h = r,
|
||||
._is_finished_request_global = false,
|
||||
._user_context = undefined,
|
||||
|
@ -233,7 +234,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 = http.methodToEnum(util.fio2str(r.*.method)),
|
||||
.method_str = util.fio2str(r.*.method),
|
||||
.h = r,
|
||||
._is_finished_request_global = false,
|
||||
._user_context = undefined,
|
||||
|
@ -254,7 +256,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 = http.methodToEnum(util.fio2str(r.*.method)),
|
||||
.method_str = util.fio2str(r.*.method),
|
||||
.h = r,
|
||||
._is_finished_request_global = false,
|
||||
._user_context = undefined,
|
||||
|
|
Loading…
Add table
Reference in a new issue