Index: java/org/apache/commons/httpclient/methods/PostMethod.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
retrieving revision 1.43
diff -u -r1.43 PostMethod.java
--- java/org/apache/commons/httpclient/methods/PostMethod.java 26 May 2003 22:07:22 -0000 1.43
+++ java/org/apache/commons/httpclient/methods/PostMethod.java 14 Jun 2003 15:27:41 -0000
@@ -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 NameValuePairs.
*/
@@ -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]);
}
}
}
Index: test/org/apache/commons/httpclient/TestMethodCharEncoding.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodCharEncoding.java,v
retrieving revision 1.1
diff -u -r1.1 TestMethodCharEncoding.java
--- test/org/apache/commons/httpclient/TestMethodCharEncoding.java 17 Apr 2003 11:34:19 -0000 1.1
+++ test/org/apache/commons/httpclient/TestMethodCharEncoding.java 14 Jun 2003 15:27:43 -0000
@@ -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
};
Index: test/org/apache/commons/httpclient/TestMethodsNoHost.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v
retrieving revision 1.18
diff -u -r1.18 TestMethodsNoHost.java
--- test/org/apache/commons/httpclient/TestMethodsNoHost.java 13 Jun 2003 21:32:17 -0000 1.18
+++ test/org/apache/commons/httpclient/TestMethodsNoHost.java 14 Jun 2003 15:27:45 -0000
@@ -1,5 +1,5 @@
/*
- * $Header: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v 1.18 2003/06/13 21:32:17 olegk Exp $
+ * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsNoHost.java,v 1.18 2003/06/13 21:32:17 olegk Exp $
* $Revision: 1.18 $
* $Date: 2003/06/13 21:32:17 $
* ====================================================================
@@ -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());
}
Index: test/org/apache/commons/httpclient/TestWebappMethods.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v
retrieving revision 1.15
diff -u -r1.15 TestWebappMethods.java
--- test/org/apache/commons/httpclient/TestWebappMethods.java 15 May 2003 18:06:03 -0000 1.15
+++ test/org/apache/commons/httpclient/TestWebappMethods.java 14 Jun 2003 15:27:48 -0000
@@ -1,5 +1,5 @@
/*
- * $Header: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v 1.15 2003/05/15 18:06:03 olegk Exp $
+ * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v 1.15 2003/05/15 18:06:03 olegk Exp $
* $Revision: 1.15 $
* $Date: 2003/05/15 18:06:03 $
* ====================================================================
@@ -301,7 +301,7 @@
fail("Unable to execute method : " + t.toString());
}
assertEquals(200,method.getStatusCode());
- assertTrue(method.getResponseBodyAsString().indexOf("quote=It%20was%20the%20best%20of%20times%2C%20it%20was%20the%20worst%20of%20times.") >= 0);
+ assertTrue(method.getResponseBodyAsString().indexOf("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times%2E") >= 0);
}
public void testPostBody() throws Exception {