Merge pull request #337 from indutny/feature-304-end

http-proxy: 304 responses should emit 'end' too
This commit is contained in:
Charlie Robbins 2012-11-29 03:51:42 -08:00
commit 8b3cfdaaea

View File

@ -256,6 +256,31 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
}
}
//
// When the `reverseProxy` `response` ends, end the
// corresponding outgoing `res` unless we have entered
// an error state. In which case, assume `res.end()` has
// already been called and the 'error' event listener
// removed.
//
var ended = false;
response.on('close', function () {
if (!ended) { response.emit('end') }
});
response.on('end', function () {
ended = true;
if (!errState) {
reverseProxy.removeListener('error', proxyError);
try { res.end() }
catch (ex) { console.error("res.end error: %s", ex.message) }
// Emit the `end` event now that we have completed proxying
self.emit('end', req, res);
}
});
// Set the headers of the client response
res.writeHead(response.statusCode, response.headers);
@ -283,31 +308,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
}
res.on('drain', ondrain);
//
// When the `reverseProxy` `response` ends, end the
// corresponding outgoing `res` unless we have entered
// an error state. In which case, assume `res.end()` has
// already been called and the 'error' event listener
// removed.
//
var ended = false;
response.on('close', function () {
if (!ended) { response.emit('end') }
});
response.on('end', function () {
ended = true;
if (!errState) {
reverseProxy.removeListener('error', proxyError);
try { res.end() }
catch (ex) { console.error("res.end error: %s", ex.message) }
// Emit the `end` event now that we have completed proxying
self.emit('end', req, res);
}
});
});
//