Skip to content

Commit

Permalink
Merge branch 'agent' into caronte
Browse files Browse the repository at this point in the history
  • Loading branch information
srossross committed Sep 17, 2013
2 parents 69f126b + 1c7ace2 commit 4ee96dd
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
55 changes: 55 additions & 0 deletions examples/https.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var http = require('http')
, https = require('https')
, caronte = require('caronte')
;
//
// Create your proxy server
//
var options = {target:'https://google.com',
agent: new https.Agent({rejectUnauthorized:false}),
};

var proxyServer = caronte.createProxyServer(options);

proxyServer.ee.on('*:error', function(err, req, res){
res.end('There was an error proxying your request');
});

console.log("Proxy server listening on port 8000");
proxyServer.listen(8000);


//
// Create your proxy server
//
var options2 = {target:'https://google.com',
headers: {'host':'google.com'},
};

var proxyServer2 = caronte.createProxyServer(options2);

proxyServer2.ee.on('*:error', function(err, req, res){
res.end('There was an error proxying your request');
});

console.log("Proxy server 2 listening on port 8001");
proxyServer2.listen(8001);

//
// Create your proxy server
//
var options3 = {target:'https://google.com',
xfwd:true};

var proxyServer3 = caronte.createProxyServer(options3);

proxyServer3.ee.on('*:error', function(err, req, res){
res.end('There was an error proxying your request');
});

console.log("Proxy server 3 listening on port 8002");
proxyServer3.listen(8002);




2 changes: 2 additions & 0 deletions examples/stand-alone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ var http = require('http'),
//
// Create your proxy server
//
console.log("Proxy server listening on port 8000");
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);

//
// Create your target server
//
console.log("Web server listening on port 9000");
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
Expand Down
9 changes: 1 addition & 8 deletions lib/caronte.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ proxy.createProxyServer = function createProxyServer(options) {
" { ",
" target : <url string to be parsed with the url module> ",
" forward: <url string to be parsed with the url module> ",
" agent : <object to be passed to http(s).request(...)> ",
" ssl : <object to be passed to https.createServer()> ",
" ws : <true/false, if you want to proxy websockets> ",
" xfwd : <true/false, adds x-forward headers> ",
Expand All @@ -41,14 +42,6 @@ proxy.createProxyServer = function createProxyServer(options) {
].join("\n"));
}

['target', 'forward'].forEach(function(key) {
if(!options[key]) return;
options[key] = url.parse(options[key]);

options[key].maxSockets = options.maxSock;
options[key].agent = options.agent || false // new (options.ssl ? https.Agent : http.Agent)(options[key].maxSockets || 100);
});

options.ee = new events.EventEmitter2({ wildcard: true, delimiter: ':' });

return {
Expand Down
19 changes: 18 additions & 1 deletion lib/caronte/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
var common = exports;
var common = exports
, http = require('http')
, https = require('https')
, extend = require('util')._extend
;

/**
* Copies the right headers from `options` and `req` to
Expand Down Expand Up @@ -32,6 +36,19 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
function(e) { outgoing[e] = req[e]; }
);

if (options.headers){
extend(outgoing.headers, options.headers);
}

if (options.agent){
outgoing.agent = options.agent;
}

if (!outgoing.agent){
var Agent = (~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? https.Agent : http.Agent);
outgoing.agent = new Agent(options.maxSock || 100);
}

outgoing.path = req.url;

return outgoing;
Expand Down

0 comments on commit 4ee96dd

Please sign in to comment.