Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.155 diff -u -r1.155 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 20 Jun 2003 16:43:18 -0000 1.155 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 23 Jun 2003 03:08:39 -0000 @@ -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,61 @@ 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'. + * + * @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. 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.44 diff -u -r1.44 PostMethod.java --- java/org/apache/commons/httpclient/methods/PostMethod.java 19 Jun 2003 20:52:07 -0000 1.44 +++ java/org/apache/commons/httpclient/methods/PostMethod.java 23 Jun 2003 03:08:41 -0000 @@ -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 NameValuePairs. */ @@ -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. * *

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(); } Index: test/org/apache/commons/httpclient/TestRequestLine.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestLine.java,v retrieving revision 1.2 diff -u -r1.2 TestRequestLine.java --- test/org/apache/commons/httpclient/TestRequestLine.java 9 Apr 2003 18:38:00 -0000 1.2 +++ test/org/apache/commons/httpclient/TestRequestLine.java 23 Jun 2003 03:08:44 -0000 @@ -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)); } 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.16 diff -u -r1.16 TestWebappMethods.java --- test/org/apache/commons/httpclient/TestWebappMethods.java 19 Jun 2003 20:52:08 -0000 1.16 +++ test/org/apache/commons/httpclient/TestWebappMethods.java 23 Jun 2003 03:08:45 -0000 @@ -301,7 +301,7 @@ fail("Unable to execute method : " + t.toString()); } assertEquals(200,method.getStatusCode()); - assertTrue(method.getResponseBodyAsString().indexOf("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times%2E") >= 0); + assertTrue(method.getResponseBodyAsString().indexOf("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.") >= 0); } public void testPostBody() throws Exception {