You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2003/06/19 22:52:08 UTC

cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestMethodCharEncoding.java TestMethodsNoHost.java TestWebappMethods.java

olegk       2003/06/19 13:52:08

  Modified:    httpclient/src/java/org/apache/commons/httpclient/methods
                        PostMethod.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestMethodCharEncoding.java TestMethodsNoHost.java
                        TestWebappMethods.java
  Log:
  Bug fix #20481 (HttpClient does not properly handle 'application/x-www-form-urlencoded' encoding)
  
  Contributed by Oleg Kalnichevski
  Reviewed by Mike Becke & Ortwin Glück
  
  Revision  Changes    Path
  1.44      +54 -8     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java
  
  Index: PostMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
  retrieving revision 1.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- PostMethod.java	26 May 2003 22:07:22 -0000	1.43
  +++ PostMethod.java	19 Jun 2003 20:52:07 -0000	1.44
  @@ -63,6 +63,7 @@
   package org.apache.commons.httpclient.methods;
   
   import java.io.IOException;
  +import java.util.BitSet;
   import java.util.Iterator;
   import java.util.Vector;
   
  @@ -113,10 +114,33 @@
       /** Log object for this class. */
       private static final Log LOG = LogFactory.getLog(PostMethod.class);
   
  -    /** The Content-Type for www-form-urlcoded. */
  +    /** The Content-Type for www-form-urlencoded. */
       public static final String FORM_URL_ENCODED_CONTENT_TYPE = 
           "application/x-www-form-urlencoded";
   
  +    /**
  +     * BitSet of www-form-url safe characters.
  +     * 
  +     */
  +    protected static final BitSet WWW_FORM_URL = new BitSet(256);
  +    // Static initializer for www_form_url
  +    static {
  +        // alpha characters
  +        for (int i = 'a'; i <= 'z'; i++) {
  +            WWW_FORM_URL.set(i);
  +        }
  +        for (int i = 'A'; i <= 'Z'; i++) {
  +            WWW_FORM_URL.set(i);
  +        }
  +        // numeric characters
  +        for (int i = '0'; i <= '9'; i++) {
  +            WWW_FORM_URL.set(i);
  +        }
  +        // blank to be replaced with +
  +        WWW_FORM_URL.set(' ');
  +    }
  +
  +
       /** 
        * The buffered request body consisting of <code>NameValuePair</code>s. 
        */
  @@ -219,6 +243,27 @@
           super.clearRequestBody();
       }
   
  +    /**
  +     * Form-urlencoding routine
  +     *
  +     * The default encoding for all forms is `application/x-www-form-urlencoded'. 
  +     * A form data set is represented in this media type as follows:
  +     *
  +     * The form field names and values are escaped: space characters are replaced 
  +     * by `+', and then reserved characters are escaped as per [URL]; that is, 
  +     * non-alphanumeric characters are replaced by `%HH', a percent sign and two 
  +     * hexadecimal digits representing the ASCII code of the character. Line breaks, 
  +     * as in multi-line text field values, are represented as CR LF pairs, i.e. `%0D%0A'.
  +     * 
  +     * @since 2.0beta2
  +     */
  +    protected String formUrlEncode(final String unescaped, final String charset)
  +      throws URIException {
  +        if (unescaped == null) {
  +            return null;
  +        }
  +        return URIUtil.encode(unescaped, WWW_FORM_URL, charset).replace(' ', '+');
  +    }
   
       /**
        * Generates request body.
  @@ -245,7 +290,7 @@
                   NameValuePair parameter = (NameValuePair) this.params.get(i);
                   String queryName = null;
                   try {
  -                    queryName = URIUtil.encodeWithinQuery(parameter.getName(), charset);
  +                    queryName = formUrlEncode(parameter.getName(), charset);
                   } catch (URIException e) {
                       if (LOG.isWarnEnabled()) {
                           LOG.warn("Encosing error: " + e.toString());
  @@ -257,7 +302,7 @@
                   String queryValue = null;
   
                   try {
  -                    queryValue = URIUtil.encodeWithinQuery(parameter.getValue(), charset);
  +                    queryValue = formUrlEncode(parameter.getValue(), charset);
                   } catch (URIException e) {
                       if (LOG.isWarnEnabled()) {
                           LOG.warn("Encosing error: " + e.toString());
  @@ -401,8 +446,9 @@
           if (parameters == null) {
               LOG.warn("Attempt to addParameters(null) ignored");
           } else {
  +            super.clearRequestBody();
               for (int i = 0; i < parameters.length; i++) {
  -                addParameter(parameters[i]);
  +                this.params.add(parameters[i]);
               }
           }
       }
  
  
  
  1.2       +7 -7      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java
  
  Index: TestMethodCharEncoding.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestMethodCharEncoding.java	17 Apr 2003 11:34:19 -0000	1.1
  +++ TestMethodCharEncoding.java	19 Jun 2003 20:52:07 -0000	1.2
  @@ -87,36 +87,36 @@
       static final String CHARSET_WIN1251 = "Cp1251";
   
       static final int SWISS_GERMAN_STUFF_UNICODE [] = {
  -        0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x20, 0x7A, 0xE4, 0x6D, 0xE4
  +        0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
       };
       
       static final int SWISS_GERMAN_STUFF_ISO8859_1 [] = {
  -        0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x20, 0x7A, 0xE4, 0x6D, 0xE4
  +        0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
       };
       
       static final int SWISS_GERMAN_STUFF_UTF8 [] = {
  -        0x47, 0x72, 0xC3, 0xBC, 0x65, 0x7A, 0x69, 0x20, 0x7A, 0xC3, 0xA4,
  +        0x47, 0x72, 0xC3, 0xBC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xC3, 0xA4,
           0x6D, 0xC3, 0xA4
       };
   
       static final int RUSSIAN_STUFF_UNICODE [] = {
  -        0x412, 0x441, 0x435, 0x43C, 0x20, 0x43F, 0x440, 0x438, 
  +        0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 
           0x432, 0x435, 0x442 
       }; 
   
       static final int RUSSIAN_STUFF_UTF8 [] = {
  -        0xD0, 0x92, 0xD1, 0x81, 0xD0, 0xB5, 0xD0, 0xBC, 0x20, 
  +        0xD0, 0x92, 0xD1, 0x81, 0xD0, 0xB5, 0xD0, 0xBC, 0x5F, 
           0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 
           0xB5, 0xD1, 0x82
       };
   
       static final int RUSSIAN_STUFF_KOI8R [] = {
  -        0xF7, 0xD3, 0xC5, 0xCD, 0x20, 0xD0, 0xD2, 0xC9, 0xD7, 
  +        0xF7, 0xD3, 0xC5, 0xCD, 0x5F, 0xD0, 0xD2, 0xC9, 0xD7, 
           0xC5, 0xD4
       };
   
       static final int RUSSIAN_STUFF_WIN1251 [] = {
  -        0xC2, 0xF1, 0xE5, 0xEC, 0x20, 0xEF, 0xF0, 0xE8, 0xE2, 
  +        0xC2, 0xF1, 0xE5, 0xEC, 0x5F, 0xEF, 0xF0, 0xE8, 0xE2, 
           0xE5, 0xF2
       };
   
  
  
  
  1.19      +5 -5      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java
  
  Index: TestMethodsNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- TestMethodsNoHost.java	13 Jun 2003 21:32:17 -0000	1.18
  +++ TestMethodsNoHost.java	19 Jun 2003 20:52:07 -0000	1.19
  @@ -116,7 +116,7 @@
               post.getRequestBodyAsString());
   
           post.setRequestBody(new NameValuePair[]{ PAIR, PAIR1, PAIR2, new NameValuePair("hasSpace", "a b c d") });
  -        assertEquals("name=value&name1=value1&name2=value2&hasSpace=a%20b%20c%20d",
  +        assertEquals("name=value&name1=value1&name2=value2&hasSpace=a+b+c+d",
               post.getRequestBodyAsString());
   
       }
  
  
  
  1.16      +5 -5      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java
  
  Index: TestWebappMethods.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- TestWebappMethods.java	15 May 2003 18:06:03 -0000	1.15
  +++ TestWebappMethods.java	19 Jun 2003 20:52:08 -0000	1.16
  @@ -301,7 +301,7 @@
               fail("Unable to execute method : " + t.toString());
           }
           assertEquals(200,method.getStatusCode());
  -        assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It%20was%20the%20best%20of%20times%2C%20it%20was%20the%20worst%20of%20times.</tt>") >= 0);
  +        assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times%2E</tt>") >= 0);
       }
   
       public void testPostBody() throws Exception {
  
  
  

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