extension.js 6.5 KB

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