Skip to content

Commit ef523d7

Browse files
committed
zap.App.Endpoint.Authenticating: don't require unauthorized handler, return 405 method not allowed for unimplemented HTTP methods
1 parent 29d3398 commit ef523d7

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/App.zig

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ pub fn Create(
256256
/// Authenticates GET requests using the Authenticator.
257257
pub fn get(self: *AuthenticatingEndpoint, arena: Allocator, context: *Context, request: Request) anyerror!void {
258258
try switch (self.authenticator.authenticateRequest(&request)) {
259-
.AuthFailed => return self.ep.*.unauthorized(arena, context, request),
259+
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, arena, context, request),
260260
.AuthOK => callHandlerIfExist("get", self.ep, arena, context, request),
261261
.Handled => {},
262262
};
@@ -265,7 +265,7 @@ pub fn Create(
265265
/// Authenticates POST requests using the Authenticator.
266266
pub fn post(self: *AuthenticatingEndpoint, arena: Allocator, context: *Context, request: Request) anyerror!void {
267267
try switch (self.authenticator.authenticateRequest(&request)) {
268-
.AuthFailed => return self.ep.*.unauthorized(arena, context, request),
268+
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, arena, context, request),
269269
.AuthOK => callHandlerIfExist("post", self.ep, arena, context, request),
270270
.Handled => {},
271271
};
@@ -274,7 +274,7 @@ pub fn Create(
274274
/// Authenticates PUT requests using the Authenticator.
275275
pub fn put(self: *AuthenticatingEndpoint, arena: Allocator, context: *Context, request: zap.Request) anyerror!void {
276276
try switch (self.authenticator.authenticateRequest(&request)) {
277-
.AuthFailed => return self.ep.*.unauthorized(arena, context, request),
277+
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, arena, context, request),
278278
.AuthOK => callHandlerIfExist("put", self.ep, arena, context, request),
279279
.Handled => {},
280280
};
@@ -283,7 +283,7 @@ pub fn Create(
283283
/// Authenticates DELETE requests using the Authenticator.
284284
pub fn delete(self: *AuthenticatingEndpoint, arena: Allocator, context: *Context, request: zap.Request) anyerror!void {
285285
try switch (self.authenticator.authenticateRequest(&request)) {
286-
.AuthFailed => return self.ep.*.unauthorized(arena, context, request),
286+
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, arena, context, request),
287287
.AuthOK => callHandlerIfExist("delete", self.ep, arena, context, request),
288288
.Handled => {},
289289
};
@@ -292,7 +292,7 @@ pub fn Create(
292292
/// Authenticates PATCH requests using the Authenticator.
293293
pub fn patch(self: *AuthenticatingEndpoint, arena: Allocator, context: *Context, request: zap.Request) anyerror!void {
294294
try switch (self.authenticator.authenticateRequest(&request)) {
295-
.AuthFailed => return self.ep.*.unauthorized(arena, context, request),
295+
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, arena, context, request),
296296
.AuthOK => callHandlerIfExist("patch", self.ep, arena, context, request),
297297
.Handled => {},
298298
};
@@ -301,7 +301,7 @@ pub fn Create(
301301
/// Authenticates OPTIONS requests using the Authenticator.
302302
pub fn options(self: *AuthenticatingEndpoint, arena: Allocator, context: *Context, request: zap.Request) anyerror!void {
303303
try switch (self.authenticator.authenticateRequest(&request)) {
304-
.AuthFailed => return self.ep.*.unauthorized(arena, context, request),
304+
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, arena, context, request),
305305
.AuthOK => callHandlerIfExist("options", self.ep, arena, context, request),
306306
.Handled => {},
307307
};
@@ -310,7 +310,7 @@ pub fn Create(
310310
/// Authenticates HEAD requests using the Authenticator.
311311
pub fn head(self: *AuthenticatingEndpoint, arena: Allocator, context: *Context, request: zap.Request) anyerror!void {
312312
try switch (self.authenticator.authenticateRequest(&request)) {
313-
.AuthFailed => return self.ep.*.unauthorized(arena, context, request),
313+
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, arena, context, request),
314314
.AuthOK => callHandlerIfExist("head", self.ep, arena, context, request),
315315
.Handled => {},
316316
};
@@ -393,6 +393,12 @@ pub fn Create(
393393
if (@hasDecl(EndPoint, fn_name)) {
394394
return @field(EndPoint, fn_name)(e, arena, ctx, r);
395395
}
396+
zap.log.debug(
397+
"Unhandled `{s}` {s} request ({s} not implemented in {s})",
398+
.{ r.method orelse "<unknown>", r.path orelse "", fn_name, @typeName(Endpoint) },
399+
);
400+
r.setStatus(.method_not_allowed);
401+
try r.sendBody("405 - method not allowed\r\n");
396402
return;
397403
}
398404

0 commit comments

Comments
 (0)