VUE_GabenParadise/node_modules/websocket-driver/lib/websocket/driver/draft76.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-08-10 12:35:19 +00:00
'use strict';
2020-08-13 08:31:15 +00:00
var Base = require('./base'),
2020-08-10 12:35:19 +00:00
Draft75 = require('./draft75'),
crypto = require('crypto'),
util = require('util');
var numberFromKey = function(key) {
2020-08-13 08:31:15 +00:00
return parseInt(key.match(/[0-9]/g).join(''), 10);
2020-08-10 12:35:19 +00:00
};
var spacesInKey = function(key) {
2020-08-13 08:31:15 +00:00
return key.match(/ /g).length;
2020-08-10 12:35:19 +00:00
};
var Draft76 = function(request, url, options) {
Draft75.apply(this, arguments);
this._stage = -1;
this._body = [];
this.version = 'hixie-76';
this._headers.clear();
this._headers.set('Upgrade', 'WebSocket');
this._headers.set('Connection', 'Upgrade');
this._headers.set('Sec-WebSocket-Origin', this._request.headers.origin);
this._headers.set('Sec-WebSocket-Location', this.url);
};
util.inherits(Draft76, Draft75);
var instance = {
BODY_SIZE: 8,
start: function() {
if (!Draft75.prototype.start.call(this)) return false;
this._started = true;
this._sendHandshakeBody();
return true;
},
close: function() {
if (this.readyState === 3) return false;
2020-08-13 08:31:15 +00:00
this._write(new Buffer([0xFF, 0x00]));
2020-08-10 12:35:19 +00:00
this.readyState = 3;
this.emit('close', new Base.CloseEvent(null, null));
return true;
},
_handshakeResponse: function() {
var headers = this._request.headers,
2020-08-13 08:31:15 +00:00
key1 = headers['sec-websocket-key1'],
number1 = numberFromKey(key1),
2020-08-10 12:35:19 +00:00
spaces1 = spacesInKey(key1),
2020-08-13 08:31:15 +00:00
key2 = headers['sec-websocket-key2'],
2020-08-10 12:35:19 +00:00
number2 = numberFromKey(key2),
spaces2 = spacesInKey(key2);
2020-08-13 08:31:15 +00:00
if (number1 % spaces1 !== 0 || number2 % spaces2 !== 0) {
this.emit('error', new Error('Client sent invalid Sec-WebSocket-Key headers'));
this.close();
return null;
}
2020-08-10 12:35:19 +00:00
this._keyValues = [number1 / spaces1, number2 / spaces2];
var start = 'HTTP/1.1 101 WebSocket Protocol Handshake',
headers = [start, this._headers.toString(), ''];
2020-08-13 08:31:15 +00:00
return new Buffer(headers.join('\r\n'), 'binary');
2020-08-10 12:35:19 +00:00
},
_handshakeSignature: function() {
if (this._body.length < this.BODY_SIZE) return null;
var md5 = crypto.createHash('md5'),
2020-08-13 08:31:15 +00:00
buffer = new Buffer(8 + this.BODY_SIZE);
2020-08-10 12:35:19 +00:00
buffer.writeUInt32BE(this._keyValues[0], 0);
buffer.writeUInt32BE(this._keyValues[1], 4);
2020-08-13 08:31:15 +00:00
new Buffer(this._body).copy(buffer, 8, 0, this.BODY_SIZE);
2020-08-10 12:35:19 +00:00
md5.update(buffer);
2020-08-13 08:31:15 +00:00
return new Buffer(md5.digest('binary'), 'binary');
2020-08-10 12:35:19 +00:00
},
_sendHandshakeBody: function() {
if (!this._started) return;
var signature = this._handshakeSignature();
if (!signature) return;
this._write(signature);
this._stage = 0;
this._open();
if (this._body.length > this.BODY_SIZE)
this.parse(this._body.slice(this.BODY_SIZE));
},
_parseLeadingByte: function(octet) {
if (octet !== 0xFF)
return Draft75.prototype._parseLeadingByte.call(this, octet);
this._closing = true;
this._length = 0;
this._stage = 1;
}
};
for (var key in instance)
Draft76.prototype[key] = instance[key];
module.exports = Draft76;