diff --git a/README.md b/README.md index f4f96a5..25e347a 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ If you are interested in _Jetzig_ you will probably find these tools interesting * :white_check_mark: Cookies. * :white_check_mark: Error handling. * :white_check_mark: Static content from /public directory. -* :x: Static content generation. -* :x: Headers (available but not yet wrapped). +* :white_check_mark: Headers (available but not yet wrapped). +* :x: Static content generation (nearly there!). * :x: Param/JSON payload parsing/abstracting. * :x: Development-mode responses for debugging. * :x: Environment configurations (develompent/production/etc.) diff --git a/build.zig b/build.zig index 3c21cb3..cf7f871 100644 --- a/build.zig +++ b/build.zig @@ -4,7 +4,7 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const template_path = b.option([]const u8, "zmpl_templates_path", "Path to templates") orelse "src/app/views/"; - const manifest : []const u8 = b.pathJoin(&.{ template_path, "zmpl.manifest.zig" }); + const manifest: []const u8 = b.pathJoin(&.{ template_path, "zmpl.manifest.zig" }); const lib = b.addStaticLibrary(.{ .name = "jetzig", @@ -24,13 +24,13 @@ pub fn build(b: *std.Build) !void { b.installArtifact(exe); - const jetzig_module = b.addModule("jetzig",.{ .root_source_file = .{ .path = "src/jetzig.zig" } }); + const jetzig_module = b.addModule("jetzig", .{ .root_source_file = .{ .path = "src/jetzig.zig" } }); exe.root_module.addImport("jetzig", jetzig_module); lib.root_module.addImport("jetzig", jetzig_module); const zmpl_dep = b.dependency( "zmpl", - .{ .target = target, .optimize = optimize, .zmpl_templates_path = template_path, .zmpl_manifest_path = manifest}, + .{ .target = target, .optimize = optimize, .zmpl_templates_path = template_path, .zmpl_manifest_path = manifest }, ); lib.root_module.addImport("zmpl", zmpl_dep.module("zmpl")); @@ -90,7 +90,10 @@ pub const CompileViewsStep = struct { .makeFn = &make, }); const compile_step_view = owner.allocator.create(CompileViewsStep) catch @panic("Out of memory"); - compile_step_view.* = .{ .step = step, .template_path = options.template_path, }; + compile_step_view.* = .{ + .step = step, + .template_path = options.template_path, + }; return compile_step_view; } diff --git a/src/jetzig.zig b/src/jetzig.zig index 37ee765..d7a172a 100644 --- a/src/jetzig.zig +++ b/src/jetzig.zig @@ -162,5 +162,5 @@ pub fn base64Decode(allocator: std.mem.Allocator, string: []const u8) ![]const u } test { - @import("std").testing.refAllDeclsRecursive(@This()); + @import("std").testing.refAllDecls(@This()); } diff --git a/src/jetzig/http.zig b/src/jetzig/http.zig index f13c758..ef0c4ff 100644 --- a/src/jetzig/http.zig +++ b/src/jetzig/http.zig @@ -7,4 +7,5 @@ pub const Request = @import("http/Request.zig"); pub const Response = @import("http/Response.zig"); pub const Session = @import("http/Session.zig"); pub const Cookies = @import("http/Cookies.zig"); +pub const Headers = @import("http/Headers.zig"); pub const status_codes = @import("http/status_codes.zig"); diff --git a/src/jetzig/http/Headers.zig b/src/jetzig/http/Headers.zig new file mode 100644 index 0000000..b64bcf5 --- /dev/null +++ b/src/jetzig/http/Headers.zig @@ -0,0 +1,30 @@ +const std = @import("std"); + +allocator: std.mem.Allocator, +headers: std.http.Headers, + +const Self = @This(); + +pub fn init(allocator: std.mem.Allocator, headers: std.http.Headers) Self { + return .{ .allocator = allocator, .headers = headers }; +} + +pub fn getFirstValue(self: *Self, key: []const u8) ?[]const u8 { + return self.headers.getFirstValue(key); +} + +pub fn append(self: *Self, key: []const u8, value: []const u8) !void { + try self.headers.append(key, value); +} + +test { + const allocator = std.testing.allocator; + var headers = std.http.Headers.init(allocator); + defer headers.deinit(); + try headers.append("foo", "bar"); + var jetzig_headers = Self.init(allocator, headers); + try std.testing.expectEqualStrings( + headers.getFirstValue("foo").?, + jetzig_headers.getFirstValue("foo").?, + ); +} diff --git a/src/jetzig/http/Request.zig b/src/jetzig/http/Request.zig index b18accf..923c43e 100644 --- a/src/jetzig/http/Request.zig +++ b/src/jetzig/http/Request.zig @@ -12,7 +12,7 @@ pub const Format = enum { HTML, JSON, UNKNOWN }; allocator: std.mem.Allocator, path: []const u8, method: Method, -headers: std.http.Headers, +headers: jetzig.http.Headers, segments: std.ArrayList([]const u8), server: *jetzig.http.Server, session: *jetzig.http.Session, @@ -68,7 +68,7 @@ pub fn init( .allocator = allocator, .path = response.request.target, .method = method, - .headers = response.request.headers, + .headers = jetzig.http.Headers.init(allocator, response.request.headers), .server = server, .segments = segments, .cookies = cookies, diff --git a/src/tests.zig b/src/tests.zig index 7ee440f..8b51ac2 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -1,7 +1,4 @@ -// const Cookies = @import("http/Cookies.zig"); - test { _ = @import("jetzig.zig"); - _ = @import("zmpl"); @import("std").testing.refAllDeclsRecursive(@This()); }