Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Can't set headers after they are sent" errors #1176

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,22 @@ module.exports = {

proxyReq.on('response', function(proxyRes) {
if(server) { server.emit('proxyRes', proxyRes, req, res); }
for(var i=0; i < web_o.length; i++) {
if(web_o[i](req, res, proxyRes, options)) { break; }
if (!res.headersSent) {
for(var i=0; i < web_o.length; i++) {
if(web_o[i](req, res, proxyRes, options)) { break; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a better fix: #1182

Copy link
Contributor Author

@thiagobustamante thiagobustamante Jun 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Frogmella,

I think that your your commit is ok and we should avoid the writeHead function. But if the headers are already sent, does not makes sense to call res.statusCode = proxyRes.statusCode;. The response status code and headers were already sent to client, so you can not modify it anymore.

}
}

// Allow us to listen when the proxy has completed
proxyRes.on('end', function () {
server.emit('end', req, res, proxyRes);
});

proxyRes.pipe(res);
if (!res.finished) {
proxyRes.on('end', function () {
server.emit('end', req, res, proxyRes);
});
proxyRes.pipe(res);
}
else {
server.emit('end', req, res, proxyRes);
}
});

//proxyReq.end();
Expand Down