You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2026/07/15 10:35:41 UTC
(httpcomponents-client) branch caching_redesign created (now dd15e390d)
This is an automated email from the ASF dual-hosted git repository.
ok2c pushed a change to branch caching_redesign
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git
at dd15e390d HTTP cache protocol interceptors to use SimpleHttpRequest internally for cache operations;tthe use of self-contained request object should simplify handling of requests with an enclosed entity such as QUERY
This branch includes the following new commits:
new 80f9d3f67 Moved cache request URI extraction and normalization methods to CacheSupport
new c3fc85663 Replaced deprecated method with an equivalent one from PathSegmentCodec
new dd15e390d HTTP cache protocol interceptors to use SimpleHttpRequest internally for cache operations;tthe use of self-contained request object should simplify handling of requests with an enclosed entity such as QUERY
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(httpcomponents-client) 01/03: Moved cache request URI extraction and normalization methods to CacheSupport
Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ok2c pushed a commit to branch caching_redesign
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git
commit 80f9d3f67aae97c4aaf2dc7d71fb3605de91245b
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sun Jul 12 13:34:59 2026 +0200
Moved cache request URI extraction and normalization methods to CacheSupport
---
.../client5/http/cache/HttpCacheEntryFactory.java | 13 +-
.../http/impl/cache/BasicHttpAsyncCache.java | 2 +-
.../hc/client5/http/impl/cache/BasicHttpCache.java | 2 +-
.../client5/http/impl/cache/CacheKeyGenerator.java | 76 ++----------
.../hc/client5/http/impl/cache/CacheSupport.java | 132 ++++++++++++++++++++-
.../cache/CachedResponseSuitabilityChecker.java | 2 +-
.../client5/http/impl/cache/HttpCacheSupport.java | 8 +-
.../http/impl/cache/TestCacheKeyGenerator.java | 50 --------
.../client5/http/impl/cache/TestCacheSupport.java | 63 ++++++++++
9 files changed, 216 insertions(+), 132 deletions(-)
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpCacheEntryFactory.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpCacheEntryFactory.java
index 3f14e4205..9b86f0bcc 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpCacheEntryFactory.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpCacheEntryFactory.java
@@ -26,7 +26,6 @@
*/
package org.apache.hc.client5.http.cache;
-import java.net.URI;
import java.time.Instant;
import java.util.Collection;
import java.util.HashSet;
@@ -34,7 +33,7 @@
import java.util.Locale;
import java.util.Set;
-import org.apache.hc.client5.http.impl.cache.CacheKeyGenerator;
+import org.apache.hc.client5.http.impl.cache.CacheSupport;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
@@ -158,8 +157,7 @@ public HttpCacheEntry create(final Instant requestInstant,
Args.notNull(host, "Host");
Args.notNull(request, "Request");
Args.notNull(response, "Origin response");
- final String s = CacheKeyGenerator.getRequestUri(host, request);
- final URI uri = CacheKeyGenerator.normalize(s);
+ final String uri = CacheSupport.requestUriNormalized(host, request);
final HeaderGroup requestHeaders = filterHopByHopHeaders(request);
// Strip AUTHORIZATION from request headers
requestHeaders.removeHeaders(HttpHeaders.AUTHORIZATION);
@@ -169,7 +167,7 @@ public HttpCacheEntry create(final Instant requestInstant,
requestInstant,
responseInstant,
request.getMethod(),
- uri.toASCIIString(),
+ uri,
requestHeaders,
response.getCode(),
responseHeaders,
@@ -204,8 +202,7 @@ public HttpCacheEntry createUpdated(
if (HttpCacheEntry.isNewer(entry, response)) {
return entry;
}
- final String s = CacheKeyGenerator.getRequestUri(host, request);
- final URI uri = CacheKeyGenerator.normalize(s);
+ final String uri = CacheSupport.requestUriNormalized(host, request);
final HeaderGroup requestHeaders = filterHopByHopHeaders(request);
// Strip AUTHORIZATION from request headers
requestHeaders.removeHeaders(HttpHeaders.AUTHORIZATION);
@@ -214,7 +211,7 @@ public HttpCacheEntry createUpdated(
requestInstant,
responseInstant,
request.getMethod(),
- uri.toASCIIString(),
+ uri,
requestHeaders,
entry.getStatus(),
mergedHeaders,
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java
index 4509a1a2c..b5b50f16e 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java
@@ -553,7 +553,7 @@ public Cancellable evictInvalidatedEntries(
!Method.isSafe(request.getMethod())) {
final String rootKey = cacheKeyGenerator.generateKey(host, request);
evict(rootKey);
- final URI requestUri = CacheKeyGenerator.normalize(CacheKeyGenerator.getRequestUri(host, request));
+ final URI requestUri = CacheSupport.requestUriNormalizedOrNull(host, request);
if (requestUri != null) {
final URI contentLocation = CacheSupport.getLocationURI(requestUri, response, HttpHeaders.CONTENT_LOCATION);
if (contentLocation != null && CacheSupport.isSameOrigin(requestUri, contentLocation)) {
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
index 59a509d56..8a5da4c92 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
@@ -349,7 +349,7 @@ public void evictInvalidatedEntries(final HttpHost host, final HttpRequest reque
!Method.isSafe(request.getMethod())) {
final String rootKey = cacheKeyGenerator.generateKey(host, request);
evict(rootKey);
- final URI requestUri = CacheKeyGenerator.normalize(CacheKeyGenerator.getRequestUri(host, request));
+ final URI requestUri = CacheSupport.requestUriNormalizedOrNull(host, request);
if (requestUri != null) {
final URI contentLocation = CacheSupport.getLocationURI(requestUri, response, HttpHeaders.CONTENT_LOCATION);
if (contentLocation != null && CacheSupport.isSameOrigin(requestUri, contentLocation)) {
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java
index e15b06ca8..51dcf4896 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java
@@ -52,14 +52,11 @@
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.MessageHeaders;
import org.apache.hc.core5.http.NameValuePair;
-import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.message.BasicHeaderElementIterator;
import org.apache.hc.core5.http.message.BasicHeaderValueFormatter;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.http.message.MessageSupport;
import org.apache.hc.core5.net.PercentCodec;
-import org.apache.hc.core5.net.URIAuthority;
-import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.CharArrayBuffer;
import org.apache.hc.core5.util.TextUtils;
@@ -81,85 +78,34 @@ public String resolve(final URI uri) {
}
/**
- * Returns text representation of the request URI of the given {@link HttpRequest}.
- * This method will use {@link HttpRequest#getPath()}, {@link HttpRequest#getScheme()} and
- * {@link HttpRequest#getAuthority()} values when available or attributes of target
- * {@link HttpHost } in order to construct an absolute URI.
- * <p>
- * This method will not attempt to ensure validity of the resultant text representation.
- *
- * @param target target host
- * @param request the {@link HttpRequest}
- *
- * @return String the request URI
+ * @deprecated Use {@link CacheSupport#requestUriRaw(HttpHost, HttpRequest)}
*/
+ @Deprecated
@Internal
public static String getRequestUri(final HttpHost target, final HttpRequest request) {
- Args.notNull(target, "Target");
- Args.notNull(request, "HTTP request");
- final StringBuilder buf = new StringBuilder();
- final URIAuthority authority = request.getAuthority();
- if (authority != null) {
- final String scheme = request.getScheme();
- buf.append(scheme != null ? scheme : URIScheme.HTTP.id).append("://");
- buf.append(authority.getHostName());
- if (authority.getPort() >= 0) {
- buf.append(":").append(authority.getPort());
- }
- } else {
- buf.append(target.getSchemeName()).append("://");
- buf.append(target.getHostName());
- if (target.getPort() >= 0) {
- buf.append(":").append(target.getPort());
- }
- }
- final String path = request.getPath();
- if (path == null) {
- buf.append("/");
- } else {
- if (buf.length() > 0 && !path.startsWith("/")) {
- buf.append("/");
- }
- buf.append(path);
- }
- return buf.toString();
+ return CacheSupport.requestUriRaw(target, request);
}
/**
- * Returns normalized representation of the request URI optimized for use as a cache key.
- * This method ensures the resultant URI has an explicit port in the authority component,
- * and explicit path component and no fragment.
+ * @deprecated Use {@link CacheSupport#normalize(URI)}
*/
+ @Deprecated
@Internal
public static URI normalize(final URI requestUri) throws URISyntaxException {
- Args.notNull(requestUri, "URI");
- final URIBuilder builder = new URIBuilder(requestUri);
- if (builder.getHost() != null) {
- if (builder.getScheme() == null) {
- builder.setScheme(URIScheme.HTTP.id);
- }
- if (builder.getPort() <= -1) {
- if (URIScheme.HTTP.same(builder.getScheme())) {
- builder.setPort(80);
- } else if (URIScheme.HTTPS.same(builder.getScheme())) {
- builder.setPort(443);
- }
- }
- }
- builder.setFragment(null);
- return builder.optimize().build();
+ return CacheSupport.normalize(requestUri);
}
/**
- * Lenient URI parser that normalizes valid {@link URI}s and returns {@code null} for malformed URIs.
+ * @deprecated Use {@link CacheSupport#normalize(URI)}
*/
+ @Deprecated
@Internal
public static URI normalize(final String requestUri) {
if (requestUri == null) {
return null;
}
try {
- return CacheKeyGenerator.normalize(new URI(requestUri));
+ return CacheSupport.normalize(new URI(requestUri));
} catch (final URISyntaxException ex) {
return null;
}
@@ -175,7 +121,7 @@ public static URI normalize(final String requestUri) {
*/
public String generateKey(final URI requestUri) {
try {
- final URI normalizeRequestUri = normalize(requestUri);
+ final URI normalizeRequestUri = CacheSupport.normalize(requestUri);
return normalizeRequestUri.toASCIIString();
} catch (final URISyntaxException ex) {
return requestUri.toASCIIString();
@@ -191,7 +137,7 @@ public String generateKey(final URI requestUri) {
* @return cache key
*/
public String generateKey(final HttpHost host, final HttpRequest request) {
- final String s = getRequestUri(host, request);
+ final String s = CacheSupport.requestUriRaw(host, request);
try {
return generateKey(new URI(s));
} catch (final URISyntaxException ex) {
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheSupport.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheSupport.java
index 2a839dad3..f61e88eac 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheSupport.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheSupport.java
@@ -27,12 +27,19 @@
package org.apache.hc.client5.http.impl.cache;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.Objects;
import org.apache.hc.client5.http.utils.URIUtils;
import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.MessageHeaders;
+import org.apache.hc.core5.http.URIScheme;
+import org.apache.hc.core5.net.URIAuthority;
+import org.apache.hc.core5.net.URIBuilder;
+import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.TextUtils;
import org.apache.hc.core5.util.TimeValue;
@@ -44,12 +51,135 @@
@Internal
public final class CacheSupport {
+ /**
+ * Returns raw representation of the request URI of the given {@link HttpRequest}.
+ * This method will use {@link HttpRequest#getPath()}, {@link HttpRequest#getScheme()} and
+ * {@link HttpRequest#getAuthority()} values when available or attributes of target
+ * {@link HttpHost } in order to construct an absolute URI.
+ * <p>
+ * This method will not attempt to ensure validity of the resultant text representation.
+ *
+ * @param target target host
+ * @param request the {@link HttpRequest}
+ *
+ * @return String the request URI
+ *
+ * @since 5.7
+ */
+ static String requestUriRaw(final HttpHost target, final HttpRequest request) {
+ Args.notNull(target, "Target");
+ Args.notNull(request, "HTTP request");
+ final StringBuilder buf = new StringBuilder();
+ final URIAuthority authority = request.getAuthority();
+ if (authority != null) {
+ final String scheme = request.getScheme();
+ buf.append(scheme != null ? scheme : URIScheme.HTTP.id).append("://");
+ buf.append(authority.getHostName());
+ if (authority.getPort() >= 0) {
+ buf.append(":").append(authority.getPort());
+ }
+ } else {
+ buf.append(target.getSchemeName()).append("://");
+ buf.append(target.getHostName());
+ if (target.getPort() >= 0) {
+ buf.append(":").append(target.getPort());
+ }
+ }
+ final String path = request.getPath();
+ if (path == null) {
+ buf.append("/");
+ } else {
+ if (buf.length() > 0 && !path.startsWith("/")) {
+ buf.append("/");
+ }
+ buf.append(path);
+ }
+ return buf.toString();
+ }
+
+ private static URIBuilder parse(final String uriRaw) throws URISyntaxException {
+ final URIBuilder uriBuilder = new URIBuilder(uriRaw);
+ return normalize(uriBuilder);
+ }
+
+ /**
+ * Returns the request URI of the given {@link HttpRequest} normalized and validated.
+ *
+ * @since 5.7
+ */
+ public static String requestUriNormalized(final HttpHost target, final HttpRequest request) {
+ Args.notNull(target, "Target");
+ Args.notNull(request, "HTTP request");
+ final String uriRaw = requestUriRaw(target, request);
+ try {
+ return parse(uriRaw).toString();
+ } catch (final URISyntaxException ignore) {
+ return uriRaw;
+ }
+ }
+
+ /**
+ * Returns the request URI of the given {@link HttpRequest} normalized and validated
+ * or {@code null} if the request URI is malformed / invalid.
+ *
+ * @since 5.7
+ */
+ public static URI requestUriNormalizedOrNull(final HttpHost target, final HttpRequest request) {
+ Args.notNull(target, "Target");
+ Args.notNull(request, "HTTP request");
+ final String uriRaw = requestUriRaw(target, request);
+ try {
+ return parse(uriRaw).build();
+ } catch (final URISyntaxException ex) {
+ return null;
+ }
+ }
+
+ /**
+ * Returns normalized representation of the request URI optimized for use as a cache key.
+ * This method ensures the resultant URI has an explicit port in the authority component,
+ * and explicit path component and no fragment.
+ *
+ * @since 5.7
+ */
+ public static URI normalize(final URI requestUri) throws URISyntaxException {
+ Args.notNull(requestUri, "URI");
+ return normalize(new URIBuilder(requestUri)).build();
+ }
+
+ static URIBuilder normalize(final URIBuilder builder) {
+ if (builder.getHost() != null) {
+ if (builder.getScheme() == null) {
+ builder.setScheme(URIScheme.HTTP.id);
+ }
+ if (builder.getPort() <= -1) {
+ if (URIScheme.HTTP.same(builder.getScheme())) {
+ builder.setPort(80);
+ } else if (URIScheme.HTTPS.same(builder.getScheme())) {
+ builder.setPort(443);
+ }
+ }
+ }
+ builder.setFragment(null);
+ builder.optimize();
+ return builder;
+ }
+
public static URI getLocationURI(final URI requestUri, final MessageHeaders response, final String headerName) {
final Header h = response.getFirstHeader(headerName);
if (h == null) {
return null;
}
- final URI locationUri = CacheKeyGenerator.normalize(h.getValue());
+ final String locationRaw = h.getValue();
+ if (TextUtils.isEmpty(locationRaw)) {
+ return null;
+ }
+ final URI locationUri;
+ try {
+ locationUri = normalize(new URI(locationRaw));
+ } catch (final URISyntaxException ignore) {
+ return requestUri;
+ }
if (locationUri == null) {
return requestUri;
}
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
index d930473fd..a53612730 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
@@ -199,7 +199,7 @@ boolean requestMethodMatch(final HttpRequest request, final HttpCacheEntry entry
boolean requestUriMatch(final HttpRequest request, final HttpCacheEntry entry) {
try {
- final URI requestURI = CacheKeyGenerator.normalize(request.getUri());
+ final URI requestURI = CacheSupport.normalize(request.getUri());
final URI cacheURI = new URI(entry.getRequestURI());
if (requestURI.isAbsolute()) {
return Objects.equals(requestURI, cacheURI);
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpCacheSupport.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpCacheSupport.java
index 8cc04c985..5453c674a 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpCacheSupport.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpCacheSupport.java
@@ -43,18 +43,16 @@
public final class HttpCacheSupport {
public static String getRequestUri(final HttpRequest request, final HttpHost target) {
- return CacheKeyGenerator.getRequestUri(target, request);
+ return CacheSupport.requestUriRaw(target, request);
}
public static URI normalize(final URI requestUri) throws URISyntaxException {
- return CacheKeyGenerator.normalize(requestUri);
+ return CacheSupport.normalize(requestUri);
}
/**
* Lenient URI parser that normalizes valid {@link URI}s and returns {@code null} for malformed URIs.
- * @deprecated Use {@link #normalizeQuietly(String)}
*/
- @Deprecated
public static URI normalizeQuetly(final String requestUri) {
if (requestUri == null) {
return null;
@@ -71,7 +69,7 @@ public static URI normalizeQuetly(final String requestUri) {
* @since 5.2
*/
public static URI normalizeQuietly(final String requestUri) {
- return CacheKeyGenerator.normalize(requestUri);
+ return normalizeQuetly(requestUri);
}
}
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheKeyGenerator.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheKeyGenerator.java
index 8fcb19553..b1784e83d 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheKeyGenerator.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheKeyGenerator.java
@@ -26,8 +26,6 @@
*/
package org.apache.hc.client5.http.impl.cache;
-import java.net.URI;
-import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -58,54 +56,6 @@ void setUp() {
extractor = CacheKeyGenerator.INSTANCE;
}
- @Test
- void testGetRequestUri() {
- Assertions.assertEquals("http://foo.example.com/stuff?huh",
- CacheKeyGenerator.getRequestUri(
- new HttpHost("bar.example.com"),
- new HttpGet("http://foo.example.com/stuff?huh")));
-
- Assertions.assertEquals("http://bar.example.com/stuff?huh",
- CacheKeyGenerator.getRequestUri(
- new HttpHost("bar.example.com"),
- new HttpGet("/stuff?huh")));
-
- Assertions.assertEquals("http://foo.example.com:8888/stuff?huh",
- CacheKeyGenerator.getRequestUri(
- new HttpHost("bar.example.com", 8080),
- new HttpGet("http://foo.example.com:8888/stuff?huh")));
-
- Assertions.assertEquals("https://bar.example.com:8443/stuff?huh",
- CacheKeyGenerator.getRequestUri(
- new HttpHost("https", "bar.example.com", 8443),
- new HttpGet("/stuff?huh")));
-
- Assertions.assertEquals("http://foo.example.com/",
- CacheKeyGenerator.getRequestUri(
- new HttpHost("bar.example.com"),
- new HttpGet("http://foo.example.com")));
-
- Assertions.assertEquals("http://bar.example.com/stuff?huh",
- CacheKeyGenerator.getRequestUri(
- new HttpHost("bar.example.com"),
- new HttpGet("stuff?huh")));
- }
-
- @Test
- void testNormalizeRequestUri() throws URISyntaxException {
- Assertions.assertEquals(URI.create("http://bar.example.com:80/stuff?huh"),
- CacheKeyGenerator.normalize(URI.create("//bar.example.com/stuff?huh")));
-
- Assertions.assertEquals(URI.create("http://bar.example.com:80/stuff?huh"),
- CacheKeyGenerator.normalize(URI.create("http://bar.example.com/stuff?huh")));
-
- Assertions.assertEquals(URI.create("http://bar.example.com:80/stuff?huh"),
- CacheKeyGenerator.normalize(URI.create("http://bar.example.com/stuff?huh#there")));
-
- Assertions.assertEquals(URI.create("http://bar.example.com:80/stuff?huh"),
- CacheKeyGenerator.normalize(URI.create("HTTP://BAR.example.com/p1/p2/../../stuff?huh")));
- }
-
@Test
void testExtractsUriFromAbsoluteUriInRequest() {
final HttpHost host = new HttpHost("bar.example.com");
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheSupport.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheSupport.java
index 8572bba77..ae8e9859f 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheSupport.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheSupport.java
@@ -27,6 +27,11 @@
package org.apache.hc.client5.http.impl.cache;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -35,6 +40,64 @@
*/
class TestCacheSupport {
+ @Test
+ void testRequestUriRaw() throws Exception {
+ Assertions.assertEquals("http://foo.example.com/stuff?huh",
+ CacheSupport.requestUriRaw(
+ new HttpHost("bar.example.com"),
+ BasicRequestBuilder.get("http://foo.example.com/stuff?huh").build()));
+
+ Assertions.assertEquals("http://bar.example.com/stuff?huh",
+ CacheSupport.requestUriRaw(
+ new HttpHost("bar.example.com"),
+ BasicRequestBuilder.get("/stuff?huh").build()));
+
+ Assertions.assertEquals("http://foo.example.com:8888/stuff?huh",
+ CacheSupport.requestUriRaw(
+ new HttpHost("bar.example.com", 8080),
+ BasicRequestBuilder.get("http://foo.example.com:8888/stuff?huh").build()));
+
+ Assertions.assertEquals("https://bar.example.com:8443/stuff?huh",
+ CacheSupport.requestUriRaw(
+ new HttpHost("https", "bar.example.com", 8443),
+ BasicRequestBuilder.get("/stuff?huh").build()));
+
+ Assertions.assertEquals("http://foo.example.com/",
+ CacheSupport.requestUriRaw(
+ new HttpHost("bar.example.com"),
+ BasicRequestBuilder.get("http://foo.example.com").build()));
+
+ Assertions.assertEquals("http://bar.example.com/stuff?huh",
+ CacheSupport.requestUriRaw(
+ new HttpHost("bar.example.com"),
+ BasicRequestBuilder.get("stuff?huh").build()));
+
+ Assertions.assertEquals("http://bar.example.com/stuff",
+ CacheSupport.requestUriRaw(
+ new HttpHost("bar.example.com"),
+ BasicRequestBuilder.get("stuff#huh").build()));
+
+ Assertions.assertEquals("http://bar.example.com/stuff",
+ CacheSupport.requestUriRaw(
+ new HttpHost("bar.example.com"),
+ BasicRequestBuilder.get("stuff").build()));
+ }
+
+ @Test
+ void testNormalizeRequestUri() throws URISyntaxException {
+ Assertions.assertEquals(URI.create("http://bar.example.com:80/stuff?huh"),
+ CacheSupport.normalize(URI.create("//bar.example.com/stuff?huh")));
+
+ Assertions.assertEquals(URI.create("http://bar.example.com:80/stuff?huh"),
+ CacheSupport.normalize(URI.create("http://bar.example.com/stuff?huh")));
+
+ Assertions.assertEquals(URI.create("http://bar.example.com:80/stuff?huh"),
+ CacheSupport.normalize(URI.create("http://bar.example.com/stuff?huh#there")));
+
+ Assertions.assertEquals(URI.create("http://bar.example.com:80/stuff?huh"),
+ CacheSupport.normalize(URI.create("HTTP://BAR.example.com/p1/p2/../../stuff?huh")));
+ }
+
@Test
void testParseDeltaSeconds() {
Assertions.assertEquals(1234L, CacheSupport.deltaSeconds("1234"));
(httpcomponents-client) 03/03: HTTP cache protocol interceptors to use SimpleHttpRequest internally for cache operations;tthe use of self-contained request object should simplify handling of requests with an enclosed entity such as QUERY
Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ok2c pushed a commit to branch caching_redesign
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git
commit dd15e390d7c98421d00edb428d3036b7afb6025e
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Tue Jul 14 20:31:09 2026 +0200
HTTP cache protocol interceptors to use SimpleHttpRequest internally for cache operations;tthe use of self-contained request object should simplify handling of requests with an enclosed entity such as QUERY
---
.../client5/http/impl/cache/AsyncCachingExec.java | 345 +++++++++++----------
.../http/impl/cache/BasicHttpAsyncCache.java | 12 +-
.../hc/client5/http/impl/cache/BasicHttpCache.java | 12 +-
.../hc/client5/http/impl/cache/CachingExec.java | 108 ++++---
.../http/impl/cache/ConditionalRequestBuilder.java | 27 +-
.../hc/client5/http/impl/cache/HttpAsyncCache.java | 11 +-
.../hc/client5/http/impl/cache/HttpCache.java | 11 +-
.../http/impl/cache/TestBasicHttpAsyncCache.java | 68 ++--
.../http/impl/cache/TestBasicHttpCache.java | 68 ++--
.../http/impl/cache/TestCachingExecChain.java | 20 +-
.../impl/cache/TestConditionalRequestBuilder.java | 68 ++--
.../http/impl/cache/TestProtocolRequirements.java | 13 +-
12 files changed, 415 insertions(+), 348 deletions(-)
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java
index 6f4079631..d0aa3cfd7 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java
@@ -44,7 +44,9 @@
import org.apache.hc.client5.http.async.AsyncExecChain;
import org.apache.hc.client5.http.async.AsyncExecChainHandler;
import org.apache.hc.client5.http.async.methods.SimpleBody;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
+import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.cache.CacheResponseStatus;
import org.apache.hc.client5.http.cache.HttpCacheContext;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
@@ -70,11 +72,13 @@
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.impl.BasicEntityDetails;
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.CapacityChannel;
-import org.apache.hc.core5.http.support.BasicRequestBuilder;
+import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
+import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.ByteArrayBuffer;
@@ -98,7 +102,7 @@ class AsyncCachingExec extends CachingExecBase implements AsyncExecChainHandler
private static final Logger LOG = LoggerFactory.getLogger(AsyncCachingExec.class);
private final HttpAsyncCache responseCache;
private final DefaultAsyncCacheRevalidator cacheRevalidator;
- private final ConditionalRequestBuilder<HttpRequest> conditionalRequestBuilder;
+ private final ConditionalRequestBuilder conditionalRequestBuilder;
private final boolean requestCollapsingEnabled;
private final CacheRequestCollapser collapser;
@@ -106,8 +110,7 @@ class AsyncCachingExec extends CachingExecBase implements AsyncExecChainHandler
super(config);
this.responseCache = Args.notNull(cache, "Response cache");
this.cacheRevalidator = cacheRevalidator;
- this.conditionalRequestBuilder = new ConditionalRequestBuilder<>(request ->
- BasicRequestBuilder.copy(request).build());
+ this.conditionalRequestBuilder = new ConditionalRequestBuilder();
this.requestCollapsingEnabled = config.isRequestCollapsingEnabled();
this.collapser = this.requestCollapsingEnabled ? new CacheRequestCollapser() : null;
}
@@ -258,6 +261,16 @@ public void doExecute(
return;
}
+ // Do not attempt to cache requests with an enclosed content body
+ // To be revised when implementing QUERY support
+ if (entityProducer != null) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("{} entity enclosing request cannot be served from cache", exchangeId);
+ }
+ chain.proceed(request, entityProducer, scope, asyncExecCallback);
+ return;
+ }
+
final RequestCacheControl requestCacheControl;
if (request.containsHeader(HttpHeaders.CACHE_CONTROL)) {
requestCacheControl = CacheControlHeaderParser.INSTANCE.parse(request);
@@ -271,141 +284,162 @@ public void doExecute(
LOG.debug("{} request cache control: {}", exchangeId, requestCacheControl);
}
- if (cacheableRequestPolicy.canBeServedFromCache(requestCacheControl, request)) {
- operation.setDependency(responseCache.match(target, request, new FutureCallback<CacheMatch>() {
+ final SimpleHttpRequest cacheRequest = prepareRequest(request, entityProducer);
- @Override
- public void completed(final CacheMatch result) {
- final CacheHit hit = result != null ? result.hit : null;
- final CacheHit root = result != null ? result.root : null;
- if (hit == null) {
- if (requestCollapsingEnabled && root == null && entityProducer == null && !requestCacheControl.isOnlyIfCached()) {
- final String cacheKey = CacheKeyGenerator.INSTANCE.generateKey(target, request);
- final CacheRequestCollapser.Token token = collapser.enter(cacheKey);
- if (token.isLeader()) {
- handleCacheMiss(requestCacheControl, null, target, request, null, scope, chain, new AsyncExecCallback() {
+ if (!cacheableRequestPolicy.canBeServedFromCache(requestCacheControl, cacheRequest)) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("{} request cannot be served from cache", exchangeId);
+ }
+ callChain(cacheRequest, scope, chain, asyncExecCallback);
+ return;
+ }
- @Override
- public AsyncDataConsumer handleResponse(
- final HttpResponse response,
- final EntityDetails entityDetails) throws HttpException, IOException {
- try {
- return asyncExecCallback.handleResponse(response, entityDetails);
- } catch (final HttpException | IOException ex) {
- token.complete();
- throw ex;
- }
- }
+ operation.setDependency(responseCache.match(target, cacheRequest, new FutureCallback<CacheMatch>() {
- @Override
- public void handleInformationResponse(final HttpResponse response) throws HttpException, IOException {
- try {
- asyncExecCallback.handleInformationResponse(response);
- } catch (final HttpException | IOException ex) {
- token.complete();
- throw ex;
- }
+ @Override
+ public void completed(final CacheMatch result) {
+ final CacheHit hit = result != null ? result.hit : null;
+ final CacheHit root = result != null ? result.root : null;
+ if (hit == null) {
+ if (requestCollapsingEnabled && root == null && !requestCacheControl.isOnlyIfCached()) {
+ final String cacheKey = CacheKeyGenerator.INSTANCE.generateKey(target, cacheRequest);
+ final CacheRequestCollapser.Token token = collapser.enter(cacheKey);
+ if (token.isLeader()) {
+ handleCacheMiss(requestCacheControl, null, target, cacheRequest, scope, chain, new AsyncExecCallback() {
+
+ @Override
+ public AsyncDataConsumer handleResponse(
+ final HttpResponse response,
+ final EntityDetails entityDetails) throws HttpException, IOException {
+ try {
+ return asyncExecCallback.handleResponse(response, entityDetails);
+ } catch (final HttpException | IOException ex) {
+ token.complete();
+ throw ex;
}
+ }
- @Override
- public void completed() {
- try {
- asyncExecCallback.completed();
- } finally {
- token.complete();
- }
+ @Override
+ public void handleInformationResponse(final HttpResponse response) throws HttpException, IOException {
+ try {
+ asyncExecCallback.handleInformationResponse(response);
+ } catch (final HttpException | IOException ex) {
+ token.complete();
+ throw ex;
}
+ }
- @Override
- public void failed(final Exception cause) {
- try {
- asyncExecCallback.failed(cause);
- } finally {
- token.complete();
- }
+ @Override
+ public void completed() {
+ try {
+ asyncExecCallback.completed();
+ } finally {
+ token.complete();
}
+ }
- });
- } else {
- // Stable holder owned by the follower: registered with the outer operation
- // exactly once so the cache-lookup dependency installed by the await task
- // is never overwritten from the outside.
- final ComplexCancellable follower = new ComplexCancellable();
- operation.setDependency(follower);
- collapser.await(token, follower, () -> {
- if (follower.isCancelled()) {
- return;
+ @Override
+ public void failed(final Exception cause) {
+ try {
+ asyncExecCallback.failed(cause);
+ } finally {
+ token.complete();
}
- follower.setDependency(responseCache.match(target, request, new FutureCallback<CacheMatch>() {
+ }
- @Override
- public void completed(final CacheMatch result) {
- final CacheHit hit = result != null ? result.hit : null;
- final CacheHit root = result != null ? result.root : null;
- if (hit == null) {
- handleCacheMiss(requestCacheControl, root, target, request, entityProducer, scope, chain, asyncExecCallback);
- } else {
- final ResponseCacheControl responseCacheControl = CacheControlHeaderParser.INSTANCE.parse(hit.entry);
- if (LOG.isDebugEnabled()) {
- LOG.debug("{} response cache control: {}", exchangeId, responseCacheControl);
- }
- context.setResponseCacheControl(responseCacheControl);
- handleCacheHit(requestCacheControl, responseCacheControl, hit, target, request, entityProducer, scope, chain, asyncExecCallback);
+ });
+ } else {
+ // Stable holder owned by the follower: registered with the outer operation
+ // exactly once so the cache-lookup dependency installed by the await task
+ // is never overwritten from the outside.
+ final ComplexCancellable follower = new ComplexCancellable();
+ operation.setDependency(follower);
+ collapser.await(token, follower, () -> {
+ if (follower.isCancelled()) {
+ return;
+ }
+ follower.setDependency(responseCache.match(target, cacheRequest, new FutureCallback<CacheMatch>() {
+
+ @Override
+ public void completed(final CacheMatch result) {
+ final CacheHit hit = result != null ? result.hit : null;
+ final CacheHit root = result != null ? result.root : null;
+ if (hit == null) {
+ handleCacheMiss(requestCacheControl, root, target, cacheRequest, scope, chain, asyncExecCallback);
+ } else {
+ final ResponseCacheControl responseCacheControl = CacheControlHeaderParser.INSTANCE.parse(hit.entry);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("{} response cache control: {}", exchangeId, responseCacheControl);
}
+ context.setResponseCacheControl(responseCacheControl);
+ handleCacheHit(requestCacheControl, responseCacheControl, hit, target, cacheRequest, scope, chain, asyncExecCallback);
}
+ }
- @Override
- public void failed(final Exception cause) {
- asyncExecCallback.failed(cause);
- }
+ @Override
+ public void failed(final Exception cause) {
+ asyncExecCallback.failed(cause);
+ }
- @Override
- public void cancelled() {
- asyncExecCallback.failed(new InterruptedIOException());
- }
+ @Override
+ public void cancelled() {
+ asyncExecCallback.failed(new InterruptedIOException());
+ }
- }));
- });
- }
- return;
+ }));
+ });
}
- handleCacheMiss(requestCacheControl, root, target, request, entityProducer, scope, chain, asyncExecCallback);
- } else {
- final ResponseCacheControl responseCacheControl = CacheControlHeaderParser.INSTANCE.parse(hit.entry);
- if (LOG.isDebugEnabled()) {
- LOG.debug("{} response cache control: {}", exchangeId, responseCacheControl);
- }
- context.setResponseCacheControl(responseCacheControl);
- handleCacheHit(requestCacheControl, responseCacheControl, hit, target, request, entityProducer, scope, chain, asyncExecCallback);
+ return;
}
+ handleCacheMiss(requestCacheControl, root, target, cacheRequest, scope, chain, asyncExecCallback);
+ } else {
+ final ResponseCacheControl responseCacheControl = CacheControlHeaderParser.INSTANCE.parse(hit.entry);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("{} response cache control: {}", exchangeId, responseCacheControl);
+ }
+ context.setResponseCacheControl(responseCacheControl);
+ handleCacheHit(requestCacheControl, responseCacheControl, hit, target, cacheRequest, scope, chain, asyncExecCallback);
}
+ }
- @Override
- public void failed(final Exception cause) {
- asyncExecCallback.failed(cause);
- }
+ @Override
+ public void failed(final Exception cause) {
+ asyncExecCallback.failed(cause);
+ }
- @Override
- public void cancelled() {
- asyncExecCallback.failed(new InterruptedIOException());
- }
+ @Override
+ public void cancelled() {
+ asyncExecCallback.failed(new InterruptedIOException());
+ }
- }));
+ }));
+ }
- } else {
- if (LOG.isDebugEnabled()) {
- LOG.debug("{} request cannot be served from cache", exchangeId);
- }
- callBackend(target, request, entityProducer, scope, chain, asyncExecCallback);
+ SimpleHttpRequest prepareRequest(final HttpRequest request,
+ final AsyncEntityProducer entityProducer) throws ProtocolException {
+ // To be revised when implementing QUERY support
+ if (entityProducer != null) {
+ throw new ProtocolException("Caching of entity enclosing requests is not supported");
}
+ return SimpleRequestBuilder.copy(request).build();
}
- void chainProceed(
- final HttpRequest request,
- final AsyncEntityProducer entityProducer,
+ void callChain(
+ final SimpleHttpRequest request,
final AsyncExecChain.Scope scope,
final AsyncExecChain chain,
final AsyncExecCallback asyncExecCallback) {
+ final SimpleBody body = request.getBody();
+ final AsyncEntityProducer entityProducer;
+ if (body != null) {
+ if (body.isText()) {
+ entityProducer = new StringAsyncEntityProducer(body.getBodyText(), body.getContentType());
+ } else {
+ entityProducer = new BasicAsyncEntityProducer(body.getBodyBytes(), body.getContentType());
+ }
+ } else {
+ entityProducer = null;
+ }
try {
chain.proceed(request, entityProducer, scope, asyncExecCallback);
} catch (final HttpException | IOException ex) {
@@ -415,8 +449,7 @@ void chainProceed(
void callBackend(
final HttpHost target,
- final HttpRequest request,
- final AsyncEntityProducer entityProducer,
+ final SimpleHttpRequest request,
final AsyncExecChain.Scope scope,
final AsyncExecChain chain,
final AsyncExecCallback asyncExecCallback) {
@@ -427,7 +460,7 @@ void callBackend(
}
final Instant requestDate = getCurrentDate();
final AtomicReference<AsyncExecCallback> callbackRef = new AtomicReference<>();
- chainProceed(request, entityProducer, scope, chain, new AsyncExecCallback() {
+ callChain(request, scope, chain, new AsyncExecCallback() {
@Override
public AsyncDataConsumer handleResponse(
@@ -564,7 +597,7 @@ public void releaseResources() {
class BackendResponseHandler implements AsyncExecCallback {
private final HttpHost target;
- private final HttpRequest request;
+ private final SimpleHttpRequest request;
private final Instant requestDate;
private final Instant responseDate;
private final AsyncExecChain.Scope scope;
@@ -573,7 +606,7 @@ class BackendResponseHandler implements AsyncExecCallback {
BackendResponseHandler(
final HttpHost target,
- final HttpRequest request,
+ final SimpleHttpRequest request,
final Instant requestDate,
final Instant responseDate,
final AsyncExecChain.Scope scope,
@@ -812,8 +845,7 @@ private void handleCacheHit(
final ResponseCacheControl responseCacheControl,
final CacheHit hit,
final HttpHost target,
- final HttpRequest request,
- final AsyncEntityProducer entityProducer,
+ final SimpleHttpRequest request,
final AsyncExecChain.Scope scope,
final AsyncExecChain chain,
final AsyncExecCallback asyncExecCallback) {
@@ -851,11 +883,7 @@ private void handleCacheHit(
triggerResponse(cacheResponse, scope, asyncExecCallback);
} else {
context.setCacheResponseStatus(CacheResponseStatus.FAILURE);
- try {
- chain.proceed(request, entityProducer, scope, asyncExecCallback);
- } catch (final HttpException | IOException ex2) {
- asyncExecCallback.failed(ex2);
- }
+ callChain(request, scope, chain, asyncExecCallback);
}
}
} else {
@@ -870,22 +898,17 @@ private void handleCacheHit(
if (LOG.isDebugEnabled()) {
LOG.debug("{} cache entry does not match the request; calling backend", exchangeId);
}
- callBackend(target, request, entityProducer, scope, chain, asyncExecCallback);
- } else if (entityProducer != null && !entityProducer.isRepeatable()) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("{} request is not repeatable; calling backend", exchangeId);
- }
- callBackend(target, request, entityProducer, scope, chain, asyncExecCallback);
+ callBackend(target, request, scope, chain, asyncExecCallback);
} else if (hit.entry.getStatus() == HttpStatus.SC_NOT_MODIFIED && !suitabilityChecker.isConditional(request)) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} non-modified cache entry does not match the non-conditional request; calling backend", exchangeId);
}
- callBackend(target, request, entityProducer, scope, chain, asyncExecCallback);
+ callBackend(target, request, scope, chain, asyncExecCallback);
} else if (cacheSuitability == CacheSuitability.REVALIDATION_REQUIRED) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} revalidation required; revalidating cache entry", exchangeId);
}
- revalidateCacheEntryWithoutFallback(responseCacheControl, hit, target, request, entityProducer, scope, chain, asyncExecCallback);
+ revalidateCacheEntryWithoutFallback(responseCacheControl, hit, target, request, scope, chain, asyncExecCallback);
} else if (cacheSuitability == CacheSuitability.STALE_WHILE_REVALIDATED) {
if (cacheRevalidator != null) {
if (LOG.isDebugEnabled()) {
@@ -909,7 +932,7 @@ private void handleCacheHit(
cacheRevalidator.revalidateCacheEntry(
hit.getEntryKey(),
asyncExecCallback,
- c -> revalidateCacheEntry(responseCacheControl, hit, target, request, entityProducer, fork, chain, c));
+ c -> revalidateCacheEntry(responseCacheControl, hit, target, request, fork, chain, c));
context.setCacheResponseStatus(CacheResponseStatus.CACHE_MODULE_RESPONSE);
final SimpleHttpResponse cacheResponse = responseGenerator.generateResponse(request, hit.entry);
context.setCacheEntry(hit.entry);
@@ -921,18 +944,18 @@ private void handleCacheHit(
if (LOG.isDebugEnabled()) {
LOG.debug("{} revalidating stale cache entry (asynchronous revalidation disabled)", exchangeId);
}
- revalidateCacheEntryWithFallback(requestCacheControl, responseCacheControl, hit, target, request, entityProducer, scope, chain, asyncExecCallback);
+ revalidateCacheEntryWithFallback(requestCacheControl, responseCacheControl, hit, target, request, scope, chain, asyncExecCallback);
}
} else if (cacheSuitability == CacheSuitability.STALE) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} revalidating stale cache entry", exchangeId);
}
- revalidateCacheEntryWithFallback(requestCacheControl, responseCacheControl, hit, target, request, entityProducer, scope, chain, asyncExecCallback);
+ revalidateCacheEntryWithFallback(requestCacheControl, responseCacheControl, hit, target, request, scope, chain, asyncExecCallback);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} cache entry not usable; calling backend", exchangeId);
}
- callBackend(target, request, entityProducer, scope, chain, asyncExecCallback);
+ callBackend(target, request, scope, chain, asyncExecCallback);
}
}
}
@@ -941,18 +964,17 @@ void revalidateCacheEntry(
final ResponseCacheControl responseCacheControl,
final CacheHit hit,
final HttpHost target,
- final HttpRequest request,
- final AsyncEntityProducer entityProducer,
+ final SimpleHttpRequest request,
final AsyncExecChain.Scope scope,
final AsyncExecChain chain,
final AsyncExecCallback asyncExecCallback) {
final Instant requestDate = getCurrentDate();
- final HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequest(
+ final SimpleHttpRequest conditional = conditionalRequestBuilder.makeConditional(
responseCacheControl,
- BasicRequestBuilder.copy(request).build(),
+ request,
hit.entry);
final HttpCacheContext context = HttpCacheContext.cast(scope.clientContext);
- chainProceed(conditionalRequest, entityProducer, scope, chain, new AsyncExecCallback() {
+ callChain(conditional, scope, chain, new AsyncExecCallback() {
final AtomicReference<AsyncExecCallback> callbackRef = new AtomicReference<>();
@@ -1000,7 +1022,7 @@ AsyncExecCallback evaluateResponse(final HttpResponse backendResponse, final Ins
if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
return new AsyncExecCallbackWrapper(() -> triggerUpdatedCacheEntryResponse(backendResponse, responseDate), asyncExecCallback::failed);
}
- return new BackendResponseHandler(target, conditionalRequest, requestDate, responseDate, scope, asyncExecCallback);
+ return new BackendResponseHandler(target, conditional, requestDate, responseDate, scope, asyncExecCallback);
}
@Override
@@ -1013,10 +1035,8 @@ public AsyncDataConsumer handleResponse(
final AsyncExecCallback callback1;
if (HttpCacheEntry.isNewer(hit.entry, backendResponse1)) {
- final HttpRequest unconditional = conditionalRequestBuilder.buildUnconditionalRequest(
- BasicRequestBuilder.copy(scope.originalRequest).build());
-
- callback1 = new AsyncExecCallbackWrapper(() -> chainProceed(unconditional, entityProducer, scope, chain, new AsyncExecCallback() {
+ final SimpleHttpRequest unconditional = conditionalRequestBuilder.makeUnconditional(request);
+ callback1 = new AsyncExecCallbackWrapper(() -> callChain(unconditional, scope, chain, new AsyncExecCallback() {
@Override
public AsyncDataConsumer handleResponse(
@@ -1104,14 +1124,13 @@ void revalidateCacheEntryWithoutFallback(
final ResponseCacheControl responseCacheControl,
final CacheHit hit,
final HttpHost target,
- final HttpRequest request,
- final AsyncEntityProducer entityProducer,
+ final SimpleHttpRequest request,
final AsyncExecChain.Scope scope,
final AsyncExecChain chain,
final AsyncExecCallback asyncExecCallback) {
final String exchangeId = scope.exchangeId;
final HttpCacheContext context = HttpCacheContext.cast(scope.clientContext);
- revalidateCacheEntry(responseCacheControl, hit, target, request, entityProducer, scope, chain, new AsyncExecCallback() {
+ revalidateCacheEntry(responseCacheControl, hit, target, request, scope, chain, new AsyncExecCallback() {
private final AtomicBoolean committed = new AtomicBoolean();
@@ -1154,14 +1173,13 @@ void revalidateCacheEntryWithFallback(
final ResponseCacheControl responseCacheControl,
final CacheHit hit,
final HttpHost target,
- final HttpRequest request,
- final AsyncEntityProducer entityProducer,
+ final SimpleHttpRequest request,
final AsyncExecChain.Scope scope,
final AsyncExecChain chain,
final AsyncExecCallback asyncExecCallback) {
final String exchangeId = scope.exchangeId;
final HttpCacheContext context = HttpCacheContext.cast(scope.clientContext);
- revalidateCacheEntry(responseCacheControl, hit, target, request, entityProducer, scope, chain, new AsyncExecCallback() {
+ revalidateCacheEntry(responseCacheControl, hit, target, request, scope, chain, new AsyncExecCallback() {
private final AtomicReference<HttpResponse> committed = new AtomicReference<>();
@@ -1236,8 +1254,7 @@ private void handleCacheMiss(
final RequestCacheControl requestCacheControl,
final CacheHit partialMatch,
final HttpHost target,
- final HttpRequest request,
- final AsyncEntityProducer entityProducer,
+ final SimpleHttpRequest request,
final AsyncExecChain.Scope scope,
final AsyncExecChain chain,
final AsyncExecCallback asyncExecCallback) {
@@ -1259,7 +1276,7 @@ private void handleCacheMiss(
triggerResponse(cacheResponse, scope, asyncExecCallback);
}
- if (partialMatch != null && partialMatch.entry.hasVariants() && entityProducer == null) {
+ if (partialMatch != null && partialMatch.entry.hasVariants() && request.getBody() == null) {
operation.setDependency(responseCache.getVariants(
partialMatch,
new FutureCallback<Collection<CacheHit>>() {
@@ -1267,9 +1284,9 @@ private void handleCacheMiss(
@Override
public void completed(final Collection<CacheHit> variants) {
if (variants != null && !variants.isEmpty()) {
- negotiateResponseFromVariants(target, request, entityProducer, scope, chain, asyncExecCallback, variants);
+ negotiateResponseFromVariants(target, request, scope, chain, asyncExecCallback, variants);
} else {
- callBackend(target, request, entityProducer, scope, chain, asyncExecCallback);
+ callBackend(target, request, scope, chain, asyncExecCallback);
}
}
@@ -1285,14 +1302,13 @@ public void cancelled() {
}));
} else {
- callBackend(target, request, entityProducer, scope, chain, asyncExecCallback);
+ callBackend(target, request, scope, chain, asyncExecCallback);
}
}
void negotiateResponseFromVariants(
final HttpHost target,
- final HttpRequest request,
- final AsyncEntityProducer entityProducer,
+ final SimpleHttpRequest request,
final AsyncExecChain.Scope scope,
final AsyncExecChain chain,
final AsyncExecCallback asyncExecCallback,
@@ -1307,12 +1323,12 @@ void negotiateResponseFromVariants(
}
}
- final HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequestFromVariants(
+ final SimpleHttpRequest conditional = conditionalRequestBuilder.makeConditionalRequestVariants(
request,
variantMap.keySet());
final Instant requestDate = getCurrentDate();
- chainProceed(conditionalRequest, entityProducer, scope, chain, new AsyncExecCallback() {
+ callChain(conditional, scope, chain, new AsyncExecCallback() {
final AtomicReference<AsyncExecCallback> callbackRef = new AtomicReference<>();
@@ -1368,19 +1384,18 @@ public AsyncDataConsumer handleResponse(
if (LOG.isDebugEnabled()) {
LOG.debug("{} 304 response did not contain ETag", exchangeId);
}
- callback = new AsyncExecCallbackWrapper(() -> callBackend(target, request, entityProducer, scope, chain, asyncExecCallback), asyncExecCallback::failed);
+ callback = new AsyncExecCallbackWrapper(() -> callBackend(target, request, scope, chain, asyncExecCallback), asyncExecCallback::failed);
} else {
final CacheHit match = variantMap.get(resultEtag);
if (match == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} 304 response did not contain ETag matching one sent in If-None-Match", exchangeId);
}
- callback = new AsyncExecCallbackWrapper(() -> callBackend(target, request, entityProducer, scope, chain, asyncExecCallback), asyncExecCallback::failed);
+ callback = new AsyncExecCallbackWrapper(() -> callBackend(target, request, scope, chain, asyncExecCallback), asyncExecCallback::failed);
} else {
if (HttpCacheEntry.isNewer(match.entry, backendResponse)) {
- final HttpRequest unconditional = conditionalRequestBuilder.buildUnconditionalRequest(
- BasicRequestBuilder.copy(request).build());
- callback = new AsyncExecCallbackWrapper(() -> callBackend(target, unconditional, entityProducer, scope, chain, asyncExecCallback), asyncExecCallback::failed);
+ final SimpleHttpRequest unconditional = conditionalRequestBuilder.makeUnconditional(request);
+ callback = new AsyncExecCallbackWrapper(() -> callBackend(target, unconditional, scope, chain, asyncExecCallback), asyncExecCallback::failed);
} else {
callback = new AsyncExecCallbackWrapper(() -> updateVariantCacheEntry(backendResponse, responseDate, match), asyncExecCallback::failed);
}
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java
index b5b50f16e..05f4adbb0 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java
@@ -36,6 +36,7 @@
import java.util.Set;
import java.util.stream.Collectors;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.cache.HttpAsyncCacheStorage;
import org.apache.hc.client5.http.cache.HttpCacheCASOperation;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
@@ -53,7 +54,6 @@
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.Method;
@@ -93,7 +93,7 @@ public BasicHttpAsyncCache(final ResourceFactory resourceFactory, final HttpAsyn
}
@Override
- public Cancellable match(final HttpHost host, final HttpRequest request, final FutureCallback<CacheMatch> callback) {
+ public Cancellable match(final HttpHost host, final SimpleHttpRequest request, final FutureCallback<CacheMatch> callback) {
final String rootKey = cacheKeyGenerator.generateKey(host, request);
if (LOG.isDebugEnabled()) {
LOG.debug("Get cache entry: {}", rootKey);
@@ -384,7 +384,7 @@ public void completed(final Boolean result) {
@Override
public Cancellable store(
final HttpHost host,
- final HttpRequest request,
+ final SimpleHttpRequest request,
final HttpResponse originResponse,
final ByteArrayBuffer content,
final Instant requestSent,
@@ -424,7 +424,7 @@ public Cancellable store(
public Cancellable update(
final CacheHit stale,
final HttpHost host,
- final HttpRequest request,
+ final SimpleHttpRequest request,
final HttpResponse originResponse,
final Instant requestSent,
final Instant responseReceived,
@@ -447,7 +447,7 @@ public Cancellable update(
public Cancellable storeFromNegotiated(
final CacheHit negotiated,
final HttpHost host,
- final HttpRequest request,
+ final SimpleHttpRequest request,
final HttpResponse originResponse,
final Instant requestSent,
final Instant responseReceived,
@@ -543,7 +543,7 @@ public void cancelled() {
@Override
public Cancellable evictInvalidatedEntries(
- final HttpHost host, final HttpRequest request, final HttpResponse response, final FutureCallback<Boolean> callback) {
+ final HttpHost host, final SimpleHttpRequest request, final HttpResponse response, final FutureCallback<Boolean> callback) {
if (LOG.isDebugEnabled()) {
LOG.debug("Flush cache entries invalidated by exchange: {}; {} {} -> {}",
host, request.getMethod(), request.getRequestUri(), response.getCode());
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
index 8a5da4c92..2d0a60fca 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
@@ -34,6 +34,7 @@
import java.util.List;
import java.util.Set;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.cache.HttpCacheCASOperation;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.HttpCacheEntryFactory;
@@ -46,7 +47,6 @@
import org.apache.hc.client5.http.validator.ValidatorType;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.Method;
@@ -139,7 +139,7 @@ private void removeInternal(final String cacheKey) {
}
@Override
- public CacheMatch match(final HttpHost host, final HttpRequest request) {
+ public CacheMatch match(final HttpHost host, final SimpleHttpRequest request) {
final String rootKey = cacheKeyGenerator.generateKey(host, request);
if (LOG.isDebugEnabled()) {
LOG.debug("Get cache root entry: {}", rootKey);
@@ -218,7 +218,7 @@ CacheHit store(final String rootKey, final String variantKey, final HttpCacheEnt
@Override
public CacheHit store(
final HttpHost host,
- final HttpRequest request,
+ final SimpleHttpRequest request,
final HttpResponse originResponse,
final ByteArrayBuffer content,
final Instant requestSent,
@@ -256,7 +256,7 @@ public CacheHit store(
public CacheHit update(
final CacheHit stale,
final HttpHost host,
- final HttpRequest request,
+ final SimpleHttpRequest request,
final HttpResponse originResponse,
final Instant requestSent,
final Instant responseReceived) {
@@ -278,7 +278,7 @@ public CacheHit update(
public CacheHit storeFromNegotiated(
final CacheHit negotiated,
final HttpHost host,
- final HttpRequest request,
+ final SimpleHttpRequest request,
final HttpResponse originResponse,
final Instant requestSent,
final Instant responseReceived) {
@@ -339,7 +339,7 @@ private void evict(final String rootKey, final HttpResponse response) {
}
@Override
- public void evictInvalidatedEntries(final HttpHost host, final HttpRequest request, final HttpResponse response) {
+ public void evictInvalidatedEntries(final HttpHost host, final SimpleHttpRequest request, final HttpResponse response) {
if (LOG.isDebugEnabled()) {
LOG.debug("Evict cache entries invalidated by exchange: {}; {} {} -> {}",
host, request.getMethod(), request.getRequestUri(), response.getCode());
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
index 16080636d..7cff168a7 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
@@ -36,7 +36,9 @@
import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.async.methods.SimpleBody;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
+import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.cache.CacheResponseStatus;
import org.apache.hc.client5.http.cache.HttpCacheContext;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
@@ -57,9 +59,9 @@
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.HttpVersion;
+import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
@@ -103,7 +105,7 @@ class CachingExec extends CachingExecBase implements ExecChainHandler {
private final HttpCache responseCache;
private final DefaultCacheRevalidator cacheRevalidator;
- private final ConditionalRequestBuilder<ClassicHttpRequest> conditionalRequestBuilder;
+ private final ConditionalRequestBuilder conditionalRequestBuilder;
private static final Logger LOG = LoggerFactory.getLogger(CachingExec.class);
@@ -111,8 +113,7 @@ class CachingExec extends CachingExecBase implements ExecChainHandler {
super(config);
this.responseCache = Args.notNull(cache, "Response cache");
this.cacheRevalidator = cacheRevalidator;
- this.conditionalRequestBuilder = new ConditionalRequestBuilder<>(classicHttpRequest ->
- ClassicRequestBuilder.copy(classicHttpRequest).build());
+ this.conditionalRequestBuilder = new ConditionalRequestBuilder();
}
@Override
@@ -157,6 +158,16 @@ ClassicHttpResponse doExecute(
return new BasicClassicHttpResponse(HttpStatus.SC_NOT_IMPLEMENTED);
}
+ // Do not attempt to cache requests with an enclosed content body
+ // To be revised when implementing QUERY support
+ final HttpEntity requestEntity = request.getEntity();
+ if (requestEntity != null) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("{} entity enclosing request cannot be served from cache", exchangeId);
+ }
+ return chain.proceed(request, scope);
+ }
+
final RequestCacheControl requestCacheControl;
if (request.containsHeader(HttpHeaders.CACHE_CONTROL)) {
requestCacheControl = CacheControlHeaderParser.INSTANCE.parse(request);
@@ -165,29 +176,31 @@ ClassicHttpResponse doExecute(
CacheControlHeaderGenerator.INSTANCE.generate(requestCacheControl, request);
}
+ final SimpleHttpRequest cacheRequest = prepareRequest(request);
+
if (LOG.isDebugEnabled()) {
LOG.debug("Request cache control: {}", requestCacheControl);
}
- if (!cacheableRequestPolicy.canBeServedFromCache(requestCacheControl, request)) {
+ if (!cacheableRequestPolicy.canBeServedFromCache(requestCacheControl, cacheRequest)) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} request cannot be served from cache", exchangeId);
}
- return callBackend(target, request, scope, chain);
+ return callBackend(target, cacheRequest, scope, chain);
}
- final CacheMatch result = responseCache.match(target, request);
+ final CacheMatch result = responseCache.match(target, cacheRequest);
final CacheHit hit = result != null ? result.hit : null;
final CacheHit root = result != null ? result.root : null;
if (hit == null) {
- return handleCacheMiss(requestCacheControl, root, target, request, scope, chain);
+ return handleCacheMiss(requestCacheControl, root, target, cacheRequest, scope, chain);
}
final ResponseCacheControl responseCacheControl = CacheControlHeaderParser.INSTANCE.parse(hit.entry);
context.setResponseCacheControl(responseCacheControl);
if (LOG.isDebugEnabled()) {
LOG.debug("{} response cache control: {}", exchangeId, responseCacheControl);
}
- return handleCacheHit(requestCacheControl, responseCacheControl, hit, target, request, scope, chain);
+ return handleCacheHit(requestCacheControl, responseCacheControl, hit, target, cacheRequest, scope, chain);
}
private static ClassicHttpResponse convert(final SimpleHttpResponse cacheResponse) {
@@ -213,9 +226,17 @@ private static ClassicHttpResponse convert(final SimpleHttpResponse cacheRespons
return response;
}
+ SimpleHttpRequest prepareRequest(final ClassicHttpRequest request) throws ProtocolException {
+ // To be revised when implementing QUERY support
+ if (request.getEntity() != null) {
+ throw new ProtocolException("Caching of entity enclosing requests is not supported");
+ }
+ return SimpleRequestBuilder.copy(request).build();
+ }
+
ClassicHttpResponse callBackend(
final HttpHost target,
- final ClassicHttpRequest request,
+ final SimpleHttpRequest request,
final ExecChain.Scope scope,
final ExecChain chain) throws IOException, HttpException {
@@ -225,7 +246,7 @@ ClassicHttpResponse callBackend(
if (LOG.isDebugEnabled()) {
LOG.debug("{} calling the backend", exchangeId);
}
- final ClassicHttpResponse backendResponse = chain.proceed(request, scope);
+ final ClassicHttpResponse backendResponse = callChain(request, scope, chain);
try {
return handleBackendResponse(target, request, scope, requestDate, getCurrentDate(), backendResponse);
} catch (final IOException | RuntimeException ex) {
@@ -234,12 +255,33 @@ ClassicHttpResponse callBackend(
}
}
+ ClassicHttpResponse callChain(
+ final SimpleHttpRequest request,
+ final ExecChain.Scope scope,
+ final ExecChain chain) throws IOException, HttpException {
+ final SimpleBody body = request.getBody();
+ final HttpEntity entity;
+ if (body != null) {
+ if (body.isText()) {
+ entity = new StringEntity(body.getBodyText(), body.getContentType());
+ } else {
+ entity = new ByteArrayEntity(body.getBodyBytes(), body.getContentType());
+ }
+ } else {
+ entity = null;
+ }
+ final ClassicHttpRequest backendRequest = ClassicRequestBuilder.copy(request)
+ .setEntity(entity)
+ .build();
+ return chain.proceed(backendRequest, scope);
+ }
+
private ClassicHttpResponse handleCacheHit(
final RequestCacheControl requestCacheControl,
final ResponseCacheControl responseCacheControl,
final CacheHit hit,
final HttpHost target,
- final ClassicHttpRequest request,
+ final SimpleHttpRequest request,
final ExecChain.Scope scope,
final ExecChain chain) throws IOException, HttpException {
final String exchangeId = scope.exchangeId;
@@ -275,7 +317,7 @@ private ClassicHttpResponse handleCacheHit(
return convert(generateGatewayTimeout());
}
context.setCacheResponseStatus(CacheResponseStatus.FAILURE);
- return chain.proceed(request, scope);
+ return callChain(request, scope, chain);
}
}
if (requestCacheControl.isOnlyIfCached()) {
@@ -289,11 +331,6 @@ private ClassicHttpResponse handleCacheHit(
LOG.debug("{} cache entry does not match the request; calling backend", exchangeId);
}
return callBackend(target, request, scope, chain);
- } else if (request.getEntity() != null && !request.getEntity().isRepeatable()) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("{} request is not repeatable; calling backend", exchangeId);
- }
- return callBackend(target, request, scope, chain);
} else if (hit.entry.getStatus() == HttpStatus.SC_NOT_MODIFIED && !suitabilityChecker.isConditional(request)) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} non-modified cache entry does not match the non-conditional request; calling backend", exchangeId);
@@ -349,24 +386,23 @@ ClassicHttpResponse revalidateCacheEntry(
final ResponseCacheControl responseCacheControl,
final CacheHit hit,
final HttpHost target,
- final ClassicHttpRequest request,
+ final SimpleHttpRequest request,
final ExecChain.Scope scope,
final ExecChain chain) throws IOException, HttpException {
final HttpCacheContext context = HttpCacheContext.cast(scope.clientContext);
Instant requestDate = getCurrentDate();
- final ClassicHttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequest(
- responseCacheControl, request, hit.entry);
+ final SimpleHttpRequest conditional = conditionalRequestBuilder.makeConditional(responseCacheControl, request, hit.entry);
- ClassicHttpResponse backendResponse = chain.proceed(conditionalRequest, scope);
+ ClassicHttpResponse backendResponse = callChain(conditional, scope, chain);
try {
Instant responseDate = getCurrentDate();
if (HttpCacheEntry.isNewer(hit.entry, backendResponse)) {
backendResponse.close();
- final ClassicHttpRequest unconditional = conditionalRequestBuilder.buildUnconditionalRequest(
- scope.originalRequest);
+
+ final SimpleHttpRequest unconditional = conditionalRequestBuilder.makeUnconditional(request);
requestDate = getCurrentDate();
- backendResponse = chain.proceed(unconditional, scope);
+ backendResponse = callChain(unconditional, scope, chain);
responseDate = getCurrentDate();
}
@@ -381,7 +417,7 @@ ClassicHttpResponse revalidateCacheEntry(
context.setCacheEntry(updated.entry);
return convert(cacheResponse);
}
- return handleBackendResponse(target, conditionalRequest, scope, requestDate, responseDate, backendResponse);
+ return handleBackendResponse(target, conditional, scope, requestDate, responseDate, backendResponse);
} catch (final IOException | RuntimeException ex) {
backendResponse.close();
throw ex;
@@ -392,7 +428,7 @@ ClassicHttpResponse revalidateCacheEntryWithoutFallback(
final ResponseCacheControl responseCacheControl,
final CacheHit hit,
final HttpHost target,
- final ClassicHttpRequest request,
+ final SimpleHttpRequest request,
final ExecChain.Scope scope,
final ExecChain chain) throws HttpException {
final String exchangeId = scope.exchangeId;
@@ -413,7 +449,7 @@ ClassicHttpResponse revalidateCacheEntryWithFallback(
final ResponseCacheControl responseCacheControl,
final CacheHit hit,
final HttpHost target,
- final ClassicHttpRequest request,
+ final SimpleHttpRequest request,
final ExecChain.Scope scope,
final ExecChain chain) throws HttpException, IOException {
final String exchangeId = scope.exchangeId;
@@ -453,7 +489,7 @@ ClassicHttpResponse revalidateCacheEntryWithFallback(
ClassicHttpResponse handleBackendResponse(
final HttpHost target,
- final ClassicHttpRequest request,
+ final SimpleHttpRequest request,
final ExecChain.Scope scope,
final Instant requestDate,
final Instant responseDate,
@@ -485,7 +521,7 @@ ClassicHttpResponse handleBackendResponse(
ClassicHttpResponse cacheAndReturnResponse(
final HttpHost target,
- final HttpRequest request,
+ final SimpleHttpRequest request,
final ExecChain.Scope scope,
final ClassicHttpResponse backendResponse,
final Instant requestSent,
@@ -564,7 +600,7 @@ private ClassicHttpResponse handleCacheMiss(
final RequestCacheControl requestCacheControl,
final CacheHit partialMatch,
final HttpHost target,
- final ClassicHttpRequest request,
+ final SimpleHttpRequest request,
final ExecChain.Scope scope,
final ExecChain chain) throws IOException, HttpException {
final String exchangeId = scope.exchangeId;
@@ -582,7 +618,7 @@ private ClassicHttpResponse handleCacheMiss(
context.setCacheResponseStatus(CacheResponseStatus.CACHE_MODULE_RESPONSE);
return convert(generateGatewayTimeout());
}
- if (partialMatch != null && partialMatch.entry.hasVariants() && request.getEntity() == null) {
+ if (partialMatch != null && partialMatch.entry.hasVariants() && request.getBody() == null) {
final List<CacheHit> variants = responseCache.getVariants(partialMatch);
if (variants != null && !variants.isEmpty()) {
return negotiateResponseFromVariants(target, request, scope, chain, variants);
@@ -594,7 +630,7 @@ private ClassicHttpResponse handleCacheMiss(
ClassicHttpResponse negotiateResponseFromVariants(
final HttpHost target,
- final ClassicHttpRequest request,
+ final SimpleHttpRequest request,
final ExecChain.Scope scope,
final ExecChain chain,
final List<CacheHit> variants) throws IOException, HttpException {
@@ -608,12 +644,12 @@ ClassicHttpResponse negotiateResponseFromVariants(
}
}
- final ClassicHttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequestFromVariants(
+ final SimpleHttpRequest conditional = conditionalRequestBuilder.makeConditionalRequestVariants(
request,
variantMap.keySet());
final Instant requestDate = getCurrentDate();
- final ClassicHttpResponse backendResponse = chain.proceed(conditionalRequest, scope);
+ final ClassicHttpResponse backendResponse = callChain(conditional, scope, chain);
try {
final Instant responseDate = getCurrentDate();
@@ -640,7 +676,7 @@ ClassicHttpResponse negotiateResponseFromVariants(
}
if (HttpCacheEntry.isNewer(match.entry, backendResponse)) {
- final ClassicHttpRequest unconditional = conditionalRequestBuilder.buildUnconditionalRequest(request);
+ final SimpleHttpRequest unconditional = conditionalRequestBuilder.makeUnconditional(request);
return callBackend(target, unconditional, scope, chain);
}
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ConditionalRequestBuilder.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ConditionalRequestBuilder.java
index ac0aec605..3391b8c23 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ConditionalRequestBuilder.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ConditionalRequestBuilder.java
@@ -28,24 +28,18 @@
import java.util.Collection;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
+import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.ResponseCacheControl;
import org.apache.hc.client5.http.validator.ETag;
-import org.apache.hc.core5.function.Factory;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHeaders;
-import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.message.BufferedHeader;
import org.apache.hc.core5.util.CharArrayBuffer;
-class ConditionalRequestBuilder<T extends HttpRequest> {
-
- private final Factory<T, T> messageCopier;
-
- ConditionalRequestBuilder(final Factory<T, T> messageCopier) {
- this.messageCopier = messageCopier;
- }
+class ConditionalRequestBuilder {
/**
* When a {@link HttpCacheEntry} is stale but 'might' be used as a response
@@ -58,8 +52,10 @@ class ConditionalRequestBuilder<T extends HttpRequest> {
* @param cacheEntry the entry that needs to be re-validated
* @return the wrapped request
*/
- public T buildConditionalRequest(final ResponseCacheControl cacheControl, final T request, final HttpCacheEntry cacheEntry) {
- final T newRequest = messageCopier.create(request);
+ public SimpleHttpRequest makeConditional(final ResponseCacheControl cacheControl,
+ final SimpleHttpRequest request,
+ final HttpCacheEntry cacheEntry) {
+ final SimpleHttpRequest newRequest = SimpleRequestBuilder.copy(request).build();
final ETag eTag = cacheEntry.getETag();
if (eTag != null) {
@@ -87,8 +83,9 @@ public T buildConditionalRequest(final ResponseCacheControl cacheControl, final
* @param variants
* @return the wrapped request
*/
- public T buildConditionalRequestFromVariants(final T request, final Collection<ETag> variants) {
- final T newRequest = messageCopier.create(request);
+ public SimpleHttpRequest makeConditionalRequestVariants(final SimpleHttpRequest request,
+ final Collection<ETag> variants) {
+ final SimpleHttpRequest newRequest = SimpleRequestBuilder.copy(request).build();
final CharArrayBuffer buffer = new CharArrayBuffer(256);
buffer.append(HttpHeaders.IF_NONE_MATCH);
buffer.append(": ");
@@ -114,8 +111,8 @@ public T buildConditionalRequestFromVariants(final T request, final Collection<E
* @param request client request we are trying to satisfy
* @return an unconditional validation request
*/
- public T buildUnconditionalRequest(final T request) {
- final T newRequest = messageCopier.create(request);
+ public SimpleHttpRequest makeUnconditional(final SimpleHttpRequest request) {
+ final SimpleHttpRequest newRequest = SimpleRequestBuilder.copy(request).build();
newRequest.addHeader(HttpHeaders.CACHE_CONTROL,HeaderConstants.CACHE_CONTROL_NO_CACHE);
newRequest.removeHeaders(HttpHeaders.IF_RANGE);
newRequest.removeHeaders(HttpHeaders.IF_MATCH);
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpAsyncCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpAsyncCache.java
index 6dd341cda..2757c478b 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpAsyncCache.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpAsyncCache.java
@@ -29,6 +29,7 @@
import java.time.Instant;
import java.util.Collection;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.core5.concurrent.Cancellable;
import org.apache.hc.core5.concurrent.FutureCallback;
@@ -43,7 +44,7 @@ interface HttpAsyncCache {
* Returns a result with either a fully matching {@link HttpCacheEntry}
* a partial match with a list of known variants or null if no match could be found.
*/
- Cancellable match(HttpHost host, HttpRequest request, FutureCallback<CacheMatch> callback);
+ Cancellable match(HttpHost host, SimpleHttpRequest request, FutureCallback<CacheMatch> callback);
/**
* Retrieves variant {@link HttpCacheEntry}s for the given hit.
@@ -56,7 +57,7 @@ Cancellable getVariants(
*/
Cancellable store(
HttpHost host,
- HttpRequest request,
+ SimpleHttpRequest request,
HttpResponse originResponse,
ByteArrayBuffer content,
Instant requestSent,
@@ -70,7 +71,7 @@ Cancellable store(
Cancellable update(
CacheHit stale,
HttpHost host,
- HttpRequest request,
+ SimpleHttpRequest request,
HttpResponse originResponse,
Instant requestSent,
Instant responseReceived,
@@ -83,7 +84,7 @@ Cancellable update(
Cancellable storeFromNegotiated(
CacheHit negotiated,
HttpHost host,
- HttpRequest request,
+ SimpleHttpRequest request,
HttpResponse originResponse,
Instant requestSent,
Instant responseReceived,
@@ -93,6 +94,6 @@ Cancellable storeFromNegotiated(
* Evicts {@link HttpCacheEntry}s invalidated by the given message exchange.
*/
Cancellable evictInvalidatedEntries(
- HttpHost host, HttpRequest request, HttpResponse response, FutureCallback<Boolean> callback);
+ HttpHost host, SimpleHttpRequest request, HttpResponse response, FutureCallback<Boolean> callback);
}
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpCache.java
index 7160ccdb3..f1f054ee0 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpCache.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/HttpCache.java
@@ -29,6 +29,7 @@
import java.time.Instant;
import java.util.List;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
@@ -41,7 +42,7 @@ interface HttpCache {
* Returns a result with either a fully matching {@link HttpCacheEntry}
* a partial match with a list of known variants or null if no match could be found.
*/
- CacheMatch match(HttpHost host, HttpRequest request);
+ CacheMatch match(HttpHost host, SimpleHttpRequest request);
/**
* Retrieves variant {@link HttpCacheEntry}s for the given hit.
@@ -53,7 +54,7 @@ interface HttpCache {
*/
CacheHit store(
HttpHost host,
- HttpRequest request,
+ SimpleHttpRequest request,
HttpResponse originResponse,
ByteArrayBuffer content,
Instant requestSent,
@@ -66,7 +67,7 @@ CacheHit store(
CacheHit update(
CacheHit stale,
HttpHost host,
- HttpRequest request,
+ SimpleHttpRequest request,
HttpResponse originResponse,
Instant requestSent,
Instant responseReceived);
@@ -78,7 +79,7 @@ CacheHit update(
CacheHit storeFromNegotiated(
CacheHit negotiated,
HttpHost host,
- HttpRequest request,
+ SimpleHttpRequest request,
HttpResponse originResponse,
Instant requestSent,
Instant responseReceived);
@@ -86,6 +87,6 @@ CacheHit storeFromNegotiated(
/**
* Evicts {@link HttpCacheEntry}s invalidated by the given message exchange.
*/
- void evictInvalidatedEntries(HttpHost host, HttpRequest request, HttpResponse response);
+ void evictInvalidatedEntries(HttpHost host, SimpleHttpRequest request, HttpResponse response);
}
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestBasicHttpAsyncCache.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestBasicHttpAsyncCache.java
index 25a8a3b02..733656595 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestBasicHttpAsyncCache.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestBasicHttpAsyncCache.java
@@ -45,6 +45,7 @@
import java.util.stream.Collectors;
import org.apache.hc.client5.http.HeadersMatcher;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.utils.DateUtils;
@@ -54,7 +55,6 @@
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.message.BasicHeader;
-import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.BasicHttpResponse;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.util.ByteArrayBuffer;
@@ -83,7 +83,7 @@ void setUp() {
@Test
void testGetCacheEntryReturnsNullOnCacheMiss() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
final CountDownLatch latch1 = new CountDownLatch(1);
final AtomicReference<CacheMatch> resultRef = new AtomicReference<>();
@@ -100,7 +100,7 @@ void testGetCacheEntryFetchesFromCacheOnCacheHitIfNoVariants() throws Exception
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
assertFalse(entry.hasVariants());
final HttpHost host = new HttpHost("foo.example.com");
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
final String key = CacheKeyGenerator.INSTANCE.generateKey(host, request);
@@ -121,7 +121,7 @@ void testGetCacheEntryFetchesFromCacheOnCacheHitIfNoVariants() throws Exception
@Test
void testGetCacheEntryReturnsNullIfNoVariantInCache() throws Exception {
- final HttpRequest origRequest = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
origRequest.setHeader("Accept-Encoding","gzip");
final ByteArrayBuffer buf = HttpTestUtils.makeRandomBuffer(128);
@@ -138,7 +138,7 @@ void testGetCacheEntryReturnsNullIfNoVariantInCache() throws Exception {
latch1.await();
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
final CountDownLatch latch2 = new CountDownLatch(1);
final AtomicReference<CacheMatch> resultRef = new AtomicReference<>();
@@ -153,7 +153,7 @@ void testGetCacheEntryReturnsNullIfNoVariantInCache() throws Exception {
@Test
void testGetCacheEntryReturnsVariantIfPresentInCache() throws Exception {
- final HttpRequest origRequest = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
origRequest.setHeader("Accept-Encoding","gzip");
final ByteArrayBuffer buf = HttpTestUtils.makeRandomBuffer(128);
@@ -170,7 +170,7 @@ void testGetCacheEntryReturnsVariantIfPresentInCache() throws Exception {
latch1.await();
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
request.setHeader("Accept-Encoding","gzip");
final CountDownLatch latch2 = new CountDownLatch(1);
@@ -186,7 +186,7 @@ void testGetCacheEntryReturnsVariantIfPresentInCache() throws Exception {
@Test
void testGetCacheEntryReturnsVariantWithMostRecentDateHeader() throws Exception {
- final HttpRequest origRequest = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
origRequest.setHeader("Accept-Encoding", "gzip");
final ByteArrayBuffer buf = HttpTestUtils.makeRandomBuffer(128);
@@ -212,7 +212,7 @@ void testGetCacheEntryReturnsVariantWithMostRecentDateHeader() throws Exception
latch1.await();
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
request.setHeader("Accept-Encoding", "gzip");
final CountDownLatch latch2 = new CountDownLatch(1);
@@ -272,7 +272,7 @@ void testGetVariantsRootNonExistentVariants() throws Exception {
void testGetVariantCacheEntriesReturnsAllVariants() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
req1.setHeader("Accept-Encoding", "gzip");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(uri);
@@ -284,7 +284,7 @@ void testGetVariantCacheEntriesReturnsAllVariants() throws Exception {
resp1.setHeader("Vary", "Accept-Encoding");
resp1.setHeader("Content-Encoding","gzip");
- final HttpRequest req2 = new HttpGet(uri);
+ final SimpleHttpRequest req2 = new SimpleHttpRequest("GET", uri);
req2.setHeader("Accept-Encoding", "identity");
final HttpResponse resp2 = HttpTestUtils.make200Response();
@@ -332,7 +332,7 @@ void testGetVariantCacheEntriesReturnsAllVariants() throws Exception {
void testUpdateCacheEntry() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
final HttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
@@ -384,7 +384,7 @@ void testUpdateCacheEntry() throws Exception {
void testUpdateVariantCacheEntry() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
req1.setHeader("User-Agent", "agent1");
final HttpResponse resp1 = HttpTestUtils.make200Response();
@@ -439,7 +439,7 @@ void testUpdateVariantCacheEntry() throws Exception {
void testUpdateCacheEntryTurnsVariant() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
req1.setHeader("User-Agent", "agent1");
final HttpResponse resp1 = HttpTestUtils.make200Response();
@@ -493,7 +493,7 @@ void testUpdateCacheEntryTurnsVariant() throws Exception {
void testStoreFromNegotiatedVariant() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
req1.setHeader("User-Agent", "agent1");
final HttpResponse resp1 = HttpTestUtils.make200Response();
@@ -515,7 +515,7 @@ void testStoreFromNegotiatedVariant() throws Exception {
Assertions.assertEquals(2, backing.map.size());
Assertions.assertSame(hit1.entry, backing.map.get(hit1.getEntryKey()));
- final HttpRequest req2 = new HttpGet(uri);
+ final SimpleHttpRequest req2 = new SimpleHttpRequest("GET", uri);
req2.setHeader("User-Agent", "agent2");
final HttpResponse resp2 = HttpTestUtils.make304Response();
@@ -544,7 +544,7 @@ void testStoreFromNegotiatedVariant() throws Exception {
@Test
void testInvalidatesUnsafeRequests() throws Exception {
- final HttpRequest request = new BasicHttpRequest("POST", "/path");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "POST", "/path");
final HttpResponse response = HttpTestUtils.make200Response();
final String key = CacheKeyGenerator.INSTANCE.generateKey(host, request);
@@ -563,7 +563,7 @@ void testInvalidatesUnsafeRequests() throws Exception {
@Test
void testDoesNotInvalidateSafeRequests() throws Exception {
- final HttpRequest request1 = new BasicHttpRequest("GET", "/");
+ final SimpleHttpRequest request1 = new SimpleHttpRequest("GET", "/");
final HttpResponse response1 = HttpTestUtils.make200Response();
final CountDownLatch latch1 = new CountDownLatch(1);
@@ -573,7 +573,7 @@ void testDoesNotInvalidateSafeRequests() throws Exception {
verifyNoMoreInteractions(backing);
- final HttpRequest request2 = new BasicHttpRequest("HEAD", "/");
+ final SimpleHttpRequest request2 = new SimpleHttpRequest("HEAD", "/");
final HttpResponse response2 = HttpTestUtils.make200Response();
final CountDownLatch latch2 = new CountDownLatch(1);
@@ -586,7 +586,7 @@ void testDoesNotInvalidateSafeRequests() throws Exception {
@Test
void testInvalidatesUnsafeRequestsWithVariants() throws Exception {
- final HttpRequest request = new BasicHttpRequest("POST", "/path");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "POST", "/path");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final Set<String> variants = new HashSet<>();
variants.add("{var1}");
@@ -617,7 +617,7 @@ void testInvalidatesUnsafeRequestsWithVariants() throws Exception {
@Test
void testInvalidateUriSpecifiedByContentLocationAndFresher() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final URI contentUri = new URIBuilder()
.setHttpHost(host)
@@ -649,7 +649,7 @@ void testInvalidateUriSpecifiedByContentLocationAndFresher() throws Exception {
@Test
void testInvalidateUriSpecifiedByLocationAndFresher() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final URI contentUri = new URIBuilder()
.setHttpHost(host)
@@ -681,7 +681,7 @@ void testInvalidateUriSpecifiedByLocationAndFresher() throws Exception {
@Test
void testDoesNotInvalidateForUnsuccessfulResponse() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -701,7 +701,7 @@ void testDoesNotInvalidateForUnsuccessfulResponse() throws Exception {
@Test
void testInvalidateUriSpecifiedByContentLocationNonCanonical() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final URI contentUri = new URIBuilder()
.setHttpHost(host)
@@ -736,7 +736,7 @@ void testInvalidateUriSpecifiedByContentLocationNonCanonical() throws Exception
@Test
void testInvalidateUriSpecifiedByContentLocationRelative() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final URI contentUri = new URIBuilder()
.setHttpHost(host)
@@ -771,7 +771,7 @@ void testInvalidateUriSpecifiedByContentLocationRelative() throws Exception {
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationOtherOrigin() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/");
final URI contentUri = new URIBuilder()
.setHost("bar.example.com")
.setPath("/")
@@ -796,7 +796,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationOtherOrigin() throws Exce
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationIfEtagsMatch() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -823,7 +823,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationIfEtagsMatch() throws Exc
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationIfOlder() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -850,7 +850,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationIfOlder() throws Exceptio
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationIfResponseHasNoEtag() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -877,7 +877,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationIfResponseHasNoEtag() thr
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationIfEntryHasNoEtag() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -903,7 +903,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationIfEntryHasNoEtag() throws
@Test
void testInvalidatesUriSpecifiedByContentLocationIfResponseHasNoDate() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -930,7 +930,7 @@ void testInvalidatesUriSpecifiedByContentLocationIfResponseHasNoDate() throws Ex
@Test
void testInvalidatesUriSpecifiedByContentLocationIfEntryHasNoDate() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -956,7 +956,7 @@ void testInvalidatesUriSpecifiedByContentLocationIfEntryHasNoDate() throws Excep
@Test
void testInvalidatesUriSpecifiedByContentLocationIfResponseHasMalformedDate() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -983,7 +983,7 @@ void testInvalidatesUriSpecifiedByContentLocationIfResponseHasMalformedDate() th
@Test
void testInvalidatesUriSpecifiedByContentLocationIfEntryHasMalformedDate() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestBasicHttpCache.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestBasicHttpCache.java
index 35f78ebe8..f091b7591 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestBasicHttpCache.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestBasicHttpCache.java
@@ -44,6 +44,7 @@
import java.util.stream.Collectors;
import org.apache.hc.client5.http.HeadersMatcher;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.utils.DateUtils;
@@ -53,7 +54,6 @@
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.message.BasicHeader;
-import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.BasicHttpResponse;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.util.ByteArrayBuffer;
@@ -82,7 +82,7 @@ void setUp() {
@Test
void testGetCacheEntryReturnsNullOnCacheMiss() {
final HttpHost host = new HttpHost("foo.example.com");
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
final CacheMatch result = impl.match(host, request);
assertNull(result);
}
@@ -92,7 +92,7 @@ void testGetCacheEntryFetchesFromCacheOnCacheHitIfNoVariants() {
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
assertFalse(entry.hasVariants());
final HttpHost host = new HttpHost("foo.example.com");
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
final String key = CacheKeyGenerator.INSTANCE.generateKey(host, request);
@@ -106,7 +106,7 @@ void testGetCacheEntryFetchesFromCacheOnCacheHitIfNoVariants() {
@Test
void testGetCacheEntryReturnsNullIfNoVariantInCache() {
- final HttpRequest origRequest = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
origRequest.setHeader("Accept-Encoding","gzip");
final ByteArrayBuffer buf = HttpTestUtils.makeRandomBuffer(128);
@@ -119,7 +119,7 @@ void testGetCacheEntryReturnsNullIfNoVariantInCache() {
impl.store(host, origRequest, origResponse, buf, now, now);
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
final CacheMatch result = impl.match(host, request);
assertNotNull(result);
assertNull(result.hit);
@@ -127,7 +127,7 @@ void testGetCacheEntryReturnsNullIfNoVariantInCache() {
@Test
void testGetCacheEntryReturnsVariantIfPresentInCache() {
- final HttpRequest origRequest = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
origRequest.setHeader("Accept-Encoding","gzip");
final ByteArrayBuffer buf = HttpTestUtils.makeRandomBuffer(128);
@@ -140,7 +140,7 @@ void testGetCacheEntryReturnsVariantIfPresentInCache() {
impl.store(host, origRequest, origResponse, buf, now, now);
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
request.setHeader("Accept-Encoding","gzip");
final CacheMatch result = impl.match(host, request);
assertNotNull(result);
@@ -149,7 +149,7 @@ void testGetCacheEntryReturnsVariantIfPresentInCache() {
@Test
void testGetCacheEntryReturnsVariantWithMostRecentDateHeader() {
- final HttpRequest origRequest = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
origRequest.setHeader("Accept-Encoding", "gzip");
final ByteArrayBuffer buf = HttpTestUtils.makeRandomBuffer(128);
@@ -171,7 +171,7 @@ void testGetCacheEntryReturnsVariantWithMostRecentDateHeader() {
impl.store(host, origRequest, origResponse1, buf, now, now);
impl.store(host, origRequest, origResponse2, buf, now, now);
- final HttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "http://foo.example.com/bar");
request.setHeader("Accept-Encoding", "gzip");
final CacheMatch result = impl.match(host, request);
assertNotNull(result);
@@ -212,7 +212,7 @@ void testGetVariantsRootNonExistentVariants() {
void testGetVariantCacheEntriesReturnsAllVariants() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
req1.setHeader("Accept-Encoding", "gzip");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(uri);
@@ -224,7 +224,7 @@ void testGetVariantCacheEntriesReturnsAllVariants() throws Exception {
resp1.setHeader("Vary", "Accept-Encoding");
resp1.setHeader("Content-Encoding","gzip");
- final HttpRequest req2 = new HttpGet(uri);
+ final SimpleHttpRequest req2 = new SimpleHttpRequest("GET", uri);
req2.setHeader("Accept-Encoding", "identity");
final HttpResponse resp2 = HttpTestUtils.make200Response();
@@ -255,7 +255,7 @@ void testGetVariantCacheEntriesReturnsAllVariants() throws Exception {
void testUpdateCacheEntry() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
final HttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
@@ -294,7 +294,7 @@ void testUpdateCacheEntry() throws Exception {
void testUpdateVariantCacheEntry() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
req1.setHeader("User-Agent", "agent1");
final HttpResponse resp1 = HttpTestUtils.make200Response();
@@ -336,7 +336,7 @@ void testUpdateVariantCacheEntry() throws Exception {
void testUpdateCacheEntryTurnsVariant() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
req1.setHeader("User-Agent", "agent1");
final HttpResponse resp1 = HttpTestUtils.make200Response();
@@ -377,7 +377,7 @@ void testUpdateCacheEntryTurnsVariant() throws Exception {
void testStoreFromNegotiatedVariant() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final URI uri = new URI("http://foo.example.com/bar");
- final HttpRequest req1 = new HttpGet(uri);
+ final SimpleHttpRequest req1 = new SimpleHttpRequest("GET", uri);
req1.setHeader("User-Agent", "agent1");
final HttpResponse resp1 = HttpTestUtils.make200Response();
@@ -392,7 +392,7 @@ void testStoreFromNegotiatedVariant() throws Exception {
Assertions.assertEquals(2, backing.map.size());
Assertions.assertSame(hit1.entry, backing.map.get(hit1.getEntryKey()));
- final HttpRequest req2 = new HttpGet(uri);
+ final SimpleHttpRequest req2 = new SimpleHttpRequest("GET", uri);
req2.setHeader("User-Agent", "agent2");
final HttpResponse resp2 = HttpTestUtils.make304Response();
@@ -416,7 +416,7 @@ void testStoreFromNegotiatedVariant() throws Exception {
@Test
void testInvalidatesUnsafeRequests() throws Exception {
- final HttpRequest request = new BasicHttpRequest("POST","/path");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "POST","/path");
final String key = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final HttpResponse response = HttpTestUtils.make200Response();
@@ -433,14 +433,14 @@ void testInvalidatesUnsafeRequests() throws Exception {
@Test
void testDoesNotInvalidateSafeRequests() {
- final HttpRequest request1 = new BasicHttpRequest("GET","/");
+ final SimpleHttpRequest request1 = new SimpleHttpRequest( "GET","/");
final HttpResponse response1 = HttpTestUtils.make200Response();
impl.evictInvalidatedEntries(host, request1, response1);
verifyNoMoreInteractions(backing);
- final HttpRequest request2 = new BasicHttpRequest("HEAD","/");
+ final SimpleHttpRequest request2 = new SimpleHttpRequest("HEAD","/");
final HttpResponse response2 = HttpTestUtils.make200Response();
impl.evictInvalidatedEntries(host, request2, response2);
@@ -449,7 +449,7 @@ void testDoesNotInvalidateSafeRequests() {
@Test
void testInvalidatesUnsafeRequestsWithVariants() throws Exception {
- final HttpRequest request = new BasicHttpRequest("POST","/path");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "POST","/path");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final Set<String> variants = new HashSet<>();
variants.add("{var1}");
@@ -477,7 +477,7 @@ void testInvalidatesUnsafeRequestsWithVariants() throws Exception {
@Test
void testInvalidateUriSpecifiedByContentLocationAndFresher() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final URI contentUri = new URIBuilder()
.setHttpHost(host)
@@ -506,7 +506,7 @@ void testInvalidateUriSpecifiedByContentLocationAndFresher() throws Exception {
@Test
void testInvalidateUriSpecifiedByLocationAndFresher() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final URI contentUri = new URIBuilder()
.setHttpHost(host)
@@ -535,7 +535,7 @@ void testInvalidateUriSpecifiedByLocationAndFresher() throws Exception {
@Test
void testDoesNotInvalidateForUnsuccessfulResponse() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -552,7 +552,7 @@ void testDoesNotInvalidateForUnsuccessfulResponse() throws Exception {
@Test
void testInvalidateUriSpecifiedByContentLocationNonCanonical() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final URI contentUri = new URIBuilder()
.setHttpHost(host)
@@ -584,7 +584,7 @@ void testInvalidateUriSpecifiedByContentLocationNonCanonical() throws Exception
@Test
void testInvalidateUriSpecifiedByContentLocationRelative() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final String rootKey = CacheKeyGenerator.INSTANCE.generateKey(host, request);
final URI contentUri = new URIBuilder()
.setHttpHost(host)
@@ -616,7 +616,7 @@ void testInvalidateUriSpecifiedByContentLocationRelative() throws Exception {
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationOtherOrigin() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/");
final URI contentUri = new URIBuilder()
.setHost("bar.example.com")
.setPath("/")
@@ -638,7 +638,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationOtherOrigin() throws Exce
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationIfEtagsMatch() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -662,7 +662,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationIfEtagsMatch() throws Exc
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationIfOlder() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -686,7 +686,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationIfOlder() throws Exceptio
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationIfResponseHasNoEtag() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -710,7 +710,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationIfResponseHasNoEtag() thr
@Test
void testDoesNotInvalidateUriSpecifiedByContentLocationIfEntryHasNoEtag() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -733,7 +733,7 @@ void testDoesNotInvalidateUriSpecifiedByContentLocationIfEntryHasNoEtag() throws
@Test
void testInvalidatesUriSpecifiedByContentLocationIfResponseHasNoDate() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -757,7 +757,7 @@ void testInvalidatesUriSpecifiedByContentLocationIfResponseHasNoDate() throws Ex
@Test
void testInvalidatesUriSpecifiedByContentLocationIfEntryHasNoDate() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -780,7 +780,7 @@ void testInvalidatesUriSpecifiedByContentLocationIfEntryHasNoDate() throws Excep
@Test
void testInvalidatesUriSpecifiedByContentLocationIfResponseHasMalformedDate() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
@@ -804,7 +804,7 @@ void testInvalidatesUriSpecifiedByContentLocationIfResponseHasMalformedDate() th
@Test
void testInvalidatesUriSpecifiedByContentLocationIfEntryHasMalformedDate() throws Exception {
- final HttpRequest request = new BasicHttpRequest("PUT", "/foo");
+ final SimpleHttpRequest request = new SimpleHttpRequest( "PUT", "/foo");
final URI contentUri = new URIBuilder()
.setHttpHost(host)
.setPath("/bar")
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
index c76b1ece0..4af20bb94 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
@@ -39,7 +39,9 @@
import java.time.temporal.ChronoUnit;
import org.apache.hc.client5.http.HttpRoute;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
+import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.auth.StandardAuthScheme;
import org.apache.hc.client5.http.cache.CacheResponseStatus;
import org.apache.hc.client5.http.cache.HttpCacheContext;
@@ -117,6 +119,7 @@ public ClassicHttpResponse execute(final ClassicHttpRequest request) throws IOEx
@Test
void testCacheableResponsesGoIntoCache() throws Exception {
final ClassicHttpRequest req1 = HttpTestUtils.makeDefaultRequest();
+ final SimpleHttpRequest cacheRequest1 = SimpleRequestBuilder.copy(req1).build();
final ClassicHttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Cache-Control", "max-age=3600");
@@ -128,7 +131,7 @@ void testCacheableResponsesGoIntoCache() throws Exception {
execute(req2);
Mockito.verify(mockExecChain).proceed(Mockito.any(), Mockito.any());
- Mockito.verify(cache).store(Mockito.eq(host), RequestEquivalent.eq(req1),
+ Mockito.verify(cache).store(Mockito.eq(host), RequestEquivalent.eq(cacheRequest1),
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
}
@@ -889,6 +892,7 @@ public int read() throws IOException {
void testTooLargeResponsesAreNotCached() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final ClassicHttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest cacheRequest = SimpleRequestBuilder.copy(request).build();
final Instant now = Instant.now();
final Instant requestSent = now.plusSeconds(3);
@@ -901,7 +905,7 @@ void testTooLargeResponsesAreNotCached() throws Exception {
originResponse.setHeader("Date", DateUtils.formatStandardDate(responseGenerated));
originResponse.setHeader("ETag", "\"etag\"");
- impl.cacheAndReturnResponse(host, request, scope, originResponse, requestSent, responseReceived);
+ impl.cacheAndReturnResponse(host, cacheRequest, scope, originResponse, requestSent, responseReceived);
Mockito.verify(cache, Mockito.never()).store(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
@@ -914,6 +918,7 @@ void testSmallEnoughResponsesAreCached() throws Exception {
final HttpHost host = new HttpHost("foo.example.com");
final ClassicHttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest cacheRequest = SimpleRequestBuilder.copy(request).build();
final Instant now = Instant.now();
final Instant requestSent = now.plusSeconds(3);
@@ -931,13 +936,13 @@ void testSmallEnoughResponsesAreCached() throws Exception {
Mockito.when(mockCache.store(
Mockito.eq(host),
- RequestEquivalent.eq(request),
+ RequestEquivalent.eq(cacheRequest),
ResponseEquivalent.eq(response),
Mockito.any(),
Mockito.eq(requestSent),
Mockito.eq(responseReceived))).thenReturn(new CacheHit("key", httpCacheEntry));
- impl.cacheAndReturnResponse(host, request, scope, originResponse, requestSent, responseReceived);
+ impl.cacheAndReturnResponse(host, cacheRequest, scope, originResponse, requestSent, responseReceived);
Mockito.verify(mockCache).store(
Mockito.any(),
@@ -1202,10 +1207,11 @@ void testNotModifiedResponseUpdatesCacheEntry() throws Exception {
// Prepare request and host
final HttpHost host = new HttpHost("foo.example.com");
final ClassicHttpRequest request = new HttpGet("http://foo.example.com/bar");
+ final SimpleHttpRequest cacheRequest = SimpleRequestBuilder.copy(request).build();
// Prepare original cache entry
final HttpCacheEntry originalEntry = HttpTestUtils.makeCacheEntry();
- Mockito.when(mockCache.match(host, request)).thenReturn(
+ Mockito.when(mockCache.match(host, cacheRequest)).thenReturn(
new CacheMatch(new CacheHit("key", originalEntry), null));
// Prepare 304 Not Modified response
@@ -1234,13 +1240,13 @@ void testNotModifiedResponseUpdatesCacheEntry() throws Exception {
.thenReturn(new CacheHit("key", cacheEntry));
// Call cacheAndReturnResponse with 304 Not Modified response
- final ClassicHttpResponse cachedResponse = impl.cacheAndReturnResponse(host, request, scope, backendResponse, requestSent, responseReceived);
+ final ClassicHttpResponse cachedResponse = impl.cacheAndReturnResponse(host, cacheRequest, scope, backendResponse, requestSent, responseReceived);
// Verify cache entry is updated
Mockito.verify(mockCache).update(
Mockito.any(),
Mockito.same(host),
- Mockito.same(request),
+ Mockito.same(cacheRequest),
Mockito.same(backendResponse),
Mockito.eq(requestSent),
Mockito.eq(responseReceived)
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestConditionalRequestBuilder.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestConditionalRequestBuilder.java
index dc11a292b..b40d564e4 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestConditionalRequestBuilder.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestConditionalRequestBuilder.java
@@ -34,6 +34,7 @@
import java.util.List;
import org.apache.hc.client5.http.HeadersMatcher;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.RequestCacheControl;
import org.apache.hc.client5.http.cache.ResponseCacheControl;
@@ -43,31 +44,27 @@
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.message.BasicHeader;
-import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.MessageSupport;
-import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class TestConditionalRequestBuilder {
- private ConditionalRequestBuilder<HttpRequest> impl;
- private HttpRequest request;
+ private ConditionalRequestBuilder impl;
@BeforeEach
void setUp() {
- impl = new ConditionalRequestBuilder<>(request -> BasicRequestBuilder.copy(request).build());
- request = new BasicHttpRequest("GET", "/");
+ impl = new ConditionalRequestBuilder();
}
@Test
- void testBuildConditionalRequestWithLastModified() {
+ void testMakeConditionalWithLastModified() {
final String theMethod = "GET";
final String theUri = "/theuri";
final String lastModified = "this is my last modified date";
- final HttpRequest basicRequest = new BasicHttpRequest(theMethod, theUri);
+ final SimpleHttpRequest basicRequest = new SimpleHttpRequest(theMethod, theUri);
basicRequest.addHeader("Accept-Encoding", "gzip");
final Header[] headers = new Header[] {
@@ -76,7 +73,7 @@ void testBuildConditionalRequestWithLastModified() {
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
final ResponseCacheControl cacheControl = ResponseCacheControl.builder().build();
- final HttpRequest newRequest = impl.buildConditionalRequest(cacheControl, basicRequest, cacheEntry);
+ final SimpleHttpRequest newRequest = impl.makeConditional(cacheControl, basicRequest, cacheEntry);
Assertions.assertEquals(theMethod, newRequest.getMethod());
Assertions.assertEquals(theUri, newRequest.getRequestUri());
@@ -100,10 +97,10 @@ void testConditionalRequestForEntryWithLastModifiedAndEtagIncludesBothAsValidato
new BasicHeader("Last-Modified", lmDate),
new BasicHeader("ETag", etag)
};
- final HttpRequest basicRequest = new BasicHttpRequest("GET", "/");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "/");
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
final ResponseCacheControl cacheControl = ResponseCacheControl.builder().build();
- final HttpRequest result = impl.buildConditionalRequest(cacheControl, basicRequest, cacheEntry);
+ final HttpRequest result = impl.makeConditional(cacheControl, origRequest, cacheEntry);
Assertions.assertEquals(lmDate,
result.getFirstHeader("If-Modified-Since").getValue());
Assertions.assertEquals(etag,
@@ -111,13 +108,13 @@ void testConditionalRequestForEntryWithLastModifiedAndEtagIncludesBothAsValidato
}
@Test
- void testBuildConditionalRequestWithETag() {
+ void testMakeConditionalWithETag() {
final String theMethod = "GET";
final String theUri = "/theuri";
final String theETag = "\"this is my eTag\"";
- final HttpRequest basicRequest = new BasicHttpRequest(theMethod, theUri);
- basicRequest.addHeader("Accept-Encoding", "gzip");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest(theMethod, theUri);
+ origRequest.addHeader("Accept-Encoding", "gzip");
final Instant now = Instant.now();
@@ -129,7 +126,7 @@ void testBuildConditionalRequestWithETag() {
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
final ResponseCacheControl cacheControl = ResponseCacheControl.builder().build();
- final HttpRequest newRequest = impl.buildConditionalRequest(cacheControl, basicRequest, cacheEntry);
+ final HttpRequest newRequest = impl.makeConditional(cacheControl, origRequest, cacheEntry);
Assertions.assertEquals(theMethod, newRequest.getMethod());
Assertions.assertEquals(theUri, newRequest.getRequestUri());
@@ -143,7 +140,7 @@ void testBuildConditionalRequestWithETag() {
@Test
void testCacheEntryWithMustRevalidateDoesEndToEndRevalidation() {
- final HttpRequest basicRequest = new BasicHttpRequest("GET","/");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET","/");
final Instant now = Instant.now();
final Instant elevenSecondsAgo = now.minusSeconds(11);
final Instant tenSecondsAgo = now.minusSeconds(10);
@@ -159,7 +156,7 @@ void testCacheEntryWithMustRevalidateDoesEndToEndRevalidation() {
.setMaxAge(5)
.setMustRevalidate(true)
.build();
- final HttpRequest result = impl.buildConditionalRequest(responseCacheControl, basicRequest, cacheEntry);
+ final HttpRequest result = impl.makeConditional(responseCacheControl, origRequest, cacheEntry);
final RequestCacheControl requestCacheControl = CacheControlHeaderParser.INSTANCE.parse(result);
Assertions.assertEquals(0, requestCacheControl.getMaxAge());
@@ -167,7 +164,7 @@ void testCacheEntryWithMustRevalidateDoesEndToEndRevalidation() {
@Test
void testCacheEntryWithProxyRevalidateDoesEndToEndRevalidation() {
- final HttpRequest basicRequest = new BasicHttpRequest("GET", "/");
+ final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "/");
final Instant now = Instant.now();
final Instant elevenSecondsAgo = now.minusSeconds(11);
final Instant tenSecondsAgo = now.minusSeconds(10);
@@ -183,7 +180,7 @@ void testCacheEntryWithProxyRevalidateDoesEndToEndRevalidation() {
.setMaxAge(5)
.setProxyRevalidate(true)
.build();
- final HttpRequest result = impl.buildConditionalRequest(responseCacheControl, basicRequest, cacheEntry);
+ final HttpRequest result = impl.makeConditional(responseCacheControl, origRequest, cacheEntry);
final RequestCacheControl requestCacheControl = CacheControlHeaderParser.INSTANCE.parse(result);
Assertions.assertEquals(0, requestCacheControl.getMaxAge());
@@ -191,77 +188,86 @@ void testCacheEntryWithProxyRevalidateDoesEndToEndRevalidation() {
@Test
void testBuildUnconditionalRequestUsesGETMethod() {
- final HttpRequest result = impl.buildUnconditionalRequest(request);
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
+ final SimpleHttpRequest result = impl.makeUnconditional(request);
Assertions.assertEquals("GET", result.getMethod());
}
@Test
void testBuildUnconditionalRequestUsesRequestUri() {
final String uri = "/theURI";
- request = new BasicHttpRequest("GET", uri);
- final HttpRequest result = impl.buildUnconditionalRequest(request);
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", uri);
+ final SimpleHttpRequest result = impl.makeUnconditional(request);
Assertions.assertEquals(uri, result.getRequestUri());
}
@Test
void testBuildUnconditionalRequestAddsCacheControlNoCache() {
- final HttpRequest result = impl.buildUnconditionalRequest(request);
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
+ final SimpleHttpRequest result = impl.makeUnconditional(request);
final RequestCacheControl requestCacheControl = CacheControlHeaderParser.INSTANCE.parse(result);
Assertions.assertTrue(requestCacheControl.isNoCache());
}
@Test
void testBuildUnconditionalRequestDoesNotUseIfRange() {
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
request.addHeader("If-Range","\"etag\"");
- final HttpRequest result = impl.buildUnconditionalRequest(request);
+ final SimpleHttpRequest result = impl.makeUnconditional(request);
Assertions.assertNull(result.getFirstHeader("If-Range"));
}
@Test
void testBuildUnconditionalRequestDoesNotUseIfMatch() {
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
request.addHeader("If-Match","\"etag\"");
- final HttpRequest result = impl.buildUnconditionalRequest(request);
+ final SimpleHttpRequest result = impl.makeUnconditional(request);
Assertions.assertNull(result.getFirstHeader("If-Match"));
}
@Test
void testBuildUnconditionalRequestDoesNotUseIfNoneMatch() {
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
request.addHeader("If-None-Match","\"etag\"");
- final HttpRequest result = impl.buildUnconditionalRequest(request);
+ final SimpleHttpRequest result = impl.makeUnconditional(request);
Assertions.assertNull(result.getFirstHeader("If-None-Match"));
}
@Test
void testBuildUnconditionalRequestDoesNotUseIfUnmodifiedSince() {
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
request.addHeader("If-Unmodified-Since", DateUtils.formatStandardDate(Instant.now()));
- final HttpRequest result = impl.buildUnconditionalRequest(request);
+ final SimpleHttpRequest result = impl.makeUnconditional(request);
Assertions.assertNull(result.getFirstHeader("If-Unmodified-Since"));
}
@Test
void testBuildUnconditionalRequestDoesNotUseIfModifiedSince() {
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
request.addHeader("If-Modified-Since", DateUtils.formatStandardDate(Instant.now()));
- final HttpRequest result = impl.buildUnconditionalRequest(request);
+ final SimpleHttpRequest result = impl.makeUnconditional(request);
Assertions.assertNull(result.getFirstHeader("If-Modified-Since"));
}
@Test
void testBuildUnconditionalRequestCarriesOtherRequestHeaders() {
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
request.addHeader("User-Agent","MyBrowser/1.0");
- final HttpRequest result = impl.buildUnconditionalRequest(request);
+ final SimpleHttpRequest result = impl.makeUnconditional(request);
Assertions.assertEquals("MyBrowser/1.0",
result.getFirstHeader("User-Agent").getValue());
}
@Test
- void testBuildConditionalRequestFromVariants() {
+ void testMakeConditionalFromVariants() {
final ETag etag1 = new ETag("123");
final ETag etag2 = new ETag("456");
final ETag etag3 = new ETag("789");
final List<ETag> variantEntries = Arrays.asList(etag1, etag2, etag3);
- final HttpRequest conditional = impl.buildConditionalRequestFromVariants(request, variantEntries);
+ final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
+ final SimpleHttpRequest conditional = impl.makeConditionalRequestVariants(request, variantEntries);
final Iterator<String> it = MessageSupport.iterateTokens(conditional, HttpHeaders.IF_NONE_MATCH);
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRequirements.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRequirements.java
index 416965372..1e96940ac 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRequirements.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRequirements.java
@@ -36,6 +36,8 @@
import java.util.Random;
import org.apache.hc.client5.http.HttpRoute;
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
+import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.auth.StandardAuthScheme;
import org.apache.hc.client5.http.cache.HttpCacheContext;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
@@ -551,6 +553,7 @@ void testMustReturnACacheEntryIfItCanRevalidateIt() throws Exception {
impl = new CachingExec(mockCache, null, config);
request = new BasicClassicHttpRequest("GET", "/thing");
+ final SimpleHttpRequest cacheRequest = SimpleRequestBuilder.copy(request).build();
final ClassicHttpRequest validate = new BasicClassicHttpRequest("GET", "/thing");
validate.setHeader("If-None-Match", "\"etag\"");
@@ -559,7 +562,7 @@ void testMustReturnACacheEntryIfItCanRevalidateIt() throws Exception {
notModified.setHeader("Date", DateUtils.formatStandardDate(now));
notModified.setHeader("ETag", "\"etag\"");
- Mockito.when(mockCache.match(Mockito.eq(host), RequestEquivalent.eq(request))).thenReturn(
+ Mockito.when(mockCache.match(Mockito.eq(host), RequestEquivalent.eq(cacheRequest))).thenReturn(
new CacheMatch(new CacheHit("key", entry), null));
Mockito.when(mockExecChain.proceed(RequestEquivalent.eq(validate), Mockito.any())).thenReturn(notModified);
final HttpCacheEntry updated = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo,
@@ -582,7 +585,7 @@ void testMustReturnACacheEntryIfItCanRevalidateIt() throws Exception {
Mockito.verify(mockCache).update(
Mockito.any(),
Mockito.eq(host),
- RequestEquivalent.eq(request),
+ RequestEquivalent.eq(cacheRequest),
ResponseEquivalent.eq(notModified),
Mockito.any(),
Mockito.any());
@@ -609,8 +612,9 @@ void testMustReturnAFreshEnoughCacheEntryIfItHasIt() throws Exception {
impl = new CachingExec(mockCache, null, config);
request = new BasicClassicHttpRequest("GET", "/thing");
+ final SimpleHttpRequest cacheRequest = SimpleRequestBuilder.copy(request).build();
- Mockito.when(mockCache.match(Mockito.eq(host), RequestEquivalent.eq(request))).thenReturn(
+ Mockito.when(mockCache.match(Mockito.eq(host), RequestEquivalent.eq(cacheRequest))).thenReturn(
new CacheMatch(new CacheHit("key", entry), null));
final ClassicHttpResponse result = execute(request);
@@ -639,8 +643,9 @@ void testAgeHeaderPopulatedFromCacheEntryCurrentAge() throws Exception {
impl = new CachingExec(mockCache, null, config);
request = new BasicClassicHttpRequest("GET", "/");
+ final SimpleHttpRequest cacheRequest = SimpleRequestBuilder.copy(request).build();
- Mockito.when(mockCache.match(Mockito.eq(host), RequestEquivalent.eq(request))).thenReturn(
+ Mockito.when(mockCache.match(Mockito.eq(host), RequestEquivalent.eq(cacheRequest))).thenReturn(
new CacheMatch(new CacheHit("key", entry), null));
final ClassicHttpResponse result = execute(request);
(httpcomponents-client) 02/03: Replaced deprecated method with an equivalent one from PathSegmentCodec
Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ok2c pushed a commit to branch caching_redesign
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git
commit c3fc85663ca57d639b460282cfbacc0a5ade69ac
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sun Jul 12 17:01:55 2026 +0200
Replaced deprecated method with an equivalent one from PathSegmentCodec
---
.../java/org/apache/hc/client5/http/rest/ResourceIfaceScanner.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/httpclient5-jakarta-rest-client/src/main/java/org/apache/hc/client5/http/rest/ResourceIfaceScanner.java b/httpclient5-jakarta-rest-client/src/main/java/org/apache/hc/client5/http/rest/ResourceIfaceScanner.java
index 21d26f166..1be23a429 100644
--- a/httpclient5-jakarta-rest-client/src/main/java/org/apache/hc/client5/http/rest/ResourceIfaceScanner.java
+++ b/httpclient5-jakarta-rest-client/src/main/java/org/apache/hc/client5/http/rest/ResourceIfaceScanner.java
@@ -47,7 +47,7 @@
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.message.MessageSupport;
import org.apache.hc.core5.http.message.ParserCursor;
-import org.apache.hc.core5.net.URLEncodedUtils;
+import org.apache.hc.core5.net.PathSegmentCodec;
import org.apache.hc.core5.util.Args;
final class ResourceIfaceScanner {
@@ -110,7 +110,7 @@ static List<PathSegment> parsePathSegments(final String value) {
if (value == null) {
return Collections.emptyList();
}
- return URLEncodedUtils.parsePathSegments(value).stream()
+ return PathSegmentCodec.parsePathSegments(value).stream()
.map(e -> {
if (e.startsWith("{") && e.endsWith("}")) {
final String param = e.substring(1, e.length() - 1);