Luukdegram / apple_pie

Basic HTTP server implementation in Zig
MIT License
163 stars 21 forks source link

using any doesn't work as expected when requesting / #65

Closed vrischmann closed 3 years ago

vrischmann commented 3 years ago

Hi,

I think there's a bug with any when requesting /.

Taking the router example and replacing this line with router.any instead I would expect the code to still work, but instead it panics:

thread 111030 panic: attempt to use null value
/home/vincent/dev/stuff/apple_pie/src/trie.zig:104:51: 0x252f4f in .apple_pie.trie.Trie(u8).get (router)
                return .{ .static = self.root.data.? };
                                                  ^
/home/vincent/dev/stuff/apple_pie/src/router.zig:106:60: 0x2518df in .apple_pie.router.Router(*Context,[]const .apple_pie.router.Route{(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant)}).serve (router)
            switch (trees[@enumToInt(request.method())].get(request.path())) {
                                                           ^
/home/vincent/dev/stuff/apple_pie/src/server.zig:197:24: 0x254ee8 in .apple_pie.server.ClientFn(*Context,.apple_pie.router.Router(*Context,[]const .apple_pie.router.Route{(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant)}).serve).handle (router)
                handler(context, &response, parsed_request) catch |err| {
                       ^
/home/vincent/dev/stuff/apple_pie/src/server.zig:151:24: 0x254962 in .apple_pie.server.ClientFn(*Context,.apple_pie.router.Router(*Context,[]const .apple_pie.router.Route{(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant),(struct .apple_pie.router.Route constant)}).serve).run (router)
            self.handle(gpa, clients, context) catch |err| {
                       ^
/home/vincent/dev/stuff/apple_pie/src/server.zig:113:26: 0x253f77 in .apple_pie.server.Server.run (router)
                .frame = async client.run(gpa, &clients, context),
                         ^
/home/vincent/dev/stuff/apple_pie/src/server.zig:56:28: 0x24bc3d in .apple_pie.server.listenAndServe (router)
    try (Server.init()).run(gpa, address, context, handler);
                           ^
/home/vincent/dev/stuff/apple_pie/examples/router.zig:22:28: 0x243a33 in main (router)
    try http.listenAndServe(
                           ^
/home/vincent/dev/devtools/zig/lib/std/start.zig:527:37: 0x23c09a in std.start.callMain (router)
            const result = root.main() catch |err| {
                                    ^
/home/vincent/dev/devtools/zig/lib/std/start.zig:469:12: 0x22014e in std.start.callMainWithArgs (router)
    return @call(.{ .modifier = .always_inline }, callMain, .{});
           ^
/home/vincent/dev/devtools/zig/lib/std/start.zig:383:17: 0x21f1d6 in std.start.posixCallMainAndExit (router)
    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
                ^
/home/vincent/dev/devtools/zig/lib/std/start.zig:296:5: 0x21efe2 in std.start._start (router)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
fish: Job 1, './zig-out/bin/router' terminated by signal SIGABRT (Abort)

as far as I understand the problem is that:

Luukdegram commented 3 years ago

Thanks for the report! The assumption that data is non-null is incorrect, and should check the optional instead and return .none when data is null. Feel free to submit a PR, if not I can too when I get to it.