diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 3c12ba6..d80c3ff 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -169,7 +169,26 @@ module.exports = { } } - if (!res.finished) { + var isChunked = proxyRes.headers["transfer-encoding"] === "chunked"; + + if (isChunked) { + // UNIT BUG: https://github.com/nginx/unit/issues/1088 + let data = []; + + // Collect all chunks + proxyRes.on("data", function (chunk) { + data.push(chunk); + }); + + // When the response ends, concatenate and send the response + proxyRes.on("end", function () { + const buffer = Buffer.concat(data); + res.setHeader("Content-Length", buffer.length); + res.removeHeader("transfer-encoding"); + res.end(buffer); + if (server) server.emit("end", req, res, proxyRes); + }); + } else if (!res.finished) { // Allow us to listen when the proxy has completed proxyRes.on('end', function () { if (server) server.emit('end', req, res, proxyRes);