HTTPCLIENT-1100: Missing Content-Length header makes cached entry invalid.

Applying patch provided by Bart Robeyns <bart dot robeyns at gmail dot com>


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1133907 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Moore 2011-06-09 15:01:42 +00:00
parent 8dbbed80cc
commit 2d017dcc0e
3 changed files with 33 additions and 1 deletions

View File

@ -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>

View File

@ -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) {

View File

@ -37,6 +37,7 @@ import org.apache.http.client.cache.HttpCacheEntry;
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() {