From 625fd385b3a72348266ed3849c8a88391059e9dd Mon Sep 17 00:00:00 2001 From: Andreas Dolk Date: Mon, 5 Dec 2022 18:59:56 +0100 Subject: [PATCH] [#1576] Fixes unexpected premature close on chunked responses --- src/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 7c4aee87b..961677014 100644 --- a/src/index.js +++ b/src/index.js @@ -397,10 +397,10 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) { // Sometimes final 0-length chunk and end of message code are in separate packets if (!properLastChunkReceived && previousChunk) { - properLastChunkReceived = ( - Buffer.compare(previousChunk.slice(-3), LAST_CHUNK.slice(0, 3)) === 0 && - Buffer.compare(buf.slice(-2), LAST_CHUNK.slice(3)) === 0 - ); + if (buf.length < 5) { + properLastChunkReceived = Buffer.compare( + Buffer.from([...previousChunk.slice(-5), ...buf]).slice(-5), LAST_CHUNK) === 0; + } } previousChunk = buf;