diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-06-15 17:33:47 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-06-15 17:33:47 +0200 |
| commit | b43985a19308cc47a7af9b74f74efdfc54be7a83 (patch) | |
| tree | 737a42da37bd5060ea7df581046382157696e8be | |
| parent | 0eb3273f257e096c398ec324a1d2d755ec19a43d (diff) | |
add headers mechanism to responsedev
| -rw-r--r-- | src/http/Response.zig | 41 | ||||
| -rw-r--r-- | src/http/serve.zig | 21 | ||||
| -rw-r--r-- | src/main.zig | 4 | ||||
| -rw-r--r-- | src/z/rest.js | 20 | ||||
| -rw-r--r-- | src/z/root.zig | 5 |
5 files changed, 82 insertions, 9 deletions
diff --git a/src/http/Response.zig b/src/http/Response.zig index 5745899..2105e16 100644 --- a/src/http/Response.zig +++ b/src/http/Response.zig @@ -27,11 +27,24 @@ pub const Message = union(enum) { status: std.http.Status, message: Message, +headers: []const std.http.Header = &.{}, pub fn with(status: std.http.Status, message: Message) @This() { return .{ .status = status, .message = message }; } +pub fn withHeaders( + status: std.http.Status, + message: Message, + headers: []const std.http.Header, +) @This() { + return .{ + .status = status, + .message = message, + .headers = headers, + }; +} + pub fn ok(message: Message) @This() { return with(.ok, message); } @@ -44,17 +57,40 @@ pub fn badRequest(message: Message) @This() { return with(.bad_request, message); } +pub const SendOptions = struct { + headers: []const std.http.Header, +}; + pub fn send( self: *const @This(), io: std.Io, + arena: std.mem.Allocator, request: *std.http.Server.Request, + options: SendOptions, ) !void { + const headers = hdrs: { + if (options.headers.len == 0) { + break :hdrs self.headers; + } + + const h = try arena.alloc(std.http.Header, options.headers.len + self.headers.len); + @memcpy(h[0..options.headers.len], options.headers); + @memcpy(h[options.headers.len..], self.headers); + break :hdrs h; + }; + switch (self.message) { .void => { - try request.respond("", .{ .status = self.status }); + try request.respond("", .{ + .status = self.status, + .extra_headers = headers, + }); }, .static => |content| { - try request.respond(content, .{ .status = self.status }); + try request.respond(content, .{ + .status = self.status, + .extra_headers = headers, + }); }, .streaming => |file| { var write_buffer: [1024]u8 = undefined; @@ -63,6 +99,7 @@ pub fn send( var body_writer = try request.respondStreaming(&write_buffer, .{ .respond_options = .{ .status = self.status, + .extra_headers = headers, .transfer_encoding = .chunked, }, }); diff --git a/src/http/serve.zig b/src/http/serve.zig index 587f4a3..036eecd 100644 --- a/src/http/serve.zig +++ b/src/http/serve.zig @@ -1,20 +1,30 @@ const std = @import("std"); +const Response = @import("Response.zig"); const RouteSet = @import("RouteSet.zig"); +pub const Options = struct { + address: std.Io.net.IpAddress, + headers: []const std.http.Header = &.{}, +}; + pub fn serve( gpa: std.mem.Allocator, comptime routes: RouteSet, - address: std.Io.net.IpAddress, + options: Options, ) !void { var threaded: std.Io.Threaded = .init(gpa, .{}); var group: std.Io.Group = .init; const io = threaded.io(); - var server = try address.listen(io, .{ .reuse_address = true }); + var server = try options.address.listen(io, .{ .reuse_address = true }); + + const send_options: Response.SendOptions = .{ + .headers = options.headers, + }; while (true) { const stream = try server.accept(io); - group.async(io, handle, .{ io, gpa, stream, &routes }); + group.async(io, handle, .{ io, gpa, stream, &routes, send_options }); } } @@ -23,6 +33,7 @@ pub fn handle( gpa: std.mem.Allocator, stream: std.Io.net.Stream, routes: *const RouteSet, + send_options: Response.SendOptions, ) void { var read_buffer: [1024 * 8]u8 = undefined; var write_buffer: [1024 * 8]u8 = undefined; @@ -44,8 +55,8 @@ pub fn handle( std.debug.print("{s} {s}\n", .{ @tagName(request.head.method), request.head.target }); if (routes.getInterface(request.head.target)) |interface| { - const message = interface.handle(gpa, arena.allocator(), io, &request); - message.send(io, &request) catch return; + const response = interface.handle(gpa, arena.allocator(), io, &request); + response.send(io, arena.allocator(), &request, send_options) catch return; } else { request.respond("", .{ .status = .not_found }) catch return; } diff --git a/src/main.zig b/src/main.zig index bf1b09f..a429d14 100644 --- a/src/main.zig +++ b/src/main.zig @@ -15,6 +15,8 @@ pub fn main(init: std.process.Init) !void { try http.serve( init.gpa, routes, - try .parseLiteral("0.0.0.0:8080"), + .{ + .address = try .parseLiteral("0.0.0.0:8080"), + } ); } diff --git a/src/z/rest.js b/src/z/rest.js new file mode 100644 index 0000000..c14944d --- /dev/null +++ b/src/z/rest.js @@ -0,0 +1,20 @@ + +const Rest = { + Request: class { + constructor(base) { + this.base = base; + } + + get(url) { return fetch(url); } + + post(url, body) { + return fetch( + url, + { + method: 'POST', + body: JSON.stringify(body), + } + ); + } + }, +}; diff --git a/src/z/root.zig b/src/z/root.zig index 26bbffc..b23ebc5 100644 --- a/src/z/root.zig +++ b/src/z/root.zig @@ -25,7 +25,10 @@ pub const html: File = .{ }; pub fn env(comptime components: []const Component) Element { - var children: []const Element = &.{script.load(@embedFile("z.js"))}; + var children: []const Element = &.{ + script.load(@embedFile("z.js")), + script.load(@embedFile("rest.js")), + }; inline for (components) |c| { const child: []const Element = &.{c.toElement()}; |