zigwebserver

Tools for writing web stuff in zig
Log | Files | Refs

main.zig (2638B)


      1 const std = @import("std");
      2 const zws = @import("zigwebserver.zig");
      3 
      4 // extremely basic http file server
      5 const Context = struct {
      6     pub fn clone(_: Context) Context {
      7         return .{};
      8     }
      9     pub fn deinit(_: Context) void {}
     10 };
     11 const Handler = struct {
     12     pub fn handle(_: Handler, res: *std.http.Server.Response, _: Context) !void {
     13         var p = try zws.Path.parse(res.allocator, res.request.target);
     14         defer p.deinit();
     15         const path = try std.fs.path.join(res.allocator, &[_][]const u8{ ".", p.path });
     16         defer res.allocator.free(path);
     17         if (std.fs.cwd().openFile(path, .{})) |file| {
     18             const md = try file.metadata();
     19             if (md.kind() == .directory) {
     20                 const index_path = try std.fs.path.join(res.allocator, &[_][]const u8{ path, "index.html" });
     21                 defer res.allocator.free(index_path);
     22                 if (std.fs.cwd().openFile(index_path, .{})) |index_file| {
     23                     const index_md = try index_file.metadata();
     24                     try serve_file(res, index_file, index_md);
     25                 } else |_| {
     26                     try serve_error(res, .not_found);
     27                 }
     28             } else {
     29                 try serve_file(res, file, md);
     30             }
     31         } else |err| {
     32             switch (err) {
     33                 error.FileNotFound => try serve_error(res, .not_found),
     34                 else => try serve_error(res, .bad_request),
     35             }
     36         }
     37         try res.finish();
     38     }
     39 
     40     fn serve_file(res: *std.http.Server.Response, file: std.fs.File, md: std.fs.File.Metadata) !void {
     41         res.transfer_encoding = .{ .content_length = md.size() };
     42         try res.send();
     43         var buf = [_]u8{0} ** 1024;
     44         while (true) {
     45             const read = try file.read(&buf);
     46             if (read == 0) break;
     47             _ = try res.writeAll(buf[0..read]);
     48         }
     49     }
     50 
     51     fn serve_error(res: *std.http.Server.Response, status: std.http.Status) !void {
     52         res.status = status;
     53         res.transfer_encoding = .chunked;
     54         try res.send();
     55         const phrase = status.phrase() orelse "error!";
     56         try std.fmt.format(res.writer(),
     57             \\ <!doctype html><html><body>{s}</body></html>
     58         , .{phrase});
     59     }
     60 };
     61 const Server = zws.Server(Context, Handler);
     62 var allocator = std.heap.GeneralPurposeAllocator(.{}){};
     63 var svr = Server{
     64     .allocator = allocator.allocator(),
     65     .address = std.net.Address{ .in = std.net.Ip4Address.init(.{ 127, 0, 0, 1 }, 8080) },
     66     .context = Context{},
     67     .handler = Handler{},
     68 };
     69 
     70 pub fn main() !void {
     71     try svr.serve();
     72 }
     73 
     74 test {
     75     _ = zws;
     76 }