1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00

http method options support

This commit is contained in:
stringnick 2023-12-27 23:25:57 +01:00 committed by Rene Schallner
parent a8a8b537fe
commit 1e849d2b05
2 changed files with 33 additions and 0 deletions

View file

@ -25,6 +25,7 @@ pub fn init(
.put = putUser, .put = putUser,
.patch = putUser, .patch = putUser,
.delete = deleteUser, .delete = deleteUser,
.options = optionsUser,
}), }),
}; };
} }
@ -141,3 +142,11 @@ fn deleteUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
} }
} }
} }
fn optionsUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
_ = e;
r.setHeader("Access-Control-Allow-Origin", "*") catch return;
r.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") catch return;
r.setStatus(zap.StatusCode.no_content);
r.markAsFinished(true);
}

View file

@ -14,6 +14,7 @@ pub const SimpleEndpointSettings = struct {
put: ?RequestFn = null, put: ?RequestFn = null,
delete: ?RequestFn = null, delete: ?RequestFn = null,
patch: ?RequestFn = null, patch: ?RequestFn = null,
options: ?RequestFn = null,
/// only applicable to AuthenticatingEndpoint /// only applicable to AuthenticatingEndpoint
unauthorized: ?RequestFn = null, unauthorized: ?RequestFn = null,
}; };
@ -32,6 +33,7 @@ pub const SimpleEndpoint = struct {
.put = s.put orelse &nop, .put = s.put orelse &nop,
.delete = s.delete orelse &nop, .delete = s.delete orelse &nop,
.patch = s.patch orelse &nop, .patch = s.patch orelse &nop,
.options = s.options orelse &nop,
.unauthorized = s.unauthorized orelse &nop, .unauthorized = s.unauthorized orelse &nop,
}, },
}; };
@ -54,6 +56,8 @@ pub const SimpleEndpoint = struct {
return self.settings.delete.?(self, r); return self.settings.delete.?(self, r);
if (std.mem.eql(u8, m, "PATCH")) if (std.mem.eql(u8, m, "PATCH"))
return self.settings.patch.?(self, r); return self.settings.patch.?(self, r);
if (std.mem.eql(u8, m, "OPTIONS"))
return self.settings.options.?(self, r);
} }
} }
}; };
@ -79,6 +83,7 @@ pub fn AuthenticatingEndpoint(comptime Authenticator: type) type {
.put = if (e.settings.put != null) put else null, .put = if (e.settings.put != null) put else null,
.delete = if (e.settings.delete != null) delete else null, .delete = if (e.settings.delete != null) delete else null,
.patch = if (e.settings.patch != null) patch else null, .patch = if (e.settings.patch != null) patch else null,
.options = if (e.settings.options != null) options else null,
.unauthorized = e.settings.unauthorized, .unauthorized = e.settings.unauthorized,
}), }),
}; };
@ -185,6 +190,25 @@ pub fn AuthenticatingEndpoint(comptime Authenticator: type) type {
.Handled => {}, .Handled => {},
} }
} }
/// here, the auth_endpoint will be passed in
pub fn options(e: *SimpleEndpoint, r: zap.SimpleRequest) void {
const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e);
switch (authEp.authenticator.authenticateRequest(&r)) {
.AuthFailed => {
if (e.settings.unauthorized) |unauthorized| {
unauthorized(authEp.endpoint, r);
return;
} else {
r.setStatus(.unauthorized);
r.sendBody("UNAUTHORIZED") catch return;
return;
}
},
.AuthOK => authEp.endpoint.settings.put.?(authEp.endpoint, r),
.Handled => {},
}
}
}; };
} }