Skip to content

Commit 9faa924

Browse files
committed
[api] First pass at removing pool and working with node v0.4.0
1 parent 34cba38 commit 9faa924

File tree

5 files changed

+102
-119
lines changed

5 files changed

+102
-119
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/config.json

Diff for: lib/node-http-proxy.js

+95-109
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,8 @@
2727
var util = require('util'),
2828
http = require('http'),
2929
events = require('events'),
30-
pool = require('pool'),
3130
ProxyTable = require('./proxy-table').ProxyTable,
32-
min = 0,
33-
max = 100;
34-
35-
// Setup the PoolManager
36-
var manager = pool.createPoolManager();
37-
manager.setMinClients(min);
38-
manager.setMaxClients(max);
31+
maxSockets = 100;
3932

4033
exports.createServer = function () {
4134
var args, callback, port, host, forward,
@@ -114,17 +107,13 @@ exports.createServer = function () {
114107
return server;
115108
};
116109

117-
exports.setMin = function (value) {
118-
min = value;
119-
manager.setMinClients(min);
110+
exports.setMaxSockets = function (value) {
111+
maxSockets = value;
120112
};
121113

122-
exports.setMax = function (value) {
123-
max = value;
124-
manager.setMaxClients(max);
125-
};
114+
exports.ProxyTable = ProxyTable;
126115

127-
var HttpProxy = function (req, res, head) {
116+
var HttpProxy = exports.HttpProxy = function (req, res, head) {
128117
this.events = {};
129118
this.req = req;
130119

@@ -177,116 +166,116 @@ HttpProxy.prototype = {
177166
},
178167

179168
proxyRequest: function (port, server) {
180-
var self = this, req = this.req, res = this.res;
169+
var self = this, req = this.req, res = this.res, reverseProxy;
181170

182-
// Open new HTTP request to internal resource with will act as a reverse proxy pass
183-
var p = manager.getPool(port, server);
171+
// Create an error handler so we can use it temporarily
172+
function error (obj) {
173+
var fn = function (err) {
174+
res.writeHead(500, {'Content-Type': 'text/plain'});
184175

185-
p.on('error', function (err) {
186-
// Remark: We should probably do something here
187-
// but this is a hot-fix because I don't think 'pool'
188-
// should be emitting this event.
189-
});
190-
191-
p.request(req.method, req.url, req.headers, function (reverse_proxy) {
192-
// Create an error handler so we can use it temporarily
193-
function error (obj) {
194-
var fn = function (err) {
195-
res.writeHead(500, {'Content-Type': 'text/plain'});
196-
197-
if (req.method !== 'HEAD') {
198-
res.write('An error has occurred: ' + JSON.stringify(err));
199-
}
200-
201-
// Response end may never come so removeListener here
202-
obj.removeListener('error', fn);
203-
res.end();
204-
};
176+
if (req.method !== 'HEAD') {
177+
res.write('An error has occurred: ' + JSON.stringify(err));
178+
}
205179

206-
return fn;
180+
// Response end may never come so removeListener here
181+
obj.removeListener('error', fn);
182+
res.end();
207183
};
208-
209-
// Add a listener for the connection timeout event
210-
var reverseProxyError = error(reverse_proxy);
211-
reverse_proxy.addListener('error', reverseProxyError);
212184

213-
// Add a listener for the reverse_proxy response event
214-
reverse_proxy.addListener('response', function (response) {
215-
if (response.headers.connection) {
216-
if (req.headers.connection) response.headers.connection = req.headers.connection;
217-
else response.headers.connection = 'close';
218-
}
219-
220-
// Set the response headers of the client response
221-
res.writeHead(response.statusCode, response.headers);
185+
return fn;
186+
};
187+
188+
// Open new HTTP request to internal resource with will act as a reverse proxy pass
189+
reverseProxy = http.request({
190+
host: server,
191+
port: port,
192+
method: req.method,
193+
path: req.url,
194+
headers: req.headers
195+
}, function (response) {
196+
197+
// Process the reverse_proxy response when it's received.
198+
if (response.headers.connection) {
199+
if (req.headers.connection) response.headers.connection = req.headers.connection;
200+
else response.headers.connection = 'close';
201+
}
222202

223-
// Status code = 304
224-
// No 'data' event and no 'end'
225-
if (response.statusCode === 304) {
226-
res.end();
227-
return;
228-
}
203+
// Set the response headers of the client response
204+
res.writeHead(response.statusCode, response.headers);
229205

230-
// Add event handler for the proxied response in chunks
231-
response.addListener('data', function (chunk) {
232-
if (req.method !== 'HEAD') {
233-
res.write(chunk, 'binary');
234-
self.body += chunk;
235-
}
236-
});
206+
// Status code = 304
207+
// No 'data' event and no 'end'
208+
if (response.statusCode === 304) {
209+
res.end();
210+
return;
211+
}
237212

238-
// Add event listener for end of proxied response
239-
response.addListener('end', function () {
240-
reverse_proxy.removeListener('error', reverseProxyError);
241-
res.end();
242-
});
213+
// Add event handler for the proxied response in chunks
214+
response.addListener('data', function (chunk) {
215+
if (req.method !== 'HEAD') {
216+
res.write(chunk, 'binary');
217+
self.body += chunk;
218+
}
243219
});
244220

245-
// Chunk the client request body as chunks from the proxied request come in
246-
req.addListener('data', function (chunk) {
247-
reverse_proxy.write(chunk, 'binary');
248-
})
249-
250-
// At the end of the client request, we are going to stop the proxied request
251-
req.addListener('end', function () {
252-
reverse_proxy.end();
221+
// Add event listener for end of proxied response
222+
response.addListener('end', function () {
223+
reverseProxy.removeListener('error', reverseProxyError);
224+
res.end();
253225
});
226+
});
254227

255-
self.unwatch(req);
228+
// Add a listener for the connection timeout event
229+
var reverseProxyError = error(reverseProxy);
230+
reverseProxy.addListener('error', reverseProxyError);
231+
232+
// Chunk the client request body as chunks from the proxied request come in
233+
req.addListener('data', function (chunk) {
234+
reverseProxy.write(chunk, 'binary');
235+
})
236+
237+
// At the end of the client request, we are going to stop the proxied request
238+
req.addListener('end', function () {
239+
reverseProxy.end();
256240
});
241+
242+
self.unwatch(req);
257243
},
258244

259245
forwardRequest: function (port, server) {
260-
var self = this, req = this.req;
246+
var self = this, req = this.req, forwardProxy;
261247

262248
// Open new HTTP request to internal resource with will act as a reverse proxy pass
263-
var p = manager.getPool(port, server);
264-
265-
p.on('error', function (err) {
266-
// Remark: We should probably do something here
267-
// but this is a hot-fix because I don't think 'pool'
268-
// should be emitting this event.
249+
forwardProxy = http.request({
250+
host: server,
251+
port: port,
252+
method: req.method,
253+
path: req.url,
254+
headers: req.headers
255+
}, function (response) {
256+
//
257+
// Ignore the response from the forward proxy since this is a 'fire-and-forget' proxy.
258+
// Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning.
259+
//
269260
});
270261

271-
p.request(req.method, req.url, req.headers, function (forward_proxy) {
272-
// Add a listener for the connection timeout event
273-
forward_proxy.addListener('error', function (err) {
274-
// Remark: Ignoring this error in the event
275-
// forward target doesn't exist.
276-
});
277-
278-
// Chunk the client request body as chunks from the proxied request come in
279-
req.addListener('data', function (chunk) {
280-
forward_proxy.write(chunk, 'binary');
281-
})
282-
283-
// At the end of the client request, we are going to stop the proxied request
284-
req.addListener('end', function () {
285-
forward_proxy.end();
286-
});
287-
288-
self.unwatch(req);
262+
// Add a listener for the connection timeout event
263+
forwardProxy.addListener('error', function (err) {
264+
// Remark: Ignoring this error in the event
265+
// forward target doesn't exist.
289266
});
267+
268+
// Chunk the client request body as chunks from the proxied request come in
269+
req.addListener('data', function (chunk) {
270+
forwardProxy.write(chunk, 'binary');
271+
})
272+
273+
// At the end of the client request, we are going to stop the proxied request
274+
req.addListener('end', function () {
275+
forwardProxy.end();
276+
});
277+
278+
self.unwatch(req);
290279
},
291280

292281
proxyWebSocketRequest: function (port, server, host) {
@@ -477,7 +466,4 @@ HttpProxy.prototype = {
477466
});
478467
};
479468
}
480-
};
481-
482-
exports.HttpProxy = HttpProxy;
483-
exports.ProxyTable = ProxyTable;
469+
};

Diff for: test/forward-proxy-test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ var fs = require('fs'),
1111
path = require('path'),
1212
request = require('request'),
1313
assert = require('assert'),
14-
helpers = require('./helpers'),
15-
TestRunner = helpers.TestRunner;
14+
helpers = require('./helpers');
1615

17-
var runner = new TestRunner(),
16+
var runner = new helpers.TestRunner(),
1817
assertProxiedWithTarget = helpers.assertProxiedWithTarget,
1918
assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget;
2019

Diff for: test/helpers.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ exports.assertProxiedWithNoTarget = function (runner, proxyPort, statusCode, cre
6363
return test;
6464
}
6565

66-
var TestRunner = function () {
66+
var TestRunner = exports.TestRunner = function () {
6767
this.testServers = [];
6868
}
6969

@@ -159,6 +159,4 @@ TestRunner.prototype.closeServers = function () {
159159
});
160160

161161
return this.testServers;
162-
};
163-
164-
exports.TestRunner = TestRunner;
162+
};

Diff for: test/node-http-proxy-test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ var vows = require('vows'),
2828
util = require('util'),
2929
request = require('request'),
3030
assert = require('assert'),
31-
helpers = require('./helpers'),
32-
TestRunner = helpers.TestRunner;
31+
helpers = require('./helpers');
3332

34-
var runner = new TestRunner(),
33+
var runner = new helpers.TestRunner(),
3534
assertProxiedWithTarget = helpers.assertProxiedWithTarget,
3635
assertProxiedWithNoTarget = helpers.assertProxiedWithNoTarget;
3736

0 commit comments

Comments
 (0)