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.146 diff -u -r1.146 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 12 May 2003 19:33:42 -0000 1.146 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 12 May 2003 20:11:23 -0000 @@ -1394,6 +1394,10 @@ * Adds a Content-Length or Transfer-Encoding: Chunked * request header, as long as no Content-Length request header * already exists. + * + * TODO: Revise this method as it is potentially error-prone. + * 'Transfer-encoding: chunked' header should not be set here + * as some sub classes may not support chunk-encoding. * * @param state current state of http requests * @param conn the connection to use for I/O Index: java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v retrieving revision 1.16 diff -u -r1.16 EntityEnclosingMethod.java --- java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java 19 Apr 2003 22:29:32 -0000 1.16 +++ java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java 12 May 2003 20:11:26 -0000 @@ -305,6 +305,9 @@ protected int getRequestContentLength() { LOG.trace("enter EntityEnclosingMethod.getRequestContentLength()"); + if (!hasRequestContent()) { + return 0; + } if (this.requestContentLength != CONTENT_LENGTH_AUTO) { return this.requestContentLength; } @@ -312,6 +315,35 @@ this.contentCache = generateRequestBody(); } return (this.contentCache == null) ? 0 : this.contentCache.length; + } + + /** + * Adds a Content-Length or Transfer-Encoding: Chunked + * request header, as long as no Content-Length request header + * already exists. + * + * @param state current state of http requests + * @param conn the connection to use for I/O + * + * @throws IOException when errors occur reading or writing to/from the + * connection + * @throws HttpException when a recoverable error occurs + */ + protected void addContentLengthRequestHeader(HttpState state, + HttpConnection conn) + throws IOException, HttpException { + LOG.trace("enter HttpMethodBase.addContentLengthRequestHeader(" + + "HttpState, HttpConnection)"); + + if ((getRequestHeader("content-length") == null) && + (getRequestHeader("Transfer-Encoding") == null)) { + int len = getRequestContentLength(); + if (len >= 0) { + addRequestHeader("Content-Length", String.valueOf(len)); + } else if ((len == CONTENT_LENGTH_CHUNKED) && (isHttp11())) { + addRequestHeader("Transfer-Encoding", "chunked"); + } + } } /** 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.14 diff -u -r1.14 TestWebappMethods.java --- test/org/apache/commons/httpclient/TestWebappMethods.java 5 Mar 2003 04:02:57 -0000 1.14 +++ test/org/apache/commons/httpclient/TestWebappMethods.java 12 May 2003 20:11:30 -0000 @@ -63,6 +63,8 @@ package org.apache.commons.httpclient; import java.io.ByteArrayInputStream; +import java.io.InputStream; + import junit.framework.*; import org.apache.commons.httpclient.methods.*; @@ -422,4 +424,89 @@ assertEquals(200,method.getStatusLine().getStatusCode()); response = method.getResponseBodyAsString(); } + + public void testEmptyPostMethod() throws Exception { + HttpClient client = createHttpClient(); + PostMethod method = new PostMethod("/" + getWebappContext() + "/body"); + + method.setRequestHeader("Content-Type", "text/plain"); + client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + String response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + + method.recycle(); + + method.setPath("/" + getWebappContext() + "/body"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setRequestBody((String)null); + client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + + method.recycle(); + + method.setPath("/" + getWebappContext() + "/body"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setRequestBody((InputStream)null); + client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + + method.recycle(); + + method.setPath("/" + getWebappContext() + "/body"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setRequestBody(""); + client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + + method.recycle(); + + method.setPath("/" + getWebappContext() + "/body"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); + client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + + method.recycle(); + + method.setPath("/" + getWebappContext() + "/body"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); + method.setRequestBody((String)null); + client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + + method.recycle(); + + method.setPath("/" + getWebappContext() + "/body"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); + method.setRequestBody((InputStream)null); + client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + response = method.getResponseBodyAsString(); + assertTrue(response.indexOf("No body submitted") >= 0); + + method.recycle(); + + method.setPath("/" + getWebappContext() + "/body"); + method.setRequestHeader("Content-Type", "text/plain"); + method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); + method.setRequestBody(""); + client.executeMethod(method); + assertEquals(200,method.getStatusLine().getStatusCode()); + response = method.getResponseBodyAsString(); + + } + }