WebSocketFrame.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /************************************************************************
  2. * Copyright 2010-2015 Brian McKelvey.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ***********************************************************************/
  16. var bufferUtil = require('./BufferUtil').BufferUtil;
  17. var bufferAllocUnsafe = require('./utils').bufferAllocUnsafe;
  18. const DECODE_HEADER = 1;
  19. const WAITING_FOR_16_BIT_LENGTH = 2;
  20. const WAITING_FOR_64_BIT_LENGTH = 3;
  21. const WAITING_FOR_MASK_KEY = 4;
  22. const WAITING_FOR_PAYLOAD = 5;
  23. const COMPLETE = 6;
  24. // WebSocketConnection will pass shared buffer objects for maskBytes and
  25. // frameHeader into the constructor to avoid tons of small memory allocations
  26. // for each frame we have to parse. This is only used for parsing frames
  27. // we receive off the wire.
  28. function WebSocketFrame(maskBytes, frameHeader, config) {
  29. this.maskBytes = maskBytes;
  30. this.frameHeader = frameHeader;
  31. this.config = config;
  32. this.maxReceivedFrameSize = config.maxReceivedFrameSize;
  33. this.protocolError = false;
  34. this.frameTooLarge = false;
  35. this.invalidCloseFrameLength = false;
  36. this.parseState = DECODE_HEADER;
  37. this.closeStatus = -1;
  38. }
  39. WebSocketFrame.prototype.addData = function(bufferList) {
  40. if (this.parseState === DECODE_HEADER) {
  41. if (bufferList.length >= 2) {
  42. bufferList.joinInto(this.frameHeader, 0, 0, 2);
  43. bufferList.advance(2);
  44. var firstByte = this.frameHeader[0];
  45. var secondByte = this.frameHeader[1];
  46. this.fin = Boolean(firstByte & 0x80);
  47. this.rsv1 = Boolean(firstByte & 0x40);
  48. this.rsv2 = Boolean(firstByte & 0x20);
  49. this.rsv3 = Boolean(firstByte & 0x10);
  50. this.mask = Boolean(secondByte & 0x80);
  51. this.opcode = firstByte & 0x0F;
  52. this.length = secondByte & 0x7F;
  53. // Control frame sanity check
  54. if (this.opcode >= 0x08) {
  55. if (this.length > 125) {
  56. this.protocolError = true;
  57. this.dropReason = 'Illegal control frame longer than 125 bytes.';
  58. return true;
  59. }
  60. if (!this.fin) {
  61. this.protocolError = true;
  62. this.dropReason = 'Control frames must not be fragmented.';
  63. return true;
  64. }
  65. }
  66. if (this.length === 126) {
  67. this.parseState = WAITING_FOR_16_BIT_LENGTH;
  68. }
  69. else if (this.length === 127) {
  70. this.parseState = WAITING_FOR_64_BIT_LENGTH;
  71. }
  72. else {
  73. this.parseState = WAITING_FOR_MASK_KEY;
  74. }
  75. }
  76. }
  77. if (this.parseState === WAITING_FOR_16_BIT_LENGTH) {
  78. if (bufferList.length >= 2) {
  79. bufferList.joinInto(this.frameHeader, 2, 0, 2);
  80. bufferList.advance(2);
  81. this.length = this.frameHeader.readUInt16BE(2);
  82. this.parseState = WAITING_FOR_MASK_KEY;
  83. }
  84. }
  85. else if (this.parseState === WAITING_FOR_64_BIT_LENGTH) {
  86. if (bufferList.length >= 8) {
  87. bufferList.joinInto(this.frameHeader, 2, 0, 8);
  88. bufferList.advance(8);
  89. var lengthPair = [
  90. this.frameHeader.readUInt32BE(2),
  91. this.frameHeader.readUInt32BE(2+4)
  92. ];
  93. if (lengthPair[0] !== 0) {
  94. this.protocolError = true;
  95. this.dropReason = 'Unsupported 64-bit length frame received';
  96. return true;
  97. }
  98. this.length = lengthPair[1];
  99. this.parseState = WAITING_FOR_MASK_KEY;
  100. }
  101. }
  102. if (this.parseState === WAITING_FOR_MASK_KEY) {
  103. if (this.mask) {
  104. if (bufferList.length >= 4) {
  105. bufferList.joinInto(this.maskBytes, 0, 0, 4);
  106. bufferList.advance(4);
  107. this.parseState = WAITING_FOR_PAYLOAD;
  108. }
  109. }
  110. else {
  111. this.parseState = WAITING_FOR_PAYLOAD;
  112. }
  113. }
  114. if (this.parseState === WAITING_FOR_PAYLOAD) {
  115. if (this.length > this.maxReceivedFrameSize) {
  116. this.frameTooLarge = true;
  117. this.dropReason = 'Frame size of ' + this.length.toString(10) +
  118. ' bytes exceeds maximum accepted frame size';
  119. return true;
  120. }
  121. if (this.length === 0) {
  122. this.binaryPayload = bufferAllocUnsafe(0);
  123. this.parseState = COMPLETE;
  124. return true;
  125. }
  126. if (bufferList.length >= this.length) {
  127. this.binaryPayload = bufferList.take(this.length);
  128. bufferList.advance(this.length);
  129. if (this.mask) {
  130. bufferUtil.unmask(this.binaryPayload, this.maskBytes);
  131. // xor(this.binaryPayload, this.maskBytes, 0);
  132. }
  133. if (this.opcode === 0x08) { // WebSocketOpcode.CONNECTION_CLOSE
  134. if (this.length === 1) {
  135. // Invalid length for a close frame. Must be zero or at least two.
  136. this.binaryPayload = bufferAllocUnsafe(0);
  137. this.invalidCloseFrameLength = true;
  138. }
  139. if (this.length >= 2) {
  140. this.closeStatus = this.binaryPayload.readUInt16BE(0);
  141. this.binaryPayload = this.binaryPayload.slice(2);
  142. }
  143. }
  144. this.parseState = COMPLETE;
  145. return true;
  146. }
  147. }
  148. return false;
  149. };
  150. WebSocketFrame.prototype.throwAwayPayload = function(bufferList) {
  151. if (bufferList.length >= this.length) {
  152. bufferList.advance(this.length);
  153. this.parseState = COMPLETE;
  154. return true;
  155. }
  156. return false;
  157. };
  158. WebSocketFrame.prototype.toBuffer = function(nullMask) {
  159. var maskKey;
  160. var headerLength = 2;
  161. var data;
  162. var outputPos;
  163. var firstByte = 0x00;
  164. var secondByte = 0x00;
  165. if (this.fin) {
  166. firstByte |= 0x80;
  167. }
  168. if (this.rsv1) {
  169. firstByte |= 0x40;
  170. }
  171. if (this.rsv2) {
  172. firstByte |= 0x20;
  173. }
  174. if (this.rsv3) {
  175. firstByte |= 0x10;
  176. }
  177. if (this.mask) {
  178. secondByte |= 0x80;
  179. }
  180. firstByte |= (this.opcode & 0x0F);
  181. // the close frame is a special case because the close reason is
  182. // prepended to the payload data.
  183. if (this.opcode === 0x08) {
  184. this.length = 2;
  185. if (this.binaryPayload) {
  186. this.length += this.binaryPayload.length;
  187. }
  188. data = bufferAllocUnsafe(this.length);
  189. data.writeUInt16BE(this.closeStatus, 0);
  190. if (this.length > 2) {
  191. this.binaryPayload.copy(data, 2);
  192. }
  193. }
  194. else if (this.binaryPayload) {
  195. data = this.binaryPayload;
  196. this.length = data.length;
  197. }
  198. else {
  199. this.length = 0;
  200. }
  201. if (this.length <= 125) {
  202. // encode the length directly into the two-byte frame header
  203. secondByte |= (this.length & 0x7F);
  204. }
  205. else if (this.length > 125 && this.length <= 0xFFFF) {
  206. // Use 16-bit length
  207. secondByte |= 126;
  208. headerLength += 2;
  209. }
  210. else if (this.length > 0xFFFF) {
  211. // Use 64-bit length
  212. secondByte |= 127;
  213. headerLength += 8;
  214. }
  215. var output = bufferAllocUnsafe(this.length + headerLength + (this.mask ? 4 : 0));
  216. // write the frame header
  217. output[0] = firstByte;
  218. output[1] = secondByte;
  219. outputPos = 2;
  220. if (this.length > 125 && this.length <= 0xFFFF) {
  221. // write 16-bit length
  222. output.writeUInt16BE(this.length, outputPos);
  223. outputPos += 2;
  224. }
  225. else if (this.length > 0xFFFF) {
  226. // write 64-bit length
  227. output.writeUInt32BE(0x00000000, outputPos);
  228. output.writeUInt32BE(this.length, outputPos + 4);
  229. outputPos += 8;
  230. }
  231. if (this.mask) {
  232. maskKey = nullMask ? 0 : ((Math.random() * 0xFFFFFFFF) >>> 0);
  233. this.maskBytes.writeUInt32BE(maskKey, 0);
  234. // write the mask key
  235. this.maskBytes.copy(output, outputPos);
  236. outputPos += 4;
  237. if (data) {
  238. bufferUtil.mask(data, this.maskBytes, output, outputPos, this.length);
  239. }
  240. }
  241. else if (data) {
  242. data.copy(output, outputPos);
  243. }
  244. return output;
  245. };
  246. WebSocketFrame.prototype.toString = function() {
  247. return 'Opcode: ' + this.opcode + ', fin: ' + this.fin + ', length: ' + this.length + ', hasPayload: ' + Boolean(this.binaryPayload) + ', masked: ' + this.mask;
  248. };
  249. module.exports = WebSocketFrame;