content_disposition.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  27. function getFilenameFromContentDispositionHeader(contentDisposition) {
  28. var needsEncodingFixup = true;
  29. var tmp = toParamRegExp('filename\\*', 'i').exec(contentDisposition);
  30. if (tmp) {
  31. tmp = tmp[1];
  32. var filename = rfc2616unquote(tmp);
  33. filename = unescape(filename);
  34. filename = rfc5987decode(filename);
  35. filename = rfc2047decode(filename);
  36. return fixupEncoding(filename);
  37. }
  38. tmp = rfc2231getparam(contentDisposition);
  39. if (tmp) {
  40. var _filename = rfc2047decode(tmp);
  41. return fixupEncoding(_filename);
  42. }
  43. tmp = toParamRegExp('filename', 'i').exec(contentDisposition);
  44. if (tmp) {
  45. tmp = tmp[1];
  46. var _filename2 = rfc2616unquote(tmp);
  47. _filename2 = rfc2047decode(_filename2);
  48. return fixupEncoding(_filename2);
  49. }
  50. function toParamRegExp(attributePattern, flags) {
  51. return new RegExp('(?:^|;)\\s*' + attributePattern + '\\s*=\\s*' + '(' + '[^";\\s][^;\\s]*' + '|' + '"(?:[^"\\\\]|\\\\"?)+"?' + ')', flags);
  52. }
  53. function textdecode(encoding, value) {
  54. if (encoding) {
  55. if (!/^[^\x00-\xFF]+$/.test(value)) {
  56. return value;
  57. }
  58. try {
  59. var decoder = new TextDecoder(encoding, { fatal: true });
  60. var bytes = new Array(value.length);
  61. for (var i = 0; i < value.length; ++i) {
  62. bytes[i] = value.charCodeAt(0);
  63. }
  64. value = decoder.decode(new Uint8Array(bytes));
  65. needsEncodingFixup = false;
  66. } catch (e) {
  67. if (/^utf-?8$/i.test(encoding)) {
  68. value = decodeURIComponent(escape(value));
  69. needsEncodingFixup = false;
  70. }
  71. }
  72. }
  73. return value;
  74. }
  75. function fixupEncoding(value) {
  76. if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {
  77. return textdecode('utf-8', value);
  78. }
  79. return value;
  80. }
  81. function rfc2231getparam(contentDisposition) {
  82. var matches = [],
  83. match = void 0;
  84. var iter = toParamRegExp('filename\\*((?!0\\d)\\d+)(\\*?)', 'ig');
  85. while ((match = iter.exec(contentDisposition)) !== null) {
  86. var _match = match,
  87. _match2 = _slicedToArray(_match, 4),
  88. n = _match2[1],
  89. quot = _match2[2],
  90. part = _match2[3];
  91. n = parseInt(n, 10);
  92. if (n in matches) {
  93. if (n === 0) {
  94. break;
  95. }
  96. continue;
  97. }
  98. matches[n] = [quot, part];
  99. }
  100. var parts = [];
  101. for (var _n = 0; _n < matches.length; ++_n) {
  102. if (!(_n in matches)) {
  103. break;
  104. }
  105. var _matches$_n = _slicedToArray(matches[_n], 2),
  106. _quot = _matches$_n[0],
  107. _part = _matches$_n[1];
  108. _part = rfc2616unquote(_part);
  109. if (_quot) {
  110. _part = unescape(_part);
  111. if (_n === 0) {
  112. _part = rfc5987decode(_part);
  113. }
  114. }
  115. parts.push(_part);
  116. }
  117. return parts.join('');
  118. }
  119. function rfc2616unquote(value) {
  120. if (value.charAt(0) === '"') {
  121. var parts = value.slice(1).split('\\"');
  122. for (var i = 0; i < parts.length; ++i) {
  123. var quotindex = parts[i].indexOf('"');
  124. if (quotindex !== -1) {
  125. parts[i] = parts[i].slice(0, quotindex);
  126. parts.length = i + 1;
  127. }
  128. parts[i] = parts[i].replace(/\\(.)/g, '$1');
  129. }
  130. value = parts.join('"');
  131. }
  132. return value;
  133. }
  134. function rfc5987decode(extvalue) {
  135. var encodingend = extvalue.indexOf('\'');
  136. if (encodingend === -1) {
  137. return extvalue;
  138. }
  139. var encoding = extvalue.slice(0, encodingend);
  140. var langvalue = extvalue.slice(encodingend + 1);
  141. var value = langvalue.replace(/^[^']*'/, '');
  142. return textdecode(encoding, value);
  143. }
  144. function rfc2047decode(value) {
  145. if (value.slice(0, 2) !== '=?' || /[\x00-\x19\x80-\xff]/.test(value)) {
  146. return value;
  147. }
  148. return value.replace(/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g, function (_, charset, encoding, text) {
  149. if (encoding === 'q' || encoding === 'Q') {
  150. text = text.replace(/_/g, ' ');
  151. text = text.replace(/=([0-9a-fA-F]{2})/g, function (_, hex) {
  152. return String.fromCharCode(parseInt(hex, 16));
  153. });
  154. return textdecode(charset, text);
  155. }
  156. try {
  157. return atob(text);
  158. } catch (e) {
  159. return text;
  160. }
  161. });
  162. }
  163. return '';
  164. }
  165. exports.getFilenameFromContentDispositionHeader = getFilenameFromContentDispositionHeader;