Provide abstraction for headers

Basic abstraction wrapping existing std.http.Headers to provide a stable
interface.
This commit is contained in:
Bob Farrell 2024-02-15 20:01:07 +00:00
parent 3ba070a72a
commit 7958480ff7
7 changed files with 43 additions and 12 deletions

View File

@ -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.)

View File

@ -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;
}

View File

@ -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());
}

View File

@ -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");

View File

@ -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").?,
);
}

View File

@ -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,

View File

@ -1,7 +1,4 @@
// const Cookies = @import("http/Cookies.zig");
test {
_ = @import("jetzig.zig");
_ = @import("zmpl");
@import("std").testing.refAllDeclsRecursive(@This());
}