You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.1 KiB

4 years ago
  1. 'use strict';
  2. var Buffer = require('safe-buffer').Buffer,
  3. Base = require('./base'),
  4. Draft75 = require('./draft75'),
  5. crypto = require('crypto'),
  6. util = require('util');
  7. var numberFromKey = function(key) {
  8. return parseInt((key.match(/[0-9]/g) || []).join(''), 10);
  9. };
  10. var spacesInKey = function(key) {
  11. return (key.match(/ /g) || []).length;
  12. };
  13. var Draft76 = function(request, url, options) {
  14. Draft75.apply(this, arguments);
  15. this._stage = -1;
  16. this._body = [];
  17. this.version = 'hixie-76';
  18. this._headers.clear();
  19. this._headers.set('Upgrade', 'WebSocket');
  20. this._headers.set('Connection', 'Upgrade');
  21. this._headers.set('Sec-WebSocket-Origin', this._request.headers.origin);
  22. this._headers.set('Sec-WebSocket-Location', this.url);
  23. };
  24. util.inherits(Draft76, Draft75);
  25. var instance = {
  26. BODY_SIZE: 8,
  27. start: function() {
  28. if (!Draft75.prototype.start.call(this)) return false;
  29. this._started = true;
  30. this._sendHandshakeBody();
  31. return true;
  32. },
  33. close: function() {
  34. if (this.readyState === 3) return false;
  35. if (this.readyState === 1) this._write(Buffer.from([0xFF, 0x00]));
  36. this.readyState = 3;
  37. this.emit('close', new Base.CloseEvent(null, null));
  38. return true;
  39. },
  40. _handshakeResponse: function() {
  41. var headers = this._request.headers,
  42. key1 = headers['sec-websocket-key1'],
  43. key2 = headers['sec-websocket-key2'];
  44. if (!key1) throw new Error('Missing required header: Sec-WebSocket-Key1');
  45. if (!key2) throw new Error('Missing required header: Sec-WebSocket-Key2');
  46. var number1 = numberFromKey(key1),
  47. spaces1 = spacesInKey(key1),
  48. number2 = numberFromKey(key2),
  49. spaces2 = spacesInKey(key2);
  50. if (number1 % spaces1 !== 0 || number2 % spaces2 !== 0)
  51. throw new Error('Client sent invalid Sec-WebSocket-Key headers');
  52. this._keyValues = [number1 / spaces1, number2 / spaces2];
  53. var start = 'HTTP/1.1 101 WebSocket Protocol Handshake',
  54. headers = [start, this._headers.toString(), ''];
  55. return Buffer.from(headers.join('\r\n'), 'binary');
  56. },
  57. _handshakeSignature: function() {
  58. if (this._body.length < this.BODY_SIZE) return null;
  59. var md5 = crypto.createHash('md5'),
  60. buffer = Buffer.allocUnsafe(8 + this.BODY_SIZE);
  61. buffer.writeUInt32BE(this._keyValues[0], 0);
  62. buffer.writeUInt32BE(this._keyValues[1], 4);
  63. Buffer.from(this._body).copy(buffer, 8, 0, this.BODY_SIZE);
  64. md5.update(buffer);
  65. return Buffer.from(md5.digest('binary'), 'binary');
  66. },
  67. _sendHandshakeBody: function() {
  68. if (!this._started) return;
  69. var signature = this._handshakeSignature();
  70. if (!signature) return;
  71. this._write(signature);
  72. this._stage = 0;
  73. this._open();
  74. if (this._body.length > this.BODY_SIZE)
  75. this.parse(this._body.slice(this.BODY_SIZE));
  76. },
  77. _parseLeadingByte: function(octet) {
  78. if (octet !== 0xFF)
  79. return Draft75.prototype._parseLeadingByte.call(this, octet);
  80. this._closing = true;
  81. this._length = 0;
  82. this._stage = 1;
  83. }
  84. };
  85. for (var key in instance)
  86. Draft76.prototype[key] = instance[key];
  87. module.exports = Draft76;