aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 96dafc9b52ce4131d21b786518c83c4af3094794 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
const std = @import("std");
const log = std.log.scoped(.server);

// Web server for mfashby.net
// It does _not_ follow the unix philosophy (:
// Initially it replaces caddy, and it needs to be capable of these things 
// to actually be usable
//  - http(s) (otherwise it's not a web server...) ✅ https isn't supported because zig http server (in fact there's no TLS server implementation). For now I'll have to use haproxy
//  - routing, including virtual host              ✅
//  - serving static content from selected folders ✅
//  - executing CGI programs                       ✅
//  - reverse proxy                                ❌

// And I should probably test it thoroughly before exposing is to the 'net

// Future possibilities: 
//  - subsume some CGI programs directly into the web server e.g. my comments program
//  - and maybe even cgit (although I might scrap it in favour of stagit if I can figure out archive downloads
//  - do something about efficiency :) it's thread-per-request right now
pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{
        .thread_safe = true,
    }){};
    defer {
        log.info("deinit gpa", .{});
        _ = gpa.deinit();
    }
    const a = gpa.allocator();

    var pool: std.Thread.Pool = undefined;
    try std.Thread.Pool.init(&pool, .{ .allocator = a, .n_jobs = 32 });
    defer pool.deinit();

    var svr = std.http.Server.init(.{ .reuse_address = true, .reuse_port = true });
    const addr = try std.net.Address.parseIp("0.0.0.0", 8081);
    try svr.listen(addr);
    log.info("listening on {}", .{addr});
    while (true) {
        // Create the Response into the heap so that the ownership goes to the handling thread
        const res = try a.create(std.http.Server.Response);
        errdefer a.destroy(res);
        res.* = try svr.accept(.{ .allocator = a });
        errdefer res.deinit();
        try pool.spawn(handle, .{res});
    }
}

fn handle(res: *std.http.Server.Response) void {
    defer res.allocator.destroy(res);
    defer res.deinit();
    handleRoute(res) catch |e| {
        log.info("error {}", .{e});
        if (@errorReturnTrace()) |trace| {
            std.debug.dumpStackTrace(trace.*);
        }
        sendError(res, e) catch |e2| {
            log.info("error sending error {}", .{e2});
            if (@errorReturnTrace()) |trace| {
                std.debug.dumpStackTrace(trace.*);
            }
        };
    };
}

fn handleRoute(res: *std.http.Server.Response) !void {
    try res.wait();

    // Route, virtual host first
    var host: []const u8 = "";
    if (res.request.headers.getFirstValue("Host")) |host_header| {
        var spl = std.mem.splitScalar(u8, host_header, ':');
        host = spl.first();
    }

    const uri = std.Uri.parseWithoutScheme(res.request.target) catch {
        res.status = .bad_request;
        const msg = "bad request target";
        res.transfer_encoding = .{ .content_length = msg.len };
        try res.send();
        try res.writeAll(msg);
        try res.finish();
        return;
    };

    if (std.mem.eql(u8, host, "localhost")) {
        if (std.mem.startsWith(u8,  uri.path, "/api")) {
            try serveCgi(res, "/home/martin/dev/mfashby.net/comments/zig-out/bin/comments",
                &.{"DATABASE_URL","SMTP_USERNAME","SMTP_PASSWORD","NOTIFICATION_ADDRESS","SMTP_SERVER"});
        } else {
            try serveStatic(res, "public");
        }
    } else {
        const ans = "You have reached mfashby.net ... but did you mean to?";
        res.status = .ok;
        res.transfer_encoding = .{ .content_length = ans.len };
        try res.send();
        try res.writeAll(ans);
        try res.finish();
    }
}

