From 0fd5884a0f23469b89a058f642fe29363178c295 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Wed, 21 Nov 2012 12:21:55 +0400 Subject: [PATCH] http-proxy: 304 responses should emit 'end' too --- lib/node-http-proxy/http-proxy.js | 50 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index 9a4f3087a..b026bb2ef 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -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); - } - }); }); //