You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mb...@apache.org on 2003/06/24 01:41:40 UTC
cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods PostMethod.java
mbecke 2003/06/23 16:41:40
Modified: httpclient/src/java/org/apache/commons/httpclient
HttpMethodBase.java
httpclient/src/test/org/apache/commons/httpclient
TestWebappMethods.java TestRequestLine.java
httpclient/src/java/org/apache/commons/httpclient/methods
PostMethod.java
Log:
Modifies form urlencoding to not encode *, ., - and _. Adds urlencoding for query params.
PR: 20481
Reviewed by: Oleg Kalnichevski and Laura Werner
Revision Changes Path
1.156 +92 -36 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java
Index: HttpMethodBase.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
retrieving revision 1.155
retrieving revision 1.156
diff -u -r1.155 -r1.156
--- HttpMethodBase.java 20 Jun 2003 16:43:18 -0000 1.155
+++ HttpMethodBase.java 23 Jun 2003 23:41:39 -0000 1.156
@@ -68,6 +68,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
+import java.util.BitSet;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.httpclient.auth.AuthScheme;
@@ -157,6 +158,32 @@
USER_AGENT = new Header("User-Agent", agent);
}
+ /**
+ * 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(' ');
+ WWW_FORM_URL.set('-');
+ WWW_FORM_URL.set('_');
+ WWW_FORM_URL.set('.');
+ WWW_FORM_URL.set('*');
+ }
+
// ----------------------------------------------------- Instance variables
/** My request headers, if any. */
@@ -465,37 +492,7 @@
*/
public void setQueryString(NameValuePair[] params) {
LOG.trace("enter HttpMethodBase.setQueryString(NameValuePair[])");
- StringBuffer buf = new StringBuffer();
- boolean needAmp = false;
- for (int i = 0; i < params.length; i++) {
- if (params[i].getName() != null) {
- if (needAmp) {
- buf.append("&");
- } else {
- needAmp = true;
- }
- String queryName = null;
- try {
- queryName = URIUtil.encodeWithinQuery(params[i].getName());
- } catch (URIException urie) {
- LOG.error("encoding error within query name", urie);
- queryName = params[i].getName();
- }
- buf.append(queryName).append("=");
- if (params[i].getValue() != null) {
- String queryValue = null;
- try {
- queryValue =
- URIUtil.encodeWithinQuery(params[i].getValue());
- } catch (URIException urie) {
- LOG.error("encoding error within query value", urie);
- queryValue = params[i].getValue();
- }
- buf.append(queryValue);
- }
- }
- }
- queryString = buf.toString();
+ queryString = formUrlEncode(params, HttpConstants.HTTP_ELEMENT_CHARSET);
}
/**
@@ -1700,7 +1697,66 @@
return buf.toString();
}
-
+
+ /**
+ * @deprecated temporary method. to be moved to commons Codec.
+ *
+ * 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'.
+ *
+ * @param pairs the values to be encoded
+ * @param charset the character set of pairs to be encoded
+ *
+ * @return the urlencoded pairs
+ *
+ * @since 2.0beta2
+ */
+ protected static String formUrlEncode(NameValuePair[] pairs, String charset) {
+
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < pairs.length; i++) {
+ if (pairs[i].getName() != null) {
+ if (i > 0) {
+ buf.append("&");
+ }
+ String queryName = pairs[i].getName();
+ try {
+ queryName = URIUtil.encode(
+ queryName,
+ WWW_FORM_URL,
+ charset
+ ).replace(' ', '+');
+ } catch (URIException urie) {
+ LOG.error("Error encoding pair name: " + queryName, urie);
+ }
+ buf.append(queryName);
+ buf.append("=");
+ if (pairs[i].getValue() != null) {
+ String queryValue = pairs[i].getValue();
+ try {
+ queryValue = URIUtil.encode(
+ queryValue,
+ WWW_FORM_URL,
+ charset
+ ).replace(' ', '+');
+ } catch (URIException urie) {
+ LOG.error("Error encoding pair value: " + queryValue, urie);
+ }
+ buf.append(queryValue);
+ }
+ }
+ }
+ return buf.toString();
+ }
+
/**
* When this method is invoked, {@link #readResponseBody
* readResponseBody(HttpState,HttpConnection)} will have been invoked.
1.17 +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.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- TestWebappMethods.java 19 Jun 2003 20:52:08 -0000 1.16
+++ TestWebappMethods.java 23 Jun 2003 23:41:39 -0000 1.17
@@ -301,7 +301,7 @@
fail("Unable to execute method : " + t.toString());
}
assertEquals(200,method.getStatusCode());
- assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times%2E</tt>") >= 0);
+ assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
}
public void testPostBody() throws Exception {
1.3 +3 -3 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestLine.java
Index: TestRequestLine.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestLine.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestRequestLine.java 9 Apr 2003 18:38:00 -0000 1.2
+++ TestRequestLine.java 23 Jun 2003 23:41:39 -0000 1.3
@@ -140,10 +140,10 @@
method = new SimpleHttpMethod();
method.setQueryString( new NameValuePair[] {
- new NameValuePair("param1", "!@#$%^&"),
+ new NameValuePair("param1", " !#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~"),
new NameValuePair("param2", "some stuff")
} );
- assertEquals("Simple /?param1=!%40%23%24%25%5E%26¶m2=some%20stuff HTTP/1.1\r\n",
+ assertEquals("Simple /?param1=+%21%23%24%25%26%27%28%29*%2B%2C-.%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D%7E¶m2=some+stuff HTTP/1.1\r\n",
method.getTestRequestLine(conn));
}
1.45 +6 -84 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.44
retrieving revision 1.45
diff -u -r1.44 -r1.45
--- PostMethod.java 19 Jun 2003 20:52:07 -0000 1.44
+++ PostMethod.java 23 Jun 2003 23:41:40 -0000 1.45
@@ -63,7 +63,6 @@
package org.apache.commons.httpclient.methods;
import java.io.IOException;
-import java.util.BitSet;
import java.util.Iterator;
import java.util.Vector;
@@ -72,8 +71,6 @@
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.NameValuePair;
-import org.apache.commons.httpclient.URIException;
-import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -118,29 +115,6 @@
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.
*/
@@ -244,28 +218,6 @@
}
/**
- * 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.
*
* <p>This method must be overwritten by sub-classes that implement
@@ -280,38 +232,8 @@
protected byte[] generateRequestBody() {
LOG.trace("enter PostMethod.renerateRequestBody()");
if (!this.params.isEmpty()) {
- String charset = getRequestCharSet();
- StringBuffer buff = new StringBuffer();
-
- for (int i = 0; i < this.params.size(); i++) {
- if (i > 0) {
- buff.append("&");
- }
- NameValuePair parameter = (NameValuePair) this.params.get(i);
- String queryName = null;
- try {
- queryName = formUrlEncode(parameter.getName(), charset);
- } catch (URIException e) {
- if (LOG.isWarnEnabled()) {
- LOG.warn("Encosing error: " + e.toString());
- }
- queryName = parameter.getName();
- }
-
- buff.append(queryName).append("=");
- String queryValue = null;
-
- try {
- queryValue = formUrlEncode(parameter.getValue(), charset);
- } catch (URIException e) {
- if (LOG.isWarnEnabled()) {
- LOG.warn("Encosing error: " + e.toString());
- }
- queryValue = parameter.getValue();
- }
- buff.append(queryValue);
- }
- return HttpConstants.getContentBytes(buff.toString());
+ String content = formUrlEncode(getParameters(), getRequestCharSet());
+ return HttpConstants.getContentBytes(content);
} else {
return super.generateRequestBody();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org