diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index 2b2c0d2..3c821e7 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -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(); - } - }); }; \ No newline at end of file diff --git a/lib/node-http-proxy/proxy-stream.js b/lib/node-http-proxy/proxy-stream.js index 3f78123..31dd2d3 100644 --- a/lib/node-http-proxy/proxy-stream.js +++ b/lib/node-http-proxy/proxy-stream.js @@ -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.