aboutsummaryrefslogtreecommitdiff
path: root/src/http/Response.zig
blob: 2105e164103df3ceea15301f9cedf431e4dfdfec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const std = @import("std");

pub const Message = union(enum) {
    void: void,
    static: []const u8,
    streaming: std.Io.File,

    pub fn json(arena: std.mem.Allocator, value: anytype) !Message {
        var allocating: std.Io.Writer.Allocating = .init(arena);

        var jsonify: std.json.Stringify = .{ .writer = &allocating.writer };
        try jsonify.write(value);

        return .string(try allocating.toOwnedSlice());
    }

    pub fn string(content: []const u8) Message {
        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,
        }) };
    }
};

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

pub fn okOrError(message: anyerror!Message) @This() {
    return ok(message catch return with(.internal_server_error, .void));
}

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,
                .extra_headers = headers,
            });
        },
        .static => |content| {
            try request.respond(content, .{
                .status = self.status,
                .extra_headers = headers,
            });
        },
        .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,
                    .extra_headers = headers,
                    .transfer_encoding = .chunked,
                },
            });

            var file_reader = file.reader(io, &read_buffer);

            _ = try file_reader.interface.streamRemaining(&body_writer.writer);
            try body_writer.end();
        },
    }
}