Skip to content

Commit ecd4679

Browse files
arturobernalgok2c
authored andcommitted
Deprecate the never-cache-responses-with-query options and make them opt-in
Assigning heuristic freshness to a response with a query component is no longer prohibited by the HTTP caching specification. ResponseCachingPolicy now consults the never-cache-query options only when they are explicitly enabled, so in the default configuration a response with a query component is cached like any other. isNeverCacheHTTP10ResponsesWithQuery, isNeverCacheHTTP11ResponsesWithQuery and their builder setters are deprecated, as is HTTP/1.0 response caching. An origin that does not want a response cached should send an explicit directive such as Cache-Control: no-cache.
1 parent cb0062c commit ecd4679

4 files changed

Lines changed: 52 additions & 20 deletions

File tree

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheConfig.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
* {@link CacheConfig#isNeverCacheHTTP11ResponsesWithQuery()},
4242
* {@link CacheConfig#isStaleIfErrorEnabled()}</p>
4343
*
44+
* <p><b>Query string and HTTP/1.0 caching.</b> The options that suppress caching of responses with a query
45+
* component are deprecated, since assigning heuristic freshness to such a response is no longer prohibited; an
46+
* origin that does not want a response cached should send an explicit directive such as
47+
* {@code Cache-Control: no-cache}. HTTP/1.0 response caching is deprecated as a whole and will be removed in a
48+
* future release.</p>
49+
*
4450
* <p><b>Cache size.</b> If the backend storage supports these limits, one
4551
* can specify the {@link CacheConfig#getMaxCacheEntries maximum number of
4652
* cache entries} as well as the {@link CacheConfig#getMaxObjectSize()}
@@ -189,7 +195,13 @@ public long getMaxObjectSize() {
189195
* Returns whether the cache will never cache HTTP 1.0 responses with a query string or not.
190196
* @return {@code true} to not cache query string responses, {@code false} to cache if explicit cache headers are
191197
* found
198+
*
199+
* @deprecated Assigning heuristic freshness to a response with a query component is no longer prohibited, so this
200+
* option no longer serves a purpose. An origin that does not want such a response cached should send an explicit
201+
* directive such as {@code Cache-Control: no-cache}. HTTP/1.0 response caching is deprecated and will be removed
202+
* in a future release.
192203
*/
204+
@Deprecated
193205
public boolean isNeverCacheHTTP10ResponsesWithQuery() {
194206
return neverCacheHTTP10ResponsesWithQuery;
195207
}
@@ -205,7 +217,12 @@ public boolean isNeverCacheHTTP10ResponsesWithQuery() {
205217
* @return {@code true} if HTTP/1.1 responses with query strings should never be cached;
206218
* {@code false} otherwise.
207219
* @since 5.4
220+
*
221+
* @deprecated Assigning heuristic freshness to a response with a query component is no longer prohibited, so this
222+
* option no longer serves a purpose. An origin that does not want such a response cached should send an explicit
223+
* directive such as {@code Cache-Control: no-cache}.
208224
*/
225+
@Deprecated
209226
public boolean isNeverCacheHTTP11ResponsesWithQuery() {
210227
return neverCacheHTTP11ResponsesWithQuery;
211228
}
@@ -499,7 +516,13 @@ public Builder setAsynchronousWorkers(final int asynchronousWorkers) {
499516
* to better emulate IE, which also never caches responses, regardless of what caching
500517
* headers may be present.
501518
* @return this instance.
519+
*
520+
* @deprecated Assigning heuristic freshness to a response with a query component is no longer prohibited, so
521+
* this option no longer serves a purpose. An origin that does not want such a response cached should send an
522+
* explicit directive such as {@code Cache-Control: no-cache}. HTTP/1.0 response caching is deprecated and will
523+
* be removed in a future release.
502524
*/
525+
@Deprecated
503526
public Builder setNeverCacheHTTP10ResponsesWithQueryString(
504527
final boolean neverCacheHTTP10ResponsesWithQuery) {
505528
this.neverCacheHTTP10ResponsesWithQuery = neverCacheHTTP10ResponsesWithQuery;
@@ -535,7 +558,12 @@ public Builder setFreshnessCheckEnabled(final boolean freshnessCheckEnabled) {
535558
*
536559
* @param neverCacheHTTP11ResponsesWithQuery whether to never cache HTTP/1.1 responses with a query string
537560
* @return this instance.
561+
*
562+
* @deprecated Assigning heuristic freshness to a response with a query component is no longer prohibited, so
563+
* this option no longer serves a purpose. An origin that does not want such a response cached should send an
564+
* explicit directive such as {@code Cache-Control: no-cache}.
538565
*/
566+
@Deprecated
539567
public Builder setNeverCacheHTTP11ResponsesWithQueryString(
540568
final boolean neverCacheHTTP11ResponsesWithQuery) {
541569
this.neverCacheHTTP11ResponsesWithQuery = neverCacheHTTP11ResponsesWithQuery;

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public class CachingExecBase {
7575
this.cacheConfig = config != null ? config : CacheConfig.DEFAULT;
7676
}
7777

78+
// The query-string caching options are deprecated but still honoured while they remain on the API.
79+
@SuppressWarnings("deprecation")
7880
CachingExecBase(final CacheConfig config) {
7981
super();
8082
this.cacheConfig = config != null ? config : CacheConfig.DEFAULT;

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ResponseCachingPolicy.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,17 @@ public boolean isResponseCacheable(final RequestCacheControl requestCacheControl
122122
return false;
123123
}
124124

125-
if (request.getPath().contains("?")) {
126-
if (neverCache1_0ResponsesWithQueryString && from1_0Origin(response)) {
127-
LOG.debug("Response is not cacheable as it had a query string");
128-
return false;
129-
} else if (!neverCache1_1ResponsesWithQueryString && !isExplicitlyCacheable(cacheControl, response)) {
130-
LOG.debug("Response is not cacheable as it is missing explicit caching headers");
131-
return false;
132-
}
125+
if (neverCache1_0ResponsesWithQueryString
126+
&& request.getPath().contains("?")
127+
&& from1_0Origin(response)) {
128+
LOG.debug("Response is not cacheable as it had a query string");
129+
return false;
130+
}
131+
if (neverCache1_1ResponsesWithQueryString
132+
&& request.getPath().contains("?")
133+
&& !isExplicitlyCacheable(cacheControl, response)) {
134+
LOG.debug("Response is not cacheable as it is missing explicit caching headers");
135+
return false;
133136
}
134137

135138
if (cacheControl.isMustUnderstand()) {

httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCachingPolicy.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -521,27 +521,21 @@ void testResponsesThatAreSmallEnoughAreCacheable() {
521521
}
522522

523523
@Test
524-
void testResponsesToGETWithQueryParamsButNoExplicitCachingAreNotCacheable() {
524+
void testResponsesToGETWithQueryParamsButNoExplicitCachingAreCacheable() {
525525
request = new BasicHttpRequest("GET", "/foo?s=bar");
526-
Assertions.assertFalse(policy.isResponseCacheable(requestCacheControl, responseCacheControl, request, response));
527-
}
528-
529-
@Test
530-
void testResponsesToHEADWithQueryParamsButNoExplicitCachingAreNotCacheable() {
531-
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
532-
Assertions.assertFalse(policy.isResponseCacheable(requestCacheControl, responseCacheControl, request, response));
526+
Assertions.assertTrue(policy.isResponseCacheable(requestCacheControl, responseCacheControl, request, response));
533527
}
534528

535529
@Test
536-
void testResponsesToGETWithQueryParamsButNoExplicitCachingAreNotCacheableEvenWhen1_0QueryCachingDisabled() {
537-
policy = new ResponseCachingPolicy(true, true, false);
530+
void testResponsesToGETWithQueryParamsAreNotCacheableWhenHTTP11QueryCachingDisabled() {
531+
policy = new ResponseCachingPolicy(true, false, true);
538532
request = new BasicHttpRequest("GET", "/foo?s=bar");
539533
Assertions.assertFalse(policy.isResponseCacheable(requestCacheControl, responseCacheControl, request, response));
540534
}
541535

542536
@Test
543-
void testResponsesToHEADWithQueryParamsButNoExplicitCachingAreNotCacheableEvenWhen1_0QueryCachingDisabled() {
544-
policy = new ResponseCachingPolicy(true, true, false);
537+
void testResponsesToHEADWithQueryParamsButNoExplicitCachingAreNotCacheable() {
538+
policy = new ResponseCachingPolicy(true, false, true);
545539
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
546540
Assertions.assertFalse(policy.isResponseCacheable(requestCacheControl, responseCacheControl, request, response));
547541
}
@@ -583,6 +577,7 @@ void testResponsesToHEADWithQueryParamsAndExplicitCachingAreCacheableEvenWhen1_0
583577

584578
@Test
585579
void getsWithQueryParametersDirectlyFrom1_0OriginsAreNotCacheable() {
580+
policy = new ResponseCachingPolicy(true, true, false);
586581
request = new BasicHttpRequest("GET", "/foo?s=bar");
587582
response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
588583
response.setVersion(HttpVersion.HTTP_1_0);
@@ -591,6 +586,7 @@ void getsWithQueryParametersDirectlyFrom1_0OriginsAreNotCacheable() {
591586

592587
@Test
593588
void headsWithQueryParametersDirectlyFrom1_0OriginsAreNotCacheable() {
589+
policy = new ResponseCachingPolicy(true, true, false);
594590
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
595591
response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
596592
response.setVersion(HttpVersion.HTTP_1_0);
@@ -660,13 +656,15 @@ void headsWithQueryParametersDirectlyFrom1_0OriginsCanBeNotCacheableEvenWithExpi
660656

661657
@Test
662658
void getsWithQueryParametersFrom1_0OriginsViaProxiesAreNotCacheable() {
659+
policy = new ResponseCachingPolicy(true, true, false);
663660
request = new BasicHttpRequest("GET", "/foo?s=bar");
664661
response.setHeader(HttpHeaders.VIA, "1.0 someproxy");
665662
Assertions.assertFalse(policy.isResponseCacheable(requestCacheControl, responseCacheControl, request, response));
666663
}
667664

668665
@Test
669666
void headsWithQueryParametersFrom1_0OriginsViaProxiesAreNotCacheable() {
667+
policy = new ResponseCachingPolicy(true, true, false);
670668
request = new BasicHttpRequest("HEAD", "/foo?s=bar");
671669
response.setHeader(HttpHeaders.VIA, "1.0 someproxy");
672670
Assertions.assertFalse(policy.isResponseCacheable(requestCacheControl, responseCacheControl, request, response));
@@ -864,6 +862,7 @@ void testIsResponseCacheable() {
864862
policy = new ResponseCachingPolicy(true, false, true);
865863
response.setCode(HttpStatus.SC_OK);
866864
response.setHeader("Date", DateUtils.formatStandardDate(now));
865+
responseCacheControl = ResponseCacheControl.builder().setMaxAge(3600).build();
867866
assertTrue(policy.isResponseCacheable(requestCacheControl, responseCacheControl, request, response));
868867
}
869868

0 commit comments

Comments
 (0)