You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by vi...@apache.org on 2013/05/26 22:00:38 UTC

svn commit: r1486460 - in /tomcat/tc7.0.x/trunk: ./ java/org/apache/catalina/connector/Request.java test/org/apache/catalina/connector/TestRequest.java webapps/docs/changelog.xml

Author: violetagg
Date: Sun May 26 20:00:38 2013
New Revision: 1486460

URL: http://svn.apache.org/r1486460
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=54984
Merged revisions 1486134, 1486217, 1486294, 1486443 from tomcat/trunk:
Ensure that the correct encoding will be used when processing the multipart data.

Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/java/org/apache/catalina/connector/Request.java
    tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestRequest.java
    tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml

Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
  Merged /tomcat/trunk:r1486134,1486217,1486294,1486443

Modified: tomcat/tc7.0.x/trunk/java/org/apache/catalina/connector/Request.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/connector/Request.java?rev=1486460&r1=1486459&r2=1486460&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/catalina/connector/Request.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/connector/Request.java Sun May 26 20:00:38 2013
@@ -2741,7 +2741,11 @@ public class Request
                         try {
                             String encoding = parameters.getEncoding();
                             if (encoding == null) {
-                                encoding = Parameters.DEFAULT_ENCODING;
+                                if (enc == null) {
+                                    encoding = Parameters.DEFAULT_ENCODING;
+                                } else {
+                                    encoding = enc;
+                                }
                             }
                             value = part.getString(encoding);
                         } catch (UnsupportedEncodingException uee) {

Modified: tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestRequest.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestRequest.java?rev=1486460&r1=1486459&r2=1486460&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestRequest.java (original)
+++ tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestRequest.java Sun May 26 20:00:38 2013
@@ -17,12 +17,17 @@
 
 package org.apache.catalina.connector;
 
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.List;
 import java.util.TreeMap;
 
 import javax.servlet.ServletException;
@@ -33,6 +38,7 @@ import javax.servlet.http.HttpServletRes
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import org.junit.Test;
 
@@ -362,7 +368,7 @@ public class TestRequest extends TomcatB
         root.addServletMapping("/", "Bug37794");
         tomcat.start();
 
-        HttpURLConnection conn = getConnection();
+        HttpURLConnection conn = getConnection("http://localhost:" + getPort() + "/");
         InputStream is = conn.getInputStream();
         assertNotNull(is);
     }
@@ -376,7 +382,7 @@ public class TestRequest extends TomcatB
         root.addServletMapping("/", "Bug37794");
         tomcat.start();
 
-        HttpURLConnection conn = getConnection();
+        HttpURLConnection conn = getConnection("http://localhost:" + getPort() + "/");
         conn.setChunkedStreamingMode(8 * 1024);
         InputStream is = conn.getInputStream();
         assertNotNull(is);
@@ -459,6 +465,34 @@ public class TestRequest extends TomcatB
         */
     }
 
+    @Test
+    public void testBug54984() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+        Context root = tomcat.addContext("",
+                System.getProperty("java.io.tmpdir"));
+        root.setAllowCasualMultipartParsing(true);
+        Tomcat.addServlet(root, "Bug54984", new Bug54984Servlet());
+        root.addServletMapping("/", "Bug54984");
+        tomcat.start();
+
+        HttpURLConnection conn = getConnection("http://localhost:" + getPort()
+                + "/parseParametersBeforeParseParts");
+
+        prepareRequestBug54984(conn);
+
+        checkResponseBug54984(conn);
+
+        conn.disconnect();
+
+        conn = getConnection("http://localhost:" + getPort() + "/");
+
+        prepareRequestBug54984(conn);
+
+        checkResponseBug54984(conn);
+
+        conn.disconnect();
+    }
+
     /**
      *
      */
@@ -576,8 +610,7 @@ public class TestRequest extends TomcatB
         }
     }
 
-    private HttpURLConnection getConnection() throws IOException {
-        final String query = "http://localhost:" + getPort() + "/";
+    private HttpURLConnection getConnection(String query) throws IOException {
         URL postURL;
         postURL = new URL(query);
         HttpURLConnection conn = (HttpURLConnection) postURL.openConnection();
@@ -590,4 +623,78 @@ public class TestRequest extends TomcatB
 
         return conn;
     }
+
+    private static class Bug54984Servlet extends HttpServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
+                throws ServletException, IOException {
+            req.setCharacterEncoding("UTF-8");
+
+            if (req.getRequestURI().endsWith("parseParametersBeforeParseParts")) {
+                req.getParameterNames();
+            }
+
+            req.getPart("part");
+
+            resp.setContentType("text/plain");
+            resp.setCharacterEncoding("UTF-8");
+
+            resp.getWriter().println("Part " + req.getParameter("part"));
+        }
+    }
+
+    private void prepareRequestBug54984(HttpURLConnection conn)
+            throws Exception {
+        String boundary = "-----" + System.currentTimeMillis();
+        conn.setRequestProperty("Content-Type",
+                "multipart/form-data; boundary=" + boundary);
+
+        PrintWriter writer = null;
+        try {
+            writer = new PrintWriter(new OutputStreamWriter(
+                    conn.getOutputStream(), "UTF-8"), true);
+            writer.append("--" + boundary).append("\r\n");
+            writer.append("Content-Disposition: form-data; name=\"part\"\r\n");
+            writer.append("Content-Type: text/plain; charset=UTF-8\r\n");
+            writer.append("\r\n");
+            writer.append("äö").append("\r\n");
+            writer.flush();
+
+            writer.append("\r\n");
+            writer.flush();
+
+            writer.append("--" + boundary + "--").append("\r\n");
+        } finally {
+            if (writer != null) {
+                writer.close();
+            }
+        }
+    }
+
+    private void checkResponseBug54984(HttpURLConnection conn)
+            throws Exception {
+        List<String> response = new ArrayList<String>();
+        int status = conn.getResponseCode();
+        if (status == HttpURLConnection.HTTP_OK) {
+            BufferedReader reader = null;
+            try {
+                reader = new BufferedReader(new InputStreamReader(
+                        conn.getInputStream(), "UTF-8"));
+                String line = null;
+                while ((line = reader.readLine()) != null) {
+                    response.add(line);
+                }
+                assertTrue(response.contains("Part äö"));
+            } finally {
+                if (reader != null) {
+                    reader.close();
+                }
+            }
+        } else {
+            fail("OK status was expected: " + status);
+        }
+    }
 }

Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1486460&r1=1486459&r2=1486460&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Sun May 26 20:00:38 2013
@@ -98,6 +98,11 @@
         that the temporary copy of the web application and not the original is
         removed when the web application stops. (markt) 
       </fix>
+      <fix>
+        <bug>54984</bug>: Use the correct encoding when processing a form data
+        posted as multipart/form-data even when the request parameters are not
+        parsed. (violetagg)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">



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