Skip to content

Commit

Permalink
[fix] indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Sep 16, 2013
1 parent 469b0d4 commit ae4d4e8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
48 changes: 48 additions & 0 deletions README.md
Expand Up @@ -45,6 +45,43 @@ You can easily add a `pass` (stages) into both the pipelines (XXX: ADD API).

In addition, every stage emits a corresponding event so introspection during the process is always available.

#### Setup a basic stand-alone proxy server

var http = require('http'),
caronte = require('caronte');
//
// Create your proxy server
//
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);

//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);

#### Setup a stand-alone proxy server with custom server logic

``` js
var http = require('http'),
caronte = require('caronte');

//
// Create a proxy server with custom application logic
//
var proxy = caronte.createProxyServer({});

var server = require('http').createServer(function(req, res) {
proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
});

console.log("listening on port 5050")
server.listen(5050);
```

### Contributing and Issues

* Search on Google/Github
Expand All @@ -53,6 +90,17 @@ In addition, every stage emits a corresponding event so introspection during the
* Commit to your local branch (which must be different from `master`)
* Submit your Pull Request (be sure to include tests and update documentation)

### Options

`caronte.createProxyServer` supports the following options:

* **target**: url string to be parsed with the url module
* **forward**: url string to be parsed with the url module
* **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
15 changes: 15 additions & 0 deletions examples/stand-alone.js
@@ -0,0 +1,15 @@
var http = require('http'),
caronte = require('caronte');
//
// Create your proxy server
//
caronte.createProxyServer({target:'http://localhost:9000'}).listen(8000);

//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
21 changes: 16 additions & 5 deletions lib/caronte/index.js
@@ -1,6 +1,8 @@
var caronte = exports,
web = require('./passes/web-incoming'),
ws = require('./passes/ws-incoming');
var caronte = exports,
extend = require('util')._extend,
parse_url = require('url').parse,
web = require('./passes/web-incoming'),
ws = require('./passes/ws-incoming');

caronte.createWebProxy = createRightProxy('web');
caronte.createWsProxy = createRightProxy('ws');
Expand Down Expand Up @@ -41,7 +43,11 @@ function createRightProxy(type) {
!(args[cntr] instanceof Buffer) &&
args[cntr] !== res
) {
options = args[cntr];
//Copy global options
options = extend({}, options);
//Overwrite with request options
extend(options, args[cntr]);

cntr--;
}

Expand All @@ -51,7 +57,12 @@ function createRightProxy(type) {

options.ee.emit(ev + 'begin', req, res);


['target', 'forward'].forEach(
function(e) {
if (typeof options[e] === 'string')
options[e] = parse_url(options[e]);
});

passes.some(function(pass) {
var evnt = ev + pass.name.toLowerCase() + ':';

Expand Down
8 changes: 8 additions & 0 deletions lib/caronte/passes/web-incoming.js
Expand Up @@ -102,6 +102,14 @@ function stream(req, res, options) {
common.setupOutgoing(options.ssl || {}, options, req)
);

proxyReq.on('error', function(err){
var ev = 'caronte:outgoing:web:';
if (options.ee.listeners(ev + 'error').length == 0){
throw err;
}
options.ee.emit(ev + 'error', err, req, res);
});

req.pipe(proxyReq);

proxyReq.on('response', function(proxyRes) {
Expand Down
7 changes: 7 additions & 0 deletions lib/caronte/passes/ws-incoming.js
Expand Up @@ -78,6 +78,13 @@ function stream(req, socket, options, head) {
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req)
);
proxyReq.on('error', function(err){
var ev = 'caronte:outgoing:ws:';
if (options.ee.listeners(ev + 'error').length == 0){
throw err;
}
options.ee.emit(ev + 'error', err, req, res);
});

proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
common.setupSocket(proxySocket);
Expand Down

0 comments on commit ae4d4e8

Please sign in to comment.