From 3fd3c96fa05fda45c7ef9ff44594644ac54f4a1e Mon Sep 17 00:00:00 2001 From: indexzero Date: Sun, 20 Mar 2011 13:17:15 -0500 Subject: [PATCH] [api] Force connection header to be `close` until keep-alive is replemented --- lib/node-http-proxy.js | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 86ed7937b..b36b0a591 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -242,7 +242,7 @@ HttpProxy.prototype.close = function () { // #### @buffer {Object} **Optional** Result from `httpProxy.buffer(req)` // HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) { - var self = this, reverseProxy, location, errState = false; + var self = this, reverseProxy, location, errState = false, opts; // // Check the proxy table for this instance to see if we need @@ -301,15 +301,21 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) { res.end(); } - // Open new HTTP request to internal resource with will act as a reverse proxy pass - reverseProxy = http.request({ + var opts = { host: host, port: port, agent: _getAgent(host, port), method: req.method, path: req.url, headers: req.headers - }, function (response) { + }; + + // Force the `connection` header to be 'close' until + // node.js core re-implements 'keep-alive'. + opts.headers['connection'] = 'close'; + + // Open new HTTP request to internal resource with will act as a reverse proxy pass + reverseProxy = http.request(opts, function (response) { // Process the `reverseProxy` `response` when it's received. if (response.headers.connection) { @@ -362,17 +368,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) { // request unless we have entered an error state. // req.on('end', function () { - // - // __Remark__ *(indexzero | 3/10/2011)*: This is a short-term workaround for a suspect error from net.js when - // `http.ClientRequest.end()` is called in reproducable, but uninvestigated scenarios - // - // net.js:313 - // throw new Error('Socket.end() called already; cannot write.'); - // ^ - // Error: Socket.end() called already; cannot write. - // at Socket.write (net.js:313:13) - // - if (!errState && (!reverseProxy.socket || reverseProxy.socket._writeQueueLast() !== 42)) { + if (!errState) { reverseProxy.end(); } }); @@ -390,20 +386,26 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) { // by `this.options.forward` ignoring errors and the subsequent response. // HttpProxy.prototype._forwardRequest = function (req) { - var self = this, port, host, forwardProxy; + var self = this, port, host, forwardProxy, opts; port = this.options.forward.port; host = this.options.forward.host; - // Open new HTTP request to internal resource with will act as a reverse proxy pass - forwardProxy = http.request({ + opts = { host: host, port: port, agent: _getAgent(host, port), method: req.method, path: req.url, headers: req.headers - }, function (response) { + }; + + // Force the `connection` header to be 'close' until + // node.js core re-implements 'keep-alive'. + opts.headers['connection'] = 'close'; + + // Open new HTTP request to internal resource with will act as a reverse proxy pass + forwardProxy = http.request(opts, function (response) { // // Ignore the response from the forward proxy since this is a 'fire-and-forget' proxy. // Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning. @@ -415,7 +417,7 @@ HttpProxy.prototype._forwardRequest = function (req) { // Remark: Ignoring this error in the event // forward target doesn't exist. // - forwardProxy.on('error', function (err) { }); + forwardProxy.once('error', function (err) { }); // Chunk the client request body as chunks from the proxied request come in req.on('data', function (chunk) {