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

Commit e002c27

Browse files
committed
Fix minor bug in 'Age' header
1 parent 4a16f35 commit e002c27

4 files changed

Lines changed: 17 additions & 12 deletions

File tree

CHANGELOG.md

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

3+
## 0.1.17 (2015-04-30)
4+
5+
* Return the proper 'Age' header and ignore the 'public' control directive
6+
37
## 0.1.16 (2015-04-29)
48

59
* Errors caused by empty HTTP headers. Fixes issue #8

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.1.16'
9+
s.version = '0.1.17'
1010

1111
s.date = Date.today.to_s
1212

lib/helpers.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def helper_response(url, actions, column, cached, response = {})
6868
headers_304 = helper_headers_200_304.call(cached, normalized) if status == 200 || status == 304
6969
headers_url = {'Location' => url} if status == 307
7070

71-
headers = [age, warning, x_cache, headers_304, headers_url].compact.reduce &:merge
71+
headers = [headers_304, age, warning, x_cache, headers_url].compact.reduce &:merge
7272

7373
{:body => body, :code => status, :headers => headers}
7474
end
@@ -249,7 +249,7 @@ def helper_current_age(now, cached)
249249
date_value = cached['Date']['timestamp'] # Required
250250
request_time = cached['X-Cache-Req-Date']['timestamp'] # Required
251251
response_time = cached['X-Cache-Res-Date']['timestamp'] # Required
252-
age_value = cached['Age'].nil? ? 0 : cached['Age']['timestamp'].to_i
252+
age_value = cached['Age'].nil? ? 0 : cached['Age'].to_i
253253

254254
apparent_age = helper_apparent_age response_time, date_value
255255
corrected_age_value = helper_corrected_age_value response_time, request_time, age_value
@@ -285,12 +285,12 @@ def helper_explicit(cached_headers)
285285
return (cached_headers['Expires']['timestamp'] - cached_headers['Date']['timestamp']) if cached_headers['Expires']
286286
end
287287

288-
# Calculate Heuristic Freshness if there's not explicit expiration time
288+
# Calculate Heuristic Freshness if there's no explicit expiration time
289289
# source: https://tools.ietf.org/html/rfc7234#section-4.2.2
290290
def helper_heuristic(now, cached, current_age)
291-
# Use 10% only if the response is public and there's a Last-Modified header
291+
# Use 10% only if there's a Last-Modified header
292292
# source: https://tools.ietf.org/html/rfc7234#section-4.2.2
293-
if cached['Cache-Control'] && cached['Cache-Control']['public'] && cached['Last-Modified']
293+
if cached['Last-Modified']
294294
result = (now - cached['Last-Modified']['timestamp']) / 10
295295

296296
# Don't cache heuristic responses more than 24 hours old, and avoid sending a 113 Warning ;)

test/test_validations.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ def setup
6565
@headers_nothing = {
6666
:request => {"If-None-Match" => ["\"myetag\""] },
6767
:cached => {
68-
"Date" => next_date,
69-
"X-Cache-Req-Date" => next_date,
70-
"X-Cache-Res-Date" => next_date,
68+
"Expires" => next_date,
69+
"Date" => date,
70+
"X-Cache-Req-Date" => date,
71+
"X-Cache-Res-Date" => date,
7172
"Cache-Control" => {
7273
"must-revalidate" => {"token"=>nil, "quoted_string"=>nil}
7374
}
@@ -121,9 +122,9 @@ def test_expired
121122
stale = CacheRules.validate_expired? @headers
122123
fresh = CacheRules.validate_expired? @headers_nothing
123124

124-
assert_equal guard, 0
125-
assert_equal stale, 1
126-
assert_equal fresh, 0
125+
assert_equal 0, guard
126+
assert_equal 1, stale
127+
assert_equal 0, fresh
127128
end
128129

129130
def test_only_if_cached

0 commit comments

Comments
 (0)