/
CacheControl.kt
120 lines (106 loc) · 3.92 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
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.http
/**
* Represents a value for a `Cache-Control` header
*
* @param visibility specifies an optional visibility such as private or public
*/
public abstract class CacheControl(public val visibility: Visibility?) {
/**
* Controls caching by proxies
*/
public enum class Visibility(internal val headerValue: String) {
/**
* Specifies that the response is cacheable by clients and shared (proxy) caches.
*/
Public("public"),
/**
* Specifies that the response is cacheable only on the client and not by shared (proxy server) caches.
*/
Private("private")
}
/**
* Represents a no-cache cache control value
*/
public class NoCache(visibility: Visibility?) : CacheControl(visibility) {
override fun toString(): String = if (visibility == null) {
"no-cache"
} else {
"no-cache, ${visibility.headerValue}"
}
override fun equals(other: Any?): Boolean {
return other is NoCache && visibility == other.visibility
}
override fun hashCode(): Int {
return visibility.hashCode()
}
}
/**
* Represents a no-store cache control value
*/
public class NoStore(visibility: Visibility?) : CacheControl(visibility) {
override fun toString(): String = if (visibility == null) {
"no-store"
} else {
"no-store, ${visibility.headerValue}"
}
override fun equals(other: Any?): Boolean {
return other is NoStore && other.visibility == visibility
}
override fun hashCode(): Int {
return visibility.hashCode()
}
}
/**
* Represents a cache control value with the specified max ages and re-validation strategies
* @property maxAgeSeconds max-age in seconds
* @property proxyMaxAgeSeconds max-age in seconds for caching proxies
* @property mustRevalidate `true` if a client must validate in spite of age
* @property proxyRevalidate `true` if a caching proxy must revalidate in spite of age
*/
public class MaxAge(
public val maxAgeSeconds: Int,
public val proxyMaxAgeSeconds: Int? = null,
public val mustRevalidate: Boolean = false,
public val proxyRevalidate: Boolean = false,
visibility: Visibility? = null
) : CacheControl(visibility) {
override fun toString(): String {
val parts = ArrayList<String>(5)
parts.add("max-age=$maxAgeSeconds")
if (proxyMaxAgeSeconds != null) {
parts.add("s-maxage=$proxyMaxAgeSeconds")
}
if (mustRevalidate) {
parts.add("must-revalidate")
}
if (proxyRevalidate) {
parts.add("proxy-revalidate")
}
if (visibility != null) {
parts.add(visibility.headerValue)
}
return parts.joinToString(", ")
}
override fun equals(other: Any?): Boolean {
return other === this || (
other is MaxAge &&
other.maxAgeSeconds == maxAgeSeconds &&
other.proxyMaxAgeSeconds == proxyMaxAgeSeconds &&
other.mustRevalidate == mustRevalidate &&
other.proxyRevalidate == proxyRevalidate &&
other.visibility == visibility
)
}
override fun hashCode(): Int {
var result = maxAgeSeconds
result = 31 * result + (proxyMaxAgeSeconds ?: 0)
result = 31 * result + mustRevalidate.hashCode()
result = 31 * result + proxyRevalidate.hashCode()
result = 31 * result + visibility.hashCode()
return result
}
}
}