diff --git a/src/handler.zig b/src/handler.zig index 06b7ae3..53a04b7 100644 --- a/src/handler.zig +++ b/src/handler.zig @@ -1,18 +1,14 @@ const std = @import("std"); -const maivi = @import("root.zig"); +const Io = std.Io; const http = std.http; const Request = http.Server.Request; -const Status = http.Status; const fern = @import("fern"); const helpers = @import("helpers.zig"); +const maivi = @import("root.zig"); -const lua = maivi.lua; - -const Self = @This(); - -pub fn task(io: std.Io, stream: std.Io.net.Stream) error{Canceled}!void { +pub fn task(io: Io, stream: Io.net.Stream) error{Canceled}!void { defer stream.close(io); var arena: std.heap.ArenaAllocator = .init(maivi.allocator); @@ -41,27 +37,28 @@ pub fn task(io: std.Io, stream: std.Io.net.Stream) error{Canceled}!void { }, else => break :blk err, }; - dispatch(.{ + var conn: Connection = .{ .request = &request, .handle = stream.socket.handle, .path = path, .arena = allocator, - }) catch |err| break :blk err; + }; + dispatch(&conn) catch |e| break :blk e; }; ret catch |e| { - fern.err().err(e).log(); + fern.err().ctx("handler.task").err(e).log(); return error.Canceled; }; } pub const Connection = struct { request: *Request, - handle: std.Io.net.Socket.Handle, + handle: Io.net.Socket.Handle, path: []const u8, arena: std.mem.Allocator, }; -pub fn dispatch(conn: Connection) !void { +pub fn dispatch(conn: *Connection) !void { const request = conn.request; var l = fern.debug().ctx("handler.dispatch") @@ -72,9 +69,9 @@ pub fn dispatch(conn: Connection) !void { while (it.next()) |header| l = l.str(header.name, header.value); l.log(); - const timer = std.Io.Timestamp.now(maivi.io, .cpu_thread); + const timer = Io.Timestamp.now(maivi.io, .cpu_thread); defer { - const elapsed = std.Io.Timestamp.durationTo(timer, std.Io.Timestamp.now(maivi.io, .cpu_thread)).toMilliseconds(); + const elapsed = Io.Timestamp.durationTo(timer, Io.Timestamp.now(maivi.io, .cpu_thread)).toMilliseconds(); fern.debug().ctx("handler.dispatch") .msg("response") .str("path", conn.path) @@ -90,7 +87,7 @@ pub fn dispatch(conn: Connection) !void { const NOTFOUND_MSG = "path not found :("; const INTERNALERR_MSG = "internal server error x_x"; -fn returnNotFound(conn: Connection) !void { +fn returnNotFound(conn: *Connection) !void { withNotFound(conn, "/404.html") catch |err| switch (err) { error.IsDirectory => return withNotFound(conn, "/404/index.html") catch |e| switch (e) { error.FileNotFound => return try respondNotFound(conn), @@ -101,12 +98,12 @@ fn returnNotFound(conn: Connection) !void { }; } -fn withNotFound(conn: Connection, try_path: []const u8) !void { - const dir = try std.Io.Dir.openDirAbsolute(maivi.io, maivi.config.buildroot, .{}); +fn withNotFound(conn: *Connection, try_path: []const u8) !void { + const dir = try Io.Dir.openDirAbsolute(maivi.io, maivi.config.buildroot, .{}); try helpers.sendfile(conn, dir, try_path); } -inline fn respondNotFound(conn: Connection) !void { +inline fn respondNotFound(conn: *Connection) !void { try conn.request.respond(NOTFOUND_MSG, .{ .status = .not_found, }); diff --git a/src/helpers.zig b/src/helpers.zig index d2d59cc..c3f2aa9 100644 --- a/src/helpers.zig +++ b/src/helpers.zig @@ -5,7 +5,8 @@ const Status = http.Status; const fern = @import("fern"); -const Handler = @import("handler.zig"); +const handler = @import("handler.zig"); +const Connection = handler.Connection; const maivi = @import("root.zig"); pub fn requestHeader(request: Request, name: []const u8) ?http.Header { @@ -51,7 +52,7 @@ pub fn respond(request: *Request, buf: []u8, opts: struct { // std.http.BodyWriter.chunkedSendFile // for reference :3 -pub fn sendfile(conn: Handler.Connection, dir: std.Io.Dir, path: []const u8) !void { +pub fn sendfile(conn: *Connection, dir: std.Io.Dir, path: []const u8) !void { const request = conn.request; const sub_path = std.mem.cutPrefix(u8, path, "/") orelse path; diff --git a/src/root.zig b/src/root.zig index 39fd7ad..429c91a 100644 --- a/src/root.zig +++ b/src/root.zig @@ -11,6 +11,7 @@ pub const Config = @import("config.zig"); pub const data = @import("data.zig"); pub const gen = @import("gen.zig"); pub const handler = @import("handler.zig"); +const Connection = handler.Connection; pub const helpers = @import("helpers.zig"); pub const lua = @import("lua/root.zig"); pub const Router = @import("router.zig"); @@ -135,7 +136,7 @@ pub fn listen() !void { // else: // with(build) $uri $uri/index.html // with(static) $uri -pub fn handle(conn: handler.Connection) !void { +pub fn handle(conn: *Connection) !void { const request = conn.request; const user_agent = helpers.requestHeader(request.*, "user-agent"); @@ -164,7 +165,7 @@ pub fn handle(conn: handler.Connection) !void { pub const MESSAGE_PATH = "/index.txt"; -fn handle_fs(conn: handler.Connection) !void { +fn handle_fs(conn: *Connection) !void { const build_dir = try std.Io.Dir.openDirAbsolute(io, config.buildroot, .{}); defer build_dir.close(io); @@ -204,7 +205,7 @@ fn handle_fs(conn: handler.Connection) !void { return error.FileNotFound; } -pub fn handletxt(conn: handler.Connection) !void { +pub fn handletxt(conn: *Connection) !void { const request = conn.request; if (conn.path.len == 1) { @@ -258,7 +259,7 @@ pub fn add_redirects() !void { } } -fn handleredirect(conn: handler.Connection) !void { +fn handleredirect(conn: *Connection) !void { const request = conn.request; const redirect_url = redirects.get(conn.path) orelse diff --git a/src/router.zig b/src/router.zig index d2ff7df..e2157cb 100644 --- a/src/router.zig +++ b/src/router.zig @@ -9,6 +9,7 @@ const Lua = zlua.Lua; const fragment = @import("fragment.zig"); const handler = @import("handler.zig"); +const Connection = handler.Connection; const helpers = @import("helpers.zig"); const maivi = @import("root.zig"); @@ -22,7 +23,7 @@ pub const Path = struct { return .{ .action = action, .config = cfg orelse .{} }; } - pub const Action = *const fn (conn: handler.Connection) anyerror!void; + pub const Action = *const fn (conn: *Connection) anyerror!void; pub const Config = struct { allowed_methods: []const http.Method = &.{.GET}, }; @@ -105,7 +106,7 @@ fn lua_add(l: *Lua) !i32 { // TODO: // - set content // - caching? -pub fn handle_lua(conn: handler.Connection) !void { +pub fn handle_lua(conn: *Connection) !void { const request = conn.request; const content_type = helpers.content_type_for_file(conn.path) orelse .html;