Skip to content

Commit

Permalink
fix(router): various bugs with not found handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Nov 13, 2024
1 parent 026c333 commit f1fbdf1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/http/route.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn Route(comptime Server: type) type {
// You can either give a void (if you don't want to pass data through) or a pointer.
comptime assert(@typeInfo(@TypeOf(data)) == .Pointer or @typeInfo(@TypeOf(data)) == .Void);
const inner_data = switch (comptime @typeInfo(@TypeOf(data))) {
.Void => undefined,
.Void => 1, // Needs to not be 0.
.Pointer => @intFromPtr(data),
else => unreachable,
};
Expand Down
20 changes: 10 additions & 10 deletions src/http/router.zig
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,20 @@ pub fn Router(comptime Server: type) type {
try self.routes.add_route(path, route);
}

fn not_found_handler(ctx: *Context, _: void) !void {
try ctx.respond(.{
.status = .@"Not Found",
.mime = Mime.HTML,
.body = "",
});
}

pub fn get_route_from_host(self: Self, path: []const u8, captures: []Capture, queries: *QueryMap) FoundRoute {
const base_404_route = comptime Route.init().get({}, not_found_handler);
const base_404_route = comptime Route.init().get({}, struct {
fn not_found_handler(ctx: *Context, _: void) !void {
try ctx.respond(.{
.status = .@"Not Found",
.mime = Mime.HTML,
.body = "",
});
}
}.not_found_handler);

return self.routes.get_route(path, captures, queries) orelse {
queries.clearRetainingCapacity();
if (self.not_found_route) |not_found| {
queries.clearRetainingCapacity();
return FoundRoute{ .route = not_found, .captures = captures[0..0], .queries = queries };
} else return FoundRoute{ .route = base_404_route, .captures = captures[0..], .queries = queries };
};
Expand Down
6 changes: 2 additions & 4 deletions src/http/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,8 @@ pub fn Server(comptime security: Security) type {
TLSType,
self.config.size_connections_max,
);
if (comptime security == .tls) {
for (tls_slice) |*tls| {
tls.* = null;
}
for (tls_slice) |*tls| {
tls.* = null;
}

// since slices are fat pointers...
Expand Down

0 comments on commit f1fbdf1

Please sign in to comment.