content_disposition.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 getFilenameFromContentDispositionHeader(contentDisposition) {
  28. let needsEncodingFixup = true;
  29. let tmp = toParamRegExp("filename\\*", "i").exec(contentDisposition);
  30. if (tmp) {
  31. tmp = tmp[1];
  32. let 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. const filename = rfc2047decode(tmp);
  41. return fixupEncoding(filename);
  42. }
  43. tmp = toParamRegExp("filename", "i").exec(contentDisposition);
  44. if (tmp) {
  45. tmp = tmp[1];
  46. let filename = rfc2616unquote(tmp);
  47. filename = rfc2047decode(filename);
  48. return fixupEncoding(filename);
  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. const decoder = new TextDecoder(encoding, {
  60. fatal: true
  61. });
  62. const bytes = Array.from(value, function (ch) {
  63. return ch.charCodeAt(0) & 0xff;
  64. });
  65. value = decoder.decode(new Uint8Array(bytes));
  66. needsEncodingFixup = false;
  67. } catch (e) {
  68. if (/^utf-?8$/i.test(encoding)) {
  69. try {
  70. value = decodeURIComponent(escape(value));
  71. needsEncodingFixup = false;
  72. } catch (err) {}
  73. }
  74. }
  75. }
  76. return value;
  77. }
  78. function fixupEncoding(value) {
  79. if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {
  80. value = textdecode("utf-8", value);
  81. if (needsEncodingFixup) {
  82. value = textdecode("iso-8859-1", value);
  83. }
  84. }
  85. return value;
  86. }
  87. function rfc2231getparam(contentDispositionStr) {
  88. const matches = [];
  89. let match;
  90. const iter = toParamRegExp("filename\\*((?!0\\d)\\d+)(\\*?)", "ig");
  91. while ((match = iter.exec(contentDispositionStr)) !== null) {
  92. let [, n, quot, part] = match;
  93. n = parseInt(n, 10);
  94. if (n in matches) {
  95. if (n === 0) {
  96. break;
  97. }
  98. continue;
  99. }
  100. matches[n] = [quot, part];
  101. }
  102. const parts = [];
  103. for (let n = 0; n < matches.length; ++n) {
  104. if (!(n in matches)) {
  105. break;
  106. }
  107. let [quot, part] = matches[n];
  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.startsWith('"')) {
  121. const parts = value.slice(1).split('\\"');
  122. for (let i = 0; i < parts.length; ++i) {
  123. const 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. const encodingend = extvalue.indexOf("'");
  136. if (encodingend === -1) {
  137. return extvalue;
  138. }
  139. const encoding = extvalue.slice(0, encodingend);
  140. const langvalue = extvalue.slice(encodingend + 1);
  141. const value = langvalue.replace(/^[^']*'/, "");
  142. return textdecode(encoding, value);
  143. }
  144. function rfc2047decode(value) {
  145. if (!value.startsWith("=?") || /[\x00-\x19\x80-\xff]/.test(value)) {
  146. return value;
  147. }
  148. return value.replace(/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g, function (matches, 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 (match, hex) {
  152. return String.fromCharCode(parseInt(hex, 16));
  153. });
  154. return textdecode(charset, text);
  155. }
  156. try {
  157. text = atob(text);
  158. } catch (e) {}
  159. return textdecode(charset, text);
  160. });
  161. }
  162. return "";
  163. }