Skip to content

Instantly share code, notes, and snippets.

@nowelium
Created February 20, 2012 13:26
Show Gist options
  • Save nowelium/1869184 to your computer and use it in GitHub Desktop.
Save nowelium/1869184 to your computer and use it in GitHub Desktop.
todo ti-websocket-client RFC6455
//
// ti-websocket-client を RFC6455 に対応してみようとする試み。(未完成)
// * 下記を参考にtitanium的に置き換えてみる
// https://github.com/einaros/ws/blob/master/lib/Sender.js
// https://github.com/einaros/ws/blob/master/lib/BufferUtilWindows.js
//
WebSocket.prototype._create_frame = function(opcode, d, last_frame) {
if(typeof last_frame === 'undefined') {
last_frame = true;
}
if(last_frame === false && opcode >= 0x8 && opcode <= 0xf) {
return false;
}
var data = d || '';
var dataLength = Utils.byte_length(data);
var dataOffset = 2;
if(!this._masking_disabled){
dataOffset = 6;
}
var secondByte = dataLength;
if(BUFFER_SIZE <= dataLength){
dataOffset += 8;
secondByte = 127;
} else if(125 < dataLength){
dataOffset += 2;
secondByte = 126;
}
var totalLength = 0;
if(this._masking_disabled){
totalLength = dataOffset;
} else {
totalLength = dataLength + dataOffset;
}
var outputBuffer = Ti.createBuffer({ length: totalLength });
if(last_frame){
Ti.Codec.encodeNumber({
source: opcode | 0x80,
dest: outputBuffer,
position: 0,
type: Ti.Codec.TYPE_BYTE
});
} else {
Ti.Codec.encodeNumber({
source: opcode,
dest: outputBuffer,
position: 0,
type: Ti.Codec.TYPE_BYTE
});
}
if(this._masking_disabled){
Ti.Codec.encodeNumber({
source: secondByte,
dest: outputBuffer,
position: 1,
type: Ti.Codec.TYPE_BYTE
});
} else {
Ti.Codec.encodeNumber({
source: (secondByte | 0x80),
dest: outputBuffer,
position: 1,
type: Ti.Codec.TYPE_BYTE
});
}
switch(secondByte){
case 126:
Ti.Codec.encodeNumber({
source: dataLength,
dest: outputBuffer,
position: 2,
type: Ti.Codec.TYPE_SHORT,
byteOrder: Ti.Codec.BIG_ENDIAN
});
break;
case 127:
Ti.Codec.encodeNumber({
source: 0,
dest: outputBuffer,
position: 2,
type: Ti.Codec.TYPE_BYTE
});
Ti.Codec.encodeNumber({
source: dataLength,
dest: outputBuffer,
position: 6,
type: Ti.Codec.TYPE_LONG,
byteOrder: Ti.Codec.BIG_ENDIAN
});
}
var getRandomMask = function (){
var keys = [];
keys.push(Math.floor(Math.random() * 255) & 0xff);
keys.push(Math.floor(Math.random() * 255) & 0xff);
keys.push(Math.floor(Math.random() * 255) & 0xff);
keys.push(Math.floor(Math.random() * 255) & 0xff);
return keys;
};
var applyMask = function(source, masks, output, offset, length){
var buffer = Ti.createBuffer({ length: length });
Ti.Codec.encodeString({
source: source,
dest: buffer
});
var string = Ti.Codec.decodeString({
source: buffer,
charset: Ti.Codec.CHARSET_ASCII
});
var maskNum = masks.length;
var i = 0;
for(; i < length - 3; i += 4){
Ti.Codec.encodeNumber({
source: string.charCodeAt(i) ^ masks[i % maskNum],
dest: output,
position: offset + i,
type: Ti.Codec.TYPE_BYTE
});
}
switch(length % 4){
case 3:
Ti.Codec.encodeNumber({
source: source[i + 2] ^ masks[2],
dest: output,
position: offset + i + 2,
type: Ti.Codec.TYPE_BYTE
});
case 2:
Ti.Codec.encodeNumber({
source: source[i + 1] ^ masks[1],
dest: output,
position: offset + i + 1,
type: Ti.Codec.TYPE_BYTE
});
case 1:
Ti.Codec.encodeNumber({
source: source[i] ^ masks[0],
dest: output,
position: offset + i,
type: Ti.Codec.TYPE_BYTE
});
case 0:
break;
}
};
if(!this._masking_disabled){
var mask = getRandomMask();
Ti.Codec.encodeNumber({
source: mask[0],
dest: outputBuffer,
position: dataOffset - 4,
type: Ti.Codec.TYPE_BYTE
});
Ti.Codec.encodeNumber({
source: mask[1],
dest: outputBuffer,
position: dataOffset - 3,
type: Ti.Codec.TYPE_BYTE
});
Ti.Codec.encodeNumber({
source: mask[2],
dest: outputBuffer,
position: dataOffset - 2,
type: Ti.Codec.TYPE_BYTE
});
Ti.Codec.encodeNumber({
source: mask[3],
dest: outputBuffer,
position: dataOffset - 1,
type: Ti.Codec.TYPE_BYTE
});
applyMask(data, mask, outputBuffer, dataOffset, dataLength);
}
return outputBuffer;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment