Skip to content

Commit ba6701d

Browse files
karlseguinsjorsdonkers
authored andcommitted
Don't emit incorrect empty chunk
When we only have 1 or 2 bytes missing from a chunk (i.e. the tailing \n or \r\n), don't emit an empty chunk if we have more data available to process.
1 parent a8fcee0 commit ba6701d

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/http/client.zig

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,24 +1342,38 @@ const Reader = struct {
13421342

13431343
const size = self.size.?;
13441344
const missing = self.missing;
1345+
13451346
if (data.len >= missing) {
1346-
// we have a complete chunk;
1347-
var chunk: ?[]u8 = data;
1347+
self.size = null;
1348+
self.missing = 0;
13481349
if (missing == 1) {
1349-
const last = missing - 1;
1350-
if (data[last] != '\n') {
1350+
if (data[0] != '\n') {
13511351
return error.InvalidChunk;
13521352
}
1353-
chunk = null;
1354-
} else {
1355-
const last = missing - 2;
1356-
if (data[last] != '\r' or data[missing - 1] != '\n') {
1353+
if (data.len == 1) {
1354+
return .{ true, .{ .data = null, .done = size == 0, .unprocessed = null } };
1355+
}
1356+
return self.process(data[1..]);
1357+
}
1358+
1359+
if (missing == 2) {
1360+
if (data[0] != '\r' or data[1] != '\n') {
13571361
return error.InvalidChunk;
13581362
}
1359-
chunk = if (last == 0) null else data[0..last];
1363+
1364+
if (data.len == 2) {
1365+
return .{ true, .{ .data = null, .done = size == 0, .unprocessed = null } };
1366+
}
1367+
return self.process(data[2..]);
13601368
}
1361-
self.size = null;
1362-
self.missing = 0;
1369+
1370+
// we have a complete chunk;
1371+
var chunk: ?[]u8 = data;
1372+
const last = missing - 2;
1373+
if (data[last] != '\r' or data[missing - 1] != '\n') {
1374+
return error.InvalidChunk;
1375+
}
1376+
chunk = if (last == 0) null else data[0..last];
13631377

13641378
const unprocessed = data[missing..];
13651379

0 commit comments

Comments
 (0)