diff options
author | zachir <zachir@librem.one> | 2024-03-23 17:57:56 -0500 |
---|---|---|
committer | zachir <zachir@librem.one> | 2024-03-23 17:57:56 -0500 |
commit | 0c165e5908d86e46bce3a331a2e3c5239c3ac1de (patch) | |
tree | d2675a4cdb1f475214c16fe98bc15e7b0de7340f /src | |
parent | 7272ebfa4fd495b8d52b758c5564f9944a51e3f2 (diff) |
Add better error handling for more status codes
This redoes a lot of the error handling so we can add in more status
codes.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.zig | 80 |
1 files changed, 49 insertions, 31 deletions
diff --git a/src/main.zig b/src/main.zig index 1cf65e3..47d7f7e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -26,34 +26,24 @@ fn read_files(target: []const u8, buffer: []u8, allocator: mem.Allocator) !usize file_path = try fmt.allocPrint(allocator, "{s}{s}", .{ cwd, target }); log.info("Loading file {s}...", .{file_path}); // Determine if the requested file exists - if (fs.cwd().openFile(file_path, .{})) |file| { - defer file.close(); - var stat = try file.stat(); - switch (stat.kind) { - .directory => { - if (mem.endsWith(u8, target, "/")) { - file_path = try fmt.allocPrint(allocator, "{s}index.html", .{target}); - } else { - file_path = try fmt.allocPrint(allocator, "{s}/index.html", .{target}); - } - return read_files(file_path, buffer, allocator); - }, - .file => { - return try file.readAll(buffer); - }, - else => { - return 0; - }, - } - } else |err| { - switch (err) { - error.FileNotFound => { - return 0; - }, - else => { - return err; - }, - } + const file = try fs.cwd().openFile(file_path, .{}); + defer file.close(); + var stat = try file.stat(); + switch (stat.kind) { + .directory => { + if (mem.endsWith(u8, target, "/")) { + file_path = try fmt.allocPrint(allocator, "{s}index.html", .{target}); + } else { + file_path = try fmt.allocPrint(allocator, "{s}/index.html", .{target}); + } + return read_files(file_path, buffer, allocator); + }, + .file => { + return try file.readAll(buffer); + }, + else => { + return 0; + }, } } @@ -69,8 +59,31 @@ fn handle_request(response: *http.Server.Response, allocator: mem.Allocator) !vo try response.headers.append("connection", "keep-alive"); } - const size = try read_files(response.request.target, &read, allocator); - if (size > 0) { + const size = read_files(response.request.target, &read, allocator) catch |err| { + switch (err) { + error.AccessDenied => { + response.status = .forbidden; + }, + error.FileNotFound => { + response.status = .not_found; + }, + error.OutOfMemory => { + response.status = .uri_too_long; + }, + else => { + return err; + }, + } + response.transfer_encoding = .{ .content_length = 0 }; + try response.do(); + return; + }; + if (size >= BUFFER_LIMIT) { + response.status = .payload_too_large; + response.transfer_encoding = .{ .content_length = 0 }; + try response.do(); + return; + } else if (size > 0) { // Get the file extension, and set the content-type header if it exists // (using mime.zig) // To do this, we iterate through response.request.target in reverse @@ -155,7 +168,12 @@ fn run_server(server: *http.Server, allocator: mem.Allocator) !void { }; // Process the request - try handle_request(&response, allocator); + handle_request(&response, allocator) catch |err| { + response.status = .internal_server_error; + response.transfer_encoding = .{ .content_length = 0 }; + try response.do(); + return err; + }; } } } |