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.util.Iterator;
30
31 import org.apache.hc.client5.http.cache.HeaderConstants;
32 import org.apache.hc.core5.http.HeaderElement;
33 import org.apache.hc.core5.http.HttpRequest;
34 import org.apache.hc.core5.http.HttpVersion;
35 import org.apache.hc.core5.http.ProtocolVersion;
36 import org.apache.hc.core5.http.message.MessageSupport;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43 class CacheableRequestPolicy {
44
45 private static final Logger LOG = LoggerFactory.getLogger(CacheableRequestPolicy.class);
46
47
48
49
50
51
52
53
54 public boolean isServableFromCache(final HttpRequest request) {
55 final String method = request.getMethod();
56
57 final ProtocolVersion pv = request.getVersion() != null ? request.getVersion() : HttpVersion.DEFAULT;
58 if (HttpVersion.HTTP_1_1.compareToVersion(pv) != 0) {
59 LOG.debug("non-HTTP/1.1 request is not serveable from cache");
60 return false;
61 }
62
63 if (!method.equals(HeaderConstants.GET_METHOD) && !method.equals(HeaderConstants.HEAD_METHOD)) {
64 if (LOG.isDebugEnabled()) {
65 LOG.debug("{} request is not serveable from cache", method);
66 }
67 return false;
68 }
69
70 if (request.countHeaders(HeaderConstants.PRAGMA) > 0) {
71 LOG.debug("request with Pragma header is not serveable from cache");
72 return false;
73 }
74
75 final Iterator<HeaderElement> it = MessageSupport.iterate(request, HeaderConstants.CACHE_CONTROL);
76 while (it.hasNext()) {
77 final HeaderElement cacheControlElement = it.next();
78 if (HeaderConstants.CACHE_CONTROL_NO_STORE.equalsIgnoreCase(cacheControlElement.getName())) {
79 LOG.debug("Request with no-store is not serveable from cache");
80 return false;
81 }
82 if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equalsIgnoreCase(cacheControlElement.getName())) {
83 LOG.debug("Request with no-cache is not serveable from cache");
84 return false;
85 }
86 }
87
88 LOG.debug("Request is serveable from cache");
89 return true;
90 }
91
92 }