aboutsummaryrefslogtreecommitdiff
path: root/src/http
diff options
context:
space:
mode:
Diffstat (limited to 'src/http')
-rw-r--r--src/http/Response.zig7
-rw-r--r--src/http/handler/root.zig5
-rw-r--r--src/http/handler/static.zig6
3 files changed, 7 insertions, 11 deletions
diff --git a/src/http/Response.zig b/src/http/Response.zig
index 69bec65..62c53f5 100644
--- a/src/http/Response.zig
+++ b/src/http/Response.zig
@@ -18,10 +18,8 @@ pub const Message = union(enum) {
return .{ .static = content };
}
- pub fn file(io: std.Io, path: []const u8) !Message {
- return .{ .streaming = try std.Io.Dir.cwd().openFile(io, path, .{
- .allow_directory = false,
- }) };
+ pub fn file(f: std.Io.File) Message {
+ return .{ .streaming = f };
}
};
@@ -97,6 +95,7 @@ pub fn send(
});
},
.streaming => |file| {
+ defer file.close(io);
var write_buffer: [1024]u8 = undefined;
var read_buffer: [1024]u8 = undefined;
diff --git a/src/http/handler/root.zig b/src/http/handler/root.zig
index 33fdb84..634888c 100644
--- a/src/http/handler/root.zig
+++ b/src/http/handler/root.zig
@@ -9,7 +9,6 @@ test "interface" {
const C = Context(.{ "api", "images", .id, "metadata" });
const context: C = .{
.post = post,
- .get = get,
};
const Query = struct {
@@ -25,10 +24,6 @@ test "interface" {
return .ok(.string(request.params.id.as([]const u8)));
}
-
- fn get(request: C.Request) Response {
- return .ok(Response.Message.file(request.io, "./some/path.txt") catch return .with(.not_found, .void));
- }
};
_ = M.context.interface();
diff --git a/src/http/handler/static.zig b/src/http/handler/static.zig
index d53f213..df307b7 100644
--- a/src/http/handler/static.zig
+++ b/src/http/handler/static.zig
@@ -2,10 +2,12 @@ const Interface = @import("Interface.zig");
const Context = @import("context.zig").Context;
const Response = @import("../Response.zig");
-pub fn Static(route: anytype, content: []const u8) Context(route) {
+pub fn Static(route: anytype, content: []const u8, mime: []const u8) Context(route) {
const get = struct {
fn get(_: Context(route).Request) Response {
- return .ok(.string(content));
+ return .withHeaders(.ok, .string(content), &.{
+ .{ .name = "Content-Type", .value = mime },
+ });
}
}.get;