Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.apache.hc.core5.http.nio.DataStreamChannel;
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.ByteArrayBuffer;
Expand Down Expand Up @@ -534,8 +535,10 @@ void callChain(
} else {
entityProducer = null;
}
final HttpRequest backendRequest = BasicRequestBuilder.copy(request).build();
scope.clientContext.setRequest(backendRequest);
try {
chain.proceed(request, entityProducer, scope, asyncExecCallback);
chain.proceed(backendRequest, entityProducer, scope, asyncExecCallback);
} catch (final HttpException | IOException ex) {
asyncExecCallback.failed(ex);
}
Expand Down Expand Up @@ -753,7 +756,8 @@ public void cancelled() {
context.setResponseCacheControl(responseCacheControl);
final boolean cacheable = responseCachingPolicy.isResponseCacheable(
requestCacheControl, responseCacheControl, request, backendResponse,
isTargetAuthenticated(context, scope.route));
isTargetAuthenticated(context, scope.route))
&& isVaryConsistent(request, backendResponse, context);
if (cacheable) {
storeRequestIfModifiedSinceFor304Response(request, backendResponse);
if (LOG.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ ClassicHttpResponse callChain(
final ClassicHttpRequest backendRequest = ClassicRequestBuilder.copy(request)
.setEntity(entity)
.build();
scope.clientContext.setRequest(backendRequest);
return chain.proceed(backendRequest, scope);
}

Expand Down Expand Up @@ -553,7 +554,8 @@ ClassicHttpResponse handleBackendResponse(
context.setResponseCacheControl(responseCacheControl);
final boolean cacheable = responseCachingPolicy.isResponseCacheable(
requestCacheControl, responseCacheControl, request, backendResponse,
isTargetAuthenticated(context, scope.route));
isTargetAuthenticated(context, scope.route))
&& isVaryConsistent(request, backendResponse, context);
if (cacheable) {
storeRequestIfModifiedSinceFor304Response(request, backendResponse);
if (LOG.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package org.apache.hc.client5.http.impl.cache;

import java.time.Instant;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.hc.client5.http.HttpRoute;
Expand Down Expand Up @@ -138,6 +139,26 @@ static boolean isTargetAuthenticated(final HttpClientContext context, final Http
return authExchange != null && authExchange.getState() == AuthExchange.State.SUCCESS;
}

/**
* Determines whether the request headers nominated by the {@code Vary} header of
* the response have the same values in the request seen by the cache and in the
* request last sent to the backend. Headers such as {@code Cookie} are added by the
* protocol layer below the cache, so a response varying on them would otherwise be
* stored under the variant of a request that did not carry them.
*/
static boolean isVaryConsistent(
final HttpRequest request,
final HttpResponse backendResponse,
final HttpClientContext context) {
final HttpRequest sentRequest = context.getRequest();
if (sentRequest == null || sentRequest == request || !backendResponse.containsHeader(HttpHeaders.VARY)) {
return true;
}
final List<String> variantNames = CacheKeyGenerator.variantNames(backendResponse);
return CacheKeyGenerator.INSTANCE.generateVariantKey(request, variantNames).equals(
CacheKeyGenerator.INSTANCE.generateVariantKey(sentRequest, variantNames));
}

SimpleHttpResponse generateCachedResponse(
final HttpRequest request,
final HttpCacheEntry entry,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.testing.caching.async;

import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicInteger;

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.HttpCacheContext;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.cache.HeapResourceFactory;
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
import org.apache.hc.client5.testing.async.AbstractSimpleServerExchangeHandler;
import org.apache.hc.client5.testing.extension.async.CachingHttpAsyncClientResource;
import org.apache.hc.client5.testing.extension.async.ServerProtocolLevel;
import org.apache.hc.client5.testing.extension.async.TestAsyncServerResources;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.util.Timeout;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

/**
* A response that varies on the Cookie header must not be served to an
* execution context that sends different cookies.
*/
class TestCachingAsyncClientCookies {

static final Timeout TIMEOUT = Timeout.ofMinutes(1);

@RegisterExtension
private final TestAsyncServerResources serverResources;
@RegisterExtension
private final CachingHttpAsyncClientResource clientResource;

private final AtomicInteger originHits = new AtomicInteger();

TestCachingAsyncClientCookies() throws Exception {
this.serverResources = new TestAsyncServerResources(URIScheme.HTTP, TIMEOUT, ServerProtocolLevel.STANDARD);
this.serverResources.configureServer(bootstrap -> bootstrap
.register("*", () -> new AbstractSimpleServerExchangeHandler() {

@Override
protected SimpleHttpResponse handle(
final SimpleHttpRequest request,
final HttpCoreContext context) {
originHits.incrementAndGet();
final Header cookie = request.getFirstHeader(HttpHeaders.COOKIE);
final SimpleHttpResponse response = new SimpleHttpResponse(HttpStatus.SC_OK);
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=60");
response.setHeader(HttpHeaders.VARY, HttpHeaders.COOKIE);
response.setBody(
"page for " + (cookie != null ? cookie.getValue() : "anonymous"),
ContentType.TEXT_PLAIN);
return response;
}

}));
this.clientResource = new CachingHttpAsyncClientResource(HttpVersionPolicy.FORCE_HTTP_1);
this.clientResource.configure(builder -> builder
.setResourceFactory(HeapResourceFactory.INSTANCE));
}

HttpHost startServer() throws Exception {
final InetSocketAddress address = serverResources.server().start();
return new HttpHost(serverResources.scheme().id, "localhost", address.getPort());
}

static HttpCacheContext createContext(final String session) {
final HttpCacheContext context = HttpCacheContext.create();
final BasicCookieStore cookieStore = new BasicCookieStore();
if (session != null) {
final BasicClientCookie cookie = new BasicClientCookie("SESSION", session);
cookie.setDomain("localhost");
cookie.setPath("/");
cookieStore.addCookie(cookie);
}
context.setCookieStore(cookieStore);
return context;
}

void assertPage(
final CloseableHttpAsyncClient client,
final HttpHost target,
final HttpCacheContext context,
final String expected) throws Exception {
final SimpleHttpResponse response = client.execute(SimpleRequestBuilder.get()
.setHttpHost(target)
.setPath("/home")
.build(), context, null)
.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
Assertions.assertEquals(expected, response.getBodyText());
}

@Test
void testResponseVaryingOnCookieNotServedToContextWithOtherCookies() throws Exception {
final HttpHost target = startServer();
final CloseableHttpAsyncClient client = clientResource.client();

assertPage(client, target, createContext("alice"), "page for SESSION=alice");
assertPage(client, target, createContext("bob"), "page for SESSION=bob");
assertPage(client, target, createContext(null), "page for anonymous");
Assertions.assertEquals(3, originHits.get());

assertPage(client, target, createContext(null), "page for anonymous");
Assertions.assertEquals(3, originHits.get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.testing.caching.sync;

import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.hc.client5.http.cache.HttpCacheContext;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.impl.cache.HeapResourceFactory;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
import org.apache.hc.client5.testing.extension.sync.CachingHttpClientResource;
import org.apache.hc.client5.testing.extension.sync.TestServerResources;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.util.Timeout;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

/**
* A response that varies on the Cookie header must not be served to an
* execution context that sends different cookies.
*/
class TestCachingClientCookies {

static final Timeout TIMEOUT = Timeout.ofMinutes(1);

@RegisterExtension
private final TestServerResources serverResources;
@RegisterExtension
private final CachingHttpClientResource clientResource;

private final AtomicInteger originHits = new AtomicInteger();

TestCachingClientCookies() throws Exception {
this.serverResources = new TestServerResources(URIScheme.HTTP, TIMEOUT);
this.serverResources.configureServer(bootstrap -> bootstrap
.register("*", (request, response, context) -> {
originHits.incrementAndGet();
final Header cookie = request.getFirstHeader(HttpHeaders.COOKIE);
response.setCode(HttpStatus.SC_OK);
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=60");
response.setHeader(HttpHeaders.VARY, HttpHeaders.COOKIE);
response.setEntity(new StringEntity(
"page for " + (cookie != null ? cookie.getValue() : "anonymous"),
ContentType.TEXT_PLAIN));
}));
this.clientResource = new CachingHttpClientResource();
this.clientResource.configure(builder -> builder
.setResourceFactory(HeapResourceFactory.INSTANCE));
}

HttpHost startServer() throws Exception {
final InetSocketAddress address = serverResources.server().start();
return new HttpHost(serverResources.scheme().id, "localhost", address.getPort());
}

static HttpCacheContext createContext(final String session) {
final HttpCacheContext context = HttpCacheContext.create();
final BasicCookieStore cookieStore = new BasicCookieStore();
if (session != null) {
final BasicClientCookie cookie = new BasicClientCookie("SESSION", session);
cookie.setDomain("localhost");
cookie.setPath("/");
cookieStore.addCookie(cookie);
}
context.setCookieStore(cookieStore);
return context;
}

static void assertPage(
final CloseableHttpClient client,
final HttpHost target,
final HttpCacheContext context,
final String expected) throws Exception {
client.execute(target, new HttpGet("/home"), context, response -> {
Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
Assertions.assertEquals(expected, EntityUtils.toString(response.getEntity()));

Check warning on line 111 in httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientCookies.java

View workflow job for this annotation

GitHub Actions / build (macos-latest, 11)

[deprecation] toString(HttpEntity) in EntityUtils has been deprecated

Check warning on line 111 in httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientCookies.java

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 25)

[deprecation] toString(HttpEntity) in EntityUtils has been deprecated

Check warning on line 111 in httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientCookies.java

View workflow job for this annotation

GitHub Actions / build (macos-latest, 25)

[deprecation] toString(HttpEntity) in EntityUtils has been deprecated

Check warning on line 111 in httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientCookies.java

View workflow job for this annotation

GitHub Actions / build (8, ubuntu-latest)

[deprecation] toString(HttpEntity) in EntityUtils has been deprecated

Check warning on line 111 in httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientCookies.java

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 21)

[deprecation] toString(HttpEntity) in EntityUtils has been deprecated

Check warning on line 111 in httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientCookies.java

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 11)

[deprecation] toString(HttpEntity) in EntityUtils has been deprecated

Check warning on line 111 in httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientCookies.java

View workflow job for this annotation

GitHub Actions / build (macos-latest, 17)

[deprecation] toString(HttpEntity) in EntityUtils has been deprecated

Check warning on line 111 in httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientCookies.java

View workflow job for this annotation

GitHub Actions / build (macos-latest, 21)

[deprecation] toString(HttpEntity) in EntityUtils has been deprecated

Check warning on line 111 in httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientCookies.java

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 17)

[deprecation] toString(HttpEntity) in EntityUtils has been deprecated
return null;
});
}

@Test
void testResponseVaryingOnCookieNotServedToContextWithOtherCookies() throws Exception {
final HttpHost target = startServer();
final CloseableHttpClient client = clientResource.client();

assertPage(client, target, createContext("alice"), "page for SESSION=alice");
assertPage(client, target, createContext("bob"), "page for SESSION=bob");
assertPage(client, target, createContext(null), "page for anonymous");
Assertions.assertEquals(3, originHits.get());

assertPage(client, target, createContext(null), "page for anonymous");
Assertions.assertEquals(3, originHits.get());
}

}
Loading