Skip to content

Commit b3676f3

Browse files
committed
HTTP cache protocol interceptors to route requests that cannot be correctly represented as SimpleHttpRequest directly to the request execution pipeline bypassing the caching layer
1 parent 3a46ebc commit b3676f3

2 files changed

Lines changed: 10 additions & 19 deletions

File tree

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
import org.apache.hc.core5.http.HttpRequest;
7373
import org.apache.hc.core5.http.HttpResponse;
7474
import org.apache.hc.core5.http.HttpStatus;
75-
import org.apache.hc.core5.http.ProtocolException;
7675
import org.apache.hc.core5.http.impl.BasicEntityDetails;
7776
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
7877
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
@@ -261,11 +260,10 @@ public void doExecute(
261260
return;
262261
}
263262

264-
// Do not attempt to cache requests with an enclosed content body
265-
// To be revised when implementing QUERY support
266-
if (entityProducer != null) {
263+
final SimpleHttpRequest cacheRequest = prepareRequest(request, entityProducer);
264+
if (cacheRequest == null) {
267265
if (LOG.isDebugEnabled()) {
268-
LOG.debug("{} entity enclosing request cannot be served from cache", exchangeId);
266+
LOG.debug("{} request cannot be correctly executed and cached", exchangeId);
269267
}
270268
chain.proceed(request, entityProducer, scope, asyncExecCallback);
271269
return;
@@ -284,8 +282,6 @@ public void doExecute(
284282
LOG.debug("{} request cache control: {}", exchangeId, requestCacheControl);
285283
}
286284

287-
final SimpleHttpRequest cacheRequest = prepareRequest(request, entityProducer);
288-
289285
if (!cacheableRequestPolicy.canBeServedFromCache(requestCacheControl, cacheRequest)) {
290286
if (LOG.isDebugEnabled()) {
291287
LOG.debug("{} request cannot be served from cache", exchangeId);
@@ -416,10 +412,10 @@ public void cancelled() {
416412
}
417413

418414
SimpleHttpRequest prepareRequest(final HttpRequest request,
419-
final AsyncEntityProducer entityProducer) throws ProtocolException {
415+
final AsyncEntityProducer entityProducer) {
420416
// To be revised when implementing QUERY support
421417
if (entityProducer != null) {
422-
throw new ProtocolException("Caching of entity enclosing requests is not supported");
418+
return null;
423419
}
424420
return SimpleRequestBuilder.copy(request).build();
425421
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import org.apache.hc.core5.http.HttpHost;
6262
import org.apache.hc.core5.http.HttpStatus;
6363
import org.apache.hc.core5.http.HttpVersion;
64-
import org.apache.hc.core5.http.ProtocolException;
6564
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
6665
import org.apache.hc.core5.http.io.entity.EntityUtils;
6766
import org.apache.hc.core5.http.io.entity.StringEntity;
@@ -158,12 +157,10 @@ ClassicHttpResponse doExecute(
158157
return new BasicClassicHttpResponse(HttpStatus.SC_NOT_IMPLEMENTED);
159158
}
160159

161-
// Do not attempt to cache requests with an enclosed content body
162-
// To be revised when implementing QUERY support
163-
final HttpEntity requestEntity = request.getEntity();
164-
if (requestEntity != null) {
160+
final SimpleHttpRequest cacheRequest = prepareRequest(request);
161+
if (cacheRequest == null) {
165162
if (LOG.isDebugEnabled()) {
166-
LOG.debug("{} entity enclosing request cannot be served from cache", exchangeId);
163+
LOG.debug("{} request cannot be correctly executed and cached", exchangeId);
167164
}
168165
return chain.proceed(request, scope);
169166
}
@@ -176,8 +173,6 @@ ClassicHttpResponse doExecute(
176173
CacheControlHeaderGenerator.INSTANCE.generate(requestCacheControl, request);
177174
}
178175

179-
final SimpleHttpRequest cacheRequest = prepareRequest(request);
180-
181176
if (LOG.isDebugEnabled()) {
182177
LOG.debug("Request cache control: {}", requestCacheControl);
183178
}
@@ -226,10 +221,10 @@ private static ClassicHttpResponse convert(final SimpleHttpResponse cacheRespons
226221
return response;
227222
}
228223

229-
SimpleHttpRequest prepareRequest(final ClassicHttpRequest request) throws ProtocolException {
224+
SimpleHttpRequest prepareRequest(final ClassicHttpRequest request) {
230225
// To be revised when implementing QUERY support
231226
if (request.getEntity() != null) {
232-
throw new ProtocolException("Caching of entity enclosing requests is not supported");
227+
return null;
233228
}
234229
return SimpleRequestBuilder.copy(request).build();
235230
}

0 commit comments

Comments
 (0)