Skip to content

Commit 32dcb04

Browse files
committed
Merge pull request #482 from srossross/caronte
[merge] https & agent
2 parents 4a517fb + 7ad5c0f commit 32dcb04

File tree

7 files changed

+58
-19
lines changed

7 files changed

+58
-19
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ server.listen(5050);
107107

108108
* **target**: url string to be parsed with the url module
109109
* **forward**: url string to be parsed with the url module
110+
* **agent**: object to be passed to http(s).request (see Node's [https agent](http://nodejs.org/api/https.html#https_class_https_agent) and [http agent](http://nodejs.org/api/http.html#http_class_http_agent) objects)
111+
112+
If you are using the `proxyServer.listen` method, the following options are also applicable:
113+
110114
* **ssl**: object to be passed to https.createServer()
111115
* **ws**: true/false, if you want to proxy websockets
112116
* **xfwd**: true/false, adds x-forward headers
113-
* **maxSock**: maximum number of sockets
117+
114118

115119
### Test
116120

@@ -146,3 +150,4 @@ Logo created by [Diego Pasquali](http://dribbble.com/diegopq)
146150
>OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
147151
>THE SOFTWARE.
148152
153+

examples/https-secure.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var caronte = require('caronte'),
2+
https = require('https');
3+
/*
4+
* Create your proxy server pointing to a secure domain
5+
* Enable ssl validation
6+
*/
7+
var options = {target : 'https://google.com',
8+
agent : https.globalAgent,
9+
headers: {host: 'google.com'}
10+
};
11+
12+
var proxyServer = caronte.createProxyServer(options);
13+
console.log("Proxy server listening on port 8000");
14+
proxyServer.listen(8000);
15+

examples/https.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var caronte = require('caronte');
2+
/*
3+
* Create your proxy server pointing to a secure domain
4+
*/
5+
var options = {target:'https://google.com'};
6+
7+
var proxyServer = caronte.createProxyServer(options);
8+
console.log("Proxy server listening on port 8000");
9+
proxyServer.listen(8000);
10+

examples/stand-alone.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ var http = require('http'),
33
//
44
// Create your proxy server
55
//
6+
console.log("Proxy server listening on port 8000");
67
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);
78

89
//
910
// Create your target server
1011
//
12+
console.log("Web server listening on port 9000");
1113
http.createServer(function (req, res) {
1214
res.writeHead(200, { 'Content-Type': 'text/plain' });
1315
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));

lib/caronte.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ proxy.createProxyServer = function createProxyServer(options) {
2828
" { ",
2929
" target : <url string to be parsed with the url module> ",
3030
" forward: <url string to be parsed with the url module> ",
31+
" agent : <object to be passed to http(s).request> ",
3132
" ssl : <object to be passed to https.createServer()> ",
3233
" ws : <true/false, if you want to proxy websockets> ",
3334
" xfwd : <true/false, adds x-forward headers> ",
34-
" maxSock: <maximum number of sockets> ",
35-
" agent : <http agent for pooled connections> ",
3635
" } ",
3736
" ",
3837
"NOTE: `options.ws` and `options.ssl` are optional. ",
@@ -41,14 +40,6 @@ proxy.createProxyServer = function createProxyServer(options) {
4140
].join("\n"));
4241
}
4342

44-
['target', 'forward'].forEach(function(key) {
45-
if(!options[key]) return;
46-
options[key] = url.parse(options[key]);
47-
48-
options[key].maxSockets = options.maxSock;
49-
options[key].agent = options.agent || false // new (options.ssl ? https.Agent : http.Agent)(options[key].maxSockets || 100);
50-
});
51-
5243
options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });
5344

5445
return {

lib/caronte/common.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var common = exports;
1+
var common = exports,
2+
extend = require('util')._extend;
23

34
/**
45
* Copies the right headers from `options` and `req` to
@@ -24,14 +25,19 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
2425
outgoing.port = options[forward || 'target'].port ||
2526
(~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? 443 : 80);
2627

27-
['host', 'hostname', 'socketPath', 'agent'].forEach(
28+
['host', 'hostname', 'socketPath'].forEach(
2829
function(e) { outgoing[e] = options[forward || 'target'][e]; }
2930
);
3031

3132
['method', 'headers'].forEach(
3233
function(e) { outgoing[e] = req[e]; }
3334
);
3435

36+
if (options.headers){
37+
extend(outgoing.headers, options.headers);
38+
}
39+
40+
outgoing.agent = options.agent || false;
3541
outgoing.path = req.url;
3642

3743
return outgoing;

test/lib-caronte-common-test.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ describe('lib/caronte/common.js', function () {
77
var outgoing = {};
88
common.setupOutgoing(outgoing,
99
{
10+
agent : '?',
1011
target: {
1112
host : 'hey',
1213
hostname : 'how',
1314
socketPath: 'are',
1415
port : 'you',
15-
agent : '?'
16-
}
16+
},
17+
headers: {'fizz': 'bang', 'overwritten':true},
1718
},
1819
{
1920
method : 'i',
2021
url : 'am',
21-
headers : 'proxy'
22+
headers : {'pro':'xy','overwritten':false}
2223
});
2324

2425
expect(outgoing.host).to.eql('hey');
@@ -29,18 +30,27 @@ describe('lib/caronte/common.js', function () {
2930

3031
expect(outgoing.method).to.eql('i');
3132
expect(outgoing.path).to.eql('am');
32-
expect(outgoing.headers).to.eql('proxy')
33+
34+
expect(outgoing.headers.pro).to.eql('xy');
35+
expect(outgoing.headers.fizz).to.eql('bang');
36+
expect(outgoing.headers.overwritten).to.eql(true);
37+
});
38+
39+
it('should set the agent to false if none is given', function () {
40+
var outgoing = {};
41+
common.setupOutgoing(outgoing, {target: {},}, {});
42+
expect(outgoing.agent).to.eql(false);
3343
});
3444

3545
it('set the port according to the protocol', function () {
3646
var outgoing = {};
3747
common.setupOutgoing(outgoing,
38-
{
48+
{
49+
agent : '?',
3950
target: {
4051
host : 'how',
4152
hostname : 'are',
4253
socketPath: 'you',
43-
agent : '?',
4454
protocol: 'https:'
4555
}
4656
},

0 commit comments

Comments
 (0)