diff --git a/CHANGES b/CHANGES index 456604c..6d983fd 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,8 @@ ## 0.3.0 / ... + * BUG: The Age response header was not being set properly when a stale + entry was validated. This would result in Age values that exceeded + the freshness lifetime in responses. * BUG: A cached entry in a heap meta store could be unintentionally modified by request processing since the cached objects were being returned directly. The result was typically missing/incorrect header diff --git a/TODO b/TODO index dc05255..5e7e1f6 100644 --- a/TODO +++ b/TODO @@ -5,7 +5,6 @@ - BUG: Response body written to cache each time validation succeeds (actually, I'm not positive whether this is happening or not but it looks like it is). - - BUG: Age should always be 0 when a request is validated. - Are we doing HEAD properly? - liberal, conservative, sane caching configs - Sample app diff --git a/lib/rack/cache/core.rb b/lib/rack/cache/core.rb index 280973a..373da4e 100644 --- a/lib/rack/cache/core.rb +++ b/lib/rack/cache/core.rb @@ -172,6 +172,7 @@ def perform_validate trace "cache entry valid" @response = entry.dup @response.headers.delete('Age') + @response.headers.delete('Date') @response.headers['X-Origin-Status'] = '304' %w[Date Expires Cache-Control Etag Last-Modified].each do |name| next unless value = original_response.headers[name] diff --git a/lib/rack/cache/headers.rb b/lib/rack/cache/headers.rb index a4ef441..185d6a9 100644 --- a/lib/rack/cache/headers.rb +++ b/lib/rack/cache/headers.rb @@ -148,13 +148,12 @@ def no_store? # The date, as specified by the Date header. When no Date header is present, # set the Date header to Time.now and return. def date - @date ||= - if date = headers['Date'] - Time.httpdate(date) - else - headers['Date'] = now.httpdate unless headers.frozen? - now - end + if date = headers['Date'] + Time.httpdate(date) + else + headers['Date'] = now.httpdate unless headers.frozen? + now + end end # The age of the response. diff --git a/test/headers_test.rb b/test/headers_test.rb index da48606..4cf4d34 100644 --- a/test/headers_test.rb +++ b/test/headers_test.rb @@ -8,7 +8,7 @@ class MockResponse < Rack::MockResponse describe 'Rack::Cache::Headers' do before :each do - @now = Time.now + @now = Time.httpdate(Time.now.httpdate) @res = MockResponse.new(200, {'Date' => @now.httpdate}, '') @one_hour_ago = Time.httpdate((Time.now - (60**2)).httpdate) end @@ -51,7 +51,7 @@ class MockResponse < Rack::MockResponse describe 'Rack::Cache::ResponseHeaders' do before :each do - @now = Time.now + @now = Time.httpdate(Time.now.httpdate) @one_hour_ago = Time.httpdate((Time.now - (60**2)).httpdate) @one_hour_later = Time.httpdate((Time.now + (60**2)).httpdate) @res = MockResponse.new(200, {'Date' => @now.httpdate}, '') @@ -89,6 +89,13 @@ class MockResponse < Rack::MockResponse @res.extend Rack::Cache::ResponseHeaders @res.date.should.be.close Time.now, 1 end + it 'returns the correct date when the header is modified directly' do + @res = MockResponse.new(200, { 'Date' => @one_hour_ago.httpdate }, '') + @res.extend Rack::Cache::ResponseHeaders + @res.date.should.be == @one_hour_ago + @res.headers['Date'] = @now.httpdate + @res.date.should.be == @now + end end describe '#expires_at' do