diff --git a/examples/app/auth.zig b/examples/app/auth.zig index ebb7103..c8569e0 100644 --- a/examples/app/auth.zig +++ b/examples/app/auth.zig @@ -65,6 +65,7 @@ const MyEndpoint = struct { pub fn delete(_: *MyEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} pub fn patch(_: *MyEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} pub fn options(_: *MyEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} + pub fn head(_: *MyEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} }; pub fn main() !void { diff --git a/examples/app/basic.zig b/examples/app/basic.zig index 44c8650..eef9e5d 100644 --- a/examples/app/basic.zig +++ b/examples/app/basic.zig @@ -66,6 +66,7 @@ const SimpleEndpoint = struct { pub fn delete(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} pub fn patch(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} pub fn options(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} + pub fn head(_: *SimpleEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} }; const StopEndpoint = struct { diff --git a/examples/app/errors.zig b/examples/app/errors.zig index 0bd3b83..e29a1c2 100644 --- a/examples/app/errors.zig +++ b/examples/app/errors.zig @@ -53,6 +53,7 @@ const ErrorEndpoint = struct { pub fn delete(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} pub fn patch(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} pub fn options(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} + pub fn head(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {} }; const StopEndpoint = struct { diff --git a/examples/endpoint/error.zig b/examples/endpoint/error.zig index 2dafa45..dfe0142 100644 --- a/examples/endpoint/error.zig +++ b/examples/endpoint/error.zig @@ -20,3 +20,4 @@ pub fn put(_: *ErrorEndpoint, _: zap.Request) !void {} pub fn delete(_: *ErrorEndpoint, _: zap.Request) !void {} pub fn patch(_: *ErrorEndpoint, _: zap.Request) !void {} pub fn options(_: *ErrorEndpoint, _: zap.Request) !void {} +pub fn head(_: *ErrorEndpoint, _: zap.Request) !void {} diff --git a/examples/endpoint/stopendpoint.zig b/examples/endpoint/stopendpoint.zig index 2b12bd6..aa83dde 100644 --- a/examples/endpoint/stopendpoint.zig +++ b/examples/endpoint/stopendpoint.zig @@ -23,3 +23,4 @@ pub fn put(_: *StopEndpoint, _: zap.Request) !void {} pub fn delete(_: *StopEndpoint, _: zap.Request) !void {} pub fn patch(_: *StopEndpoint, _: zap.Request) !void {} pub fn options(_: *StopEndpoint, _: zap.Request) !void {} +pub fn head(_: *StopEndpoint, _: zap.Request) !void {} diff --git a/examples/endpoint/userweb.zig b/examples/endpoint/userweb.zig index 2d13bf9..ec1ad78 100644 --- a/examples/endpoint/userweb.zig +++ b/examples/endpoint/userweb.zig @@ -127,7 +127,12 @@ pub fn delete(self: *UserWeb, r: zap.Request) !void { pub fn options(_: *UserWeb, r: zap.Request) !void { try r.setHeader("Access-Control-Allow-Origin", "*"); - try r.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS"); + try r.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD"); + r.setStatus(zap.http.StatusCode.no_content); + r.markAsFinished(true); +} + +pub fn head(_: *UserWeb, r: zap.Request) !void { r.setStatus(zap.http.StatusCode.no_content); r.markAsFinished(true); } diff --git a/examples/endpoint_auth/endpoint_auth.zig b/examples/endpoint_auth/endpoint_auth.zig index 0cf4879..fc15e7a 100644 --- a/examples/endpoint_auth/endpoint_auth.zig +++ b/examples/endpoint_auth/endpoint_auth.zig @@ -38,6 +38,7 @@ const Endpoint = struct { pub fn delete(_: *Endpoint, _: zap.Request) !void {} pub fn patch(_: *Endpoint, _: zap.Request) !void {} pub fn options(_: *Endpoint, _: zap.Request) !void {} + pub fn head(_: *Endpoint, _: zap.Request) !void {} }; pub fn main() !void { diff --git a/examples/middleware_with_endpoint/middleware_with_endpoint.zig b/examples/middleware_with_endpoint/middleware_with_endpoint.zig index 6fe9e34..275d7d6 100644 --- a/examples/middleware_with_endpoint/middleware_with_endpoint.zig +++ b/examples/middleware_with_endpoint/middleware_with_endpoint.zig @@ -157,6 +157,7 @@ const HtmlEndpoint = struct { pub fn delete(_: *HtmlEndpoint, _: zap.Request) !void {} pub fn patch(_: *HtmlEndpoint, _: zap.Request) !void {} pub fn options(_: *HtmlEndpoint, _: zap.Request) !void {} + pub fn head(_: *HtmlEndpoint, _: zap.Request) !void {} pub fn get(_: *HtmlEndpoint, r: zap.Request) !void { var buf: [1024]u8 = undefined; diff --git a/src/App.zig b/src/App.zig index 5068e42..c8e36dd 100644 --- a/src/App.zig +++ b/src/App.zig @@ -117,6 +117,7 @@ pub fn Create( .DELETE => self.endpoint.*.delete(arena, app_context, r), .PATCH => self.endpoint.*.patch(arena, app_context, r), .OPTIONS => self.endpoint.*.options(arena, app_context, r), + .HEAD => self.endpoint.*.head(arena, app_context, r), else => error.UnsupportedHtmlRequestMethod, }; if (ret) { @@ -173,6 +174,7 @@ pub fn Create( "delete", "patch", "options", + "head", }; const params_to_check = [_]type{ *T, @@ -305,6 +307,15 @@ pub fn Create( .Handled => {}, }; } + + /// Authenticates HEAD requests using the Authenticator. + pub fn head(self: *AuthenticatingEndpoint, arena: Allocator, context: *Context, request: zap.Request) anyerror!void { + try switch (self.authenticator.authenticateRequest(&request)) { + .AuthFailed => return self.ep.*.unauthorized(arena, context, request), + .AuthOK => self.ep.*.head(arena, context, request), + .Handled => {}, + }; + } }; } }; diff --git a/src/endpoint.zig b/src/endpoint.zig index da38a3e..5d95488 100644 --- a/src/endpoint.zig +++ b/src/endpoint.zig @@ -19,6 +19,7 @@ //! pub fn delete(_: *Self, _: zap.Request) !void {} //! pub fn patch(_: *Self, _: zap.Request) !void {} //! pub fn options(_: *Self, _: zap.Request) !void {} +//! pub fn head(_: *Self, _: zap.Request) !void {} //! //! // optional, if auth stuff is used: //! pub fn unauthorized(_: *Self, _: zap.Request) !void {} @@ -49,6 +50,7 @@ //! pub fn delete(_: *StopEndpoint, _: zap.Request) !void {} //! pub fn patch(_: *StopEndpoint, _: zap.Request) !void {} //! pub fn options(_: *StopEndpoint, _: zap.Request) !void {} +//! pub fn head(_: *StopEndpoint, _: zap.Request) !void {} //! }; //! ``` @@ -97,6 +99,7 @@ pub fn checkEndpointType(T: type) void { "delete", "patch", "options", + "head", }; const params_to_check = [_]type{ @@ -192,6 +195,7 @@ pub const Binder = struct { .DELETE => self.endpoint.*.delete(r), .PATCH => self.endpoint.*.patch(r), .OPTIONS => self.endpoint.*.options(r), + .HEAD => self.endpoint.*.head(r), else => error.UnsupportedHtmlRequestMethod, }; if (ret) { @@ -295,6 +299,15 @@ pub fn Authenticating(EndpointType: type, Authenticator: type) type { .Handled => {}, }; } + + /// Authenticates HEAD requests using the Authenticator. + pub fn head(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void { + try switch (self.authenticator.authenticateRequest(&r)) { + .AuthFailed => return self.ep.*.unauthorized(r), + .AuthOK => self.ep.*.head(r), + .Handled => {}, + }; + } }; } diff --git a/src/middleware.zig b/src/middleware.zig index b5b92e2..2dcfd43 100644 --- a/src/middleware.zig +++ b/src/middleware.zig @@ -109,6 +109,7 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime EndpointType: any .DELETE => try self.endpoint.*.delete(r), .PATCH => try self.endpoint.*.patch(r), .OPTIONS => try self.endpoint.*.options(r), + .HEAD => try self.endpoint.*.head(r), else => {}, } } diff --git a/src/tests/test_auth.zig b/src/tests/test_auth.zig index 6f84057..514b4c9 100644 --- a/src/tests/test_auth.zig +++ b/src/tests/test_auth.zig @@ -170,6 +170,7 @@ pub const Endpoint = struct { pub fn delete(_: *Endpoint, _: zap.Request) !void {} pub fn patch(_: *Endpoint, _: zap.Request) !void {} pub fn options(_: *Endpoint, _: zap.Request) !void {} + pub fn head(_: *Endpoint, _: zap.Request) !void {} }; // // end of http client code