summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzachir <zachir@librem.one>2024-03-26 00:50:22 -0500
committerzachir <zachir@librem.one>2024-03-26 00:50:22 -0500
commitd033325a449f752588ed459dba57da6742f79676 (patch)
treeef6c417a82f6e75948483844c6b71e43236c0bb8
parentea1bdedd6b1e93c6ea69b4746550331ffcd52a16 (diff)
Expand all modules to std.*
This is for greater clarity as to what everything is.
-rw-r--r--src/main.zig56
1 files changed, 23 insertions, 33 deletions
diff --git a/src/main.zig b/src/main.zig
index b3c1b55..e611aac 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -19,38 +19,27 @@
// Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
const std = @import("std");
+const log = std.log.scoped(.server);
/// zhttpd relies on mime in addition to std, which is included as made by
/// andrewrk at https://github.com/andrewrk/mime.git
const mime = @import("mime/mime.zig");
-const Address = std.net.Address;
-const ArrayList = std.ArrayList;
-const debug = std.debug;
-const fmt = std.fmt;
-const heap = std.heap;
-const http = std.http;
-const fs = std.fs;
-const log = std.log.scoped(.server);
-const mem = std.mem;
-const bufferedReader = std.io.bufferedReader;
-const getcwd = std.os.getcwd;
-
/// The server has a hardcoded address and port of 127.0.0.1:8080
/// The path requested for a file has the limit of fs.MAX_PATH_BYTES, which is
/// the maxmimum length for a file path in the OS
/// The maximum file size is 1 << 21, aka (1 * 2^20), which is 2 MB.
const server_addr = "127.0.0.1";
const server_port = 8080;
-const max_path_bytes = fs.MAX_PATH_BYTES;
+const max_path_bytes = std.fs.MAX_PATH_BYTES;
const buffer_limit = 1 << 21;
/// readFiles() reads the file to a provided buffer, and returns the number of
/// bytes read.
-/// readFiles() can fail from fmt.allocPrint(), fs.cwd().openFile(),
+/// readFiles() can fail from std.fmt.allocPrint(), std.fs.cwd().openFile(),
/// file.stat(), and file.readAll()
-/// fmt.allocPrint() will return an AllocPrintError
-/// fs.cwd().openFile() will return a File.OpenError
+/// std.fmt.allocPrint() will return an AllocPrintError
+/// std.fs.cwd().openFile() will return a File.OpenError
/// file.stat() will return a StatError
/// file.readAll() will return a ReadError
fn readFiles(target: []const u8, buffer: []u8) !usize {
@@ -62,19 +51,20 @@ fn readFiles(target: []const u8, buffer: []u8) !usize {
var file_path = try allocator.alloc(u8, max_path_bytes);
defer allocator.free(file_path);
var cwd_buffer = [_]u8{0} ** max_path_bytes;
- const cwd = try getcwd(&cwd_buffer);
- file_path = try fmt.allocPrint(allocator, "{s}{s}", .{ cwd, target });
+ const cwd = try std.os.getcwd(&cwd_buffer);
+ file_path = try std.fmt.allocPrint(allocator, "{s}{s}", .{ cwd, target });
+
log.info("Loading file {s}...", .{file_path});
// Determine if the requested file exists
- const file = try fs.cwd().openFile(file_path, .{});
+ const file = try std.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});
+ if (std.mem.endsWith(u8, target, "/")) {
+ file_path = try std.fmt.allocPrint(allocator, "{s}index.html", .{target});
} else {
- file_path = try fmt.allocPrint(allocator, "{s}/index.html", .{target});
+ file_path = try std.fmt.allocPrint(allocator, "{s}/index.html", .{target});
}
return readFiles(file_path, buffer);
},
@@ -102,7 +92,7 @@ fn readFiles(target: []const u8, buffer: []u8) !usize {
/// - 404 Not Found
/// - 413 Payload Too Large
/// - 414 URI Too Long
-fn handleRequest(response: *http.Server.Response) !void {
+fn handleRequest(response: *std.http.Server.Response) !void {
// Log the request details
log.info("{s} {s} {s}", .{ @tagName(response.request.method), @tagName(response.request.version), response.request.target });
@@ -147,7 +137,7 @@ fn handleRequest(response: *http.Server.Response) !void {
// a '.' indicates a file extension
var i: usize = response.request.target.len;
var mime_type: ?mime.Type = undefined;
- if (mem.endsWith(u8, response.request.target, "/")) {
+ if (std.mem.endsWith(u8, response.request.target, "/")) {
log.warn("Dir requested, returning index.html!", .{});
try response.headers.append("content-type", "text/html");
} else {
@@ -168,7 +158,7 @@ fn handleRequest(response: *http.Server.Response) !void {
}
if (i <= 0) {
log.warn("No extension detected!", .{});
- if (mem.indexOf(u8, &read, "<html")) |_| {
+ if (std.mem.indexOf(u8, &read, "<html")) |_| {
try response.headers.append("content-type", "text/html");
} else {
try response.headers.append("content-type", "text/plain");
@@ -187,7 +177,7 @@ fn handleRequest(response: *http.Server.Response) !void {
const body = read[0..size];
log.info("{}", .{body.len});
// Check if the request target contains "?chunked"
- if (mem.indexOf(u8, response.request.target, "?chunked") != null) {
+ if (std.mem.indexOf(u8, response.request.target, "?chunked") != null) {
response.transfer_encoding = .chunked;
} else {
response.transfer_encoding = .{ .content_length = size };
@@ -214,7 +204,7 @@ fn handleRequest(response: *http.Server.Response) !void {
/// resonse.wait() will return a WaitError
/// runServer() handles the following error codes:
/// - 500 (Internal Server Error)
-fn runServer(server: *http.Server) !void {
+fn runServer(server: *std.http.Server) !void {
var fba_buffer: [buffer_limit]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&fba_buffer);
const allocator = fba.allocator();
@@ -245,7 +235,7 @@ fn runServer(server: *http.Server) !void {
}
}
-pub fn printInfo(stderr: fs.File.Writer) !void {
+pub fn printInfo(stderr: std.fs.File.Writer) !void {
try stderr.print("zhttpd version 0.1.0, Copyright (C) 2024 ZachIR\n", .{});
try stderr.print("zhttpd comes with ABSOLUTELY NO WARRANTY. This is ", .{});
try stderr.print("free software, and you are welcome to ", .{});
@@ -258,8 +248,8 @@ pub fn printInfo(stderr: fs.File.Writer) !void {
/// main() can fail exit from server.listen() and runServer(), which do not
/// specify the error types they can return.
pub fn main() !void {
- var fba_buffer: [@sizeOf(http.Server)]u8 = undefined;
- var fba = heap.FixedBufferAllocator.init(&fba_buffer);
+ var fba_buffer: [@sizeOf(std.http.Server)]u8 = undefined;
+ var fba = std.heap.FixedBufferAllocator.init(&fba_buffer);
const allocator = fba.allocator();
//const stdout = std.io.getStdOut().writer();
@@ -268,14 +258,14 @@ pub fn main() !void {
try printInfo(stderr);
// Initialize the server
- var server = http.Server.init(allocator, .{ .reuse_address = true });
+ var server = std.http.Server.init(allocator, .{ .reuse_address = true });
defer server.deinit();
// Log the server address and port
try stderr.print("Server is running at {s}:{d}\n", .{ server_addr, server_port });
// Parse the server address
- const address = Address.parseIp(server_addr, server_port) catch unreachable;
+ const address = std.net.Address.parseIp(server_addr, server_port) catch unreachable;
try server.listen(address);
// Run the server
@@ -283,7 +273,7 @@ pub fn main() !void {
// Handle server errors
log.err("server error: {}\n", .{err});
if (@errorReturnTrace()) |trace| {
- debug.dumpStackTrace(trace.*);
+ std.debug.dumpStackTrace(trace.*);
}
std.os.exit(1);
};