You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2003/05/15 20:06:03 UTC

cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestWebappMethods.java

olegk       2003/05/15 11:06:03

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpMethodBase.java
               httpclient/src/java/org/apache/commons/httpclient/methods
                        EntityEnclosingMethod.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestWebappMethods.java
  Log:
  Bug fix #19771
  
  Changelog:
  - Entity enclosing methods correctly handle null & empty request content
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.147     +8 -4      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.146
  retrieving revision 1.147
  diff -u -r1.146 -r1.147
  --- HttpMethodBase.java	12 May 2003 19:33:42 -0000	1.146
  +++ HttpMethodBase.java	15 May 2003 18:06:02 -0000	1.147
  @@ -1394,6 +1394,10 @@
        * Adds a <tt>Content-Length</tt> or <tt>Transfer-Encoding: Chunked</tt>
        * request header, as long as no <tt>Content-Length</tt> 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
  
  
  
  1.17      +36 -4     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java
  
  Index: EntityEnclosingMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- EntityEnclosingMethod.java	19 Apr 2003 22:29:32 -0000	1.16
  +++ EntityEnclosingMethod.java	15 May 2003 18:06:02 -0000	1.17
  @@ -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 <tt>Content-Length</tt> or <tt>Transfer-Encoding: Chunked</tt>
  +     * request header, as long as no <tt>Content-Length</tt> 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");
  +            }
  +        }
       }
   
       /**
  
  
  
  1.15      +91 -4     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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- TestWebappMethods.java	5 Mar 2003 04:02:57 -0000	1.14
  +++ TestWebappMethods.java	15 May 2003 18:06:03 -0000	1.15
  @@ -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();
  +
  +    }
  +
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org