Issue #6545 - Add more mime-types

+ Adding webp + avif + apng based on current
  `Accept` header offerings from current browsers.

Firefox 90.0
`Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8`

Chrome 92.0.4515.107
`Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9`

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2021-07-27 10:31:44 -05:00
parent 330fc0ba0b
commit 8971ea991f
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
2 changed files with 79 additions and 72 deletions

View File

@ -3,11 +3,13 @@ aif=audio/x-aiff
aifc=audio/x-aiff
aiff=audio/x-aiff
apk=application/vnd.android.package-archive
apng=image/apng
asc=text/plain
asf=video/x.ms.asf
asx=video/x.ms.asx
au=audio/basic
avi=video/x-msvideo
avif=image/avif
bcpio=application/x-bcpio
bin=application/octet-stream
bmp=image/bmp
@ -170,6 +172,7 @@ vxml=application/voicexml+xml
wasm=application/wasm
wav=audio/x-wav
wbmp=image/vnd.wap.wbmp
webp=image/webp
wml=text/vnd.wap.wml
wmlc=application/vnd.wap.wmlc
wmls=text/vnd.wap.wmlscript

View File

@ -13,47 +13,48 @@
package org.eclipse.jetty.http;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
public class MimeTypesTest
{
@Test
public void testGetMimeByExtensionGzip()
public static Stream<Arguments> mimeTypesByExtensionCases()
{
assertMimeTypeByExtension("application/gzip", "test.gz");
return Stream.of(
Arguments.of("test.gz", "application/gzip"),
Arguments.of("foo.webp", "image/webp"),
Arguments.of("zed.avif", "image/avif"),
// make sure that filename case isn't an issue
Arguments.of("test.png", "image/png"),
Arguments.of("TEST.PNG", "image/png"),
Arguments.of("Test.Png", "image/png"),
Arguments.of("test.txt", "text/plain"),
Arguments.of("TEST.TXT", "text/plain"),
// Make sure that multiple dots don't interfere
Arguments.of("org.eclipse.jetty.Logo.png", "image/png"),
// Make sure that a deep path doesn't interfere
Arguments.of("org/eclipse/jetty/Logo.png", "image/png"),
// Make sure that path that looks like a filename doesn't interfere
Arguments.of("org/eclipse.jpg/jetty/Logo.png", "image/png")
);
}
@Test
public void testGetMimeByExtensionPng()
@ParameterizedTest
@MethodSource("mimeTypesByExtensionCases")
public void testMimeTypesByExtension(String filename, String expectedMimeType)
{
assertMimeTypeByExtension("image/png", "test.png");
assertMimeTypeByExtension("image/png", "TEST.PNG");
assertMimeTypeByExtension("image/png", "Test.Png");
}
@Test
public void testGetMimeByExtensionPngMultiDot()
{
assertMimeTypeByExtension("image/png", "org.eclipse.jetty.Logo.png");
}
@Test
public void testGetMimeByExtensionPngDeepPath()
{
assertMimeTypeByExtension("image/png", "/org/eclipse/jetty/Logo.png");
}
@Test
public void testGetMimeByExtensionText()
{
assertMimeTypeByExtension("text/plain", "test.txt");
assertMimeTypeByExtension("text/plain", "TEST.TXT");
MimeTypes mimetypes = new MimeTypes();
String contentType = mimetypes.getMimeByExtension(filename);
assertThat("MimeTypes.getMimeByExtension(\"" + filename + "\")",
contentType, is(expectedMimeType));
}
@Test
@ -64,60 +65,63 @@ public class MimeTypesTest
assertNull(contentType);
}
private void assertMimeTypeByExtension(String expectedMimeType, String filename)
public static Stream<Arguments> charsetFromContentTypeCases()
{
MimeTypes mimetypes = new MimeTypes();
String contentType = mimetypes.getMimeByExtension(filename);
String prefix = "MimeTypes.getMimeByExtension(" + filename + ")";
assertNotNull(contentType, prefix);
assertEquals(expectedMimeType, contentType, prefix);
return Stream.of(
Arguments.of("foo/bar;charset=abc;some=else", "abc"),
Arguments.of("foo/bar;charset=abc", "abc"),
Arguments.of("foo/bar ; charset = abc", "abc"),
Arguments.of("foo/bar ; charset = abc ; some=else", "abc"),
Arguments.of("foo/bar;other=param;charset=abc;some=else", "abc"),
Arguments.of("foo/bar;other=param;charset=abc", "abc"),
Arguments.of("foo/bar other = param ; charset = abc", "abc"),
Arguments.of("foo/bar other = param ; charset = abc ; some=else", "abc"),
Arguments.of("foo/bar other = param ; charset = abc", "abc"),
Arguments.of("foo/bar other = param ; charset = \"abc\" ; some=else", "abc"),
Arguments.of("foo/bar", null),
Arguments.of("foo/bar;charset=uTf8", "utf-8"),
Arguments.of("foo/bar;other=\"charset=abc\";charset=uTf8", "utf-8"),
Arguments.of("application/pdf; charset=UTF-8", "utf-8"),
Arguments.of("application/pdf;; charset=UTF-8", "utf-8"),
Arguments.of("application/pdf;;; charset=UTF-8", "utf-8"),
Arguments.of("application/pdf;;;; charset=UTF-8", "utf-8"),
Arguments.of("text/html;charset=utf-8", "utf-8")
);
}
private void assertCharsetFromContentType(String contentType, String expectedCharset)
@ParameterizedTest
@MethodSource("charsetFromContentTypeCases")
public void testCharsetFromContentType(String contentType, String expectedCharset)
{
assertThat("getCharsetFromContentType(\"" + contentType + "\")",
MimeTypes.getCharsetFromContentType(contentType), is(expectedCharset));
}
@Test
public void testCharsetFromContentType()
public static Stream<Arguments> contentTypeWithoutCharsetCases()
{
assertCharsetFromContentType("foo/bar;charset=abc;some=else", "abc");
assertCharsetFromContentType("foo/bar;charset=abc", "abc");
assertCharsetFromContentType("foo/bar ; charset = abc", "abc");
assertCharsetFromContentType("foo/bar ; charset = abc ; some=else", "abc");
assertCharsetFromContentType("foo/bar;other=param;charset=abc;some=else", "abc");
assertCharsetFromContentType("foo/bar;other=param;charset=abc", "abc");
assertCharsetFromContentType("foo/bar other = param ; charset = abc", "abc");
assertCharsetFromContentType("foo/bar other = param ; charset = abc ; some=else", "abc");
assertCharsetFromContentType("foo/bar other = param ; charset = abc", "abc");
assertCharsetFromContentType("foo/bar other = param ; charset = \"abc\" ; some=else", "abc");
assertCharsetFromContentType("foo/bar", null);
assertCharsetFromContentType("foo/bar;charset=uTf8", "utf-8");
assertCharsetFromContentType("foo/bar;other=\"charset=abc\";charset=uTf8", "utf-8");
assertCharsetFromContentType("application/pdf; charset=UTF-8", "utf-8");
assertCharsetFromContentType("application/pdf;; charset=UTF-8", "utf-8");
assertCharsetFromContentType("application/pdf;;; charset=UTF-8", "utf-8");
assertCharsetFromContentType("application/pdf;;;; charset=UTF-8", "utf-8");
assertCharsetFromContentType("text/html;charset=utf-8", "utf-8");
return Stream.of(
Arguments.of("foo/bar;charset=abc;some=else", "foo/bar;some=else"),
Arguments.of("foo/bar;charset=abc", "foo/bar"),
Arguments.of("foo/bar ; charset = abc", "foo/bar"),
Arguments.of("foo/bar ; charset = abc ; some=else", "foo/bar;some=else"),
Arguments.of("foo/bar;other=param;charset=abc;some=else", "foo/bar;other=param;some=else"),
Arguments.of("foo/bar;other=param;charset=abc", "foo/bar;other=param"),
Arguments.of("foo/bar ; other = param ; charset = abc", "foo/bar ; other = param"),
Arguments.of("foo/bar ; other = param ; charset = abc ; some=else", "foo/bar ; other = param;some=else"),
Arguments.of("foo/bar ; other = param ; charset = abc", "foo/bar ; other = param"),
Arguments.of("foo/bar ; other = param ; charset = \"abc\" ; some=else", "foo/bar ; other = param;some=else"),
Arguments.of("foo/bar", "foo/bar"),
Arguments.of("foo/bar;charset=uTf8", "foo/bar"),
Arguments.of("foo/bar;other=\"charset=abc\";charset=uTf8", "foo/bar;other=\"charset=abc\""),
Arguments.of("text/html;charset=utf-8", "text/html")
);
}
@Test
public void testContentTypeWithoutCharset()
@ParameterizedTest
@MethodSource("contentTypeWithoutCharsetCases")
public void testContentTypeWithoutCharset(String contentTypeWithCharset, String expectedContentType)
{
assertEquals("foo/bar;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=abc;some=else"));
assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=abc"));
assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar ; charset = abc"));
assertEquals("foo/bar;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar ; charset = abc ; some=else"));
assertEquals("foo/bar;other=param;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar;other=param;charset=abc;some=else"));
assertEquals("foo/bar;other=param", MimeTypes.getContentTypeWithoutCharset("foo/bar;other=param;charset=abc"));
assertEquals("foo/bar ; other = param", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = abc"));
assertEquals("foo/bar ; other = param;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = abc ; some=else"));
assertEquals("foo/bar ; other = param", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = abc"));
assertEquals("foo/bar ; other = param;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = \"abc\" ; some=else"));
assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar"));
assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=uTf8"));
assertEquals("foo/bar;other=\"charset=abc\"", MimeTypes.getContentTypeWithoutCharset("foo/bar;other=\"charset=abc\";charset=uTf8"));
assertEquals("text/html", MimeTypes.getContentTypeWithoutCharset("text/html;charset=utf-8"));
assertThat("MimeTypes.getContentTypeWithoutCharset(\"" + contentTypeWithCharset + "\")",
MimeTypes.getContentTypeWithoutCharset(contentTypeWithCharset), is(expectedContentType));
}
}