暫無描述
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. var TOKEN = /([!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+)/,
  3. NOTOKEN = /([^!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z])/g,
  4. QUOTED = /"((?:\\[\x00-\x7f]|[^\x00-\x08\x0a-\x1f\x7f"\\])*)"/,
  5. PARAM = new RegExp(TOKEN.source + '(?:=(?:' + TOKEN.source + '|' + QUOTED.source + '))?'),
  6. EXT = new RegExp(TOKEN.source + '(?: *; *' + PARAM.source + ')*', 'g'),
  7. EXT_LIST = new RegExp('^' + EXT.source + '(?: *, *' + EXT.source + ')*$'),
  8. NUMBER = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?$/;
  9. var hasOwnProperty = Object.prototype.hasOwnProperty;
  10. var Parser = {
  11. parseHeader: function(header) {
  12. var offers = new Offers();
  13. if (header === '' || header === undefined) return offers;
  14. if (!EXT_LIST.test(header))
  15. throw new SyntaxError('Invalid Sec-WebSocket-Extensions header: ' + header);
  16. var values = header.match(EXT);
  17. values.forEach(function(value) {
  18. var params = value.match(new RegExp(PARAM.source, 'g')),
  19. name = params.shift(),
  20. offer = {};
  21. params.forEach(function(param) {
  22. var args = param.match(PARAM), key = args[1], data;
  23. if (args[2] !== undefined) {
  24. data = args[2];
  25. } else if (args[3] !== undefined) {
  26. data = args[3].replace(/\\/g, '');
  27. } else {
  28. data = true;
  29. }
  30. if (NUMBER.test(data)) data = parseFloat(data);
  31. if (hasOwnProperty.call(offer, key)) {
  32. offer[key] = [].concat(offer[key]);
  33. offer[key].push(data);
  34. } else {
  35. offer[key] = data;
  36. }
  37. }, this);
  38. offers.push(name, offer);
  39. }, this);
  40. return offers;
  41. },
  42. serializeParams: function(name, params) {
  43. var values = [];
  44. var print = function(key, value) {
  45. if (value instanceof Array) {
  46. value.forEach(function(v) { print(key, v) });
  47. } else if (value === true) {
  48. values.push(key);
  49. } else if (typeof value === 'number') {
  50. values.push(key + '=' + value);
  51. } else if (NOTOKEN.test(value)) {
  52. values.push(key + '="' + value.replace(/"/g, '\\"') + '"');
  53. } else {
  54. values.push(key + '=' + value);
  55. }
  56. };
  57. for (var key in params) print(key, params[key]);
  58. return [name].concat(values).join('; ');
  59. }
  60. };
  61. var Offers = function() {
  62. this._byName = {};
  63. this._inOrder = [];
  64. };
  65. Offers.prototype.push = function(name, params) {
  66. if (!hasOwnProperty.call(this._byName, name))
  67. this._byName[name] = [];
  68. this._byName[name].push(params);
  69. this._inOrder.push({ name: name, params: params });
  70. };
  71. Offers.prototype.eachOffer = function(callback, context) {
  72. var list = this._inOrder;
  73. for (var i = 0, n = list.length; i < n; i++)
  74. callback.call(context, list[i].name, list[i].params);
  75. };
  76. Offers.prototype.byName = function(name) {
  77. return this._byName[name] || [];
  78. };
  79. Offers.prototype.toArray = function() {
  80. return this._inOrder.slice();
  81. };
  82. module.exports = Parser;