From c315f2bc2eacc33e2a6e0fcb5b775e20aa506aa7 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sun, 17 Aug 2025 09:45:08 +0200 Subject: [PATCH] http.BodyWriter: improve clarity of chunked state machine This is theoretically a bugfix as well, since it enforces the correct limit on the first write after writing the header. This theoretical bug hasn't been hit in practice though as far as I know. --- lib/std/http.zig | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/std/http.zig b/lib/std/http.zig index b06ee245a8..dcc96ba741 100644 --- a/lib/std/http.zig +++ b/lib/std/http.zig @@ -864,7 +864,7 @@ pub const BodyWriter = struct { const bw = w.http_protocol_output; switch (w.state.chunk_len) { 0 => {}, - 1 => try bw.writeByte('\n'), + 1 => unreachable, // Wrote more data than specified in chunk header. 2 => try bw.writeAll("\r\n"), else => unreachable, // An earlier write call indicated more data would follow. } @@ -963,17 +963,15 @@ pub const BodyWriter = struct { return error.Unimplemented; }; const out = bw.http_protocol_output; - switch (bw.state.chunk_len) { + l: switch (bw.state.chunk_len) { 0 => { const header_buf = try out.writableArray(chunk_header_template.len); @memcpy(header_buf, chunk_header_template); writeHex(header_buf[0..chunk_len_digits], data_len); - const n = try out.sendFileHeader(w.buffered(), file_reader, limit); - bw.state.chunk_len = data_len + 2 - n; - const ret = w.consume(n); - return ret; + bw.state.chunk_len = data_len + 2; + continue :l bw.state.chunk_len; }, - 1 => unreachable, + 1 => unreachable, // Wrote more data than specified in chunk header. 2 => { try out.writeAll("\r\n"); bw.state.chunk_len = 0; @@ -1003,11 +1001,10 @@ pub const BodyWriter = struct { const header_buf = try out.writableArray(chunk_header_template.len); @memcpy(header_buf, chunk_header_template); writeHex(header_buf[0..chunk_len_digits], data_len); - const n = try out.writeSplatHeader(w.buffered(), data, splat); - bw.state.chunk_len = data_len + 2 - n; - return w.consume(n); + bw.state.chunk_len = data_len + 2; + continue :l bw.state.chunk_len; }, - 1 => unreachable, + 1 => unreachable, // Wrote more data than specified in chunk header. 2 => { try out.writeAll("\r\n"); bw.state.chunk_len = 0;