[refactor] clean up

This commit is contained in:
yawnt 2013-06-12 18:44:11 +02:00
parent 27d23592d1
commit 5b570e1215
2 changed files with 18 additions and 30 deletions

View File

@ -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
@ -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();
}
});
};

View File

@ -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.