2
0

content_disposition.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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. var _util = require("../shared/util.js");
  28. function getFilenameFromContentDispositionHeader(contentDisposition) {
  29. let needsEncodingFixup = true;
  30. let tmp = toParamRegExp("filename\\*", "i").exec(contentDisposition);
  31. if (tmp) {
  32. tmp = tmp[1];
  33. let filename = rfc2616unquote(tmp);
  34. filename = unescape(filename);
  35. filename = rfc5987decode(filename);
  36. filename = rfc2047decode(filename);
  37. return fixupEncoding(filename);
  38. }
  39. tmp = rfc2231getparam(contentDisposition);
  40. if (tmp) {
  41. const filename = rfc2047decode(tmp);
  42. return fixupEncoding(filename);
  43. }
  44. tmp = toParamRegExp("filename", "i").exec(contentDisposition);
  45. if (tmp) {
  46. tmp = tmp[1];
  47. let filename = rfc2616unquote(tmp);
  48. filename = rfc2047decode(filename);
  49. return fixupEncoding(filename);
  50. }
  51. function toParamRegExp(attributePattern, flags) {
  52. return new RegExp("(?:^|;)\\s*" + attributePattern + "\\s*=\\s*" + "(" + '[^";\\s][^;\\s]*' + "|" + '"(?:[^"\\\\]|\\\\"?)+"?' + ")", flags);
  53. }
  54. function textdecode(encoding, value) {
  55. if (encoding) {
  56. if (!/^[\x00-\xFF]+$/.test(value)) {
  57. return value;
  58. }
  59. try {
  60. const decoder = new TextDecoder(encoding, {
  61. fatal: true
  62. });
  63. const buffer = (0, _util.stringToBytes)(value);
  64. value = decoder.decode(buffer);
  65. needsEncodingFixup = false;
  66. } catch (e) {}
  67. }
  68. return value;
  69. }
  70. function fixupEncoding(value) {
  71. if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {
  72. value = textdecode("utf-8", value);
  73. if (needsEncodingFixup) {
  74. value = textdecode("iso-8859-1", value);
  75. }
  76. }
  77. return value;
  78. }
  79. function rfc2231getparam(contentDispositionStr) {
  80. const matches = [];
  81. let match;
  82. const iter = toParamRegExp("filename\\*((?!0\\d)\\d+)(\\*?)", "ig");
  83. while ((match = iter.exec(contentDispositionStr)) !== null) {
  84. let [, n, quot, part] = match;
  85. n = parseInt(n, 10);
  86. if (n in matches) {
  87. if (n === 0) {
  88. break;
  89. }
  90. continue;
  91. }
  92. matches[n] = [quot, part];
  93. }
  94. const parts = [];
  95. for (let n = 0; n < matches.length; ++n) {
  96. if (!(n in matches)) {
  97. break;
  98. }
  99. let [quot, part] = matches[n];
  100. part = rfc2616unquote(part);
  101. if (quot) {
  102. part = unescape(part);
  103. if (n === 0) {
  104. part = rfc5987decode(part);
  105. }
  106. }
  107. parts.push(part);
  108. }
  109. return parts.join("");
  110. }
  111. function rfc2616unquote(value) {
  112. if (value.startsWith('"')) {
  113. const parts = value.slice(1).split('\\"');
  114. for (let i = 0; i < parts.length; ++i) {
  115. const quotindex = parts[i].indexOf('"');
  116. if (quotindex !== -1) {
  117. parts[i] = parts[i].slice(0, quotindex);
  118. parts.length = i + 1;
  119. }
  120. parts[i] = parts[i].replace(/\\(.)/g, "$1");
  121. }
  122. value = parts.join('"');
  123. }
  124. return value;
  125. }
  126. function rfc5987decode(extvalue) {
  127. const encodingend = extvalue.indexOf("'");
  128. if (encodingend === -1) {
  129. return extvalue;
  130. }
  131. const encoding = extvalue.slice(0, encodingend);
  132. const langvalue = extvalue.slice(encodingend + 1);
  133. const value = langvalue.replace(/^[^']*'/, "");
  134. return textdecode(encoding, value);
  135. }
  136. function rfc2047decode(value) {
  137. if (!value.startsWith("=?") || /[\x00-\x19\x80-\xff]/.test(value)) {
  138. return value;
  139. }
  140. return value.replace(/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g, function (matches, charset, encoding, text) {
  141. if (encoding === "q" || encoding === "Q") {
  142. text = text.replace(/_/g, " ");
  143. text = text.replace(/=([0-9a-fA-F]{2})/g, function (match, hex) {
  144. return String.fromCharCode(parseInt(hex, 16));
  145. });
  146. return textdecode(charset, text);
  147. }
  148. try {
  149. text = atob(text);
  150. } catch (e) {}
  151. return textdecode(charset, text);
  152. });
  153. }
  154. return "";
  155. }