lib/protocol/http/header/cache_control.rb in protocol-http-0.24.7 vs lib/protocol/http/header/cache_control.rb in protocol-http-0.25.0
- old
+ new
@@ -1,9 +1,10 @@
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.
+# Copyright, 2023, by Thomas Morgan.
require_relative 'split'
module Protocol
module HTTP
@@ -12,15 +13,19 @@
PRIVATE = 'private'
PUBLIC = 'public'
NO_CACHE = 'no-cache'
NO_STORE = 'no-store'
MAX_AGE = 'max-age'
+ S_MAXAGE = 's-maxage'
STATIC = 'static'
DYNAMIC = 'dynamic'
STREAMING = 'streaming'
+ MUST_REVALIDATE = 'must-revalidate'
+ PROXY_REVALIDATE = 'proxy-revalidate'
+
def initialize(value = nil)
super(value&.downcase)
end
def << value
@@ -53,14 +58,43 @@
def no_store?
self.include?(NO_STORE)
end
+ # Indicates that a response must not be used once it is stale.
+ # See https://www.rfc-editor.org/rfc/rfc9111.html#name-must-revalidate
+ def must_revalidate?
+ self.include?(MUST_REVALIDATE)
+ end
+
+ # Like must-revalidate, but for shared caches only.
+ # See https://www.rfc-editor.org/rfc/rfc9111.html#name-proxy-revalidate
+ def proxy_revalidate?
+ self.include?(PROXY_REVALIDATE)
+ end
+
+ # The maximum time, in seconds, a response should be considered fresh.
+ # See https://www.rfc-editor.org/rfc/rfc9111.html#name-max-age-2
def max_age
- if value = self.find{|value| value.start_with?(MAX_AGE)}
+ find_integer_value(MAX_AGE)
+ end
+
+ # Like max-age, but for shared caches only, which should use it before
+ # max-age when present.
+ # See https://www.rfc-editor.org/rfc/rfc9111.html#name-s-maxage
+ def s_maxage
+ find_integer_value(S_MAXAGE)
+ end
+
+ private
+
+ def find_integer_value(value_name)
+ if value = self.find{|value| value.start_with?(value_name)}
_, age = value.split('=', 2)
- return Integer(age)
+ if age =~ /\A[0-9]+\z/
+ return Integer(age)
+ end
end
end
end
end
end