Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ pub fn build(b: *std.Build) void {

const tests = b.addTest(.{
.name = "tests",
.root_source_file = b.path("./src/tests.zig"),
.root_module = b.addModule("tests", .{
.root_source_file = b.path("./src/tests.zig"),
.target = target,
.optimize = optimize,
}),
});
tests.root_module.addImport("tardy", tardy);
tests.root_module.addImport("secsock", secsock);
Expand All @@ -55,15 +59,17 @@ fn add_example(
name: []const u8,
link_libc: bool,
target: std.Build.ResolvedTarget,
optimize: std.builtin.Mode,
optimize: std.builtin.OptimizeMode,
zzz_module: *std.Build.Module,
) void {
const example = b.addExecutable(.{
.name = name,
.root_source_file = b.path(b.fmt("./examples/{s}/main.zig", .{name})),
.target = target,
.optimize = optimize,
.strip = false,
.root_module = b.addModule(name, .{
.root_source_file = b.path(b.fmt("./examples/{s}/main.zig", .{name})),
.target = target,
.optimize = optimize,
.strip = false,
}),
});

if (link_libc) {
Expand Down
10 changes: 5 additions & 5 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
.name = .zzz,
.fingerprint = 0xc3273dca261a7ae0,
.version = "0.3.0",
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.1",
.dependencies = .{
.tardy = .{
.url = "git+https://github.com/tardy-org/tardy?ref=v0.3.0#cd454060f3b6006368d53c05ab96cd16c73c34de",
.hash = "tardy-0.3.0-69wrgi7PAwDFhO7m0aXae6N15s2b28VIOrnRrSHHake6",
.url = "git+https://github.com/tardy-org/tardy#d4a0f9bb2bff953617fa8fe93c89f44d70fb5f20",
.hash = "tardy-0.3.0-69wrgiz7AwB71cnrHSRYOjvv-9hFXlbLhnm5ol20XL9E",
},
.secsock = .{
.url = "git+https://github.com/tardy-org/secsock?ref=v0.1.0#263dcd630e32c7a5c7a0522a8d1fd04e39b75c24",
.hash = "secsock-0.0.0-p0qurf09AQD95s1NQF2MGpBqMmFz7cKZWibsgv_SQBAr",
.url = "git+https://github.com/gjuchault/secsock.git?ref=chore/zig-0.15-support#315a5f0ae1d62e6ebd0b5a59a8e252dc382d31cd",
.hash = "secsock-0.0.0-p0qurSdBAQC4ffMaiI6HuIj2cLKh_A1aFfxckZ0VLdLz",
},
},

Expand Down
26 changes: 14 additions & 12 deletions src/http/middlewares/compression.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,35 @@ const Next = @import("../router/middleware.zig").Next;
const Layer = @import("../router/middleware.zig").Layer;
const TypedMiddlewareFn = @import("../router/middleware.zig").TypedMiddlewareFn;

const Kind = union(enum) {
gzip: std.compress.gzip.Options,
};
const Kind = union(enum) { gzip: struct {
level: std.compress.flate.Compress.Level = .default,
} };

/// Compression Middleware.
///
/// Provides a Compression Layer for all routes under this that
/// will properly compress the body and add the proper `Content-Encoding` header.
pub fn Compression(comptime compression: Kind) Layer {
const func: TypedMiddlewareFn(void) = switch (compression) {
.gzip => |inner| struct {
.gzip => |_| struct {
fn gzip_mw(next: *Next, _: void) !Respond {
const respond = try next.run();
const response = next.context.response;
if (response.body) |body| if (respond == .standard) {
var compressed = try std.ArrayListUnmanaged(u8).initCapacity(next.context.allocator, body.len);
errdefer compressed.deinit(next.context.allocator);
var compressed = try std.Io.Writer.Allocating.initCapacity(next.context.allocator, body.len);
errdefer compressed.deinit();

var body_stream = std.io.fixedBufferStream(body);
try std.compress.gzip.compress(
body_stream.reader(),
compressed.writer(next.context.allocator),
inner,
var compress = std.compress.flate.Compress.init(
&compressed.writer,
&.{},
.{ .level = compression.gzip.level, .container = .gzip },
);
try compress.writer.writeAll(body);
try compress.writer.flush();

try response.headers.put("Content-Encoding", "gzip");
response.body = try compressed.toOwnedSlice(next.context.allocator);
var compressed_list = compressed.toArrayList();
response.body = try compressed_list.toOwnedSlice(next.context.allocator);
return .standard;
};

Expand Down
2 changes: 1 addition & 1 deletion src/http/router/fs_dir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub const FsDir = struct {
response.status = .OK;
response.mime = mime;

try response.headers_into_writer(ctx.header_buffer.writer(), stat.size);
try response.headers_into_writer(ctx.header_buffer.writer(ctx.allocator), stat.size);
const headers = ctx.header_buffer.items;
const length = try ctx.socket.send_all(ctx.runtime, headers);
if (headers.len != length) return error.SendingHeadersFailed;
Expand Down
12 changes: 6 additions & 6 deletions src/http/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub const Server = struct {
provision.zc_recv_buffer = ZeroCopy(u8).init(rt.allocator, config.socket_buffer_bytes) catch {
@panic("attempting to allocate more memory than available. (ZeroCopyBuffer)");
};
provision.header_buffer = std.ArrayList(u8).init(rt.allocator);
provision.header_buffer = std.ArrayList(u8).empty;
provision.arena = std.heap.ArenaAllocator.init(rt.allocator);
provision.captures = rt.allocator.alloc(Capture, config.capture_count_max) catch {
@panic("attempting to allocate more memory than available. (Captures)");
Expand Down Expand Up @@ -287,7 +287,7 @@ pub const Server = struct {
},
);

log.info("rt{d} - \"{s} {s}\" {s} ({})", .{
log.info("rt{d} - \"{s} {s}\" {s} ({f})", .{
rt.id,
@tagName(provision.request.method.?),
provision.request.uri.?,
Expand Down Expand Up @@ -377,7 +377,7 @@ pub const Server = struct {
};

const next_respond: Respond = next.run() catch |e| blk: {
log.warn("rt{d} - \"{s} {s}\" {} ({})", .{
log.warn("rt{d} - \"{s} {s}\" {} ({f})", .{
rt.id,
@tagName(provision.request.method.?),
provision.request.uri.?,
Expand Down Expand Up @@ -423,7 +423,7 @@ pub const Server = struct {
const body = provision.response.body orelse "";
const content_length = body.len;

try provision.response.headers_into_writer(provision.header_buffer.writer(), content_length);
try provision.response.headers_into_writer(provision.header_buffer.writer(rt.allocator), content_length);
const headers = provision.header_buffer.items;

var sent: usize = 0;
Expand Down Expand Up @@ -455,7 +455,7 @@ pub const Server = struct {
},
};

log.info("connection ({}) closed", .{secure.socket.addr});
log.info("connection ({f}) closed", .{secure.socket.addr});

if (!accept_queued.*) {
try rt.spawn(
Expand Down Expand Up @@ -505,7 +505,7 @@ pub const Server = struct {
) catch {
@panic("attempting to allocate more memory than available. (ZeroCopy)");
};
provision.header_buffer = std.ArrayList(u8).init(rt.allocator);
provision.header_buffer = std.ArrayList(u8).empty;
provision.arena = std.heap.ArenaAllocator.init(rt.allocator);
provision.captures = rt.allocator.alloc(Capture, self.config.capture_count_max) catch {
@panic("attempting to allocate more memory than available. (Captures)");
Expand Down
2 changes: 1 addition & 1 deletion src/http/sse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub const SSE = struct {
var list = try std.ArrayListUnmanaged(u8).initCapacity(ctx.allocator, 0);
errdefer list.deinit(ctx.allocator);

try ctx.response.headers_into_writer(ctx.header_buffer.writer(), null);
try ctx.response.headers_into_writer(ctx.header_buffer.writer(ctx.allocator), null);
const headers = ctx.header_buffer.items;

const sent = try ctx.socket.send_all(ctx.runtime, headers);
Expand Down