summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-07-18 13:49:44 +0000
committerMartin Ashby <martin@ashbysoft.com>2023-07-18 13:49:44 +0000
commit1fd4ff689da6cdd253b13c9da8eaf00f49eb53eb (patch)
tree282ad97c2dbea825835c649151b66bc6708f429e
parent3710fda82ef98929af141feaa57ccafd9d7a7d9d (diff)
downloadzigwebserver-1fd4ff689da6cdd253b13c9da8eaf00f49eb53eb.tar.gz
zigwebserver-1fd4ff689da6cdd253b13c9da8eaf00f49eb53eb.tar.bz2
zigwebserver-1fd4ff689da6cdd253b13c9da8eaf00f49eb53eb.tar.xz
zigwebserver-1fd4ff689da6cdd253b13c9da8eaf00f49eb53eb.zip
zig fmt
-rw-r--r--src/main.zig100
1 files changed, 50 insertions, 50 deletions
diff --git a/src/main.zig b/src/main.zig
index 3739218..a37f6a0 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -2,62 +2,62 @@ const std = @import("std");
// extremely basic http file server
pub fn main() !void {
- var allocator = std.heap.GeneralPurposeAllocator(.{}){};
- defer _ = allocator.deinit();
- const alloc = allocator.allocator();
- var svr = std.http.Server.init(alloc, .{.reuse_address = true});
- defer svr.deinit();
- const addr = try std.net.Address.resolveIp("127.0.0.1", 8080);
-
- try svr.listen(addr);
- while (true) {
- var res = try svr.accept(.{.allocator = alloc });
- defer res.deinit();
- try res.wait();
- const target = res.request.target;
- const path = try std.fs.path.join(alloc, &[_][]const u8{".", target});
- defer alloc.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(alloc, &[_][]const u8{path, "index.html"});
- defer alloc.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);
+ var allocator = std.heap.GeneralPurposeAllocator(.{}){};
+ defer _ = allocator.deinit();
+ const alloc = allocator.allocator();
+ var svr = std.http.Server.init(alloc, .{ .reuse_address = true });
+ defer svr.deinit();
+ const addr = try std.net.Address.resolveIp("127.0.0.1", 8080);
+
+ try svr.listen(addr);
+ while (true) {
+ var res = try svr.accept(.{ .allocator = alloc });
+ defer res.deinit();
+ try res.wait();
+ const target = res.request.target;
+ const path = try std.fs.path.join(alloc, &[_][]const u8{ ".", target });
+ defer alloc.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(alloc, &[_][]const u8{ path, "index.html" });
+ defer alloc.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),
+ }
}
- } 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();
}
- 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.do();
- var buf = [_]u8{0} ** 1024;
- while (true) {
- const read = try file.read(&buf);
- if (read == 0) break;
- _ = try res.write(buf[0..read]);
- }
+ res.transfer_encoding = .{ .content_length = md.size() };
+ try res.do();
+ var buf = [_]u8{0} ** 1024;
+ while (true) {
+ const read = try file.read(&buf);
+ if (read == 0) break;
+ _ = try res.write(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.do();
- const phrase = status.phrase() orelse "error!";
- try std.fmt.format(res.writer(),
- \\ <!doctype html><html><body>{s}</body></html>
- , .{phrase});
+ res.status = status;
+ res.transfer_encoding = .chunked;
+ try res.do();
+ const phrase = status.phrase() orelse "error!";
+ try std.fmt.format(res.writer(),
+ \\ <!doctype html><html><body>{s}</body></html>
+ , .{phrase});
}