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
|
const std = @import("std");
const Code = enum(u16) {
OK = 250,
UNKNOWN = 404,
};
const Response = struct {
code: Code,
pass: bool,
msg: []const u8,
};
const Message = struct {
name: []const u8,
pass: []const Code,
fail: []const Code,
handle: fn (*std.Io.Writer, *std.Io.net.Server, args: anytype) anyerror!Response,
};
const Messages = union(enum(u8)) {
HELO = Message{
.name = "HELO",
.pass = .{.OK},
.fail = .{.UNKNOWN},
.handle = blk: {
const f = struct {
pub fn handle(w: *std.Io.Writer, s: *std.Io.net.Server, args: anytype) !Response {
var resp: Response = .{ .code = .OK, .pass = true, .contents = undefined };
if (args.domain) |d| {
resp.contents = d;
try w.print("{d} {s}\r\n", .{ resp.code, resp.contents });
try w.flush();
return resp;
}
try w.print("{d} ", .{resp.code});
var buf: [64]u8 = undefined;
var fw: std.Io.Writer = .fixed(&buf);
try s.socket.address.format(&fw);
const full = fw.buffered();
const host = if (std.mem.lastIndexOfScalar(u8, full, ':')) |i| full[0..i] else full;
try w.print("[{s}]\r\n", .{host});
resp.contents = host;
return resp;
}
};
break :blk f.handle;
},
},
};
|