From 1a3c1e354fe688f7bafb6ff283336b9f075ae7ad Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 10 Apr 2021 15:29:27 -0300 Subject: [PATCH] multipart: add body reading --- src/main.zig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main.zig b/src/main.zig index e2c9e40..5fe9a07 100644 --- a/src/main.zig +++ b/src/main.zig @@ -58,6 +58,7 @@ const ContentDisposition = struct { const Part = struct { disposition: ContentDisposition, content_type: []const u8, + body: []const u8, }; const Multipart = struct { @@ -170,9 +171,25 @@ const Multipart = struct { } } + // the rest of the reader until we find a matching boundary is the part body. + // hzzp does not do it for us because it cant find a body encoding + // (content-length, content-encoding) + // + // we can use the fact that we know the reader is FixedBufferStream + // to extract the remaining body, then trim the boundary! + // + // THIS ASSUMES ONLY ONE FILE IS IN THE WIRE. + + const remaining_body = self.stream.buffer[self.stream.pos..self.stream.buffer.len]; + + var end_boundary_buf: [512]u8 = undefined; + 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); + return Part{ .disposition = content_disposition.?, .content_type = content_type.?, + .body = actual_body, }; } };