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
|
// 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 net = std.net;
const os = std.os;
const fs = std.fs;
const heap = std.heap;
const SYS_sendfile = 40;
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 mem: [512]u8 = undefined;
const allocator = &heap.FixedBufferAllocator.init(mem[0..mem.len]).allocator;
var f = try fs.cwd().openFile("index.html", .{});
var stat = try f.stat();
const hdr = std.fmt.allocPrint(
allocator,
"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);
_ = sendfile(conn.file.handle, f.handle, 0, stat.size);
conn.file.close();
os.exit(0);
},
else => blk: {
conn.file.close();
},
}
}
}
fn sendfile(outfd: i32, infd: i32, offset: u64, count: usize) usize {
return syscall4(SYS_sendfile, @bitCast(usize, @as(isize, outfd)), @bitCast(usize, @as(isize, infd)), offset, count);
}
fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3),
[arg4] "{r10}" (arg4)
: "rcx", "r11", "memory"
);
}
|