Author: pete
Date: Thu Dec  2 00:10:16 2010
New Revision: 1041223

URL: http://svn.apache.org/viewvc?rev=1041223&view=rev
Log:
fix a character encoding bug with multipart/form-data encoded forms: 

the multipart upload request gets no character encoding since the browser does 
not send the content type. so we use the application encoding which we also 
demand for multipart forms using the form attribute "accept-encoding" = 
application encoding.

this should get us encoding-safe string in a multipart form.

problem encountered with jetty 6 and 7 on os x using firefox, safari + chrome

Modified:
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/def/RequestCycleSettings.java

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java?rev=1041223&r1=1041222&r2=1041223&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java 
(original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java 
Thu Dec  2 00:10:16 2010
@@ -1560,6 +1560,13 @@ public class Form<T> extends WebMarkupCo
                        if (isMultiPart())
                        {
                                tag.put("enctype", "multipart/form-data");
+                               //
+                               // require the application-encoding for 
multipart/form-data to be sure to
+                               // get multipart-uploaded characters with the 
proper encoding on the following request.
+                               //
+                               // see 
http://stackoverflow.com/questions/546365/utf-8-text-is-garbled-when-form-is-posted-as-multipart-form-data
+                               //
+                               tag.put("accept-encoding", 
getApplication().getRequestCycleSettings().getResponseRequestEncoding());
                        }
                        else
                        {

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java?rev=1041223&r1=1041222&r2=1041223&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/MultipartServletWebRequestImpl.java
 Thu Dec  2 00:10:16 2010
@@ -27,6 +27,7 @@ import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.wicket.Application;
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.util.lang.Bytes;
 import org.apache.wicket.util.string.StringValue;
@@ -125,9 +126,17 @@ public class MultipartServletWebRequestI
 
                // The encoding that will be used to decode the string 
parameters
                // It should NOT be null at this point, but it may be
-               // if the older Servlet API 2.2 is used
+               // especially if the older Servlet API 2.2 is used
                String encoding = request.getCharacterEncoding();
 
+               // The encoding can also be null when using multipart/form-data 
encoded forms.
+               // In that case we use the [application-encoding] which we 
always demand using
+               // the attribute 'accept-encoding' in wicket forms.
+               if(encoding == null)
+               {
+                       encoding = 
Application.get().getRequestCycleSettings().getResponseRequestEncoding();
+               }
+               
                // set encoding specifically when we found it
                if (encoding != null)
                {

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/def/RequestCycleSettings.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/def/RequestCycleSettings.java?rev=1041223&r1=1041222&r2=1041223&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/def/RequestCycleSettings.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/def/RequestCycleSettings.java
 Thu Dec  2 00:10:16 2010
@@ -23,6 +23,7 @@ import java.util.List;
 import org.apache.wicket.response.filter.IResponseFilter;
 import org.apache.wicket.settings.IExceptionSettings;
 import org.apache.wicket.settings.IRequestCycleSettings;
+import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.time.Duration;
 
 /**
@@ -179,9 +180,10 @@ public class RequestCycleSettings implem
        /**
         * @see 
org.apache.wicket.settings.IRequestCycleSettings#setResponseRequestEncoding(java.lang.String)
         */
-       public void setResponseRequestEncoding(final String 
responseRequestEncoding)
+       public void setResponseRequestEncoding(final String encoding)
        {
-               this.responseRequestEncoding = responseRequestEncoding;
+               Args.notNull(encoding, "encoding");
+               this.responseRequestEncoding = encoding;
        }
 
        /**


Reply via email to