2013-09-03 14:09:35 -05:00

52 lines
1.2 KiB
JavaScript

/*!
* 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;
});