From 79f7f99528661162ae4153856888f078f666e017 Mon Sep 17 00:00:00 2001 From: cronopio Date: Tue, 3 Sep 2013 14:09:35 -0500 Subject: [PATCH] [lib] initial draft to websockets passes --- lib/caronte/passes/ws.js | 53 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/caronte/passes/ws.js b/lib/caronte/passes/ws.js index 54e117ba5..e91002e46 100644 --- a/lib/caronte/passes/ws.js +++ b/lib/caronte/passes/ws.js @@ -1 +1,52 @@ -// ws \ No newline at end of file +/*! + * 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; +}); \ No newline at end of file