Author: markt
Date: Thu Jan  4 17:23:41 2018
New Revision: 1820138

URL: http://svn.apache.org/viewvc?rev=1820138&view=rev
Log:
Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=61916
Extend the AddDefaultCharsetFilter to add a character set when the content type 
is set via setHeader() or addHeader() as well as when it is set via 
setContentType().

Modified:
    tomcat/trunk/java/org/apache/catalina/filters/AddDefaultCharsetFilter.java
    tomcat/trunk/test/org/apache/catalina/filters/TestAddCharSetFilter.java
    tomcat/trunk/webapps/docs/changelog.xml

Modified: 
tomcat/trunk/java/org/apache/catalina/filters/AddDefaultCharsetFilter.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/filters/AddDefaultCharsetFilter.java?rev=1820138&r1=1820137&r2=1820138&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/filters/AddDefaultCharsetFilter.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/filters/AddDefaultCharsetFilter.java 
Thu Jan  4 17:23:41 2018
@@ -121,6 +121,24 @@ public class AddDefaultCharsetFilter ext
         }
 
         @Override
+        public void setHeader(String name, String value) {
+            if (name.trim().equalsIgnoreCase("content-type")) {
+                setContentType(value);
+            } else {
+                super.setHeader(name, value);
+            }
+        }
+
+        @Override
+        public void addHeader(String name, String value) {
+            if (name.trim().equalsIgnoreCase("content-type")) {
+                setContentType(value);
+            } else {
+                super.setHeader(name, value);
+            }
+        }
+
+        @Override
         public void setCharacterEncoding(String charset) {
             super.setCharacterEncoding(charset);
             encoding = charset;

Modified: 
tomcat/trunk/test/org/apache/catalina/filters/TestAddCharSetFilter.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/filters/TestAddCharSetFilter.java?rev=1820138&r1=1820137&r2=1820138&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/filters/TestAddCharSetFilter.java 
(original)
+++ tomcat/trunk/test/org/apache/catalina/filters/TestAddCharSetFilter.java Thu 
Jan  4 17:23:41 2018
@@ -48,12 +48,12 @@ public class TestAddCharSetFilter extend
 
     @Test
     public void testNoneSpecifiedMode2() throws Exception {
-        doTest(null, "ISO-8859-2", 2);
+        doTest(null, "ISO-8859-2", 2, true);
     }
 
     @Test
     public void testNoneSpecifiedMode3() throws Exception {
-        doTest(null, "ISO-8859-3", 3);
+        doTest(null, "ISO-8859-3", 3, true);
     }
 
     @Test
@@ -83,10 +83,13 @@ public class TestAddCharSetFilter extend
 
 
     private void doTest(String encoding, String expected) throws Exception {
-        doTest(encoding, expected, 1);
+        doTest(encoding, expected, 1, true);
+        tearDown();
+        setUp();
+        doTest(encoding, expected, 1, false);
     }
 
-    private void doTest(String encoding, String expected, int mode)
+    private void doTest(String encoding, String expected, int mode, boolean 
useSetContentType)
             throws Exception {
         // Setup Tomcat instance
         Tomcat tomcat = getTomcatInstance();
@@ -95,7 +98,7 @@ public class TestAddCharSetFilter extend
         Context ctx = tomcat.addContext("", null);
 
         // Add the Servlet
-        CharsetServlet servlet = new CharsetServlet(mode);
+        CharsetServlet servlet = new CharsetServlet(mode, useSetContentType);
         Tomcat.addServlet(ctx, "servlet", servlet);
         ctx.addServletMappingDecoded("/", "servlet");
 
@@ -128,30 +131,53 @@ public class TestAddCharSetFilter extend
         private static final String OUTPUT = "OK";
 
         private final int mode;
+        private final boolean useSetContentType;
 
-        public CharsetServlet(int mode) {
+        public CharsetServlet(int mode, boolean useSetContentType) {
             this.mode = mode;
+            this.useSetContentType = useSetContentType;
         }
 
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                 throws ServletException, IOException {
 
+            String value;
             switch (mode) {
                 case 1:
-                    resp.setContentType("text/plain");
+                    value = "text/plain";
+                    if (useSetContentType) {
+                        resp.setContentType(value);
+                    } else {
+                        resp.setHeader("Content-Type", value);
+                    }
                     break;
                 case 2:
-                    resp.setContentType("text/plain;charset=ISO-8859-2");
+                    value = "text/plain;charset=ISO-8859-2";
+                    if (useSetContentType) {
+                        resp.setContentType(value);
+                    } else {
+                        resp.setHeader("Content-Type", value);
+                    }
                     break;
                 case 3:
-                    resp.setContentType("text/plain");
-                    resp.setCharacterEncoding("ISO-8859-3");
+                    if (useSetContentType) {
+                        resp.setContentType("text/plain");
+                        resp.setCharacterEncoding("ISO-8859-3");
+                    } else {
+                        resp.setHeader("Content-Type", 
"text/plain;charset=ISO-8859-3");
+                    }
                     break;
                 default:
-                    resp.setContentType("text/plain;charset=ISO-8859-4");
+                    value = "text/plain;charset=ISO-8859-4";
+                    if (useSetContentType) {
+                        resp.setContentType(value);
+                    } else {
+                        resp.setHeader("Content-Type", value);
+                    }
                     break;
             }
+
             resp.getWriter().print(OUTPUT);
         }
     }

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1820138&r1=1820137&r2=1820138&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Thu Jan  4 17:23:41 2018
@@ -56,6 +56,12 @@
       <fix>
         Minor HTTP/2 push fixes. (remm)
       </fix>
+      <fix>
+        <bug>61916</bug>: Extend the <code>AddDefaultCharsetFilter</code> to 
add
+        a character set when the content type is set via
+        <code>setHeader()</code> or <code>addHeader()</code> as well as when it
+        is set via <code>setContentType()</code>. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">



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

Reply via email to