Showing with 19 additions and 1 deletion.
  1. +19 −1 lib/node-http-proxy/http-proxy.js
@@ -277,6 +277,19 @@ 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 +309,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();
}
}