Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.

Commit 2098e94

Browse files
committed
Make cache rules consistent. Closes #12
1 parent 01ed829 commit 2098e94

6 files changed

Lines changed: 27 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# Changelog
22

3+
# 0.3.0 (2015-05-03)
4+
5+
* Make cache rules consistent based RFC spec
6+
* Add required 'cached' headers to tests
7+
38
# 0.2.0 (2015-05-02)
49

10+
* Version bump
11+
512
# 0.1.20 (2015-05-01)
613

714
* Recently cached responses are cached correctly. Fixes issue #11

cache_rules.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require 'date'
66

77
Gem::Specification.new do |s|
88
s.name = 'cache_rules'
9-
s.version = '0.2.0'
9+
s.version = '0.3.0'
1010

1111
s.date = Date.today.to_s
1212

lib/helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ def helper_heuristic(now, cached, current_age)
293293
if cached['Last-Modified']
294294
result = (now - cached['Last-Modified']['timestamp']) / 10
295295

296-
# Don't cache heuristic responses more than 24 hours old, and avoid sending a 113 Warning ;)
296+
# Don't return heuristic responses more than 24 hours old, and avoid sending a 113 Warning ;)
297297
# source: https://tools.ietf.org/html/rfc7234#section-4.2.2
298-
current_age > 86400 ? 0 : (now + result)
298+
current_age > 86400 ? 0 : result
299299
else
300300
0
301301
end

lib/validations.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def validate_expired?(headers)
5151
headers[:cached]['Cache-Control']['max-age'] &&
5252
current_age > headers[:cached]['Cache-Control']['max-age']['token'].to_i
5353

54+
5455
to_bit { (response_is_fresh != true) }
5556
end
5657

@@ -82,23 +83,22 @@ def validate_must_revalidate?(headers)
8283
to_bit { (( cached = headers[:cached]['Cache-Control'] )) && ( cached['must-revalidate'] || cached['proxy-revalidate'] ) }
8384
end
8485

85-
# Verify if we're explicitly told not to cache the response
86+
# Verify if we're explicitly told not to serve a response without revalidation
8687
def validate_no_cache?(headers)
8788
request_headers, cached_headers = headers.values_at :request, :cached
8889
return 1 if cached_headers.length == 0
8990

9091
# Must revalidate if this request header exists
9192
# source: https://tools.ietf.org/html/rfc7234#section-5.2.1.4
92-
if request_headers['Cache-Control']
93-
_, current_age = helper_freshness_lifetime.call cached_headers
94-
95-
# If max-age is 0 or if the current age is above the max-age
96-
# source: https://tools.ietf.org/html/rfc7234#section-5.2.1.1
97-
return 1 if (( request = request_headers['Cache-Control'] )) &&
98-
request['no-cache'] ||
99-
(request['max-age'] &&
100-
(request['max-age']['token'].to_s == "0" || current_age > request['max-age']['token'].to_i))
101-
end
93+
return 1 if (request_headers['Cache-Control'] && request_headers['Cache-Control']['no-cache'])
94+
95+
_, current_age = helper_freshness_lifetime.call cached_headers
96+
97+
# If max-age is 0 or if the current age is above the max-age
98+
# source: https://tools.ietf.org/html/rfc7234#section-5.2.1.1
99+
return 1 if request_headers['Cache-Control'] &&
100+
request_headers['Cache-Control']['max-age'] &&
101+
(request_headers['Cache-Control']['max-age']['token'].to_s == "0" || current_age > request_headers['Cache-Control']['max-age']['token'].to_i)
102102

103103
# source: https://tools.ietf.org/html/rfc7234#section-5.2.2.2
104104
# source: https://tools.ietf.org/html/rfc7234#section-3.2

test/test_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def test_heuristic
362362
too_old = CacheRules.helper_heuristic now, @cached_headers, 86401
363363
noop = CacheRules.helper_heuristic(now, {}, 42)
364364

365-
assert_equal last_modified, 1420378785
365+
assert_equal last_modified, 16560
366366
assert_equal not_public, 0
367367
assert_equal too_old, 0
368368
assert_equal noop, 0

test/test_validations.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,23 @@ def test_no_cache
168168
}
169169
headers2 = {
170170
:request => {},
171-
:cached => {'Cache-Control' => {'no-cache'=>{'quoted_string'=>"Cookie"}}}
171+
:cached => {'Cache-Control' => {'no-cache'=>{'quoted_string'=>"Cookie"}}, "Date" => {"httpdate" => "Thu, 01 Jan 2015 07:03:45 GMT", "timestamp"=>1420095825}, "X-Cache-Req-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Res-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}}
172172
}
173173
headers2_nil = {
174174
:request => {},
175-
:cached => {'Cache-Control' => {'no-cache'=>{'quoted_string'=>nil}}}
175+
:cached => {'Cache-Control' => {'no-cache'=>{'quoted_string'=>nil}}, "Date" => {"httpdate" => "Thu, 01 Jan 2015 07:03:45 GMT", "timestamp"=>1420095825}, "X-Cache-Req-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Res-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}}
176176
}
177177
headers3 = {
178178
:request => {},
179-
:cached => {'Cache-Control' => {'s-maxage'=>{'token'=>"0"}}}
179+
:cached => {'Cache-Control' => {'s-maxage'=>{'token'=>"0"}}, "Date" => {"httpdate" => "Thu, 01 Jan 2015 07:03:45 GMT", "timestamp"=>1420095825}, "X-Cache-Req-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Res-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}}
180180
}
181181
headers4 = {
182182
:request => {},
183-
:cached => {'Cache-Control' => {'max-age'=>{'token'=>0}}}
183+
:cached => {'Cache-Control' => {'max-age'=>{'token'=>0}}, "Date" => {"httpdate" => "Thu, 01 Jan 2015 07:03:45 GMT", "timestamp"=>1420095825}, "X-Cache-Req-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Res-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}}
184184
}
185185
headers5 = {
186186
:request => {'Pragma' => {'no-cache'=>{'token'=>nil}}},
187-
:cached => {'Cache-Control' => {}}
187+
:cached => {'Cache-Control' => {}, "Date" => {"httpdate" => "Thu, 01 Jan 2015 07:03:45 GMT", "timestamp"=>1420095825}, "X-Cache-Req-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}, "X-Cache-Res-Date" => {"httpdate"=>"Sat, 03 Jan 2015 07:03:45 GMT", "timestamp"=>1420268625}}
188188
}
189189

190190
guard = CacheRules.validate_no_cache? @no_headers

0 commit comments

Comments
 (0)