Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fragmented messages support and server-to-client masked messages support #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions flash-src/src/net/gimite/websocket/WebSocket.as
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class WebSocket extends EventDispatcher {
private var logger:IWebSocketLogger;
private var base64Encoder:Base64Encoder = new Base64Encoder();

private var income_buffer:String = "";

public function WebSocket(
id:int, url:String, protocols:Array, origin:String,
proxyHost:String, proxyPort:int,
Expand Down Expand Up @@ -322,22 +324,30 @@ public class WebSocket extends EventDispatcher {
pos = -1;
if (frame.rsv != 0) {
close(1002, "RSV must be 0.");
} else if (frame.mask) {
close(1002, "Frame from server must not be masked.");
} else if (frame.opcode >= 0x08 && frame.opcode <= 0x0f && frame.payload.length >= 126) {
close(1004, "Payload of control frame must be less than 126 bytes.");
} else {
switch (frame.opcode) {
case OPCODE_CONTINUATION:
close(1003, "Received continuation frame, which is not implemented.");
break;
case OPCODE_TEXT:
var data:String = readUTFBytes(frame.payload, 0, frame.payload.length);
try {
this.dispatchEvent(new WebSocketEvent("message", encodeURIComponent(data)));
} catch (ex:URIError) {
close(1007, "URIError while encoding the received data.");
}
case OPCODE_TEXT:
if(frame.masking) {
for (var i:int = 0; i < frame.payload.length; i++) {
frame.payload[i] = frame.mask[i % 4] ^ frame.payload[i];
}
}
var data:String = readUTFBytes(frame.payload, 0, frame.payload.length);

this.income_buffer += data;

if(frame.fin) {
try {
this.dispatchEvent(new WebSocketEvent("message", encodeURIComponent(this.income_buffer)));
} catch (ex:URIError) {
close(1007, "URIError while encoding the received data.");
}

this.income_buffer = "";
}
break;
case OPCODE_BINARY:
// See https://github.com/gimite/web-socket-js/pull/89
Expand Down Expand Up @@ -493,9 +503,7 @@ public class WebSocket extends EventDispatcher {
frame.fin = (buffer[0] & 0x80) != 0;
frame.rsv = (buffer[0] & 0x70) >> 4;
frame.opcode = buffer[0] & 0x0f;
// Payload unmasking is not implemented because masking frames from server
// is not allowed. This field is used only for error checking.
frame.mask = (buffer[1] & 0x80) != 0;
frame.masking = (buffer[1] & 0x80) != 0;
plength = buffer[1] & 0x7f;

if (plength == 126) {
Expand Down Expand Up @@ -524,7 +532,16 @@ public class WebSocket extends EventDispatcher {
return null;
}

}
} else {
buffer.position = 2;
buffer.endian = Endian.BIG_ENDIAN;
}

if(frame.masking) {
frame.mask = new ByteArray();
frame.mask.writeUnsignedInt(buffer.readUnsignedInt());
hlength += 4;
}

if (buffer.length < hlength + plength) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion flash-src/src/net/gimite/websocket/WebSocketFrame.as
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class WebSocketFrame {

// Fields below are not used when used as a parameter of sendFrame().
public var length:uint = 0;
public var mask:Boolean = false;
public var masking:Boolean = false;
public var mask:ByteArray;

}

Expand Down