authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-05-02 22:19:27-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-01 16:35:28-07:00
log847afd0a47021934801a9d44ba39a8a241de5458
tree1b71d69448735323eb0b9296ad0b9499fe401826
parente4126aa213e30d4fe93981a27160acc56e3db4f8

std.http: fix chunked header offset calculation


1 files changed, 6 insertions(+), 5 deletions(-)

lib/std/http.zig+6-5
...@@ -809,8 +809,9 @@ pub const BodyWriter = struct {...@@ -809,8 +809,9 @@ pub const BodyWriter = struct {
809 end,809 end,
810810
811 pub const Chunked = union(enum) {811 pub const Chunked = union(enum) {
812 /// Index of the hex-encoded chunk length in the chunk header812 /// Index to the start of the hex-encoded chunk length in the chunk
813 /// within the buffer of `BodyWriter.http_protocol_output`.813 /// header within the buffer of `BodyWriter.http_protocol_output`.
814 /// Buffered chunk data starts here plus length of `chunk_header_template`.
814 offset: usize,815 offset: usize,
815 /// We are in the middle of a chunk and this is how many bytes are816 /// We are in the middle of a chunk and this is how many bytes are
816 /// left until the next header. This includes +2 for "\r"\n", and817 /// left until the next header. This includes +2 for "\r"\n", and
...@@ -830,7 +831,7 @@ pub const BodyWriter = struct {...@@ -830,7 +831,7 @@ pub const BodyWriter = struct {
830 .end, .none, .content_length => return w.http_protocol_output.flush(),831 .end, .none, .content_length => return w.http_protocol_output.flush(),
831 .chunked => |*chunked| switch (chunked.*) {832 .chunked => |*chunked| switch (chunked.*) {
832 .offset => |*offset| {833 .offset => |*offset| {
833 try w.http_protocol_output.flushLimit(.limited(w.http_protocol_output.end - offset.*));834 try w.http_protocol_output.flushLimit(.limited(offset.*));
834 offset.* = 0;835 offset.* = 0;
835 },836 },
836 .chunk_len => return w.http_protocol_output.flush(),837 .chunk_len => return w.http_protocol_output.flush(),
...@@ -1025,8 +1026,8 @@ pub const BodyWriter = struct {...@@ -1025,8 +1026,8 @@ pub const BodyWriter = struct {
1025 },1026 },
1026 .chunk_len => |chunk_len| l: switch (chunk_len) {1027 .chunk_len => |chunk_len| l: switch (chunk_len) {
1027 0 => {1028 0 => {
1028 const header_buf = try bw.writableArray(chunk_header_template.len);
1029 const off = bw.end;1029 const off = bw.end;
1030 const header_buf = try bw.writableArray(chunk_header_template.len);
1030 @memcpy(header_buf, chunk_header_template);1031 @memcpy(header_buf, chunk_header_template);
1031 chunked.* = .{ .offset = off };1032 chunked.* = .{ .offset = off };
1032 continue :state .{ .offset = off };1033 continue :state .{ .offset = off };
...@@ -1074,8 +1075,8 @@ pub const BodyWriter = struct {...@@ -1074,8 +1075,8 @@ pub const BodyWriter = struct {
1074 },1075 },
1075 .chunk_len => |chunk_len| l: switch (chunk_len) {1076 .chunk_len => |chunk_len| l: switch (chunk_len) {
1076 0 => {1077 0 => {
1077 const header_buf = try bw.writableArray(chunk_header_template.len);
1078 const offset = bw.end;1078 const offset = bw.end;
1079 const header_buf = try bw.writableArray(chunk_header_template.len);
1079 @memcpy(header_buf, chunk_header_template);1080 @memcpy(header_buf, chunk_header_template);
1080 chunked.* = .{ .offset = offset };1081 chunked.* = .{ .offset = offset };
1081 continue :state .{ .offset = offset };1082 continue :state .{ .offset = offset };