Skip to content

Commit 8b3cfda

Browse files
committed
Merge pull request #337 from indutny/feature-304-end
http-proxy: 304 responses should emit 'end' too
2 parents 22639b3 + 0fd5884 commit 8b3cfda

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

lib/node-http-proxy/http-proxy.js

+25-25
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,31 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
256256
}
257257
}
258258

259+
//
260+
// When the `reverseProxy` `response` ends, end the
261+
// corresponding outgoing `res` unless we have entered
262+
// an error state. In which case, assume `res.end()` has
263+
// already been called and the 'error' event listener
264+
// removed.
265+
//
266+
var ended = false;
267+
response.on('close', function () {
268+
if (!ended) { response.emit('end') }
269+
});
270+
271+
response.on('end', function () {
272+
ended = true;
273+
if (!errState) {
274+
reverseProxy.removeListener('error', proxyError);
275+
276+
try { res.end() }
277+
catch (ex) { console.error("res.end error: %s", ex.message) }
278+
279+
// Emit the `end` event now that we have completed proxying
280+
self.emit('end', req, res);
281+
}
282+
});
283+
259284
// Set the headers of the client response
260285
res.writeHead(response.statusCode, response.headers);
261286

@@ -283,31 +308,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
283308
}
284309

285310
res.on('drain', ondrain);
286-
287-
//
288-
// When the `reverseProxy` `response` ends, end the
289-
// corresponding outgoing `res` unless we have entered
290-
// an error state. In which case, assume `res.end()` has
291-
// already been called and the 'error' event listener
292-
// removed.
293-
//
294-
var ended = false;
295-
response.on('close', function () {
296-
if (!ended) { response.emit('end') }
297-
});
298-
299-
response.on('end', function () {
300-
ended = true;
301-
if (!errState) {
302-
reverseProxy.removeListener('error', proxyError);
303-
304-
try { res.end() }
305-
catch (ex) { console.error("res.end error: %s", ex.message) }
306-
307-
// Emit the `end` event now that we have completed proxying
308-
self.emit('end', req, res);
309-
}
310-
});
311311
});
312312

313313
//

0 commit comments

Comments
 (0)