Skip to content

https #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 21, 2013
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ server.listen(5050);

* **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 (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)

If you are using the `proxyServer.listen` method, the following options are also applicable:

* **ssl**: object to be passed to https.createServer()
* **ws**: true/false, if you want to proxy websockets
* **xfwd**: true/false, adds x-forward headers
* **maxSock**: maximum number of sockets


### Test

Expand Down Expand Up @@ -144,3 +148,4 @@ Logo created by [Diego Pasquali](http://dribbble.com/diegopq)
>OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
>THE SOFTWARE.


15 changes: 15 additions & 0 deletions examples/https-secure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var caronte = require('caronte'),
https = require('https');
/*
* Create your proxy server pointing to a secure domain
* Enable ssl validation
*/
var options = {target : 'https://google.com',
agent : https.globalAgent,
headers: {host: 'google.com'}
};

var proxyServer = caronte.createProxyServer(options);
console.log("Proxy server listening on port 8000");
proxyServer.listen(8000);

10 changes: 10 additions & 0 deletions examples/https.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var caronte = require('caronte');
/*
* Create your proxy server pointing to a secure domain
*/
var options = {target:'https://google.com'};

var proxyServer = caronte.createProxyServer(options);
console.log("Proxy server listening on port 8000");
proxyServer.listen(8000);

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
11 changes: 1 addition & 10 deletions lib/caronte.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ 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> ",
" maxSock: <maximum number of sockets> ",
" agent : <http agent for pooled connections> ",
" } ",
" ",
"NOTE: `options.ws` and `options.ssl` are optional. ",
Expand All @@ -41,14 +40,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
10 changes: 8 additions & 2 deletions lib/caronte/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var common = exports;
var common = exports,
extend = require('util')._extend;

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

['host', 'hostname', 'socketPath', 'agent'].forEach(
['host', 'hostname', 'socketPath'].forEach(
function(e) { outgoing[e] = options[forward || 'target'][e]; }
);

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

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

outgoing.agent = options.agent || false;
outgoing.path = req.url;

return outgoing;
Expand Down
22 changes: 16 additions & 6 deletions test/lib-caronte-common-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ describe('lib/caronte/common.js', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
agent : '?',
target: {
host : 'hey',
hostname : 'how',
socketPath: 'are',
port : 'you',
agent : '?'
}
},
headers: {'fizz': 'bang', 'overwritten':true},
},
{
method : 'i',
url : 'am',
headers : 'proxy'
headers : {'pro':'xy','overwritten':false}
});

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

expect(outgoing.method).to.eql('i');
expect(outgoing.path).to.eql('am');
expect(outgoing.headers).to.eql('proxy')

expect(outgoing.headers.pro).to.eql('xy');
expect(outgoing.headers.fizz).to.eql('bang');
expect(outgoing.headers.overwritten).to.eql(true);
});

it('should set the agent to false if none is given', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {target: {},}, {});
expect(outgoing.agent).to.eql(false);
});

it('set the port according to the protocol', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
{
agent : '?',
target: {
host : 'how',
hostname : 'are',
socketPath: 'you',
agent : '?',
protocol: 'https:'
}
},
Expand Down