-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathCacheControl.java
152 lines (133 loc) · 3.46 KB
/
CacheControl.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.httpproxy.impl;
import java.math.BigInteger;
/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class CacheControl {
private int maxAge;
private int maxStale;
private int minFresh;
private boolean noCache;
private boolean noStore;
private boolean noTransform;
private boolean onlyIfCached;
private boolean mustRevalidate;
private boolean mustUnderstand;
private boolean _private;
private boolean proxyRevalidate;
private boolean _public;
private int sMaxage;
public CacheControl parse(String header) {
noCache = false;
noStore = false;
noTransform = false;
onlyIfCached = false;
mustRevalidate = false;
mustUnderstand = false;
_private = false;
proxyRevalidate = false;
_public = false;
maxAge = -1;
maxStale = -1;
minFresh = -1;
sMaxage = -1;
String[] parts = header.split(","); // No regex
for (String part : parts) {
part = part.trim().toLowerCase();
switch (part) {
case "public":
_public = true;
break;
case "no-cache":
noCache = true;
break;
case "no-store":
noStore = true;
break;
case "no-transform":
noTransform = true;
break;
case "only-if-cached":
onlyIfCached = true;
break;
case "must-revalidate":
mustRevalidate = true;
break;
case "must-understand":
mustUnderstand = true;
break;
case "private":
_private = true;
break;
case "proxy-revalidate":
proxyRevalidate = true;
break;
default:
maxAge = Math.max(maxAge, loadInt(part, "max-age="));
maxStale = Math.max(maxStale, loadInt(part, "max-stale="));
minFresh = Math.max(minFresh, loadInt(part, "min-fresh="));
sMaxage = Math.max(sMaxage, loadInt(part, "s-maxage="));
break;
}
}
return this;
}
private static int loadInt(String part, String prefix) {
if (part.startsWith(prefix)) {
BigInteger valueRaw = new BigInteger(part.substring(prefix.length()));
return valueRaw
.min(BigInteger.valueOf(Integer.MAX_VALUE))
.max(BigInteger.ZERO).intValueExact();
}
return -1;
}
public int maxAge() {
return maxAge;
}
public boolean isPublic() {
return _public;
}
public int maxStale() {
return maxStale;
}
public int minFresh() {
return minFresh;
}
public boolean isNoCache() {
return noCache;
}
public boolean isNoStore() {
return noStore;
}
public boolean isNoTransform() {
return noTransform;
}
public boolean isOnlyIfCached() {
return onlyIfCached;
}
public boolean isMustRevalidate() {
return mustRevalidate;
}
public boolean isMustUnderstand() {
return mustUnderstand;
}
public boolean isPrivate() {
return _private;
}
public boolean isProxyRevalidate() {
return proxyRevalidate;
}
public int sMaxage() {
return sMaxage;
}
}