Skip to content

Commit

Permalink
Fix truncated chunked responses
Browse files Browse the repository at this point in the history
  • Loading branch information
jpetazzo authored and indexzero committed Mar 9, 2013
1 parent af9eb06 commit ef66833
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Expand Down

0 comments on commit ef66833

Please sign in to comment.