fn serveCgi(res: *std.http.Server.Response, executable: []const u8,
            comptime env_copy: []const []const u8) !void {
    var child = std.ChildProcess.init(&.{executable}, res.allocator);
    child.stdin_behavior = .Pipe;
    child.stdout_behavior = .Pipe;
    child.stderr_behavior = .Pipe;

    var env = std.process.EnvMap.init(res.allocator);
    try env.put("REQUEST_METHOD", @tagName(res.request.method));
    try env.put("REQUEST_URI", res.request.target);
    inline for (env_copy) |key| {
        try env.put(key, std.os.getenv(key) orelse "");
    }
    var clb = [_]u8{0}**30;
    if (res.request.method == .POST) {
        if (res.request.content_length) |cl| {
            try env.put("CONTENT_LENGTH", try std.fmt.bufPrint(&clb, "{}", .{cl}));
        }
    }
    child.env_map = &env;
    try child.spawn();
    
    if (res.request.method == .POST) {
        if (res.request.content_length) |cl| {
            log.info("sending {} data as CGI body", .{cl});
            try pump(res.reader(), child.stdin.?.writer(), cl);
        } else {
            _ = try pumpUnknown(res.reader(), child.stdin.?.writer());
        }
    }
    var stdout = std.ArrayList(u8).init(res.allocator);
    var stderr = std.ArrayList(u8).init(res.allocator);
    defer stdout.deinit();
    defer stderr.deinit();
    defer {
        if (stderr.items.len>0) {
            log.err("CGI error: {s}", .{stderr.items});
        }
    }
    try child.collectOutput(&stdout, &stderr, 1_000_000);
    const term = try child.wait();
    if (term.Exited != 0) {
        return error.ProcessError;
    }

    var fbs = std.io.fixedBufferStream(stdout.items);
    var reader = fbs.reader();
    var headerLine = std.ArrayList(u8).init(res.allocator);
    defer headerLine.deinit();
    while (true) {
        headerLine.clearRetainingCapacity();
        try reader.streamUntilDelimiter(headerLine.writer(), '\r', 8192);
        _ = try reader.skipBytes(1, .{}); // \n
        if (headerLine.items.len == 0) {
            break;
        }
        var spl = std.mem.splitScalar(u8, headerLine.items, ':');
        const key = try std.ascii.allocLowerString(res.allocator, spl.first());
        defer res.allocator.free(key);
        if (std.mem.eql(u8, key, "status")) {
            const value = spl.rest();
            var spl2 = std.mem.splitScalar(u8, std.mem.trim(u8, value, " "), ' ');
            res.status = @enumFromInt(try std.fmt.parseInt(u16, spl2.first(), 10));
            log.info("status from CGI {}", .{res.status});
        } else if (std.mem.eql(u8, key, "content-length")) {
            const value = spl.rest();
            res.transfer_encoding = .{.content_length = try std.fmt.parseInt(usize, value, 10)};
            log.info("transfer_encoding from CGI {}", .{res.transfer_encoding});
        } else {
            const value = spl.rest();
            try res.headers.append(key, std.mem.trim(u8, value, " "));
            log.info("header from CGI {s}: {s}", .{key, value});
        }
    }

    if (res.transfer_encoding == .content_length) {
        try res.send();
        try pump(reader, res.writer(), res.transfer_encoding.content_length);
    } else {
        res.transfer_encoding = .chunked;
        try res.send();
        _ = try pumpUnknown(reader, res.writer());
    }
    try res.finish();
}

