Skip to content

Commit

Permalink
add parse options
Browse files Browse the repository at this point in the history
  • Loading branch information
aetheon committed Oct 29, 2020
1 parent b71e249 commit ddd4187
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
9 changes: 9 additions & 0 deletions docs/WebSocketServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ by a trusted proxy or other intermediary within your own
infrastructure.
More info: [X-Forwarded-For on Wikipedia](http://en.wikipedia.org/wiki/X-Forwarded-For)

**parseExtensions** - Boolean - *Default: true*
Whether or not to parse 'sec-websocket-extension' headers. Array is exposed to WebSocketRequest.requestedExtensions.

**parseCookies** - Boolean - *Default: true*
Whether or not to parse 'cookie' headers. Array is exposed to WebSocketRequest.cookies.

Events
------
There are three events emitted by a WebSocketServer instance that allow you to handle incoming requests, establish connections, and detect when a connection has been closed.
Expand All @@ -103,3 +109,6 @@ Emitted whenever a new WebSocket connection is accepted.
`function(webSocketConnection, closeReason, description)`

Whenever a connection is closed for any reason, the WebSocketServer instance will emit a `close` event, passing a reference to the WebSocketConnection instance that was closed. `closeReason` is the numeric reason status code for the connection closure, and `description` is a textual description of the close reason, if available.

### close
`function(webSocketConnection, closeReason, description)`
36 changes: 22 additions & 14 deletions lib/WebSocketRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ function WebSocketRequest(socket, httpRequest, serverConfig) {
this.remoteAddress = socket.remoteAddress;
this.remoteAddresses = [this.remoteAddress];
this.serverConfig = serverConfig;

// Watch for the underlying TCP socket closing before we call accept
this._socketIsClosing = false;
this._socketCloseHandler = this._handleSocketCloseBeforeAccept.bind(this);
this.socket.on('end', this._socketCloseHandler);
this.socket.on('close', this._socketCloseHandler);

this._resolved = false;
}

Expand Down Expand Up @@ -174,12 +174,20 @@ WebSocketRequest.prototype.readHandshake = function() {
}

// Extensions are optional.
var extensionsString = request.headers['sec-websocket-extensions'];
this.requestedExtensions = this.parseExtensions(extensionsString);
if (this.serverConfig.parseExtensions) {
var extensionsString = request.headers['sec-websocket-extensions'];
this.requestedExtensions = this.parseExtensions(extensionsString);
} else {
this.requestedExtensions = [];
}

// Cookies are optional
var cookieString = request.headers['cookie'];
this.cookies = this.parseCookies(cookieString);
if (this.serverConfig.parseCookies) {
var cookieString = request.headers['cookie'];
this.cookies = this.parseCookies(cookieString);
} else {
this.cookies = [];
}
};

WebSocketRequest.prototype.parseExtensions = function(extensionsString) {
Expand Down Expand Up @@ -248,7 +256,7 @@ WebSocketRequest.prototype.parseCookies = function(str) {

WebSocketRequest.prototype.accept = function(acceptedProtocol, allowedOrigin, cookies) {
this._verifyResolution();

// TODO: Handle extensions

var protocolFullCase;
Expand Down Expand Up @@ -426,21 +434,21 @@ WebSocketRequest.prototype.accept = function(acceptedProtocol, allowedOrigin, co
// if (negotiatedExtensions) {
// response += 'Sec-WebSocket-Extensions: ' + negotiatedExtensions.join(', ') + '\r\n';
// }

// Mark the request resolved now so that the user can't call accept or
// reject a second time.
this._resolved = true;
this.emit('requestResolved', this);

response += '\r\n';

var connection = new WebSocketConnection(this.socket, [], acceptedProtocol, false, this.serverConfig);
connection.webSocketVersion = this.webSocketVersion;
connection.remoteAddress = this.remoteAddress;
connection.remoteAddresses = this.remoteAddresses;

var self = this;

if (this._socketIsClosing) {
// Handle case when the client hangs up before we get a chance to
// accept the connection and send our side of the opening handshake.
Expand All @@ -452,7 +460,7 @@ WebSocketRequest.prototype.accept = function(acceptedProtocol, allowedOrigin, co
cleanupFailedConnection(connection);
return;
}

self._removeSocketCloseListeners();
connection._addSocketEventListeners();
});
Expand All @@ -464,12 +472,12 @@ WebSocketRequest.prototype.accept = function(acceptedProtocol, allowedOrigin, co

WebSocketRequest.prototype.reject = function(status, reason, extraHeaders) {
this._verifyResolution();

// Mark the request resolved now so that the user can't call accept or
// reject a second time.
this._resolved = true;
this.emit('requestResolved', this);

if (typeof(status) !== 'number') {
status = 403;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/WebSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ WebSocketServer.prototype.mount = function(config) {
// See: http://en.wikipedia.org/wiki/X-Forwarded-For
ignoreXForwardedFor: false,

// If this is true, 'cookie' headers are parsed and exposed as WebSocketRequest.cookies
parseCookies: true,

// If this is true, 'sec-websocket-extensions' headers are parsed and exposed as WebSocketRequest.requestedExtensions
parseExtensions: true,

// The Nagle Algorithm makes more efficient use of network resources
// by introducing a small delay before sending small packets so that
// multiple messages can be batched together before going onto the
Expand Down

0 comments on commit ddd4187

Please sign in to comment.