aboutsummaryrefslogtreecommitdiff
path: root/src/http/Response.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/http/Response.zig')
-rw-r--r--src/http/Response.zig35
1 files changed, 33 insertions, 2 deletions
diff --git a/src/http/Response.zig b/src/http/Response.zig
index a5f7727..8d10992 100644
--- a/src/http/Response.zig
+++ b/src/http/Response.zig
@@ -3,14 +3,14 @@ const std = @import("std");
pub const Message = union(enum) {
void: void,
static: []const u8,
- file_content: std.Io.File,
+ streaming: std.Io.File,
pub fn string(content: []const u8) Message {
return .{ .static = content };
}
pub fn file(io: std.Io, path: []const u8) !Message {
- return .{ .file_content = try std.Io.Dir.cwd().openFile(io, path, .{
+ return .{ .streaming = try std.Io.Dir.cwd().openFile(io, path, .{
.allow_directory = false,
}) };
}
@@ -26,3 +26,34 @@ pub fn with(status: std.http.Status, message: Message) @This() {
pub fn ok(message: Message) @This() {
return with(.ok, message);
}
+
+pub fn send(
+ self: *const @This(),
+ io: std.Io,
+ request: *std.http.Server.Request,
+) !void {
+ switch (self.message) {
+ .void => {
+ try request.respond("", .{ .status = self.status });
+ },
+ .static => |content| {
+ try request.respond(content, .{ .status = self.status });
+ },
+ .streaming => |file| {
+ var write_buffer: [1024]u8 = undefined;
+ var read_buffer: [1024]u8 = undefined;
+
+ var body_writer = try request.respondStreaming(&write_buffer, .{
+ .respond_options = .{
+ .status = self.status,
+ .transfer_encoding = .chunked,
+ },
+ });
+
+ var file_reader = file.reader(io, &read_buffer);
+
+ _ = try file_reader.interface.streamRemaining(&body_writer.writer);
+ try body_writer.end();
+ },
+ }
+}