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.145 diff -u -r1.145 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 8 May 2003 17:33:51 -0000 1.145 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 8 May 2003 21:29:40 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v 1.145 2003/05/08 17:33:51 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v 1.145 2003/05/08 17:33:51 olegk Exp $ * $Revision: 1.145 $ * $Date: 2003/05/08 17:33:51 $ * @@ -1383,9 +1383,9 @@ } /** - * Adds a Content-Length or Transfer-Encoding: Chunked - * request header, as long as no Content-Length request header - * already exists. + * This method should be overridden by entity enclosing methods in order + * to add appropriate Content-Length or Transfer-Encoding + * request headers. * * @param state current state of http requests * @param conn the connection to use for I/O @@ -1399,16 +1399,6 @@ throws IOException, HttpException { LOG.trace("enter HttpMethodBase.addContentLengthRequestHeader(" + "HttpState, HttpConnection)"); - - // add content length or chunking - int len = getRequestContentLength(); - if (getRequestHeader("content-length") == null) { - if (0 < len) { - setRequestHeader("Content-Length", String.valueOf(len)); - } else if (http11 && (len < 0)) { - setRequestHeader("Transfer-Encoding", "chunked"); - } - } } /** 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 8 May 2003 21:29:43 -0000 @@ -315,6 +315,38 @@ } /** + * 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 = 0; + if (hasRequestContent()) { + len = getRequestContentLength(); + } + if (len >= 0) { + addRequestHeader("Content-Length", String.valueOf(len)); + } else if ((len == CONTENT_LENGTH_CHUNKED) && (isHttp11())) { + addRequestHeader("Transfer-Encoding", "chunked"); + } + } + } + + /** * Sets the request body to be the specified inputstream. * * @param body Request body content as {@link java.io.InputStream} Index: java/org/apache/commons/httpclient/methods/MultipartPostMethod.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java,v retrieving revision 1.17 diff -u -r1.17 MultipartPostMethod.java --- java/org/apache/commons/httpclient/methods/MultipartPostMethod.java 7 Apr 2003 19:23:36 -0000 1.17 +++ java/org/apache/commons/httpclient/methods/MultipartPostMethod.java 8 May 2003 21:29:45 -0000 @@ -287,6 +287,30 @@ } } + /** + * Adds a Content-Length or Transfer-Encoding: Chunked + * request header. + * + * @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) { + int len = getRequestContentLength(); + if (len >= 0) { + addRequestHeader("Content-Length", String.valueOf(len)); + } + } + } /** * Clear my request body. 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 8 May 2003 21:29:50 -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(); + + } + }