You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/03/17 15:20:02 UTC

svn commit: r1301941 - in /httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity: LaxContentLengthStrategy.java StrictContentLengthStrategy.java

Author: olegk
Date: Sat Mar 17 14:20:02 2012
New Revision: 1301941

URL: http://svn.apache.org/viewvc?rev=1301941&view=rev
Log:
HTTPCORE-296: content length strategy implementations to support configurable implicit content length (used when the content length is not explicitly specified in the message)

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java?rev=1301941&r1=1301940&r2=1301941&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/LaxContentLengthStrategy.java Sat Mar 17 14:20:02 2012
@@ -57,8 +57,27 @@ import org.apache.http.protocol.HTTP;
 @Immutable
 public class LaxContentLengthStrategy implements ContentLengthStrategy {
 
-    public LaxContentLengthStrategy() {
+    private final int implicitLen;
+    
+    /**
+     * Creates <tt>LaxContentLengthStrategy</tt> instance with the given length used per default
+     * when content length is not explicitly specified in the message.
+     * 
+     * @param implicit implicit content length.
+     * 
+     * @since 4.2
+     */
+    public LaxContentLengthStrategy(int implicitLen) {
         super();
+        this.implicitLen = implicitLen;
+    }
+
+    /**
+     * Creates <tt>LaxContentLengthStrategy</tt> instance. {@link ContentLengthStrategy#IDENTITY} 
+     * is used per default when content length is not explicitly specified in the message.
+     */
+    public LaxContentLengthStrategy() {
+        this(IDENTITY);
     }
 
     public long determineLength(final HttpMessage message) throws HttpException {
@@ -70,7 +89,6 @@ public class LaxContentLengthStrategy im
         boolean strict = params.isParameterTrue(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
 
         Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
-        Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
         // We use Transfer-Encoding if present and ignore Content-Length.
         // RFC2616, 4.4 item number 3
         if (transferEncodingHeader != null) {
@@ -106,7 +124,9 @@ public class LaxContentLengthStrategy im
                 }
                 return IDENTITY;
             }
-        } else if (contentLengthHeader != null) {
+        }
+        Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
+        if (contentLengthHeader != null) {
             long contentlen = -1;
             Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
             if (strict && headers.length > 1) {
@@ -129,9 +149,8 @@ public class LaxContentLengthStrategy im
             } else {
                 return IDENTITY;
             }
-        } else {
-            return IDENTITY;
         }
+        return this.implicitLen;
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java?rev=1301941&r1=1301940&r2=1301941&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/entity/StrictContentLengthStrategy.java Sat Mar 17 14:20:02 2012
@@ -49,8 +49,27 @@ import org.apache.http.protocol.HTTP;
 @Immutable
 public class StrictContentLengthStrategy implements ContentLengthStrategy {
 
-    public StrictContentLengthStrategy() {
+    private final int implicitLen;
+    
+    /**
+     * Creates <tt>StrictContentLengthStrategy</tt> instance with the given length used per default
+     * when content length is not explicitly specified in the message.
+     * 
+     * @param implicit implicit content length.
+     * 
+     * @since 4.2
+     */
+    public StrictContentLengthStrategy(int implicitLen) {
         super();
+        this.implicitLen = implicitLen;
+    }
+
+    /**
+     * Creates <tt>StrictContentLengthStrategy</tt> instance. {@link ContentLengthStrategy#IDENTITY} 
+     * is used per default when content length is not explicitly specified in the message.
+     */
+    public StrictContentLengthStrategy() {
+        this(IDENTITY);
     }
 
     public long determineLength(final HttpMessage message) throws HttpException {
@@ -61,7 +80,6 @@ public class StrictContentLengthStrategy
         // it is either missing or has the single value "chunked". So we
         // treat it as a single-valued header here.
         Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
-        Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
         if (transferEncodingHeader != null) {
             String s = transferEncodingHeader.getValue();
             if (HTTP.CHUNK_CODING.equalsIgnoreCase(s)) {
@@ -77,7 +95,9 @@ public class StrictContentLengthStrategy
                 throw new ProtocolException(
                         "Unsupported transfer encoding: " + s);
             }
-        } else if (contentLengthHeader != null) {
+        }
+        Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
+        if (contentLengthHeader != null) {
             String s = contentLengthHeader.getValue();
             try {
                 long len = Long.parseLong(s);
@@ -88,9 +108,8 @@ public class StrictContentLengthStrategy
             } catch (NumberFormatException e) {
                 throw new ProtocolException("Invalid content length: " + s);
             }
-        } else {
-            return IDENTITY;
         }
+        return this.implicitLen;
     }
 
 }