From ac9d9395447ed68b1ed7de6e4309710a7593cfbf Mon Sep 17 00:00:00 2001 From: markt Date: Sun, 15 Apr 2007 00:22:47 +0000 Subject: [PATCH] Fix bug 42119 using approach suggested by Leigh L Klotz Jr of using RequestUtil implementation. 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 | 45 ++++++++++++----------- webapps/docs/changelog.xml | 5 +++ 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/java/org/apache/tomcat/util/http/ContentType.java b/java/org/apache/tomcat/util/http/ContentType.java index 404bbee2b..940f0c635 100644 --- a/java/org/apache/tomcat/util/http/ContentType.java +++ b/java/org/apache/tomcat/util/http/ContentType.java @@ -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, + * null 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()); + } diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 86d2640bf..fa2ddd9e3 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -153,6 +153,11 @@ The poller now has good performance, so remove firstReadTimeout. (remm) + + 42119 Fix return value for request.getCharacterEncoding() when + Content-Type headers contain parameters other than charset. Patch by + Leigh L Klotz Jr. (markt) + -- 2.11.0