add support for stream end

This commit is contained in:
Luna 2021-04-13 16:20:32 -03:00
parent 28c6126ff8
commit 11079f2c4d
1 changed files with 10 additions and 1 deletions

View File

@ -149,7 +149,7 @@ const Multipart = struct {
var reader = self.stream.reader();
// first self.boundary.len+2 bytes MUST be boundary + \r + \n
var boundary_buffer: [512]u8 = undefined;
const maybe_boundary_raw = (try reader.readUntilDelimiterOrEof(&boundary_buffer, '\n')).?;
const maybe_boundary_raw = (try reader.readUntilDelimiterOrEof(&boundary_buffer, '\n')) orelse return null;
const maybe_boundary_strip1 = std.mem.trimRight(u8, maybe_boundary_raw, "\n");
const maybe_boundary_strip2 = std.mem.trimRight(u8, maybe_boundary_strip1, "\r");
@ -248,6 +248,9 @@ const Multipart = struct {
const possible_end = it.next() orelse return error.MissingNextPrefixOrEndSuffix;
if (std.mem.startsWith(u8, possible_end, "--")) {
// we just got the ending boundary marker. the reader should be disabled
// for future reads.
self.stream.pos = self.stream.buffer.len;
return Part{
.allocator = allocator,
.disposition = content_disposition.?,
@ -392,4 +395,10 @@ test "multipart" {
std.testing.expectEqualSlices(u8, "file2", part2.disposition.name);
std.testing.expectEqualSlices(u8, "data.json", part2.disposition.filename);
std.testing.expectEqualSlices(u8, PART2_REAL_BODY, part2.body);
// stop the loop (if there were any) afterwards
std.testing.expectEqual(
@as(?Part, null),
try multipart.next(&hzzp_buffer, std.testing.allocator),
);
}