Fix bug 42119 using approach suggested by Leigh L Klotz Jr of using RequestUtil imple...
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 15 Apr 2007 00:22:47 +0000 (00:22 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 15 Apr 2007 00:22:47 +0000 (00:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@528896 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/tomcat/util/http/ContentType.java
webapps/docs/changelog.xml

index 404bbee..940f0c6 100644 (file)
@@ -29,27 +29,30 @@ package org.apache.tomcat.util.http;
  */
 public class ContentType {
 
-    // Basically return everything after ";charset="
-    // If no charset specified, use the HTTP default (ASCII) character set.
-    public static String getCharsetFromContentType(String type) {
-        if (type == null) {
-            return null;
-        }
-        int semi = type.indexOf(";");
-        if (semi == -1) {
-            return null;
-        }
-        int charsetLocation = type.indexOf("charset=", semi);
-        if (charsetLocation == -1) {
-            return null;
-        }
-       String afterCharset = type.substring(charsetLocation + 8);
-        // The charset value in a Content-Type header is allowed to be quoted
-        // and charset values can't contain quotes.  Just convert any quote
-        // chars into spaces and let trim clean things up.
-        afterCharset = afterCharset.replace('"', ' ');
-        String encoding = afterCharset.trim();
-        return encoding;
+    /**
+     * Parse the character encoding from the specified content type header.
+     * If the content type is null, or there is no explicit character encoding,
+     * <code>null</code> is returned.
+     *
+     * @param contentType a content type header
+     */
+    public static String getCharsetFromContentType(String contentType) {
+
+        if (contentType == null)
+            return (null);
+        int start = contentType.indexOf("charset=");
+        if (start < 0)
+            return (null);
+        String encoding = contentType.substring(start + 8);
+        int end = encoding.indexOf(';');
+        if (end >= 0)
+            encoding = encoding.substring(0, end);
+        encoding = encoding.trim();
+        if ((encoding.length() > 2) && (encoding.startsWith("\""))
+            && (encoding.endsWith("\"")))
+            encoding = encoding.substring(1, encoding.length() - 1);
+        return (encoding.trim());
+
     }
 
 
index 86d2640..fa2ddd9 100644 (file)
       <update>
         The poller now has good performance, so remove firstReadTimeout. (remm)
       </update>
+      <fix>
+        <bug>42119</bug> Fix return value for request.getCharacterEncoding() when
+        Content-Type headers contain parameters other than charset. Patch by
+        Leigh L Klotz Jr. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Webapps">