From 0b4129a8cf7e0220df42faca8cea22dc8a72c98a Mon Sep 17 00:00:00 2001 From: Szymon Nowakowski Date: Tue, 22 Jul 2025 20:22:52 +0200 Subject: [PATCH] Comsume whole body at once --- packages/myid/src/http/Parser.zig | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/myid/src/http/Parser.zig b/packages/myid/src/http/Parser.zig index dc26d56..06f3bb1 100644 --- a/packages/myid/src/http/Parser.zig +++ b/packages/myid/src/http/Parser.zig @@ -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; } },