Skip to content
Permalink
Browse files
Accept image/jpg for compatibility reasons
https://bugs.webkit.org/show_bug.cgi?id=246131
rdar://100835192

Reviewed by Said Abou-Hallawa and Darin Adler.

Blink & Gecko seem to render JPEG images served with `Content-Type: image/jpg`
while WebKit decides to download it because the correct content type is
supposed to be `image/jpeg`. This is an interoperability risk so we should
align.

In this patch, we now treat `image/jpg` as a valid JPEG content type to align
with other browser engines.

Also revert a couple of unintended changes that were made in 237163@main:
- The logic in normalizedImageMIMEType() was for CURL only but the condition got
  mistakenly reversed.
- The normalized MIME type was previously used for both supportedImageMIMETypeSet
  and additionalSupportedImageMIMETypes. However, 237163@main stopped using the
  normalized MIME type for supportedImageMIMETypeSet.

* LayoutTests/http/tests/images/jpg-invalid-content-type-expected.html: Added.
* LayoutTests/http/tests/images/jpg-invalid-content-type.html: Added.
* Source/WebCore/platform/MIMETypeRegistry.cpp:
(WebCore::MIMETypeRegistry::isSupportedImageMIMEType):

Canonical link: https://commits.webkit.org/255268@main
  • Loading branch information
cdumez committed Oct 7, 2022
1 parent 05f0430 commit 0d17dc0c310f9b1d05268ab63e967c38133892f0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
@@ -0,0 +1,7 @@
<DOCTYPE html>
<html>
<body>
<p>Tests that a JPEG image served with `Content-Type: image/jpg` gets rendered, and not downloaded.</p>
<iframe id="testFrame" src="/resources/square20.jpg"></iframe>
</body>
</html>
@@ -0,0 +1,18 @@
<DOCTYPE html>
<html>
<body>
<p>Tests that a JPEG image served with `Content-Type: image/jpg` gets rendered, and not downloaded.</p>
<iframe id="testFrame" src="about:blank"></iframe>
<script>
if (window.testRunner)
testRunner.waitUntilDone();

let testFrame = document.getElementById("testFrame");
testFrame.src = "/resources/load-and-stall.py?name=square20.jpg&mimeType=image%2Fjpg";
testFrame.onload = () => {
if (window.testRunner)
testRunner.notifyDone();
};
</script>
</body>
</html>
@@ -113,6 +113,7 @@ constexpr ComparableCaseFoldingASCIILiteral supportedImageMIMETypeArray[] = {
#if !USE(CG) && USE(OPENJPEG)
"image/jpeg2000",
#endif
"image/jpg",
#if USE(JPEGXL)
"image/jxl",
#endif
@@ -422,9 +423,11 @@ bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
}
});
#endif
if (supportedImageMIMETypeSet.contains(mimeType))

String normalizedMIMEType = normalizedImageMIMEType(mimeType);
if (supportedImageMIMETypeSet.contains(normalizedMIMEType))
return true;
return additionalSupportedImageMIMETypes().contains(normalizedImageMIMEType(mimeType));
return additionalSupportedImageMIMETypes().contains(normalizedMIMEType);
}

bool MIMETypeRegistry::isSupportedImageVideoOrSVGMIMEType(const String& mimeType)
@@ -715,8 +718,6 @@ bool MIMETypeRegistry::isSupportedModelMIMEType(const String& mimeType)
static String normalizedImageMIMEType(const String& mimeType)
{
#if USE(CURL)
return mimeType;
#else
// FIXME: Since this is only used in isSupportedImageMIMEType, we should consider removing the non-image types below.
static constexpr std::pair<ComparableLettersLiteral, ASCIILiteral> mimeTypeAssociationArray[] = {
{ "application/ico", "image/vnd.microsoft.icon"_s },
@@ -769,6 +770,8 @@ static String normalizedImageMIMEType(const String& mimeType)
static constexpr SortedArrayMap associationMap { mimeTypeAssociationArray };
auto normalizedType = associationMap.tryGet(mimeType);
return normalizedType ? *normalizedType : mimeType;
#else
return mimeType;
#endif
}

@@ -845,7 +848,7 @@ bool MIMETypeRegistry::isJPEGMIMEType(const String& mimeType)
return false;
return CFEqual(destinationUTI.get(), jpegUTI());
#else
return mimeType == "image/jpeg"_s;
return mimeType == "image/jpeg"_s || mimeType == "image/jpg"_s;
#endif
}

@@ -85,7 +85,7 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN

if (equalLettersIgnoringASCIICase(mimeType, "image/png"_s))
return kUTTypePNG;
if (equalLettersIgnoringASCIICase(mimeType, "image/jpeg"_s))
if (equalLettersIgnoringASCIICase(mimeType, "image/jpeg"_s) || equalLettersIgnoringASCIICase(mimeType, "image/jpg"_s))
return jpegUTI();
if (equalLettersIgnoringASCIICase(mimeType, "image/gif"_s))
return kUTTypeGIF;

0 comments on commit 0d17dc0

Please sign in to comment.