summaryrefslogtreecommitdiff
path: root/ext/web/01_mimesniff.js
diff options
context:
space:
mode:
authorAndreu Botella <andreu@andreubotella.com>2022-03-20 14:31:12 +0100
committerGitHub <noreply@github.com>2022-03-20 14:31:12 +0100
commit593801e265e7609ac0bd695fcb450e479d5db776 (patch)
treeca6825cbe046fc7206f0e948dc6ddebe14319d3f /ext/web/01_mimesniff.js
parent0bc286ab47d82496b90efb429e16efccc10e5f2d (diff)
cleanup(web, fetch): dedupe minesniff / "extract a MIME type" algorithm (#14044)
Closes #14002
Diffstat (limited to 'ext/web/01_mimesniff.js')
-rw-r--r--ext/web/01_mimesniff.js49
1 files changed, 48 insertions, 1 deletions
diff --git a/ext/web/01_mimesniff.js b/ext/web/01_mimesniff.js
index c6ac7c66c..d2c784d6e 100644
--- a/ext/web/01_mimesniff.js
+++ b/ext/web/01_mimesniff.js
@@ -12,6 +12,7 @@
const {
ArrayPrototypeIncludes,
Map,
+ MapPrototypeGet,
MapPrototypeHas,
MapPrototypeSet,
RegExpPrototypeTest,
@@ -207,5 +208,51 @@
return serialization;
}
- window.__bootstrap.mimesniff = { parseMimeType, essence, serializeMimeType };
+ /**
+ * Part of the Fetch spec's "extract a MIME type" algorithm
+ * (https://fetch.spec.whatwg.org/#concept-header-extract-mime-type).
+ * @param {string[] | null} headerValues The result of getting, decoding and
+ * splitting the "Content-Type" header.
+ * @returns {MimeType | null}
+ */
+ function extractMimeType(headerValues) {
+ if (headerValues === null) return null;
+
+ let charset = null;
+ let essence_ = null;
+ let mimeType = null;
+ for (const value of headerValues) {
+ const temporaryMimeType = parseMimeType(value);
+ if (
+ temporaryMimeType === null ||
+ essence(temporaryMimeType) == "*/*"
+ ) {
+ continue;
+ }
+ mimeType = temporaryMimeType;
+ if (essence(mimeType) !== essence_) {
+ charset = null;
+ const newCharset = MapPrototypeGet(mimeType.parameters, "charset");
+ if (newCharset !== undefined) {
+ charset = newCharset;
+ }
+ essence_ = essence(mimeType);
+ } else {
+ if (
+ !MapPrototypeHas(mimeType.parameters, "charset") &&
+ charset !== null
+ ) {
+ MapPrototypeSet(mimeType.parameters, "charset", charset);
+ }
+ }
+ }
+ return mimeType;
+ }
+
+ window.__bootstrap.mimesniff = {
+ parseMimeType,
+ essence,
+ serializeMimeType,
+ extractMimeType,
+ };
})(this);