2
2
3
3
# Released under the MIT License.
4
4
# Copyright, 2020-2023, by Samuel Williams.
5
+ # Copyright, 2023, by Thomas Morgan.
5
6
6
7
require_relative 'split'
7
8
@@ -14,11 +15,15 @@ class CacheControl < Split
14
15
NO_CACHE = 'no-cache'
15
16
NO_STORE = 'no-store'
16
17
MAX_AGE = 'max-age'
18
+ S_MAXAGE = 's-maxage'
17
19
18
20
STATIC = 'static'
19
21
DYNAMIC = 'dynamic'
20
22
STREAMING = 'streaming'
21
23
24
+ MUST_REVALIDATE = 'must-revalidate'
25
+ PROXY_REVALIDATE = 'proxy-revalidate'
26
+
22
27
def initialize ( value = nil )
23
28
super ( value &.downcase )
24
29
end
@@ -55,11 +60,40 @@ def no_store?
55
60
self . include? ( NO_STORE )
56
61
end
57
62
63
+ # Indicates that a response must not be used once it is stale.
64
+ # See https://www.rfc-editor.org/rfc/rfc9111.html#name-must-revalidate
65
+ def must_revalidate?
66
+ self . include? ( MUST_REVALIDATE )
67
+ end
68
+
69
+ # Like must-revalidate, but for shared caches only.
70
+ # See https://www.rfc-editor.org/rfc/rfc9111.html#name-proxy-revalidate
71
+ def proxy_revalidate?
72
+ self . include? ( PROXY_REVALIDATE )
73
+ end
74
+
75
+ # The maximum time, in seconds, a response should be considered fresh.
76
+ # See https://www.rfc-editor.org/rfc/rfc9111.html#name-max-age-2
58
77
def max_age
59
- if value = self . find { |value | value . start_with? ( MAX_AGE ) }
78
+ find_integer_value ( MAX_AGE )
79
+ end
80
+
81
+ # Like max-age, but for shared caches only, which should use it before
82
+ # max-age when present.
83
+ # See https://www.rfc-editor.org/rfc/rfc9111.html#name-s-maxage
84
+ def s_maxage
85
+ find_integer_value ( S_MAXAGE )
86
+ end
87
+
88
+ private
89
+
90
+ def find_integer_value ( value_name )
91
+ if value = self . find { |value | value . start_with? ( value_name ) }
60
92
_ , age = value . split ( '=' , 2 )
61
93
62
- return Integer ( age )
94
+ if age =~ /\A [0-9]+\z /
95
+ return Integer ( age )
96
+ end
63
97
end
64
98
end
65
99
end
0 commit comments