-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lib] initial draft to websockets passes
- Loading branch information
Showing
1 changed file
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); |