From a7f9d581b92b3a001cbb634e1f99dfcbe0fdc53c Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 12 Apr 2021 23:57:45 -0300 Subject: [PATCH] dupe strings given by hzzp into owned allocator --- src/main.zig | 54 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/main.zig b/src/main.zig index ac19d7a..1973d8c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,6 +3,8 @@ const http = @import("apple_pie"); const hzzp = @import("hzzp"); const mimetypes = @import("mimetypes"); +const fmt = std.fmt; + const images_dir_path = "./images"; var registry: mimetypes.Registry = undefined; @@ -58,14 +60,54 @@ fn generateImageId(buffer: []u8) []const u8 { const StreamT = std.io.FixedBufferStream([]const u8); const ContentDisposition = struct { + allocator: *std.mem.Allocator, name: []const u8, filename: []const u8, + + const Self = @This(); + + pub fn deinit(self: *Self) void { + self.allocator.free(self.name); + self.allocator.free(self.filename); + } + + pub fn format(self: Self, comptime f: []const u8, options: fmt.FormatOptions, writer: anytype) !void { + if (f.len != 0) { + @compileError("Unknown format character: '" ++ f ++ "'"); + } + + return fmt.format( + writer, + "Disposition{{.name='{s}', .filename='{s}'}}", + .{ self.name, self.filename }, + ); + } }; const Part = struct { + allocator: *std.mem.Allocator, disposition: ContentDisposition, content_type: []const u8, body: []const u8, + + const Self = @This(); + + pub fn deinit(self: *Self) void { + self.disposition.deinit(); + self.allocator.free(self.content_type); + } + + pub fn format(self: Self, comptime f: []const u8, options: fmt.FormatOptions, writer: anytype) !void { + if (f.len != 0) { + @compileError("Unknown format character: '" ++ f ++ "'"); + } + + return fmt.format( + writer, + "Part{{.content_type='{s}', .disposition={}, .body='{s}'}}", + .{ self.content_type, self.disposition, self.body }, + ); + } }; const Multipart = struct { @@ -103,7 +145,7 @@ const Multipart = struct { }; } - pub fn next(self: *Self, hzzp_buffer: []u8) !?Part { + pub fn next(self: *Self, hzzp_buffer: []u8, allocator: *std.mem.Allocator) !?Part { var reader = self.stream.reader(); // first self.boundary.len+2 bytes MUST be boundary + \r + \n var boundary_buffer: [512]u8 = undefined; @@ -162,12 +204,13 @@ const Multipart = struct { } content_disposition = ContentDisposition{ - .name = dispo_name, - .filename = dispo_filename, + .allocator = allocator, + .name = try std.mem.dupe(allocator, u8, dispo_name), + .filename = try std.mem.dupe(allocator, u8, dispo_filename), }; std.log.debug("got content disposition for part! {}", .{content_disposition}); } else if (std.mem.eql(u8, header.name, "Content-Type")) { - content_type = header.value; + content_type = try std.mem.dupe(allocator, u8, header.value); std.log.debug("got content type for part! {s}", .{content_type}); } }, @@ -193,7 +236,10 @@ const Multipart = struct { const boundary_end_marker = try std.fmt.bufPrint(&end_boundary_buf, "{s}--\r\n", .{self.boundary}); const actual_body = std.mem.trim(u8, remaining_body, boundary_end_marker); + std.debug.warn("ctype out of this: '{s}'", .{content_type.?}); + return Part{ + .allocator = allocator, .disposition = content_disposition.?, .content_type = content_type.?, .body = actual_body,