Skip to content

Commit

Permalink
0.8.8 fix some WS issues
Browse files Browse the repository at this point in the history
  • Loading branch information
micnic committed Nov 17, 2016
1 parent 372eb04 commit 5722b35
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 0.8.8
- Fix big WebSocket frames parsing
- Fix possible server crash by sending invalid WebSocket frames

## 0.8.7
- Fix client WebSocket connection creation
- Fix dynamic HTTP host removal
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
<img src="https://raw.github.com/micnic/simpleS/master/logo.png"/>

# 0.8.7
# 0.8.8

[![Gitter](https://badges.gitter.im/simples.png)](https://gitter.im/micnic/simpleS)

Expand Down
17 changes: 14 additions & 3 deletions lib/mixins/ws-mixin.js
Expand Up @@ -132,6 +132,9 @@ WsMixin.processConnection = function (connection, client) {
} else {
connection.emit('message', message);
}

// Reset message after emit
message = null;
}

// Pipe the connection to the net socket and the parser
Expand Down Expand Up @@ -165,13 +168,21 @@ WsMixin.processConnection = function (connection, client) {

// Prepare the message for emitting
if (frame.opcode === 0) {
concatenatePayload(frame.data);
if (message) {
concatenatePayload(frame.data);
} else {
parser.state = -1;
parser.emit('error', Error('Invalid continuation frame'));
}
} else if (message) {
parser.state = -1;
parser.emit('error', Error('Continuation frame expected'));
} else {
createMessage(frame);
}

// Check if it is the last frame in the sequence
if (frame.fin) {
if (!parser.state !== -1 && frame.fin) {
emitMessage();
}
}
Expand Down Expand Up @@ -225,7 +236,7 @@ WsMixin.prepareHandshake = function (connection) {
protocols = connection.protocols.join(', ');

// Prepare the connection head
connection.head += 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n';
connection.head += 'HTTP/1.1 101 Switching Protocols\r\n';
connection.head += 'Connection: Upgrade\r\n';
connection.head += 'Upgrade: WebSocket\r\n';
connection.head += 'Sec-WebSocket-Accept: ' + handshake + '\r\n';
Expand Down
8 changes: 5 additions & 3 deletions lib/parsers/ws.js
Expand Up @@ -3,6 +3,8 @@
var stream = require('stream'),
utils = require('simples/utils/utils');

var usize = 65536;

var WsParser = function (limit, client) {

// Call stream.Writable in this context
Expand Down Expand Up @@ -245,9 +247,9 @@ WsParser.prototype.unmaskData = function () {

// Unmask frame data using 64KB chunks
function unmask() {
if (frame.length - index > 65535) {
utils.xor(frame.data.slice(index, index + 65535), frame.mask);
index += 65535;
if (frame.length - index > usize) {
utils.xor(frame.data.slice(index, index + usize), frame.mask);
index += usize;
setImmediate(unmask);
} else {

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "simples",
"version": "0.8.7-1",
"version": "0.8.8",
"description": "Simple Web Framework",
"keywords": [
"client",
Expand Down

0 comments on commit 5722b35

Please sign in to comment.