content_disposition.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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. exports.getFilenameFromContentDispositionHeader = getFilenameFromContentDispositionHeader;
  27. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  28. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  29. function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } 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"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  30. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  31. function getFilenameFromContentDispositionHeader(contentDisposition) {
  32. var needsEncodingFixup = true;
  33. var tmp = toParamRegExp('filename\\*', 'i').exec(contentDisposition);
  34. if (tmp) {
  35. tmp = tmp[1];
  36. var filename = rfc2616unquote(tmp);
  37. filename = unescape(filename);
  38. filename = rfc5987decode(filename);
  39. filename = rfc2047decode(filename);
  40. return fixupEncoding(filename);
  41. }
  42. tmp = rfc2231getparam(contentDisposition);
  43. if (tmp) {
  44. var _filename = rfc2047decode(tmp);
  45. return fixupEncoding(_filename);
  46. }
  47. tmp = toParamRegExp('filename', 'i').exec(contentDisposition);
  48. if (tmp) {
  49. tmp = tmp[1];
  50. var _filename2 = rfc2616unquote(tmp);
  51. _filename2 = rfc2047decode(_filename2);
  52. return fixupEncoding(_filename2);
  53. }
  54. function toParamRegExp(attributePattern, flags) {
  55. return new RegExp('(?:^|;)\\s*' + attributePattern + '\\s*=\\s*' + '(' + '[^";\\s][^;\\s]*' + '|' + '"(?:[^"\\\\]|\\\\"?)+"?' + ')', flags);
  56. }
  57. function textdecode(encoding, value) {
  58. if (encoding) {
  59. if (!/^[\x00-\xFF]+$/.test(value)) {
  60. return value;
  61. }
  62. try {
  63. var decoder = new TextDecoder(encoding, {
  64. fatal: true
  65. });
  66. var bytes = Array.from(value, function (ch) {
  67. return ch.charCodeAt(0) & 0xFF;
  68. });
  69. value = decoder.decode(new Uint8Array(bytes));
  70. needsEncodingFixup = false;
  71. } catch (e) {
  72. if (/^utf-?8$/i.test(encoding)) {
  73. try {
  74. value = decodeURIComponent(escape(value));
  75. needsEncodingFixup = false;
  76. } catch (err) {}
  77. }
  78. }
  79. }
  80. return value;
  81. }
  82. function fixupEncoding(value) {
  83. if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {
  84. value = textdecode('utf-8', value);
  85. if (needsEncodingFixup) {
  86. value = textdecode('iso-8859-1', value);
  87. }
  88. }
  89. return value;
  90. }
  91. function rfc2231getparam(contentDisposition) {
  92. var matches = [],
  93. match;
  94. var iter = toParamRegExp('filename\\*((?!0\\d)\\d+)(\\*?)', 'ig');
  95. while ((match = iter.exec(contentDisposition)) !== null) {
  96. var _match = match,
  97. _match2 = _slicedToArray(_match, 4),
  98. n = _match2[1],
  99. quot = _match2[2],
  100. part = _match2[3];
  101. n = parseInt(n, 10);
  102. if (n in matches) {
  103. if (n === 0) {
  104. break;
  105. }
  106. continue;
  107. }
  108. matches[n] = [quot, part];
  109. }
  110. var parts = [];
  111. for (var n = 0; n < matches.length; ++n) {
  112. if (!(n in matches)) {
  113. break;
  114. }
  115. var _matches$n = _slicedToArray(matches[n], 2),
  116. quot = _matches$n[0],
  117. part = _matches$n[1];
  118. part = rfc2616unquote(part);
  119. if (quot) {
  120. part = unescape(part);
  121. if (n === 0) {
  122. part = rfc5987decode(part);
  123. }
  124. }
  125. parts.push(part);
  126. }
  127. return parts.join('');
  128. }
  129. function rfc2616unquote(value) {
  130. if (value.startsWith('"')) {
  131. var parts = value.slice(1).split('\\"');
  132. for (var i = 0; i < parts.length; ++i) {
  133. var quotindex = parts[i].indexOf('"');
  134. if (quotindex !== -1) {
  135. parts[i] = parts[i].slice(0, quotindex);
  136. parts.length = i + 1;
  137. }
  138. parts[i] = parts[i].replace(/\\(.)/g, '$1');
  139. }
  140. value = parts.join('"');
  141. }
  142. return value;
  143. }
  144. function rfc5987decode(extvalue) {
  145. var encodingend = extvalue.indexOf('\'');
  146. if (encodingend === -1) {
  147. return extvalue;
  148. }
  149. var encoding = extvalue.slice(0, encodingend);
  150. var langvalue = extvalue.slice(encodingend + 1);
  151. var value = langvalue.replace(/^[^']*'/, '');
  152. return textdecode(encoding, value);
  153. }
  154. function rfc2047decode(value) {
  155. if (!value.startsWith('=?') || /[\x00-\x19\x80-\xff]/.test(value)) {
  156. return value;
  157. }
  158. return value.replace(/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g, function (_, charset, encoding, text) {
  159. if (encoding === 'q' || encoding === 'Q') {
  160. text = text.replace(/_/g, ' ');
  161. text = text.replace(/=([0-9a-fA-F]{2})/g, function (_, hex) {
  162. return String.fromCharCode(parseInt(hex, 16));
  163. });
  164. return textdecode(charset, text);
  165. }
  166. try {
  167. text = atob(text);
  168. } catch (e) {}
  169. return textdecode(charset, text);
  170. });
  171. }
  172. return '';
  173. }