diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java b/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java index 04d8cebe6f4..ea68317c2f2 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java @@ -21,7 +21,6 @@ package org.eclipse.jetty.http; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.net.URL; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -30,14 +29,11 @@ import java.util.HashSet; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; -import java.util.MissingResourceException; import java.util.Properties; -import java.util.ResourceBundle; import java.util.Set; import org.eclipse.jetty.util.ArrayTrie; import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.Loader; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.Trie; import org.eclipse.jetty.util.log.Log; @@ -399,6 +395,12 @@ public class MimeTypes quote=false; continue; } + + if(';'==b && state<=8) + { + state = 1; + continue; + } switch(state) { @@ -408,8 +410,6 @@ public class MimeTypes quote=true; break; } - if (';'==b) - state=1; break; case 1: if ('c'==b) state=2; else if (' '!=b) state=0; break; diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java index 362a211d03c..8e39c05ae25 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java @@ -18,9 +18,11 @@ package org.eclipse.jetty.http; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import org.junit.Test; @@ -75,25 +77,34 @@ public class MimeTypesTest assertNotNull(prefix,contentType); assertEquals(prefix,expectedMimeType,contentType); } + + private void assertCharsetFromContentType(String contentType, String expectedCharset) + { + assertThat("getCharsetFromContentType(\"" + contentType + "\")", + MimeTypes.getCharsetFromContentType(contentType), is(expectedCharset)); + } @Test public void testCharsetFromContentType() { - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar;charset=abc;some=else")); - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar;charset=abc")); - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar ; charset = abc")); - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar ; charset = abc ; some=else")); - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar;other=param;charset=abc;some=else")); - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar;other=param;charset=abc")); - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar other = param ; charset = abc")); - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar other = param ; charset = abc ; some=else")); - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar other = param ; charset = abc")); - assertEquals("abc",MimeTypes.getCharsetFromContentType("foo/bar other = param ; charset = \"abc\" ; some=else")); - assertEquals(null,MimeTypes.getCharsetFromContentType("foo/bar")); - assertEquals("utf-8",MimeTypes.getCharsetFromContentType("foo/bar;charset=uTf8")); - assertEquals("utf-8",MimeTypes.getCharsetFromContentType("foo/bar;other=\"charset=abc\";charset=uTf8")); - assertEquals("utf-8",MimeTypes.getCharsetFromContentType("application/pdf;;; charset=UTF-8")); - assertEquals("utf-8",MimeTypes.getCharsetFromContentType("text/html;charset=utf-8")); + 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"); } @Test