Skip to content

Commit

Permalink
Fix chunked http bug
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Jul 14, 2024
1 parent 91e2201 commit 0c64032
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0c64032

Please sign in to comment.