Skip to content

Commit

Permalink
[refactor] clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Jun 12, 2013
1 parent 27d2359 commit 5b570e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
35 changes: 17 additions & 18 deletions lib/node-http-proxy/http-proxy.js
Expand Up @@ -155,6 +155,18 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
req.pipe(new ForwardStream(options.forward));
}

//
// Handle 'error' events from the `req` (e.g. `Parse Error`).
//
req.on('error', proxyError);

//Aborts pReq if client aborts the connection.
req.on('close', function () {
if (!errState) {
pReq.abort();
}
});

//
// #### function proxyError (err)
// #### @err {Error} Error contacting the proxy target
Expand All @@ -179,27 +191,14 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
// This NODE_ENV=production behavior is mimics Express and
// Connect.
//
if (process.env.NODE_ENV === 'production') {
res.write('Internal Server Error');
}
else {
res.write('An error has occurred: ' + JSON.stringify(err));
}

res.write(process.env.NODE_ENV === 'production' ?
'Internal Server Error' :
'An error has occurred: ' + JSON.stringify(err) ;
);
}

try { res.end() }
catch (ex) { console.error('res.end error: %s', ex.message) }
}

//
// Handle 'error' events from the `req` (e.g. `Parse Error`).
//
req.on('error', proxyError);

//Aborts pReq if client aborts the connection.
req.on('close', function () {
if (!errState) {
pReq.abort();
}
});
};
13 changes: 1 addition & 12 deletions lib/node-http-proxy/proxy-stream.js
Expand Up @@ -86,18 +86,7 @@ ProxyStream.prototype.start = function (req) {
//
// Process the `pReq` `pRes` when it's received.
//
if (req.httpVersion === '1.0') {
if (req.headers.connection) {
pRes.headers.connection = req.headers.connection
} else {
pRes.headers.connection = 'close'
}
} else if (!pRes.headers.connection) {
if (req.headers.connection) { pRes.headers.connection = req.headers.connection }
else {
pRes.headers.connection = 'keep-alive'
}
}
pRes.headers.connection = req.headers.connection || req.httpVersion === '1.0' ? 'close' : 'keep-alive';

// Remove `Transfer-Encoding` header if client's protocol is HTTP/1.0
// or if this is a DELETE request with no content-length header.
Expand Down

0 comments on commit 5b570e1

Please sign in to comment.