commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient HttpMethodBase.java
Date Tue, 11 Mar 2003 22:20:10 GMT
olegk       2003/03/11 14:20:10

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpMethodBase.java
  Log:
  Bug #11190 fix
  
  Changelog:
  - HttpMethodBase#getResponseContentLength() method introduced
  - HttpMethodBase#readResponseBody(HttpConnection) method refactored
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.122     +74 -38    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.121
  retrieving revision 1.122
  diff -u -r1.121 -r1.122
  --- HttpMethodBase.java	7 Mar 2003 13:50:52 -0000	1.121
  +++ HttpMethodBase.java	11 Mar 2003 22:20:09 -0000	1.122
  @@ -649,6 +649,67 @@
           }        
       }
   
  +
  +    /**
  +     * Return the length (in bytes) of my response body, as specified in a
  +     * <tt>Content-Length</tt> header.
  +     *
  +     * <p>
  +     * Return <tt>-1</tt> when the content-length is unknown.
  +     * </p>
  +     *
  +     * @return content length, if <tt>Content-Length</tt> header is available.

  +     *          <tt>0</tt> indicates that the request has no body.
  +     *          If <tt>Content-Length</tt> header is not present, the method

  +     *          returns  <tt>-1</tt>.
  +     */
  +    protected int getResponseContentLength() {
  +        Header[] headers = getResponseHeaderGroup().getHeaders("Content-Length");
  +        if (headers.length == 0) {
  +            return -1;
  +        }
  +        if (headers.length > 1) {
  +            LOG.warn("Multiple content-length headers detected");
  +        }
  +        for (int i = headers.length - 1; i >= 0; i++) {
  +            Header header = headers[i];
  +            String lengthValue = null;
  +            // we're using this just in case the content length is duplicated
  +            // i.e. '57, 57'
  +            try {
  +                HeaderElement[] lengthElements = header.getValues();
  +
  +                if (lengthElements.length > 1) {
  +                    // looks like the content length header was duplicated. if so
  +                    // they won't be key=value pairs so we just want to get
  +                    // the name not the value (which should be null)
  +                    // take the first value and ignore any others
  +                    lengthValue = lengthElements[0].getName();
  +                } else {
  +                    lengthValue = header.getValue();
  +                }
  +            }
  +            catch(HttpException e) {
  +                if (LOG.isWarnEnabled()) {
  +                    LOG.warn("Invalid content-length value: " + e.getMessage());
  +                }
  +                lengthValue = null;
  +            }
  +            if (lengthValue != null) {
  +                try {
  +                    return Integer.parseInt(lengthValue);
  +                } catch(NumberFormatException e) {
  +                    if (LOG.isWarnEnabled()) {
  +                        LOG.warn("Invalid content-length value: " + e.getMessage());
  +                    }
  +                }
  +            }
  +            // See if we can have better luck with another header, if present
  +        }
  +        return -1;
  +    }
  +
  +
       /**
        * Return my response body, if any, as a byte array. Otherwise return
        * <tt>null</tt>.
  @@ -1798,7 +1859,6 @@
           LOG.trace("enter HttpMethodBase.readResponseBody(HttpState, HttpConnection)");
   
           responseBody = null; // is this desired?
  -        Header lengthHeader = getResponseHeader("Content-Length");
           Header transferEncodingHeader = getResponseHeader("Transfer-Encoding");
           InputStream is = conn.getResponseInputStream();
           if (Wire.enabled()) {
  @@ -1807,45 +1867,21 @@
           InputStream result = null;
           // We use Transfer-Encoding if present and ignore Content-Length.
           // RFC2616, 4.4 item number 3
  -        if (null != transferEncodingHeader) {
  +        if (transferEncodingHeader != null) {
               if ("chunked".equalsIgnoreCase(transferEncodingHeader.getValue())) {
                   result = new ChunkedInputStream(is, this);
               }
  -        } else if (null != lengthHeader) {
  -
  -            // we're using this just in case the content length is duplicated
  -            // i.e. '57, 57'
  -            HeaderElement[] lengthElements = lengthHeader.getValues();
  -            String lengthValue = null;
  -
  -            if (lengthElements.length > 1) {
  -                // looks like the content length header was duplicated. if so
  -                // they won't be key=value pairs so we just want to get
  -                // the name not the value (which should be null)
  -                // take the first value and ignore any others
  -                lengthValue = lengthElements[0].getName();
  +        } else {
  +            int expectedLength = getResponseContentLength();
  +            if (expectedLength == -1) {
  +                if (canResponseHaveBody(statusLine.getStatusCode()) && 
  +                !getName().equalsIgnoreCase("connect")) {
  +                    result = is;            
  +                }
               } else {
  -                lengthValue = lengthHeader.getValue();
  -            }
  -
  -            try {
  -                int expectedLength = Integer.parseInt(lengthValue);
  -                // FIXME: what if the content length is 0, perhaps we should
  -                // just return an empty stream in that case
                   result = new ContentLengthInputStream(is, expectedLength);
  -            } catch (NumberFormatException e) {
  -                throw new HttpException(
  -                    "Unable to parse server response content length: '"
  -                    + lengthValue + "'"
  -                );
  -
               }
  -
  -        } else if (canResponseHaveBody(statusLine.getStatusCode())
  -                && !getName().equals(ConnectMethod.NAME)) {
  -            result = is;
  -        }
  -
  +        } 
           // if there is a result - ALWAYS wrap it in an observer which will
           // close the underlying stream as soon as it is consumed, and notify
           // the watcher that the stream has been consumed.
  
  
  

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


Mime
View raw message