summaryrefslogtreecommitdiff
path: root/main.zig
blob: 6c85c6dee3c7d1a352b3222496791b72a79f1e51 (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
// Copyright (c) 2020, Terin Stock
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

const std = @import("std");
const builtin = @import("builtin");
const net = std.net;
const os = std.os;
const fs = std.fs;
const heap = std.heap;

pub fn main() !void {
    var act = os.Sigaction{
        .sigaction = os.SIG_IGN,
        .mask = os.empty_sigset,
        .flags = (os.SA_SIGINFO | os.SA_RESTART | os.SA_RESETHAND),
    };
    os.sigaction(os.SIGCHLD, &act, null);

    var opt = net.StreamServer.Options{
        .kernel_backlog = 128,
        .reuse_address = false,
    };
    var srv = net.StreamServer.init(opt);
    var addr = try net.Address.parseIp6("::", 8080);

    try srv.listen(addr);

    while (true) {
        var conn = try srv.accept();
        var pid = try os.fork();
        switch (pid) {
            0 => blk: {
                var buf: [512]u8 = undefined;

                var f = try fs.cwd().openFile("index.html", .{});
                var stat = try f.stat();

                const hdr = std.fmt.bufPrint(
                    buf[0..buf.len],
                    "HTTP/1.1 200 OK\r\nServer: ziggetty\r\nConnection: closed\r\nContent-Length: {}\r\n\r\n",
                    .{stat.size},
                ) catch unreachable;
                _ = try conn.file.write(hdr);

                // TODO: remove this cast
                const size = @intCast(usize, stat.size);
                const sent = try sendfile(conn.file.handle, f.handle, null, size);
                conn.file.close();
                os.exit(0);
            },
            else => blk: {
                conn.file.close();
            },
        }
    }
}

const SendFileError = error{
    InputOutput,
    NoMem,
    Overflow,
    Unseekable,
    WouldBlock,
} || os.UnexpectedError;

fn sendfile(outfd: os.fd_t, infd: os.fd_t, offset: ?*i64, count: usize) SendFileError!usize {
    while (true) {
        var rc: usize = undefined;
        var err: usize = undefined;
        rc = os.linux.sendfile(outfd, infd, offset, count);
        err = os.errno(rc);

        switch (err) {
            0 => return @intCast(usize, rc),
            else => return os.unexpectedErrno(err),

            os.EBADF => unreachable,
            os.EINVAL => unreachable,
            os.EFAULT => unreachable,
            os.EAGAIN => if (std.event.Loop.instance) |loop| {
                loop.waitUntilFdWritable(outfd);
                continue;
            } else {
                return error.WouldBlock;
            },
            os.EIO => return error.InputOutput,
            os.ENOMEM => return error.NoMem,
            os.EOVERFLOW => return error.Overflow,
            os.ESPIPE => return error.Unseekable,
        }
    }
}