hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1133907 - in /httpcomponents/httpclient/trunk: ./ httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/
Date Thu, 09 Jun 2011 15:01:42 GMT
Author: jonm
Date: Thu Jun  9 15:01:42 2011
New Revision: 1133907

URL: http://svn.apache.org/viewvc?rev=1133907&view=rev
Log:
HTTPCLIENT-1100: Missing Content-Length header makes cached entry invalid.
Applying patch provided by Bart Robeyns <bart dot robeyns at gmail dot com>

Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheValidityPolicy.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheValidityPolicy.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1133907&r1=1133906&r2=1133907&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Thu Jun  9 15:01:42 2011
@@ -1,5 +1,8 @@
 Changes since 4.1.1
 
+* [HTTPCLIENT-1100] Missing Content-Length header makes cached entry invalid
+  Contributed by Bart Robeyns <bart dot robeyns at gmail dot com>
+
 * [HTTPCLIENT-1098] Avoid expensive reverse DNS lookup on connect timeout exception. 
   Contributed by Thomas Boettcher <tboett at gmail.com>
 

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheValidityPolicy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheValidityPolicy.java?rev=1133907&r1=1133906&r2=1133907&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheValidityPolicy.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheValidityPolicy.java
Thu Jun  9 15:01:42 2011
@@ -204,6 +204,10 @@ class CacheValidityPolicy {
         }
     }
 
+    protected boolean hasContentLengthHeader(HttpCacheEntry entry) {
+        return null != entry.getFirstHeader(HTTP.CONTENT_LEN);
+	}
+    
     /**
      * This matters for deciding whether the cache entry is valid to serve as a
      * response. If these values do not match, we might have a partial response
@@ -211,7 +215,7 @@ class CacheValidityPolicy {
      * @return boolean indicating whether actual length matches Content-Length
      */
     protected boolean contentLengthHeaderMatchesActualLength(final HttpCacheEntry entry)
{
-        return getContentLengthValue(entry) == entry.getResource().length();
+        return !hasContentLengthHeader(entry) || getContentLengthValue(entry) == entry.getResource().length();
     }
 
     protected long getApparentAgeSecs(final HttpCacheEntry entry) {

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheValidityPolicy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheValidityPolicy.java?rev=1133907&r1=1133906&r2=1133907&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheValidityPolicy.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheValidityPolicy.java
Thu Jun  9 15:01:42 2011
@@ -37,6 +37,7 @@ import org.apache.http.client.cache.Http
 import org.apache.http.impl.cookie.DateUtils;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.protocol.HTTP;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -330,6 +331,30 @@ public class TestCacheValidityPolicy {
         HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
         assertFalse(impl.isRevalidatable(entry));
     }
+    
+    @Test
+    public void testMissingContentLengthDoesntInvalidateEntry() {
+        final int contentLength = 128;
+        Header[] headers = {}; // no Content-Length header
+        HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers, HttpTestUtils.getRandomBytes(contentLength));
+        assertTrue(impl.contentLengthHeaderMatchesActualLength(entry));
+    }
+    
+    @Test
+    public void testCorrectContentLengthDoesntInvalidateEntry() {
+        final int contentLength = 128;
+        Header[] headers = { new BasicHeader(HTTP.CONTENT_LEN, Integer.toString(contentLength))
};
+        HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers, HttpTestUtils.getRandomBytes(contentLength));
+        assertTrue(impl.contentLengthHeaderMatchesActualLength(entry));
+    }
+    
+    @Test
+    public void testWrongContentLengthInvalidatesEntry() {
+        final int contentLength = 128;
+        Header[] headers = {new BasicHeader(HTTP.CONTENT_LEN, Integer.toString(contentLength+1))};
+        HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers, HttpTestUtils.getRandomBytes(contentLength));
+        assertFalse(impl.contentLengthHeaderMatchesActualLength(entry));
+    }
 
     @Test
     public void testMalformedDateHeaderIsIgnored() {



Mime
View raw message