From 73381cf71ae92b9ed1c2da5986aa7ca31a7cf2e8 Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 22 Sep 2010 02:37:25 -0400 Subject: [PATCH] [debug] Removed pool as a dependency for stress test --- lib/node-http-proxy.js | 125 +++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 60 deletions(-) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index c78948b..e08a55b 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -145,78 +145,83 @@ HttpProxy.prototype = { self.body = ''; // Open new HTTP request to internal resource with will act as a reverse proxy pass - var p = manager.getPool(port, server); - sys.puts('Current pool count for ' + req.headers.host + ":" + port + ' ' + p.clients.length + ', Busy: ' + p.getBusy() + ', Free: ' + p.getFree()); + //var p = manager.getPool(port, server); + //sys.puts('Current pool count for ' + req.headers.host + ":" + port + ' ' + p.clients.length + ', Busy: ' + p.getBusy() + ', Free: ' + p.getFree()); - p.on('error', function (err) { + //p.on('error', function (err) { // Remark: We should probably do something here // but this is a hot-fix because I don't think 'pool' // should be emitting this event. - }); + //}); + + var client = http.createClient(port, server); + var reverse_proxy = client.request(req.method, req.url, req.headers); - p.request(req.method, req.url, req.headers, function (reverse_proxy) { - // Create an error handler so we can use it temporarily - var error = function (err) { - res.writeHead(500, {'Content-Type': 'text/plain'}); + //p.request(req.method, req.url, req.headers, function (reverse_proxy) { + // Create an error handler so we can use it temporarily + var error = function (err) { + res.writeHead(500, {'Content-Type': 'text/plain'}); + + if(req.method !== 'HEAD') { + res.write('An error has occurred: ' + JSON.stringify(err)); + } + + // Response end may never come so removeListener here + reverse_proxy.removeListener('error', error); + res.end(); + }; + + // Add a listener for the connection timeout event + reverse_proxy.addListener('error', error); + + // Add a listener for the reverse_proxy response event + reverse_proxy.addListener('response', function (response) { + if (response.headers.connection) { + if (req.headers.connection) response.headers.connection = req.headers.connection; + else response.headers.connection = 'close'; + } + + // Set the response headers of the client response + res.writeHead(response.statusCode, response.headers); + + // Status code = 304 + // No 'data' event and no 'end' + if (response.statusCode === 304) { + res.end(); + return; + } + + // Add event handler for the proxied response in chunks + response.addListener('data', function (chunk) { if(req.method !== 'HEAD') { - res.write('An error has occurred: ' + JSON.stringify(err)); + res.write(chunk, 'binary'); + self.body += chunk; } - - // Response end may never come so removeListener here + }); + + // Add event listener for end of proxied response + response.addListener('end', function () { + // Remark: Emit the end event for testability + self.emitter.emit('proxy', null, self.body); reverse_proxy.removeListener('error', error); res.end(); - }; - - // Add a listener for the connection timeout event - reverse_proxy.addListener('error', error); - - // Add a listener for the reverse_proxy response event - reverse_proxy.addListener('response', function (response) { - if (response.headers.connection) { - if (req.headers.connection) response.headers.connection = req.headers.connection; - else response.headers.connection = 'close'; - } - - // Set the response headers of the client response - res.writeHead(response.statusCode, response.headers); - - // Status code = 304 - // No 'data' event and no 'end' - if (response.statusCode === 304) { - res.end(); - return; - } - - // Add event handler for the proxied response in chunks - response.addListener('data', function (chunk) { - if(req.method !== 'HEAD') { - res.write(chunk, 'binary'); - self.body += chunk; - } - }); - - // Add event listener for end of proxied response - response.addListener('end', function () { - // Remark: Emit the end event for testability - self.emitter.emit('proxy', null, self.body); - reverse_proxy.removeListener('error', error); - res.end(); - }); }); - - // Chunk the client request body as chunks from the proxied request come in - req.addListener('data', function (chunk) { - reverse_proxy.write(chunk, 'binary'); - }) - - // At the end of the client request, we are going to stop the proxied request - req.addListener('end', function () { - reverse_proxy.end(); - }); - - self.unwatch(req); }); + + // Chunk the client request body as chunks from the proxied request come in + req.addListener('data', function (chunk) { + reverse_proxy.write(chunk, 'binary'); + }) + + // At the end of the client request, we are going to stop the proxied request + req.addListener('end', function () { + reverse_proxy.end(); + }); + + self.unwatch(req); + + //}); }, proxyWebSocketRequest: function (port, server, host) {