diff --git a/examples/benchmark/main.zig b/examples/benchmark/main.zig index b6c8cd2..ea2c648 100644 --- a/examples/benchmark/main.zig +++ b/examples/benchmark/main.zig @@ -61,7 +61,7 @@ pub fn main() !void { defer t.deinit(); var router = try Router.init(allocator, &.{ - Route.init("/").serve_embedded_file(http.Mime.HTML, @embedFile("index.html")).layer(), + Route.init("/").embed_file(.{ .mime = http.Mime.HTML }, @embedFile("index.html")).layer(), Route.init("/hi/%s").get({}, hi_handler).layer(), }, .{}); defer router.deinit(allocator); diff --git a/examples/multithread/main.zig b/examples/multithread/main.zig index bae66e0..4b4dd2c 100644 --- a/examples/multithread/main.zig +++ b/examples/multithread/main.zig @@ -81,7 +81,7 @@ pub fn main() !void { defer t.deinit(); var router = try Router.init(allocator, &.{ - Route.init("/").serve_embedded_file(http.Mime.HTML, @embedFile("index.html")).layer(), + Route.init("/").embed_file(.{ .mime = http.Mime.HTML }, @embedFile("index.html")).layer(), Route.init("/hi/%s").get({}, hi_handler).layer(), Route.init("/redirect").get({}, redir_handler).layer(), Route.init("/post").post({}, post_handler).layer(), diff --git a/examples/sse/main.zig b/examples/sse/main.zig index 2c0c186..a0e0c65 100644 --- a/examples/sse/main.zig +++ b/examples/sse/main.zig @@ -106,7 +106,7 @@ pub fn main() !void { defer broadcast.deinit(); var router = try Router.init(allocator, &.{ - Route.init("/").serve_embedded_file(http.Mime.HTML, @embedFile("index.html")).layer(), + Route.init("/").embed_file(.{ .mime = http.Mime.HTML }, @embedFile("index.html")).layer(), Route.init("/kill").get({}, kill_handler).layer(), Route.init("/stream").get({}, sse_handler).layer(), Route.init("/message").post(&broadcast, msg_handler).layer(), diff --git a/examples/tls/main.zig b/examples/tls/main.zig index d99e3f2..4298a30 100644 --- a/examples/tls/main.zig +++ b/examples/tls/main.zig @@ -49,8 +49,8 @@ pub fn main() !void { defer t.deinit(); var router = try Router.init(allocator, &.{ - Route.init("/embed/pico.min.css").serve_embedded_file( - http.Mime.CSS, + Route.init("/embed/pico.min.css").embed_file( + .{ .mime = http.Mime.CSS }, @embedFile("embed/pico.min.css"), ).layer(), diff --git a/src/http/encoding.zig b/src/http/encoding.zig new file mode 100644 index 0000000..f2b401a --- /dev/null +++ b/src/http/encoding.zig @@ -0,0 +1,7 @@ +pub const Encoding = enum { + gzip, + compress, + deflate, + br, + zstd, +}; diff --git a/src/http/lib.zig b/src/http/lib.zig index 42f1223..a883317 100644 --- a/src/http/lib.zig +++ b/src/http/lib.zig @@ -3,6 +3,7 @@ pub const Method = @import("method.zig").Method; pub const Request = @import("request.zig").Request; pub const Response = @import("response.zig").Response; pub const Mime = @import("mime.zig").Mime; +pub const Encoding = @import("encoding.zig").Encoding; pub const Date = @import("date.zig").Date; pub const Headers = @import("../core/case_string_map.zig").CaseStringMap([]const u8); diff --git a/src/http/router/route.zig b/src/http/router/route.zig index c91e11d..aa9936f 100644 --- a/src/http/router/route.zig +++ b/src/http/router/route.zig @@ -9,6 +9,7 @@ const Method = @import("../method.zig").Method; const Request = @import("../request.zig").Request; const Response = @import("../response.zig").Response; const Mime = @import("../mime.zig").Mime; +const Encoding = @import("../encoding.zig").Encoding; const FsDir = @import("fs_dir.zig").FsDir; const Context = @import("../context.zig").Context; @@ -166,10 +167,17 @@ pub const Route = struct { return inner_route(.PATCH, self, data, handler_fn); } + const ServeEmbeddedOptions = struct { + /// If you are serving a compressed file, please + /// set the correct encoding type. + encoding: ?Encoding = null, + mime: ?Mime = null, + }; + /// Define a GET handler to serve an embedded file. - pub fn serve_embedded_file( + pub fn embed_file( self: *const Self, - comptime mime: ?Mime, + comptime opts: ServeEmbeddedOptions, comptime bytes: []const u8, ) Self { return self.get({}, struct { @@ -205,9 +213,16 @@ pub const Route = struct { } } + if (opts.encoding) |encoding| { + ctx.response.headers.put_assume_capacity( + "Content-Encoding", + @tagName(encoding), + ); + } + return try ctx.respond(.{ .status = .OK, - .mime = mime, + .mime = opts.mime, .body = bytes, }); }