aboutsummaryrefslogtreecommitdiff
path: root/server/src/main.zig
blob: f9f204bd05737951559f1ca8482a57a2257a0739 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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 _ = 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 });
    defer svr.deinit();
    const port = 8081;
    const addr = std.net.Address.initIp4(.{0,0,0,0}, port);
    try svr.listen(addr);
    log.info("listening on {}", .{addr});
    try acceptLoop(a, &svr, &pool, struct{
        fn cancelled() bool {
            return false;
        }
    });
}

fn acceptLoop(a: std.mem.Allocator, svr: *std.http.Server, pool: *std.Thread.Pool, cancel: anytype) !void {
    while (!cancel.cancelled()) {
        // 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();
        if (cancel.cancelled()) {
            res.deinit();
            a.destroy(res);
            break;
        }
        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 });
    defer res.allocator.free(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});
        },
    }
}

const Fixture = struct {
    a: std.mem.Allocator,
    port: u16,
    t: std.Thread,
    cancel: std.atomic.Value(bool),
    base_url: []const u8,
    thread_pool: std.Thread.Pool,
    server: std.http.Server,
    client: std.http.Client,

    fn init(a: std.mem.Allocator) !*Fixture {
        var res: *Fixture = try a.create(Fixture);
        errdefer a.destroy(res);
        res.a = a;
        try std.Thread.Pool.init(&res.thread_pool, .{ .allocator = a, .n_jobs = 32 });
        errdefer res.thread_pool.deinit();
        res.server = std.http.Server.init(.{ .reuse_address = true, .reuse_port = true });
        errdefer res.server.deinit();
        const addr = std.net.Address.initIp4(.{127,0,0,1}, 0);
        try res.server.listen(addr);
        res.port = res.server.socket.listen_address.in.getPort();
        res.base_url = try std.fmt.allocPrint(a, "http://localhost:{}", .{res.port});
        errdefer a.free(res.base_url);
        res.cancel = std.atomic.Value(bool).init(false);
        res.t = try std.Thread.spawn(.{}, acceptLoop, .{a, &res.server, &res.thread_pool, res});
        res.client = .{.allocator = a};
        return res;
    }

    fn deinit(self: *Fixture) void {
        self.client.deinit();
        self.a.free(self.base_url);
        self.cancel.store(true, .Unordered);
        // Trigger the server's accept() method to wake it up
        if (std.net.tcpConnectToAddress(std.net.Address.initIp4(.{127,0,0,1}, self.port))) |stream| {
            stream.close();
        } else |_| {}
        self.t.join();
        self.thread_pool.deinit();
        self.server.deinit();
        self.a.destroy(self);
    }

    fn cancelled(self: *Fixture) bool {
        return self.cancel.load(.Unordered);
    }
};


test "static pages" {
    const a = std.testing.allocator;
    var f = try Fixture.init(a);
    defer f.deinit();
    const TestCase = struct {
        path: []const u8,
        file: []const u8,
    };
    const test_cases = [_]TestCase{
        .{ .path = "/", .file = "public/index.html"},
        .{ .path = "/posts/2018-05-31-new-site/", .file = "public/posts/2018-05-31-new-site/index.html"},
    };

    for (test_cases) |test_case| {
        const url = try std.fmt.allocPrint(a, "{s}{s}", .{f.base_url, test_case.path});
        defer a.free(url);
        var fr = try f.client.fetch(a, .{
            .location = .{.url = url}
            });
        defer fr.deinit();
        try std.testing.expectEqual(std.http.Status.ok, fr.status);
        const expected = try std.fs.cwd().readFileAlloc(a, test_case.file, 1_000_000);
        defer a.free(expected);
        try std.testing.expectEqualStrings(expected, fr.body.?);
    }
}

// test "404" {

// }