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.

222 lines
6.7 KiB

4 years ago
  1. 'use strict';
  2. //
  3. // Allowed token characters:
  4. //
  5. // '!', '#', '$', '%', '&', ''', '*', '+', '-',
  6. // '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
  7. //
  8. // tokenChars[32] === 0 // ' '
  9. // tokenChars[33] === 1 // '!'
  10. // tokenChars[34] === 0 // '"'
  11. // ...
  12. //
  13. // prettier-ignore
  14. const tokenChars = [
  15. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
  17. 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
  18. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
  19. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
  20. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
  21. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
  22. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
  23. ];
  24. /**
  25. * Adds an offer to the map of extension offers or a parameter to the map of
  26. * parameters.
  27. *
  28. * @param {Object} dest The map of extension offers or parameters
  29. * @param {String} name The extension or parameter name
  30. * @param {(Object|Boolean|String)} elem The extension parameters or the
  31. * parameter value
  32. * @private
  33. */
  34. function push(dest, name, elem) {
  35. if (Object.prototype.hasOwnProperty.call(dest, name)) dest[name].push(elem);
  36. else dest[name] = [elem];
  37. }
  38. /**
  39. * Parses the `Sec-WebSocket-Extensions` header into an object.
  40. *
  41. * @param {String} header The field value of the header
  42. * @return {Object} The parsed object
  43. * @public
  44. */
  45. function parse(header) {
  46. const offers = {};
  47. if (header === undefined || header === '') return offers;
  48. var params = {};
  49. var mustUnescape = false;
  50. var isEscaping = false;
  51. var inQuotes = false;
  52. var extensionName;
  53. var paramName;
  54. var start = -1;
  55. var end = -1;
  56. for (var i = 0; i < header.length; i++) {
  57. const code = header.charCodeAt(i);
  58. if (extensionName === undefined) {
  59. if (end === -1 && tokenChars[code] === 1) {
  60. if (start === -1) start = i;
  61. } else if (code === 0x20 /* ' ' */ || code === 0x09 /* '\t' */) {
  62. if (end === -1 && start !== -1) end = i;
  63. } else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
  64. if (start === -1) {
  65. throw new SyntaxError(`Unexpected character at index ${i}`);
  66. }
  67. if (end === -1) end = i;
  68. const name = header.slice(start, end);
  69. if (code === 0x2c) {
  70. push(offers, name, params);
  71. params = {};
  72. } else {
  73. extensionName = name;
  74. }
  75. start = end = -1;
  76. } else {
  77. throw new SyntaxError(`Unexpected character at index ${i}`);
  78. }
  79. } else if (paramName === undefined) {
  80. if (end === -1 && tokenChars[code] === 1) {
  81. if (start === -1) start = i;
  82. } else if (code === 0x20 || code === 0x09) {
  83. if (end === -1 && start !== -1) end = i;
  84. } else if (code === 0x3b || code === 0x2c) {
  85. if (start === -1) {
  86. throw new SyntaxError(`Unexpected character at index ${i}`);
  87. }
  88. if (end === -1) end = i;
  89. push(params, header.slice(start, end), true);
  90. if (code === 0x2c) {
  91. push(offers, extensionName, params);
  92. params = {};
  93. extensionName = undefined;
  94. }
  95. start = end = -1;
  96. } else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
  97. paramName = header.slice(start, i);
  98. start = end = -1;
  99. } else {
  100. throw new SyntaxError(`Unexpected character at index ${i}`);
  101. }
  102. } else {
  103. //
  104. // The value of a quoted-string after unescaping must conform to the
  105. // token ABNF, so only token characters are valid.
  106. // Ref: https://tools.ietf.org/html/rfc6455#section-9.1
  107. //
  108. if (isEscaping) {
  109. if (tokenChars[code] !== 1) {
  110. throw new SyntaxError(`Unexpected character at index ${i}`);
  111. }
  112. if (start === -1) start = i;
  113. else if (!mustUnescape) mustUnescape = true;
  114. isEscaping = false;
  115. } else if (inQuotes) {
  116. if (tokenChars[code] === 1) {
  117. if (start === -1) start = i;
  118. } else if (code === 0x22 /* '"' */ && start !== -1) {
  119. inQuotes = false;
  120. end = i;
  121. } else if (code === 0x5c /* '\' */) {
  122. isEscaping = true;
  123. } else {
  124. throw new SyntaxError(`Unexpected character at index ${i}`);
  125. }
  126. } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
  127. inQuotes = true;
  128. } else if (end === -1 && tokenChars[code] === 1) {
  129. if (start === -1) start = i;
  130. } else if (start !== -1 && (code === 0x20 || code === 0x09)) {
  131. if (end === -1) end = i;
  132. } else if (code === 0x3b || code === 0x2c) {
  133. if (start === -1) {
  134. throw new SyntaxError(`Unexpected character at index ${i}`);
  135. }
  136. if (end === -1) end = i;
  137. var value = header.slice(start, end);
  138. if (mustUnescape) {
  139. value = value.replace(/\\/g, '');
  140. mustUnescape = false;
  141. }
  142. push(params, paramName, value);
  143. if (code === 0x2c) {
  144. push(offers, extensionName, params);
  145. params = {};
  146. extensionName = undefined;
  147. }
  148. paramName = undefined;
  149. start = end = -1;
  150. } else {
  151. throw new SyntaxError(`Unexpected character at index ${i}`);
  152. }
  153. }
  154. }
  155. if (start === -1 || inQuotes) {
  156. throw new SyntaxError('Unexpected end of input');
  157. }
  158. if (end === -1) end = i;
  159. const token = header.slice(start, end);
  160. if (extensionName === undefined) {
  161. push(offers, token, {});
  162. } else {
  163. if (paramName === undefined) {
  164. push(params, token, true);
  165. } else if (mustUnescape) {
  166. push(params, paramName, token.replace(/\\/g, ''));
  167. } else {
  168. push(params, paramName, token);
  169. }
  170. push(offers, extensionName, params);
  171. }
  172. return offers;
  173. }
  174. /**
  175. * Builds the `Sec-WebSocket-Extensions` header field value.
  176. *
  177. * @param {Object} extensions The map of extensions and parameters to format
  178. * @return {String} A string representing the given object
  179. * @public
  180. */
  181. function format(extensions) {
  182. return Object.keys(extensions)
  183. .map((extension) => {
  184. var configurations = extensions[extension];
  185. if (!Array.isArray(configurations)) configurations = [configurations];
  186. return configurations
  187. .map((params) => {
  188. return [extension]
  189. .concat(
  190. Object.keys(params).map((k) => {
  191. var values = params[k];
  192. if (!Array.isArray(values)) values = [values];
  193. return values
  194. .map((v) => (v === true ? k : `${k}=${v}`))
  195. .join('; ');
  196. })
  197. )
  198. .join('; ');
  199. })
  200. .join(', ');
  201. })
  202. .join(', ');
  203. }
  204. module.exports = { format, parse };