Fix truncated chunked responses

This commit is contained in:
jpetazzo 2012-11-04 09:27:12 -08:00 committed by indexzero
parent af9eb06e47
commit ef66833c4d

View File

@ -277,6 +277,20 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
if (!ended) { response.emit('end') } 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 () { response.on('end', function () {
ended = true; ended = true;
if (!errState) { if (!errState) {
@ -296,7 +310,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
function ondata(chunk) { function ondata(chunk) {
if (res.writable) { 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(); response.pause();
} }
} }