Comsume whole body at once

This commit is contained in:
2025-07-22 20:22:52 +02:00
parent ad63456230
commit 0b4129a8cf

View File

@@ -78,12 +78,29 @@ pub fn init() Parser {
} }
pub fn consume(self: *Parser, chars: []const u8) Error!ConsumeResult { pub fn consume(self: *Parser, chars: []const u8) Error!ConsumeResult {
for (chars, 1..) |*c_ptr, len| { var i: usize = 0;
const res = try self.consumeChar(c_ptr); while (i < chars.len) {
switch (self.state) {
.body => |body| {
const to_consume = @min(chars.len - i, self.content_length - body.len);
const new_body = body.ptr[0 .. body.len + to_consume];
self.state = .{ .body = new_body };
i += to_consume;
return .{
.consumed = i,
.done = new_body.len >= self.content_length,
};
},
else => {
const res = try self.consumeChar(&chars[i]);
i += 1;
if (res == .done) return .{ if (res == .done) return .{
.consumed = len, .consumed = i,
.done = true, .done = true,
}; };
},
}
} }
return .{ return .{
@@ -287,7 +304,6 @@ pub fn consumeChar(self: *Parser, c_ptr: *const u8) Error!ConsumeCharResult {
const new_body = body.ptr[0 .. body.len + 1]; const new_body = body.ptr[0 .. body.len + 1];
self.state = .{ .body = new_body }; self.state = .{ .body = new_body };
if (new_body.len >= self.content_length) { if (new_body.len >= self.content_length) {
log.debug("End of request ({} body bytes)", .{new_body.len});
return .done; return .done;
} }
}, },