Skip to content

Commit 02177f5

Browse files
committed
cache-control: add must-revalidate, proxy-revalidate, s-maxage
1 parent 8b5b25f commit 02177f5

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

lib/protocol/http/header/cache_control.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ class CacheControl < Split
1414
NO_CACHE = 'no-cache'
1515
NO_STORE = 'no-store'
1616
MAX_AGE = 'max-age'
17+
S_MAXAGE = 's-maxage'
1718

1819
STATIC = 'static'
1920
DYNAMIC = 'dynamic'
2021
STREAMING = 'streaming'
2122

23+
MUST_REVALIDATE = 'must-revalidate'
24+
PROXY_REVALIDATE = 'proxy-revalidate'
25+
2226
def initialize(value = nil)
2327
super(value&.downcase)
2428
end
@@ -55,11 +59,40 @@ def no_store?
5559
self.include?(NO_STORE)
5660
end
5761

62+
# Indicates that a response must not be used once it is stale.
63+
# See https://www.rfc-editor.org/rfc/rfc9111.html#name-must-revalidate
64+
def must_revalidate?
65+
self.include?(MUST_REVALIDATE)
66+
end
67+
68+
# Like must-revalidate, but for shared caches only.
69+
# See https://www.rfc-editor.org/rfc/rfc9111.html#name-proxy-revalidate
70+
def proxy_revalidate?
71+
self.include?(PROXY_REVALIDATE)
72+
end
73+
74+
# The maximum time, in seconds, a response should be considered fresh.
75+
# See https://www.rfc-editor.org/rfc/rfc9111.html#name-max-age-2
5876
def max_age
5977
if value = self.find{|value| value.start_with?(MAX_AGE)}
6078
_, age = value.split('=', 2)
6179

62-
return Integer(age)
80+
if age =~ /^[0-9]+$/
81+
return Integer(age)
82+
end
83+
end
84+
end
85+
86+
# Like max-age, but for shared caches only, which should use it before
87+
# max-age when present.
88+
# See https://www.rfc-editor.org/rfc/rfc9111.html#name-s-maxage
89+
def s_maxage
90+
if value = self.find{|value| value.start_with?(S_MAXAGE)}
91+
_, age = value.split('=', 2)
92+
93+
if age =~ /^[0-9]+$/
94+
return Integer(age)
95+
end
6396
end
6497
end
6598
end

test/protocol/http/header/cache_control.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@
88
describe Protocol::HTTP::Header::CacheControl do
99
let(:header) {subject.new(description)}
1010

11-
with "max-age=60, public" do
11+
with "max-age=60, s-maxage=30, public" do
1212
it "correctly parses cache header" do
1313
expect(header).to have_attributes(
1414
public?: be == true,
1515
private?: be == false,
1616
max_age: be == 60,
17+
s_maxage: be == 30,
18+
)
19+
end
20+
end
21+
22+
with "max-age=-10, s-maxage=0x22" do
23+
it "gracefully handles invalid values" do
24+
expect(header).to have_attributes(
25+
max_age: be == nil,
26+
s_maxage: be == nil,
1727
)
1828
end
1929
end
@@ -51,6 +61,22 @@
5161
end
5262
end
5363

64+
with "must-revalidate" do
65+
it "correctly parses cache header" do
66+
expect(header).to have_attributes(
67+
must_revalidate?: be == true,
68+
)
69+
end
70+
end
71+
72+
with "proxy-revalidate" do
73+
it "correctly parses cache header" do
74+
expect(header).to have_attributes(
75+
proxy_revalidate?: be == true,
76+
)
77+
end
78+
end
79+
5480
with "#<<" do
5581
let(:header) {subject.new}
5682

0 commit comments

Comments
 (0)