From 745cc5c65502f1c1bcd69392e526d63f04653e11 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sat, 17 Sep 2022 21:44:39 -0700 Subject: Handle missing 0\r\n chunk in HTTP client --- src/http_client_async.zig | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/http_client_async.zig b/src/http_client_async.zig index c73db732e..cb59ef173 100644 --- a/src/http_client_async.zig +++ b/src/http_client_async.zig @@ -485,13 +485,29 @@ pub fn onClose( _ = socket; log("Closed {s}\n", .{client.url.href}); + const in_progress = client.state.stage != .done and client.state.stage != .fail; + + // if the peer closed after a full chunk, treat this + // as if the transfer had complete, browsers appear to ignore + // a missing 0\r\n chunk + if (in_progress and client.state.transfer_encoding == .chunked) { + if (picohttp.phr_decode_chunked_is_in_data(&client.state.chunked_decoder) == 0) { + if (client.state.compressed_body orelse client.state.body_out_str) |body| { + if (body.list.items.len > 0) { + client.done(comptime is_ssl, if (is_ssl) &http_thread.https_context else &http_thread.http_context, socket); + return; + } + } + } + } + if (client.allow_retry) { client.allow_retry = false; client.start(client.state.request_body, client.state.body_out_str.?); return; } - if (client.state.stage != .done and client.state.stage != .fail) + if (in_progress) client.fail(error.ConnectionClosed); } pub fn onTimeout( -- cgit v1.2.3