diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index 445ef53..9387b29 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -277,6 +277,20 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { if (!ended) { response.emit('end') } }); + // + // After reading a chunked response, the underlying socket + // will hit EOF and emit a 'end' event, which will abort + // the request. If the socket was paused at that time, + // pending data gets discarded, truncating the response. + // This code makes sure that we flush pending data. + // + response.connection.on('end', function () { + if (response.readable && response.resume) { + { + response.resume(); + } + }); + response.on('end', function () { ended = true; if (!errState) { @@ -296,7 +310,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { function ondata(chunk) { if (res.writable) { - if (false === res.write(chunk) && response.pause) { + // Only pause if the underlying buffers are full, + // *and* the connection is not in 'closing' state. + // Otherwise, the pause will cause pending data to + // be discarded and silently lost. + if (false === res.write(chunk) && response.pause + && response.connection.readable) { response.pause(); } }