Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add splitting of comma separated cache-control values #3432

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -23,6 +23,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -60,6 +61,8 @@ public class ResponseCacheManager {

private static final String VARY_WILDCARD = "*";

private static final Pattern CACHE_CONTROL_SPLITTER = Pattern.compile("\\s*,\\s*");

final CacheKeyGenerator cacheKeyGenerator;

final List<AfterCacheExchangeMutator> afterCacheExchangeMutators;
Expand Down Expand Up @@ -193,8 +196,8 @@ private boolean isVaryWildcard(ServerHttpResponse response) {
private boolean isCacheControlAllowed(HttpMessage request) {
HttpHeaders headers = request.getHeaders();
List<String> cacheControlHeader = headers.getOrEmpty(HttpHeaders.CACHE_CONTROL);

return cacheControlHeader.stream().noneMatch(forbiddenCacheControlValues::contains);
return cacheControlHeader.stream().flatMap(CACHE_CONTROL_SPLITTER::splitAsStream)
.noneMatch(forbiddenCacheControlValues::contains);
}

private static boolean hasRequestBody(ServerHttpRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ void responseShouldNotBeCacheable() {
response.setStatusCode(HttpStatus.OK);

assertThat(cacheManagerToTest.isResponseCacheable(response)).isFalse();

headers = new HttpHeaders();
headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate");
response = new MockServerHttpResponse();
response.getHeaders().putAll(headers);
response.setStatusCode(HttpStatus.OK);

assertThat(cacheManagerToTest.isResponseCacheable(response)).isFalse();
}

}