summaryrefslogtreecommitdiff
path: root/src/zigwebserver.zig
blob: 64bd6494d00c250516c0297e5f56ef076964d2d3 (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
const std = @import("std");

/// Wrapper around a std.http.Server to run a multi-threaded HTTP server using thread-per-request and arena-per-request
/// Context is .clone()'d and passed to each request, useful for passing user data to each request handler e.g. a database connection.
/// HandlerType provides a single method handle() which is used to actually handle requests.
pub fn Server(comptime Context: type, comptime Handler: type) type {
    return struct {
        address: std.net.Address,
        context: Context,
        handler: Handler,

        max_header_size: usize = 8192,
        n_threads: u32 = 50,

        allocator: std.mem.Allocator,

        pub fn serve(self: @This()) !void {
            var tp = std.Thread.Pool{ .threads = &[_]std.Thread{}, .allocator = self.allocator };
            try tp.init(.{ .allocator = self.allocator, .n_jobs = self.n_threads });
            defer tp.deinit();

            var svr_internal = std.http.Server.init(.{ .reuse_address = true });
            defer svr_internal.deinit();
            try svr_internal.listen(self.address);
            std.log.info("server listening on {}", .{self.address});
            while (true) {
                var aa = std.heap.ArenaAllocator.init(self.allocator); // will be freed by the spawned thread.
                var conn = try svr_internal.accept(.{ .allocator = aa.allocator(), .header_strategy = .{ .dynamic = self.max_header_size } });
                const ctx: Context = self.context.clone();
                try tp.spawn(handle, .{ self, &conn, ctx, aa });
            }
        }

        fn handle(self: @This(), res: *std.http.Server.Response, ctx: Context, aa: std.heap.ArenaAllocator) void {
            defer aa.deinit();
            defer ctx.deinit();
            defer res.deinit();
            if (res.wait()) {
                if (self.handler.handle(res, ctx)) {
                    std.log.info("Success handling request [{s} {s} {s}] status {d} client {}", .{ @tagName(res.request.method), res.request.target, @tagName(res.request.version), @intFromEnum(res.status), res.address });
                } else |err| {
                    std.log.info("Error handling request [{s} {s} {s}] client {} error {}", .{ @tagName(res.request.method), res.request.target, @tagName(res.request.version), res.address, err });
                    if (handle_simple_response(res, "<html><body>Server error!</body></html>", .internal_server_error)) {} else |err2| {
                        std.log.err("Error sending error page for {} : {}", .{ res.address, err2 });
                    }
                }
            } else |_| {
                // Do nothing
            }

            if (res.state != .finished) {
                std.log.err("request wasn't finished!", .{});
            }
        }

        fn handle_simple_response(res: *std.http.Server.Response, content: []const u8, status: std.http.Status) !void {
            res.status = status;
            res.transfer_encoding = .{ .content_length = content.len };
            try res.headers.append("content-type", "text/html");
            try res.send();
            try res.writer().writeAll(content);
            try res.finish();
        }
    };
}

pub const Params = std.StringHashMap([]const u8);

/// Routing component for an http server with wildcard matching and parameter
/// Handles matching a request to a handler.
/// Handler pattern can either be matched exactly
/// or it can have matcher segments, so
/// "/" -> matches request for "/" only
/// "/foo" matches request for "/foo" only
/// "/foo/{bar}/baz" matches request for "/foo/123/baz" and "/foo/bar/baz", and Params would contain "bar":"123" and "bar":"bar" respectively.
/// or it can have terminating wildcards, so
/// "/foo/*" -> matches "/foo", "/foo/bar","/foo/bar/baz"
/// "/*" -> matches all requests
/// TODO something clever to parse path parameters into the appropriate types, maybe smth like "/foo/{bar:u32}/baz"
/// TODO something to handle query parameters and request body too
pub fn Router(comptime Response: type, comptime Context: type, comptime ErrorType: type) type {
    return struct {
        pub const Handler = struct {
            method: std.http.Method,
            pattern: []const u8,
            handle_fn: *const fn (Response, Context, Params) ErrorType!void,
        };

        allocator: std.mem.Allocator,

        handlers: []const Handler,

        notfound: *const fn (Response, Context) ErrorType!void,

        pub fn handle(self: @This(), res: Response, ctx: Context) ErrorType!void {
            var p = try Path.parse(self.allocator, res.request.target);
            defer p.deinit();
            const path = p.path;

            handler_loop: for (self.handlers) |handler| {
                if (handler.method != res.request.method) {
                    continue :handler_loop;
                }

                var path_params: Params = std.StringHashMap([]const u8).init(self.allocator);
                defer path_params.deinit();

                var handle_split = std.mem.splitScalar(u8, handler.pattern, '/');
                var req_split = std.mem.splitScalar(u8, path, '/');

                while (true) {
                    const maybe_handle_seg = handle_split.next();
                    const maybe_req_seg = req_split.next();
                    if (maybe_handle_seg == null and maybe_req_seg == null) {
                        // End of both handler and request, they matched this far so
                        // the handler must handle.
                        try handler.handle_fn(res, ctx, path_params);
                        break :handler_loop;
                    } else if (maybe_handle_seg != null and std.mem.eql(u8, maybe_handle_seg.?, "*")) {
                        // Wildcard, this matches
                        try handler.handle_fn(res, ctx, path_params);
                        break :handler_loop;
                    } else if (maybe_handle_seg == null or maybe_req_seg == null) {
                        // path lengths don't match, try the next handler
                        continue :handler_loop;
                    } else {
                        const handle_seg = maybe_handle_seg.?;
                        const req_seg = maybe_req_seg.?;
                        if (handle_seg.len > 0 and handle_seg[0] == '{' and handle_seg[handle_seg.len - 1] == '}') {
                            // Capture and keep going
                            const key = handle_seg[1 .. handle_seg.len - 1];
                            try path_params.put(key, req_seg);
                        } else if (std.mem.eql(u8, handle_seg, req_seg)) {
                            // segments match, keep going
                        } else {
                            // mismatch, try the next handler
                            continue :handler_loop;
                        }
                    }
                }
            } else {
                try self.notfound(res, ctx);
            }
        }
    };
}

const RouterTest = struct {
    const TestRequest = struct {
        method: std.http.Method,
        target: []const u8,
    };
    const TestResponse = struct {
        request: TestRequest,
    };
    const TestCtx = struct {};
    const TestErr = error{ TestError, OutOfMemory } || Path.ParseError;
    const TestRouter = Router(TestResponse, TestCtx, TestErr);

    var notfoundinvoked = false;
    fn notfound(_: TestResponse, _: TestCtx) TestErr!void {
        notfoundinvoked = true;
    }
    var route1invoked = false;
    var route1params: ?Params = null;
    fn route1(_: TestResponse, _: TestCtx, p: Params) TestErr!void {
        route1invoked = true;
        route1params = try p.clone();
    }
    var route2invoked = false;
    fn route2(_: TestResponse, _: TestCtx, _: Params) TestErr!void {
        route2invoked = true;
    }
    fn reset() void {
        notfoundinvoked = false;
        if (route1params != null) route1params.?.deinit();
        route1params = null;
        route1invoked = false;
        route2invoked = false;
    }

    fn runTestRouter(handlers: []TestRouter.Handler, target: []const u8) !void {
        const ctx = TestCtx{};
        const req = TestRequest{
            .method = .GET,
            .target = target,
        };
        const res = TestResponse{
            .request = req,
        };
        const router = TestRouter{
            .allocator = std.testing.allocator,
            .handlers = handlers,
            .notfound = notfound,
        };
        try router.handle(res, ctx);
    }

    // fn hmof(x: []const u8, y: []const u8) std.StringHashMap([]const u8) {
    //     var hm = std.StringHashMap([]const u8).init(std.testing.allocator);
    //     hm.put(x, y) catch @panic("failed to create hmof in test");
    //     return hm;
    // }

    const TestCase = struct {
        target: []const u8,
        route1: ?[]const u8 = null,
        route2: ?[]const u8 = null,
        notfoundexpected: bool = false,
        route1expected: bool = false,
        route2expected: bool = false,
        // route1paramsexpected: ?Params = null,
    };

    fn expectEqual(maybe_pexp: ?Params, maybe_pact: ?Params) !void {
        if (maybe_pexp == null and maybe_pact == null) {
            // fine
        } else if (maybe_pexp == null or maybe_pact == null) {
            std.debug.print("isnull(pexp) = {} isnull(pact) = {}", .{ maybe_pexp == null, maybe_pact == null });
            return error.TestUnexpectedResult;
        } else {
            const pexp = maybe_pexp.?;
            const pact = maybe_pact.?;
            try std.testing.expectEqual(pexp.count(), pact.count());
            var it = pexp.keyIterator();
            var kexp = it.next();
            while (kexp != null) : (kexp = it.next()) {
                const vexp = pexp.get(kexp.?.*).?;
                const maybe_vact = pact.get(kexp.?.*);
                if (maybe_vact) |vact| {
                    std.debug.print("{s} {s}", .{ vexp, vact });
                    try std.testing.expectEqual(vexp, vact);
                } else {
                    std.debug.print("expected key {s} not found in actual", .{kexp.?.*});
                    return error.TestUnexpectedResult;
                }
            }
        }
    }

    test "router tests" {
        // var m0 = std.StringHashMap([]const u8).init(std.testing.allocator);
        // defer m0.deinit();
        // var m1 = hmof("var", "bam");
        // defer m1.deinit();
        const cases = [_]TestCase{
            .{
                .target = "/",
                .notfoundexpected = true,
            },
            .{
                .target = "/",
                .route1 = "/",
                .route1expected = true,
                // .route1paramsexpected = m0,
            },
            .{
                .target = "/foo",
                .route1 = "/bar",
                .notfoundexpected = true,
            },
            .{
                .target = "/bar",
                .route1 = "/foo",
                .route2 = "/bar",
                .route2expected = true,
            },
            .{
                .target = "/baz",
                .route1 = "/",
                .notfoundexpected = true,
            },
            .{
                .target = "/baz",
                .route1 = "/*",
                .route2 = "/bar",
                .route1expected = true,
                // .route1paramsexpected = m0,
            },
            .{
                .target = "/baz",
                .route1 = "/*",
                .route2 = "/baz",
                .route1expected = true, // first matching route takes prio
                // .route1paramsexpected = m0,
            },
            .{
                .target = "/baz",
                .route1 = "/baz",
                .route2 = "/*",
                .route1expected = true, // first matching route takes prio
                // .route1paramsexpected = m0,
            },
            .{
                .target = "/baz/bam",
                .route1 = "/baz/{var}",
                .route1expected = true,
                // .route1paramsexpected = m1,
            },
            .{
                .target = "/baz/bam/boo",
                .route1 = "/baz/{var}/boo",
                .route1expected = true,
                // .route1paramsexpected = m1,
            },
            .{
                .target = "/baz/bam/boo?somequery=foo",
                .route1 = "/baz/{var}/boo",
                .route1expected = true,
                // .route1paramsexpected = m1,
            },
            // .{
            //     .target = "/baz/bam/bar",
            //     .route1 = "/baz/{var}/boo",
            //     .notfoundexpected = true,
            // },
        };

        for (cases) |case| {
            defer reset();
            var handlers = std.ArrayList(TestRouter.Handler).init(std.testing.allocator);
            defer handlers.deinit();
            if (case.route1) |r1| {
                try handlers.append(TestRouter.Handler{ .pattern = r1, .method = .GET, .handle_fn = route1 });
            }
            if (case.route2) |r2| {
                try handlers.append(TestRouter.Handler{ .pattern = r2, .method = .GET, .handle_fn = route2 });
            }
            try runTestRouter(handlers.items, case.target);
            try std.testing.expectEqual(case.notfoundexpected, notfoundinvoked);
            try std.testing.expectEqual(case.route1expected, route1invoked);
            try std.testing.expectEqual(case.route2expected, route2invoked);
            // try expectEqual(case.route1paramsexpected, route1params); // TODO assert captures
        }
    }
};

/// HTTP path parsing
/// which is a subset of URI parsing :)
/// RFC-3986
pub const Path = struct {
    allocator: std.mem.Allocator,
    path: []const u8,
    query: []const u8,
    fragment: []const u8, // technically I think the fragment is never received on the server anyway
    query_parsed: ?Form = null,

    pub const ParseError = error{Malformatted} || Form.ParseError;

    pub fn parse(allocator: std.mem.Allocator, str: []const u8) ParseError!Path {
        var path: []const u8 = str;
        var query: []const u8 = "";
        var fragment: []const u8 = "";
        const f_ix = std.mem.indexOfScalar(u8, str, '#');
        const q_ix = std.mem.indexOfScalar(u8, str, '?');
        if (q_ix) |q| {
            path = str[0..q];
            if (f_ix) |f| {
                if (f < q) {
                    return ParseError.Malformatted;
                }
                query = str[(q + 1)..f];
                fragment = str[(f + 1)..];
            } else {
                query = str[(q + 1)..];
            }
        } else if (f_ix) |f| {
            path = str[0..f];
            fragment = str[(f + 1)..];
        }
        return Path{
            .allocator = allocator,
            .path = path,
            .query = query,
            .fragment = fragment,
        };
    }

    pub fn get_query_param(self: *Path, key: []const u8) !?[]const u8 {
        if (self.query_parsed == null) {
            self.query_parsed = try Form.parse(self.allocator, self.query);
        }
        return self.query_parsed.?.data.get(key);
    }

    pub fn query_to_struct(self: *Path, comptime T: type) !T {
        if (self.query_parsed == null) {
            self.query_parsed = try Form.parse(self.allocator, self.query);
        }
        return self.query_parsed.?.form_to_struct(T);
    }

    pub fn deinit(self: *Path) void {
        if (self.query_parsed != null) {
            self.query_parsed.?.deinit();
        }
    }
};

const PathTest = struct {
    test "path" {
        var p = try Path.parse(std.testing.allocator, "/");
        defer p.deinit();
        try assertPath("/", "", "", p);
    }

    fn assertPath(path: []const u8, query: []const u8, fragment: []const u8, actual: Path) !void {
        try std.testing.expectEqualSlices(u8, path, actual.path);
        try std.testing.expectEqualSlices(u8, query, actual.query);
        try std.testing.expectEqualSlices(u8, fragment, actual.fragment);
    }

    test "query" {
        var p = try Path.parse(std.testing.allocator, "/foo?bar=baz");
        defer p.deinit();
        try assertPath("/foo", "bar=baz", "", p);
    }

    test "query and fragment" {
        var p = try Path.parse(std.testing.allocator, "/foo?bar=baz#frag");
        defer p.deinit();
        try assertPath("/foo", "bar=baz", "frag", p);
    }

    test "fragment" {
        var p = try Path.parse(std.testing.allocator, "/foo#frag");
        defer p.deinit();
        try assertPath("/foo", "", "frag", p);
    }

    test "query param" {
        var p = try Path.parse(std.testing.allocator, "/foo?bar=baz#frag");
        defer p.deinit();
        const v1 = try p.get_query_param("bar");
        try std.testing.expect(v1 != null);
        try std.testing.expectEqualSlices(u8, "baz", v1.?);
        const v2 = try p.get_query_param("bam");
        try std.testing.expect(v2 == null);
    }

    test "query param mixed" {
        var p = try Path.parse(std.testing.allocator, "/foo?bar=baz&ba+m=bo+om&zigzag#frag");
        defer p.deinit();
        try assertPath("/foo", "bar=baz&ba+m=bo+om&zigzag", "frag", p);
        const v1 = try p.get_query_param("bar");
        try std.testing.expect(v1 != null);
        try std.testing.expectEqualSlices(u8, "baz", v1.?);
        const v2 = try p.get_query_param("ba m");
        try std.testing.expect(v2 != null);
        try std.testing.expectEqualSlices(u8, "bo om", v2.?);
    }

    test "query to struct" {
        var p = try Path.parse(std.testing.allocator, "/foo?bar=ba+z&bam=55&zigzag#frag");
        defer p.deinit();
        const T = struct {
            bar: []const u8,
            bam: u64,
            fn deinit(self: *@This()) void {
                std.testing.allocator.free(self.bar);
            }
        };
        var t = try p.query_to_struct(T);
        defer t.deinit();
        try std.testing.expectEqualDeep(T{ .bar = "ba z", .bam = 55 }, t);
    }
};

pub const Form = struct {
    allocator: std.mem.Allocator,
    data: std.StringHashMap([]const u8),

    const ParseError = error{ Malformatted, InvalidLength, InvalidCharacter, NoSpaceLeft } || std.mem.Allocator.Error;

    // Tries to parse key=value&key2=value2 pairs from the form.
    // Note that a URL query segment doesn't _have_ to be key-value pairs
    // so this is quite lenient.
    // Form struct owns all the keys and values in the resulting map.
    pub fn parse(allocator: std.mem.Allocator, form: []const u8) ParseError!Form {
        var res = std.StringHashMap([]const u8).init(allocator);
        var iter1 = std.mem.splitScalar(u8, form, '&');
        while (iter1.next()) |split| {
            var iter2 = std.mem.splitScalar(u8, split, '=');
            if (iter2.next()) |key| {
                if (iter2.next()) |value| {
                    try res.put(try percent_decode(allocator, key), try percent_decode(allocator, value));
                } else {
                    // Do nothing, it's a well-formatted kv pair
                }
            } else {
                // Do nothing it's not a well-formatted kv pair
            }
        }
        return Form{ .allocator = allocator, .data = res };
    }
    pub fn form_to_struct(self: *Form, comptime T: type) !T {
        return to_struct(self.allocator, T, self.data);
    }
    pub fn deinit(self: *Form) void {
        var it = self.data.iterator();
        var e = it.next();
        while (e != null) : (e = it.next()) {
            self.allocator.free(e.?.key_ptr.*);
            self.allocator.free(e.?.value_ptr.*);
        }
        self.data.deinit();
    }
};

fn percent_decode(allocator: std.mem.Allocator, str: []const u8) ![]const u8 {
    var fbs = std.io.fixedBufferStream(str);
    var rdr = fbs.reader();
    var out = std.ArrayList(u8).init(allocator);
    var wtr = out.writer();
    defer out.deinit();
    while (true) {
        const b = rdr.readByte() catch break;
        if (b == '%') {
            var hex_code: [2]u8 = undefined;
            _ = try rdr.readAll(&hex_code);
            var b2: [1]u8 = .{0};
            _ = try std.fmt.hexToBytes(&b2, &hex_code);
            try wtr.writeByte(b2[0]);
        } else if (b == '+') {
            try wtr.writeByte(' ');
        } else {
            try wtr.writeByte(b);
        }
    }
    return out.toOwnedSlice();
}

const PercentEncodeTest = struct {
    test "decode" {
        const decoded = try percent_decode(std.testing.allocator, "%C3%A7%C3%AE%C4%85%C3%B5+hithere");
        defer std.testing.allocator.free(decoded);
        try std.testing.expectEqualStrings("çîąõ hithere", decoded);
    }
};

/// Populate a struct from a hashmap
fn to_struct(allocator: std.mem.Allocator, comptime T: type, hm: std.StringHashMap([]const u8)) !T {
    const ti = @typeInfo(T);
    if (ti != .Struct) {
        @compileError("to_struct T was not a struct type");
    }
    var t: T = undefined;
    inline for (ti.Struct.fields) |field| {
        if (field.is_comptime) {
            @compileError("can't dynamically set comptime field " ++ field.name);
        }
        const value: []const u8 = hm.get(field.name) orelse {
            return error.FieldNotPresent; // TODO somehow be more useful.
        };
        switch (@typeInfo(field.type)) { // TODO handle more types, default values etc etc.
            .Int => {
                @field(t, field.name) = try std.fmt.parseInt(field.type, value, 10);
            },
            .Pointer => |ptrinfo| {
                if (ptrinfo.size != .Slice) {
                    @compileError("field pointer size " ++ @tagName(ptrinfo.size) ++ " is not supported, only []u8 is supported right now");
                }
                if (ptrinfo.child != u8) {
                    @compileError("field pointer type " ++ @tagName(@typeInfo(ptrinfo.child)) ++ " is not supported, only []u8 is supported right now");
                }
                const dvalue = try allocator.dupe(u8, value);
                errdefer allocator.free(dvalue);
                @field(t, field.name) = dvalue;
            },
            else => @compileError("field type " ++ @tagName(@typeInfo(field.type)) ++ " not supported on field " ++ field.name),
        }
    }
    return t;
}

const StructTest = struct {
    test "to struct" {
        const T = struct {
            foo: i64,
            bar: []const u8,
            pub fn deinit(self: *@This()) void {
                std.testing.allocator.free(self.bar);
            }
        };
        var hm = std.StringHashMap([]const u8).init(std.testing.allocator);
        defer hm.deinit();
        try hm.put("foo", "42");
        try hm.put("bar", "oops");
        var t = try to_struct(std.testing.allocator, T, hm);
        defer t.deinit();
        try std.testing.expectEqualDeep(T{ .foo = 42, .bar = "oops" }, t);
    }
};

test {
    _ = RouterTest;
    _ = PathTest;
    _ = PercentEncodeTest;
    _ = StructTest;
}