tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu...@apache.org
Subject cvs commit: jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/http ContentType.java
Date Wed, 28 Jul 2004 00:44:04 GMT
luehe       2004/07/27 17:44:04

  Modified:    util/java/org/apache/tomcat/util/http ContentType.java
  Log:
  Fixed Bugtraq 6152759 ("Default charset not included in Content-Type
  response header if no char encoding was specified").
  
  According to the Servlet 2.4 spec, calling:
  
    ServletResponse.setContentType("text/html");
  
  must yield these results:
  
    ServletResponse.getContentType() -> "text/html"
  
    Content-Type response header -> "text/html;charset=ISO-8859-1"
  
  Notice the absence of a charset in the result of getContentType(), but
  its presence (set to the default ISO-8859-1) in the Content-Type
  response header.
  
  Tomcat is currently not including the default charset in the
  Content-Type response header if no char encoding was specified.
  
  Revision  Changes    Path
  1.5       +34 -20    jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/http/ContentType.java
  
  Index: ContentType.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/http/ContentType.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ContentType.java	24 Feb 2004 08:50:04 -0000	1.4
  +++ ContentType.java	28 Jul 2004 00:44:04 -0000	1.5
  @@ -52,27 +52,41 @@
       }
   
   
  -    // Bad method: the user may set the charset explicitely
  -    
  -//     /** Utility method for parsing the mime type and setting
  -//      *  the encoding to locale. Also, convert from java Locale to mime
  -//      *  encodings
  -//      */
  -//     public static String constructLocalizedContentType(String type,
  -// 							Locale loc) {
  -//         // Cut off everything after the semicolon
  -//         int semi = type.indexOf(";");
  -//         if (semi != -1) {
  -//             type = type.substring(0, semi);
  -//         }
  +    /**
  +     * Returns true if the given content type contains a charset component,
  +     * false otherwise.
  +     *
  +     * @param type Content type
  +     * @return true if the given content type contains a charset component,
  +     * false otherwise
  +     */
  +    public static boolean hasCharset(String type) {
   
  -//         // Append the appropriate charset, based on the locale
  -//         String charset = LocaleToCharsetMap.getCharset(loc);
  -//         if (charset != null) {
  -//             type = type + "; charset=" + charset;
  -//         }
  +        boolean hasCharset = false;
   
  -//         return type;
  -//     }
  +        int len = type.length();
  +        int index = type.indexOf(';');
  +        while (index != -1) {
  +            index++;
  +            while (index < len && Character.isSpace(type.charAt(index))) {
  +                index++;
  +            }
  +            if (index+8 < len
  +                    && type.charAt(index) == 'c'
  +                    && type.charAt(index+1) == 'h'
  +                    && type.charAt(index+2) == 'a'
  +                    && type.charAt(index+3) == 'r'
  +                    && type.charAt(index+4) == 's'
  +                    && type.charAt(index+5) == 'e'
  +                    && type.charAt(index+6) == 't'
  +                    && type.charAt(index+7) == '=') {
  +                hasCharset = true;
  +                break;
  +            }
  +            index = type.indexOf(';', index);
  +        }
  +
  +        return hasCharset;
  +    }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org


Mime
View raw message