Skip to content

Commit

Permalink
zig/handler- add helpers to gracefully handle invalid paths
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Jan 20, 2022
1 parent 7daa96f commit 5a09d7c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
21 changes: 19 additions & 2 deletions src/handler/_internal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,29 @@ pub fn fileList(alloc: std.mem.Allocator, path: string) ![]const string {

pub fn assert(cond: bool, response: *http.Response, comptime fmt: string, args: anytype) !void {
if (!cond) {
try response.writer().print(fmt, args);
return error.HttpNoOp;
return fail(response, fmt, args);
}
}

pub fn fail(response: *http.Response, comptime fmt: string, args: anytype) (http.Response.Writer.Error || error{HttpNoOp}) {
try response.writer().print(fmt, args);
return error.HttpNoOp;
}

pub fn reqRemote(request: http.Request, response: *http.Response, id: u64) !db.Remote {
const alloc = request.arena;
const r = try db.Remote.byKey(alloc, .id, id);
return r orelse fail(response, "error: remote by id '{d}' not found\n", .{id});
}

pub fn reqUser(request: http.Request, response: *http.Response, r: db.Remote, name: string) !db.User {
const alloc = request.arena;
const u = try r.findUserBy(alloc, .name, name);
return u orelse fail(response, "error: user by name '{s}' not found\n", .{name});
}

pub fn reqPackage(request: http.Request, response: *http.Response, u: db.User, name: string) !db.Package {
const alloc = request.arena;
const p = try u.findPackageBy(alloc, .name, name);
return p orelse fail(response, "error: package by name '{s}' not found\n", .{name});
}
14 changes: 7 additions & 7 deletions src/handler/package.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const _internal = @import("./_internal.zig");
pub fn get(_: void, response: *http.Response, request: http.Request, args: struct { remote: u64, user: string, package: string }) !void {
const alloc = request.arena;
const u = try _internal.getUserOp(response, request);
const r = try db.Remote.byKey(alloc, .id, args.remote);
const o = try r.?.findUserBy(alloc, .name, args.user);
const p = try o.?.findPackageBy(alloc, .name, args.package);
const v = try p.?.versions(alloc);
const r = try _internal.reqRemote(request, response, args.remote);
const o = try _internal.reqUser(request, response, r, args.user);
const p = try _internal.reqPackage(request, response, o, args.package);
const v = try p.versions(alloc);

try _internal.writePageResponse(alloc, response, request, "/package.pek", .{
.aquila_version = @import("root").version,
.user = u,
.repo = r.?,
.owner = o.?,
.pkg = p.?,
.repo = r,
.owner = o,
.pkg = p,
.versions = v,
});
}
10 changes: 5 additions & 5 deletions src/handler/user.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const _internal = @import("./_internal.zig");
pub fn get(_: void, response: *http.Response, request: http.Request, args: struct { remote: u64, user: string }) !void {
const alloc = request.arena;
const u = try _internal.getUserOp(response, request);
const r = try db.Remote.byKey(alloc, .id, args.remote);
const o = try r.?.findUserBy(alloc, .name, args.user);
const p = try o.?.packages(alloc);
const r = try _internal.reqRemote(request, response, args.remote);
const o = try _internal.reqUser(request, response, r, args.user);
const p = try o.packages(alloc);

try _internal.writePageResponse(alloc, response, request, "/user.pek", .{
.aquila_version = @import("root").version,
.user = u,
.repo = r.?,
.owner = o.?,
.repo = r,
.owner = o,
.pkgs = p,
});
}

0 comments on commit 5a09d7c

Please sign in to comment.