Skip to content

Commit

Permalink
[lib] initial draft to websockets passes
Browse files Browse the repository at this point in the history
  • Loading branch information
cronopio committed Sep 3, 2013
1 parent 4480699 commit 79f7f99
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion lib/caronte/passes/ws.js
@@ -1 +1,52 @@
// ws
/*!
* Array of passes.
*
* A `pass` is just a function that is executed on `req, res, options`
* so that you can easily add new checks while still keeping the base
* flexible.
*/

/*
* Websockets Passes
*
*/

var passes = exports;

[
/*
* WebSocket requests must have the `GET` method and
* the `upgrade:websocket` header
*/
function checkMethodAndHeader (req, res, options) {
if (req.method !== 'GET' || req.headers.upgrade.toLowerCase() !== 'websocket') {
req.end();
// Return true to prevent the next passes to be executed
return true;
}
},

/**
* Sets `x-forwarded-*` headers if specified in config.
*
*/

function XHeaders(req, res, options) {
if(!options.xfwd) return;

var values = {
for : req.connection.remoteAddress || req.socket.remoteAddress,
port : req.connection.remotePort || req.socket.remotePort,
proto: req.connection.pair ? 'wss' : 'ws'
};

['for', 'port', 'proto'].forEach(function(header) {
req.headers['x-forwarded-' + header] =
(req.headers['x-forwarded-' + header] || '') +
(req.headers['x-forwarded-' + header] ? ',' : '') +
values[header]
});
}
].forEach(function(func) {
passes[func.name] = func;
});

0 comments on commit 79f7f99

Please sign in to comment.