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
|
const std = @import("std");
const zws = @import("zigwebserver.zig");
// extremely basic http file server
const Context = struct {
pub fn clone(_: Context) Context {
return .{};
}
pub fn deinit(_: Context) void {}
};
const Handler = struct {
pub fn handle(_: Handler, res: *std.http.Server.Response, _: Context) !void {
var p = try zws.Path.parse(res.allocator, res.request.target);
defer p.deinit();
const path = try std.fs.path.join(res.allocator, &[_][]const u8{ ".", p.path });
defer res.allocator.free(path);
if (std.fs.cwd().openFile(path, .{})) |file| {
const md = try file.metadata();
if (md.kind() == .directory) {
const index_path = try std.fs.path.join(res.allocator, &[_][]const u8{ path, "index.html" });
defer res.allocator.free(index_path);
if (std.fs.cwd().openFile(index_path, .{})) |index_file| {
const index_md = try index_file.metadata();
try serve_file(res, index_file, index_md);
} else |_| {
try serve_error(res, .not_found);
}
} else {
try serve_file(res, file, md);
}
} else |err| {
switch (err) {
error.FileNotFound => try serve_error(res, .not_found),
else => try serve_error(res, .bad_request),
}
}
try res.finish();
}
fn serve_file(res: *std.http.Server.Response, file: std.fs.File, md: std.fs.File.Metadata) !void {
res.transfer_encoding = .{ .content_length = md.size() };
try res.send();
var buf = [_]u8{0} ** 1024;
while (true) {
const read = try file.read(&buf);
if (read == 0) break;
_ = try res.writeAll(buf[0..read]);
}
}
fn serve_error(res: *std.http.Server.Response, status: std.http.Status) !void {
res.status = status;
res.transfer_encoding = .chunked;
try res.send();
const phrase = status.phrase() orelse "error!";
try std.fmt.format(res.writer(),
\\ <!doctype html><html><body>{s}</body></html>
, .{phrase});
}
};
const Server = zws.Server(Context, Handler);
var allocator = std.heap.GeneralPurposeAllocator(.{}){};
var svr = Server{
.allocator = allocator.allocator(),
.address = std.net.Address{ .in = std.net.Ip4Address.init(.{ 127, 0, 0, 1 }, 8080) },
.context = Context{},
.handler = Handler{},
};
pub fn main() !void {
try svr.serve();
}
test {
_ = zws;
}
|