Skip to content

Commit 1ce37cb

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 ff52680 commit 1ce37cb

4 files changed

Lines changed: 34 additions & 3 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,11 @@ public boolean isResponseCacheable(final RequestCacheControl requestCacheControl
123123
}
124124

125125
if (request.getPath().contains("?")) {
126-
if (neverCache1_0ResponsesWithQueryString && from1_0Origin(response)) {
126+
if (neverCache1_0ResponsesWithQueryString && from1_0Origin(response) || neverCache1_1ResponsesWithQueryString) {
127127
LOG.debug("Response is not cacheable as it had a query string");
128128
return false;
129-
} else if (!neverCache1_1ResponsesWithQueryString && !isExplicitlyCacheable(cacheControl, response)) {
129+
}
130+
if (!isExplicitlyCacheable(cacheControl, response)) {
130131
LOG.debug("Response is not cacheable as it is missing explicit caching headers");
131132
return false;
132133
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,9 +861,9 @@ void testIsResponseCacheable() {
861861
request = new BasicHttpRequest("GET","/foo?s=bar");
862862
// HTTPbis working group says ok if explicitly indicated by
863863
// response headers
864-
policy = new ResponseCachingPolicy(true, false, true);
865864
response.setCode(HttpStatus.SC_OK);
866865
response.setHeader("Date", DateUtils.formatStandardDate(now));
866+
responseCacheControl = ResponseCacheControl.builder().setMaxAge(3600).build();
867867
assertTrue(policy.isResponseCacheable(requestCacheControl, responseCacheControl, request, response));
868868
}
869869

0 commit comments

Comments
 (0)