/
CacheControl.kt
285 lines (257 loc) · 10 KB
/
CacheControl.kt
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (C) 2019 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3
import java.util.concurrent.TimeUnit
import kotlin.time.Duration
import okhttp3.internal.commonBuild
import okhttp3.internal.commonClampToInt
import okhttp3.internal.commonForceCache
import okhttp3.internal.commonForceNetwork
import okhttp3.internal.commonImmutable
import okhttp3.internal.commonNoCache
import okhttp3.internal.commonNoStore
import okhttp3.internal.commonNoTransform
import okhttp3.internal.commonOnlyIfCached
import okhttp3.internal.commonParse
import okhttp3.internal.commonToString
/**
* A Cache-Control header with cache directives from a server or client. These directives set policy
* on what responses can be stored, and which requests can be satisfied by those stored responses.
*
* See [RFC 7234, 5.2](https://tools.ietf.org/html/rfc7234#section-5.2).
*/
class CacheControl internal constructor(
/**
* In a response, this field's name "no-cache" is misleading. It doesn't prevent us from caching
* the response; it only means we have to validate the response with the origin server before
* returning it. We can do this with a conditional GET.
*
* In a request, it means do not use a cache to satisfy the request.
*/
@get:JvmName("noCache") val noCache: Boolean,
/** If true, this response should not be cached. */
@get:JvmName("noStore") val noStore: Boolean,
/** The duration past the response's served date that it can be served without validation. */
@get:JvmName("maxAgeSeconds") val maxAgeSeconds: Int,
/**
* The "s-maxage" directive is the max age for shared caches. Not to be confused with "max-age"
* for non-shared caches, As in Firefox and Chrome, this directive is not honored by this cache.
*/
@get:JvmName("sMaxAgeSeconds") val sMaxAgeSeconds: Int,
val isPrivate: Boolean,
val isPublic: Boolean,
@get:JvmName("mustRevalidate") val mustRevalidate: Boolean,
@get:JvmName("maxStaleSeconds") val maxStaleSeconds: Int,
@get:JvmName("minFreshSeconds") val minFreshSeconds: Int,
/**
* This field's name "only-if-cached" is misleading. It actually means "do not use the network".
* It is set by a client who only wants to make a request if it can be fully satisfied by the
* cache. Cached responses that would require validation (ie. conditional gets) are not permitted
* if this header is set.
*/
@get:JvmName("onlyIfCached") val onlyIfCached: Boolean,
@get:JvmName("noTransform") val noTransform: Boolean,
@get:JvmName("immutable") val immutable: Boolean,
internal var headerValue: String?,
) {
@JvmName("-deprecated_noCache")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "noCache"),
level = DeprecationLevel.ERROR,
)
fun noCache(): Boolean = noCache
@JvmName("-deprecated_noStore")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "noStore"),
level = DeprecationLevel.ERROR,
)
fun noStore(): Boolean = noStore
@JvmName("-deprecated_maxAgeSeconds")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "maxAgeSeconds"),
level = DeprecationLevel.ERROR,
)
fun maxAgeSeconds(): Int = maxAgeSeconds
@JvmName("-deprecated_sMaxAgeSeconds")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "sMaxAgeSeconds"),
level = DeprecationLevel.ERROR,
)
fun sMaxAgeSeconds(): Int = sMaxAgeSeconds
@JvmName("-deprecated_mustRevalidate")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "mustRevalidate"),
level = DeprecationLevel.ERROR,
)
fun mustRevalidate(): Boolean = mustRevalidate
@JvmName("-deprecated_maxStaleSeconds")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "maxStaleSeconds"),
level = DeprecationLevel.ERROR,
)
fun maxStaleSeconds(): Int = maxStaleSeconds
@JvmName("-deprecated_minFreshSeconds")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "minFreshSeconds"),
level = DeprecationLevel.ERROR,
)
fun minFreshSeconds(): Int = minFreshSeconds
@JvmName("-deprecated_onlyIfCached")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "onlyIfCached"),
level = DeprecationLevel.ERROR,
)
fun onlyIfCached(): Boolean = onlyIfCached
@JvmName("-deprecated_noTransform")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "noTransform"),
level = DeprecationLevel.ERROR,
)
fun noTransform(): Boolean = noTransform
@JvmName("-deprecated_immutable")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "immutable"),
level = DeprecationLevel.ERROR,
)
fun immutable(): Boolean = immutable
override fun toString(): String = commonToString()
/** Builds a `Cache-Control` request header. */
class Builder {
internal var noCache: Boolean = false
internal var noStore: Boolean = false
internal var maxAgeSeconds = -1
internal var maxStaleSeconds = -1
internal var minFreshSeconds = -1
internal var onlyIfCached: Boolean = false
internal var noTransform: Boolean = false
internal var immutable: Boolean = false
/** Don't accept an unvalidated cached response. */
fun noCache() = commonNoCache()
/** Don't store the server's response in any cache. */
fun noStore() = commonNoStore()
/**
* Only accept the response if it is in the cache. If the response isn't cached, a `504
* Unsatisfiable Request` response will be returned.
*/
fun onlyIfCached() = commonOnlyIfCached()
/** Don't accept a transformed response. */
fun noTransform() = commonNoTransform()
fun immutable() = commonImmutable()
/**
* Sets the maximum age of a cached response. If the cache response's age exceeds [maxAge], it
* will not be used and a network request will be made.
*
* @param maxAge a non-negative duration. This is stored and transmitted with [TimeUnit.SECONDS]
* precision; finer precision will be lost.
*/
fun maxAge(maxAge: Duration) =
apply {
val maxAgeSeconds = maxAge.inWholeSeconds
require(maxAgeSeconds >= 0) { "maxAge < 0: $maxAgeSeconds" }
this.maxAgeSeconds = maxAgeSeconds.commonClampToInt()
}
fun maxStale(maxStale: Duration) =
apply {
val maxStaleSeconds = maxStale.inWholeSeconds
require(maxStaleSeconds >= 0) { "maxStale < 0: $maxStaleSeconds" }
this.maxStaleSeconds = maxStaleSeconds.commonClampToInt()
}
fun minFresh(minFresh: Duration) =
apply {
val minFreshSeconds = minFresh.inWholeSeconds
require(minFreshSeconds >= 0) { "minFresh < 0: $minFreshSeconds" }
this.minFreshSeconds = minFreshSeconds.commonClampToInt()
}
/**
* Sets the maximum age of a cached response. If the cache response's age exceeds [maxAge], it
* will not be used and a network request will be made.
*
* @param maxAge a non-negative integer. This is stored and transmitted with [TimeUnit.SECONDS]
* precision; finer precision will be lost.
*/
fun maxAge(
maxAge: Int,
timeUnit: TimeUnit,
) = apply {
require(maxAge >= 0) { "maxAge < 0: $maxAge" }
val maxAgeSecondsLong = timeUnit.toSeconds(maxAge.toLong())
this.maxAgeSeconds = maxAgeSecondsLong.commonClampToInt()
}
/**
* Accept cached responses that have exceeded their freshness lifetime by up to `maxStale`. If
* unspecified, stale cache responses will not be used.
*
* @param maxStale a non-negative integer. This is stored and transmitted with
* [TimeUnit.SECONDS] precision; finer precision will be lost.
*/
fun maxStale(
maxStale: Int,
timeUnit: TimeUnit,
) = apply {
require(maxStale >= 0) { "maxStale < 0: $maxStale" }
val maxStaleSecondsLong = timeUnit.toSeconds(maxStale.toLong())
this.maxStaleSeconds = maxStaleSecondsLong.commonClampToInt()
}
/**
* Sets the minimum number of seconds that a response will continue to be fresh for. If the
* response will be stale when [minFresh] have elapsed, the cached response will not be used and
* a network request will be made.
*
* @param minFresh a non-negative integer. This is stored and transmitted with
* [TimeUnit.SECONDS] precision; finer precision will be lost.
*/
fun minFresh(
minFresh: Int,
timeUnit: TimeUnit,
) = apply {
require(minFresh >= 0) { "minFresh < 0: $minFresh" }
val minFreshSecondsLong = timeUnit.toSeconds(minFresh.toLong())
this.minFreshSeconds = minFreshSecondsLong.commonClampToInt()
}
fun build(): CacheControl = commonBuild()
}
companion object {
/**
* Cache control request directives that require network validation of responses. Note that such
* requests may be assisted by the cache via conditional GET requests.
*/
@JvmField
val FORCE_NETWORK = commonForceNetwork()
/**
* Cache control request directives that uses the cache only, even if the cached response is
* stale. If the response isn't available in the cache or requires server validation, the call
* will fail with a `504 Unsatisfiable Request`.
*/
@JvmField
val FORCE_CACHE = commonForceCache()
/**
* Returns the cache directives of [headers]. This honors both Cache-Control and Pragma headers
* if they are present.
*/
@JvmStatic
fun parse(headers: Headers): CacheControl = commonParse(headers)
}
}