olegk 2003/02/03 13:21:20 Modified: httpclient/src/java/org/apache/commons/httpclient HttpConstants.java httpclient/src/java/org/apache/commons/httpclient/methods PostMethod.java httpclient/src/test/org/apache/commons/httpclient TestMethodsNoHost.java TestWebappPostMethod.java Log: - fixes the problem with request content-encoding not taken into account when url-enconding parameters in POST method - adds test case to ensure appropriate encoding of non-ascii characters such as German umlauts in POST method parameters Problem reported by Mathis Thomas Contributed by Oleg Kalnichevski Revision Changes Path 1.5 +4 -4 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java Index: HttpConstants.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- HttpConstants.java 28 Jan 2003 04:40:20 -0000 1.4 +++ HttpConstants.java 3 Feb 2003 21:21:15 -0000 1.5 @@ -81,7 +81,7 @@ public static final String HTTP_ELEMENT_CHARSET = "US-ASCII"; /** Default content encoding chatset */ - protected static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1"; + public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1"; /** Log object for this class. */ private static final Log LOG = LogFactory.getLog(HttpConstants.class); 1.38 +21 -15 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.37 retrieving revision 1.38 diff -u -r1.37 -r1.38 --- PostMethod.java 2 Feb 2003 04:30:13 -0000 1.37 +++ PostMethod.java 3 Feb 2003 21:21:19 -0000 1.38 @@ -123,9 +123,9 @@ /** My content encoding. */ private int contentEncoding = CUSTOM_CONTENT; - /** The Content-Type header for www-form-urlcoded. */ - public static final Header URL_ENCODED_CONTENT_TYPE = new Header("Content-Type", - "application/x-www-form-urlencoded"); + /** The Content-Type for www-form-urlcoded. */ + public static final String FORM_URL_ENCODED_CONTENT_TYPE = + "application/x-www-form-urlencoded"; /** * The buffered request body consisting of <code>NameValuePair</code>s. @@ -434,7 +434,7 @@ * * @throws IllegalArgumentException if parameters is null */ - protected static String generateRequestBody(NameValuePair[] parameters) + protected static String generateRequestBody(NameValuePair[] parameters, final String charset) throws IllegalArgumentException { LOG.trace("enter PostMethod.generateRequestBody(NameValuePair[])"); @@ -452,9 +452,11 @@ String queryName = null; try { - queryName = URIUtil.encodeWithinQuery(parameter.getName()); - } catch (URIException urie) { - LOG.error("encoding error within query name", urie); + queryName = URIUtil.encodeWithinQuery(parameter.getName(), charset); + } catch (URIException e) { + if (LOG.isWarnEnabled()) { + LOG.warn("Encosing error: " + e.toString()); + } queryName = parameter.getName(); } @@ -462,9 +464,11 @@ String queryValue = null; try { - queryValue = URIUtil.encodeWithinQuery(parameter.getValue()); + queryValue = URIUtil.encodeWithinQuery(parameter.getValue(), charset); } catch (URIException e) { - LOG.error("Encoding error within query value", e); + if (LOG.isWarnEnabled()) { + LOG.warn("Encosing error: " + e.toString()); + } queryValue = parameter.getValue(); } buff.append(queryValue); @@ -529,7 +533,7 @@ if (parametersBody == null) { throw new IllegalArgumentException("Array of parameters may not be null"); } - super.setRequestBody(generateRequestBody(parametersBody)); + super.setRequestBody(generateRequestBody(parametersBody, getRequestCharSet())); this.contentEncoding = URL_ENCODED_CONTENT; } @@ -551,7 +555,9 @@ if (this.contentEncoding == URL_ENCODED_CONTENT) { //there are some parameters, so set the contentType header - setRequestHeader(URL_ENCODED_CONTENT_TYPE); + if (getRequestHeader("Content-Type") == null) { + setRequestHeader("Content-Type", FORM_URL_ENCODED_CONTENT_TYPE); + } } } 1.15 +13 -6 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.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- TestMethodsNoHost.java 31 Jan 2003 23:23:17 -0000 1.14 +++ TestMethodsNoHost.java 3 Feb 2003 21:21:19 -0000 1.15 @@ -72,11 +72,13 @@ import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.HeadMethod; +import org.apache.commons.httpclient.util.URIUtil; /** * @author Rodney Waldhoff * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Dever</a> * @author Ortwin Glück + * @author <a href="mailto:[EMAIL PROTECTED]">Oleg Kalnichevski</a> * @version $Revision$ $Date$ */ public class TestMethodsNoHost extends TestCase { @@ -85,13 +87,14 @@ static final String NAME0 = "name0", VALUE0 = "value0"; static final String NAME1 = "name1", VALUE1 = "value1"; static final String NAME2 = "name2", VALUE2 = "value2"; + static final String NAME3 = "name3", FUNNY_CHARS = "üöäéèà"; + static final NameValuePair PAIR = new NameValuePair(NAME, VALUE); static final NameValuePair PAIR0 = new NameValuePair(NAME0, VALUE0); static final NameValuePair PAIR1 = new NameValuePair(NAME1, VALUE1); static final NameValuePair PAIR2 = new NameValuePair(NAME2, VALUE2); + static final NameValuePair PAIR3 = new NameValuePair(NAME3, FUNNY_CHARS); - - // ------------------------------------------------------------ Constructor public TestMethodsNoHost(String testName) { @@ -119,6 +122,10 @@ assertEquals("name=value&name1=value1&name2=value2&hasSpace=a%20b%20c%20d", post.getRequestBodyAsString()); + post.setRequestBody(new NameValuePair[]{ PAIR3 }); + assertEquals("name3=" + URIUtil.encodeWithinQuery( + FUNNY_CHARS, HttpConstants.DEFAULT_CONTENT_CHARSET), + post.getRequestBodyAsString()); } public void testPostSetRequestBody() throws Exception { 1.2 +6 -6 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java Index: TestWebappPostMethod.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestWebappPostMethod.java 1 Feb 2003 06:33:05 -0000 1.1 +++ TestWebappPostMethod.java 3 Feb 2003 21:21:19 -0000 1.2 @@ -154,7 +154,7 @@ String stringBody = "pname1=pvalue1&pname2=pvalue2"; method.setRequestBody(stringBody); - method.setRequestHeader(PostMethod.URL_ENCODED_CONTENT_TYPE); + method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE); verifyParams(method); } @@ -180,7 +180,7 @@ new ByteArrayInputStream("pname1=pvalue1&pname2=pvalue2".getBytes()); method.setRequestBody(streamBody); - method.setRequestHeader(PostMethod.URL_ENCODED_CONTENT_TYPE); + method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE); verifyParams(method); }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]