fn serveStatic(res: *std.http.Server.Response, dirname: []const u8) !void {
    const dirpath = try std.fs.realpathAlloc(res.allocator, dirname);
    defer res.allocator.free(dirpath);

    // Path massaging
    const uri = std.Uri.parseWithoutScheme(res.request.target) catch {
        res.status = .bad_request;
        const msg = "bad request target";
        res.transfer_encoding = .{ .content_length = msg.len };
        try res.send();
        try res.writeAll(msg);
        try res.finish();
        return;
    };
    var requested_path = uri.path;
    requested_path = try std.fs.path.join(res.allocator, &.{ dirpath, requested_path });


    const path = std.fs.realpathAlloc(res.allocator, requested_path) catch |e| {
        res.status = switch (e) {
            error.FileNotFound => .not_found,
            error.AccessDenied => .forbidden,
            error.BadPathName => .bad_request,
            else => .internal_server_error,
        };
        const msg = try std.fmt.allocPrint(res.allocator, "error: {}", .{e});
        defer res.allocator.free(msg);
        res.transfer_encoding = .{ .content_length = msg.len };
        try res.send();
        try res.writeAll(msg);
        try res.finish();
        return;
    };

    defer res.allocator.free(path);
    if (!std.mem.startsWith(u8, path, dirpath)) {
        res.status = .bad_request;
        const msg = try std.fmt.allocPrint(res.allocator, "Trying to escape the root directory {s}", .{path});
        defer res.allocator.free(msg);
        res.transfer_encoding = .{ .content_length = msg.len };
        try res.send();
        try res.writeAll(msg);
        try res.finish();
        return;
    }
    const f = std.fs.openFileAbsolute(path, .{}) catch |e| {
        res.status = switch (e) {
            error.FileNotFound => .not_found,
            error.AccessDenied => .forbidden,
            error.BadPathName, error.NameTooLong => .bad_request,
            else => .internal_server_error,
        };
        const msg = try std.fmt.allocPrint(res.allocator, "error: {}", .{e});
        defer res.allocator.free(msg);
        res.transfer_encoding = .{ .content_length = msg.len };
        try res.send();
        try res.writeAll(msg);
        try res.finish();
        return;
    };
    defer f.close();
    const stat = try f.stat();
    switch (stat.kind) {
        .file => {
            res.transfer_encoding = .{ .content_length = stat.size };
            try res.send();
            try pump(f.reader(), res.writer(), stat.size);
            try res.finish();
        },
        .directory => {
            const index_path = try std.fs.path.join(res.allocator, &.{path, "index.html"});
            defer res.allocator.free(index_path);
            const index_f = std.fs.openFileAbsolute(index_path, .{}) catch |e| {
                res.status = switch (e) {
                    error.FileNotFound => .not_found,
                    error.AccessDenied => .forbidden,
                    error.BadPathName, error.NameTooLong => .bad_request,
                    else => .internal_server_error,
                };
                const msg = try std.fmt.allocPrint(res.allocator, "error: {}", .{e});
                defer res.allocator.free(msg);
                res.transfer_encoding = .{ .content_length = msg.len };
                try res.send();
                try res.writeAll(msg);
                try res.finish();
                return;
            };
            defer index_f.close();
            const index_stat = try index_f.stat();
            res.transfer_encoding = .{ .content_length = index_stat.size };
            try res.send();
            try pump(index_f.reader(), res.writer(), index_stat.size);
            try res.finish();  
        },
        else => {
            const msg = "unable to serve unsupported file kind";
            res.status = .unavailable_for_legal_reasons;
            res.transfer_encoding = .{.content_length = msg.len};
            try res.send();
            try res.writeAll(msg);
            try res.finish();
        }
    }
    
}

fn pumpUnknown(reader: anytype, writer: anytype) !usize {
    var read: usize = 0;
    var buf: [1024]u8 = undefined;
    while (true) {
        const sz = try reader.read(&buf);
        if (sz == 0) break;
        read += sz;
        try writer.writeAll(buf[0..sz]);
    }
    return read;
}

fn pump(reader: anytype, writer: anytype, expected: usize) !void {
    var read: usize = 0;
    var buf: [1024]u8 = undefined;
    while (true) {
        const sz = try reader.read(&buf);
        if (sz == 0) break;
        read += sz;
        if (read > expected) return error.TooMuchData;
        try writer.writeAll(buf[0..sz]);
    }
    if (read != expected) {
        return error.NotEnoughData;
    }
}


fn sendError(res: *std.http.Server.Response, e: anyerror) !void {
    switch (res.state) {
        .first, .start, .waited => {
            if (res.state != .waited) {
                try res.wait();
            }
            const errmsg = try std.fmt.allocPrint(res.allocator, "Error: {}", .{e});
            defer res.allocator.free(errmsg);
            // Now send an error
            res.status = .internal_server_error;
            res.transfer_encoding = .{ .content_length = errmsg.len };
            try res.send();
            try res.writeAll(errmsg);
            try res.finish();
        },
        .responded, .finished => {
            // Too late!
            log.err("can't send an error, response already sent, state {}", .{res.state});
        },
    }
}