Comsume whole body at once
This commit is contained in:
@@ -78,12 +78,29 @@ pub fn init() Parser {
|
||||
}
|
||||
|
||||
pub fn consume(self: *Parser, chars: []const u8) Error!ConsumeResult {
|
||||
for (chars, 1..) |*c_ptr, len| {
|
||||
const res = try self.consumeChar(c_ptr);
|
||||
if (res == .done) return .{
|
||||
.consumed = len,
|
||||
.done = true,
|
||||
};
|
||||
var i: usize = 0;
|
||||
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 .{
|
||||
.consumed = i,
|
||||
.done = true,
|
||||
};
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
self.state = .{ .body = new_body };
|
||||
if (new_body.len >= self.content_length) {
|
||||
log.debug("End of request ({} body bytes)", .{new_body.len});
|
||||
return .done;
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user