Skip to content

Commit

Permalink
Merge pull request #337 from indutny/feature-304-end
Browse files Browse the repository at this point in the history
http-proxy: 304 responses should emit 'end' too
  • Loading branch information
indexzero committed Nov 29, 2012
2 parents 22639b3 + 0fd5884 commit 8b3cfda
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
});
});

//
Expand Down

0 comments on commit 8b3cfda

Please sign in to comment.