1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.impl.cache;
28
29 import java.net.URI;
30
31 import org.apache.hc.client5.http.cache.HeaderConstants;
32 import org.apache.hc.client5.http.cache.HttpCacheEntry;
33 import org.apache.hc.client5.http.utils.URIUtils;
34 import org.apache.hc.core5.http.Header;
35 import org.apache.hc.core5.http.HttpHeaders;
36 import org.apache.hc.core5.http.HttpRequest;
37 import org.apache.hc.core5.http.HttpResponse;
38
39 class CacheInvalidatorBase {
40
41 static boolean shouldInvalidateHeadCacheEntry(final HttpRequest req, final HttpCacheEntry parentCacheEntry) {
42 return requestIsGet(req) && isAHeadCacheEntry(parentCacheEntry);
43 }
44
45 static boolean requestIsGet(final HttpRequest req) {
46 return req.getMethod().equals((HeaderConstants.GET_METHOD));
47 }
48
49 static boolean isAHeadCacheEntry(final HttpCacheEntry parentCacheEntry) {
50 return parentCacheEntry != null && parentCacheEntry.getRequestMethod().equals(HeaderConstants.HEAD_METHOD);
51 }
52
53 static boolean isSameHost(final URI requestURI, final URI targetURI) {
54 return targetURI.isAbsolute() && targetURI.getAuthority().equalsIgnoreCase(requestURI.getAuthority());
55 }
56
57 static boolean requestShouldNotBeCached(final HttpRequest req) {
58 final String method = req.getMethod();
59 return notGetOrHeadRequest(method);
60 }
61
62 static boolean notGetOrHeadRequest(final String method) {
63 return !(HeaderConstants.GET_METHOD.equals(method) || HeaderConstants.HEAD_METHOD.equals(method));
64 }
65
66 private static URI getLocationURI(final URI requestUri, final HttpResponse response, final String headerName) {
67 final Header h = response.getFirstHeader(headerName);
68 if (h == null) {
69 return null;
70 }
71 final URI locationUri = HttpCacheSupport.normalizeQuietly(h.getValue());
72 if (locationUri == null) {
73 return requestUri;
74 }
75 if (locationUri.isAbsolute()) {
76 return locationUri;
77 } else {
78 return URIUtils.resolve(requestUri, locationUri);
79 }
80 }
81
82 static URI getContentLocationURI(final URI requestUri, final HttpResponse response) {
83 return getLocationURI(requestUri, response, HttpHeaders.CONTENT_LOCATION);
84 }
85
86 static URI getLocationURI(final URI requestUri, final HttpResponse response) {
87 return getLocationURI(requestUri, response, HttpHeaders.LOCATION);
88 }
89
90 static boolean responseAndEntryEtagsDiffer(final HttpResponse response,
91 final HttpCacheEntry entry) {
92 final Header entryEtag = entry.getFirstHeader(HeaderConstants.ETAG);
93 final Header responseEtag = response.getFirstHeader(HeaderConstants.ETAG);
94 if (entryEtag == null || responseEtag == null) {
95 return false;
96 }
97 return (!entryEtag.getValue().equals(responseEtag.getValue()));
98 }
99
100 static boolean responseDateOlderThanEntryDate(final HttpResponse response, final HttpCacheEntry entry) {
101 return DateSupport.isBefore(response, entry, HttpHeaders.DATE);
102 }
103
104 }