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.

223 lines
6.7 KiB

  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 (dest[name] === undefined) dest[name] = [elem];
  36. else dest[name].push(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 = Object.create(null);
  47. if (header === undefined || header === '') return offers;
  48. let params = Object.create(null);
  49. let mustUnescape = false;
  50. let isEscaping = false;
  51. let inQuotes = false;
  52. let extensionName;
  53. let paramName;
  54. let start = -1;
  55. let end = -1;
  56. let i = 0;
  57. for (; i < header.length; i++) {
  58. const code = header.charCodeAt(i);
  59. if (extensionName === undefined) {
  60. if (end === -1 && tokenChars[code] === 1) {
  61. if (start === -1) start = i;
  62. } else if (code === 0x20 /* ' ' */ || code === 0x09 /* '\t' */) {
  63. if (end === -1 && start !== -1) end = i;
  64. } else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
  65. if (start === -1) {
  66. throw new SyntaxError(`Unexpected character at index ${i}`);
  67. }
  68. if (end === -1) end = i;
  69. const name = header.slice(start, end);
  70. if (code === 0x2c) {
  71. push(offers, name, params);
  72. params = Object.create(null);
  73. } else {
  74. extensionName = name;
  75. }
  76. start = end = -1;
  77. } else {
  78. throw new SyntaxError(`Unexpected character at index ${i}`);
  79. }
  80. } else if (paramName === undefined) {
  81. if (end === -1 && tokenChars[code] === 1) {
  82. if (start === -1) start = i;
  83. } else if (code === 0x20 || code === 0x09) {
  84. if (end === -1 && start !== -1) end = i;
  85. } else if (code === 0x3b || code === 0x2c) {
  86. if (start === -1) {
  87. throw new SyntaxError(`Unexpected character at index ${i}`);
  88. }
  89. if (end === -1) end = i;
  90. push(params, header.slice(start, end), true);
  91. if (code === 0x2c) {
  92. push(offers, extensionName, params);
  93. params = Object.create(null);
  94. extensionName = undefined;
  95. }
  96. start = end = -1;
  97. } else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
  98. paramName = header.slice(start, i);
  99. start = end = -1;
  100. } else {
  101. throw new SyntaxError(`Unexpected character at index ${i}`);
  102. }
  103. } else {
  104. //
  105. // The value of a quoted-string after unescaping must conform to the
  106. // token ABNF, so only token characters are valid.
  107. // Ref: https://tools.ietf.org/html/rfc6455#section-9.1
  108. //
  109. if (isEscaping) {
  110. if (tokenChars[code] !== 1) {
  111. throw new SyntaxError(`Unexpected character at index ${i}`);
  112. }
  113. if (start === -1) start = i;
  114. else if (!mustUnescape) mustUnescape = true;
  115. isEscaping = false;
  116. } else if (inQuotes) {
  117. if (tokenChars[code] === 1) {
  118. if (start === -1) start = i;
  119. } else if (code === 0x22 /* '"' */ && start !== -1) {
  120. inQuotes = false;
  121. end = i;
  122. } else if (code === 0x5c /* '\' */) {
  123. isEscaping = true;
  124. } else {
  125. throw new SyntaxError(`Unexpected character at index ${i}`);
  126. }
  127. } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
  128. inQuotes = true;
  129. } else if (end === -1 && tokenChars[code] === 1) {
  130. if (start === -1) start = i;
  131. } else if (start !== -1 && (code === 0x20 || code === 0x09)) {
  132. if (end === -1) end = i;
  133. } else if (code === 0x3b || code === 0x2c) {
  134. if (start === -1) {
  135. throw new SyntaxError(`Unexpected character at index ${i}`);
  136. }
  137. if (end === -1) end = i;
  138. let value = header.slice(start, end);
  139. if (mustUnescape) {
  140. value = value.replace(/\\/g, '');
  141. mustUnescape = false;
  142. }
  143. push(params, paramName, value);
  144. if (code === 0x2c) {
  145. push(offers, extensionName, params);
  146. params = Object.create(null);
  147. extensionName = undefined;
  148. }
  149. paramName = undefined;
  150. start = end = -1;
  151. } else {
  152. throw new SyntaxError(`Unexpected character at index ${i}`);
  153. }
  154. }
  155. }
  156. if (start === -1 || inQuotes) {
  157. throw new SyntaxError('Unexpected end of input');
  158. }
  159. if (end === -1) end = i;
  160. const token = header.slice(start, end);
  161. if (extensionName === undefined) {
  162. push(offers, token, params);
  163. } else {
  164. if (paramName === undefined) {
  165. push(params, token, true);
  166. } else if (mustUnescape) {
  167. push(params, paramName, token.replace(/\\/g, ''));
  168. } else {
  169. push(params, paramName, token);
  170. }
  171. push(offers, extensionName, params);
  172. }
  173. return offers;
  174. }
  175. /**
  176. * Builds the `Sec-WebSocket-Extensions` header field value.
  177. *
  178. * @param {Object} extensions The map of extensions and parameters to format
  179. * @return {String} A string representing the given object
  180. * @public
  181. */
  182. function format(extensions) {
  183. return Object.keys(extensions)
  184. .map((extension) => {
  185. let configurations = extensions[extension];
  186. if (!Array.isArray(configurations)) configurations = [configurations];
  187. return configurations
  188. .map((params) => {
  189. return [extension]
  190. .concat(
  191. Object.keys(params).map((k) => {
  192. let values = params[k];
  193. if (!Array.isArray(values)) values = [values];
  194. return values
  195. .map((v) => (v === true ? k : `${k}=${v}`))
  196. .join('; ');
  197. })
  198. )
  199. .join('; ');
  200. })
  201. .join(', ');
  202. })
  203. .join(', ');
  204. }
  205. module.exports = { format, parse };