Skip to content

Commit 5c8fd99

Browse files
committed
update HTTP caching
1 parent 0f14fde commit 5c8fd99

File tree

8 files changed

+408
-87
lines changed

8 files changed

+408
-87
lines changed

src/main/java/io/vertx/httpproxy/impl/CacheControl.java

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,90 @@
1010
*/
1111
package io.vertx.httpproxy.impl;
1212

13+
import java.math.BigInteger;
14+
1315
/**
1416
* @author <a href="mailto:[email protected]">Julien Viet</a>
1517
*/
1618
public class CacheControl {
1719

1820
private int maxAge;
21+
private int maxStale;
22+
private int minFresh;
23+
private boolean noCache;
24+
private boolean noStore;
25+
private boolean noTransform;
26+
private boolean onlyIfCached;
27+
private boolean mustRevalidate;
28+
private boolean mustUnderstand;
29+
private boolean _private;
30+
private boolean proxyRevalidate;
1931
private boolean _public;
32+
private int sMaxage;
2033

2134
public CacheControl parse(String header) {
22-
maxAge = -1;
35+
noCache = false;
36+
noStore = false;
37+
noTransform = false;
38+
onlyIfCached = false;
39+
mustRevalidate = false;
40+
mustUnderstand = false;
41+
_private = false;
42+
proxyRevalidate = false;
2343
_public = false;
44+
2445
String[] parts = header.split(","); // No regex
2546
for (String part : parts) {
2647
part = part.trim().toLowerCase();
2748
switch (part) {
2849
case "public":
2950
_public = true;
3051
break;
52+
case "no-cache":
53+
noCache = true;
54+
break;
55+
case "no-store":
56+
noStore = true;
57+
break;
58+
case "no-transform":
59+
noTransform = true;
60+
break;
61+
case "only-if-cached":
62+
onlyIfCached = true;
63+
break;
64+
case "must-revalidate":
65+
mustRevalidate = true;
66+
break;
67+
case "must-understand":
68+
mustUnderstand = true;
69+
break;
70+
case "private":
71+
_private = true;
72+
break;
73+
case "proxy-revalidate":
74+
proxyRevalidate = true;
75+
break;
3176
default:
32-
if (part.startsWith("max-age=")) {
33-
maxAge = Integer.parseInt(part.substring(8));
34-
35-
}
77+
maxAge = loadInt(part, "max-age=");
78+
maxStale = loadInt(part, "max-stale=");
79+
minFresh = loadInt(part, "min-fresh=");
80+
sMaxage = loadInt(part, "s-maxage=");
3681
break;
3782
}
3883
}
3984
return this;
4085
}
4186

87+
private static int loadInt(String part, String prefix) {
88+
if (part.startsWith(prefix)) {
89+
BigInteger valueRaw = new BigInteger(part.substring(prefix.length()));
90+
return valueRaw
91+
.min(BigInteger.valueOf(Integer.MAX_VALUE))
92+
.max(BigInteger.ZERO).intValueExact();
93+
}
94+
return -1;
95+
}
96+
4297
public int maxAge() {
4398
return maxAge;
4499
}
@@ -47,4 +102,47 @@ public boolean isPublic() {
47102
return _public;
48103
}
49104

105+
public int maxStale() {
106+
return maxStale;
107+
}
108+
109+
public int minFresh() {
110+
return minFresh;
111+
}
112+
113+
public boolean isNoCache() {
114+
return noCache;
115+
}
116+
117+
public boolean isNoStore() {
118+
return noStore;
119+
}
120+
121+
public boolean isNoTransform() {
122+
return noTransform;
123+
}
124+
125+
public boolean isOnlyIfCached() {
126+
return onlyIfCached;
127+
}
128+
129+
public boolean isMustRevalidate() {
130+
return mustRevalidate;
131+
}
132+
133+
public boolean isMustUnderstand() {
134+
return mustUnderstand;
135+
}
136+
137+
public boolean isPrivate() {
138+
return _private;
139+
}
140+
141+
public boolean isProxyRevalidate() {
142+
return proxyRevalidate;
143+
}
144+
145+
public int sMaxage() {
146+
return sMaxage;
147+
}
50148
}

0 commit comments

Comments
 (0)