Skip to content

Commit

Permalink
fix incorrect Age header values after validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Nov 2, 2008
1 parent 6bcb590 commit ac1c305
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
3 changes: 3 additions & 0 deletions 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
Expand Down
1 change: 0 additions & 1 deletion TODO
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rack/cache/core.rb
Expand Up @@ -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]
Expand Down
13 changes: 6 additions & 7 deletions lib/rack/cache/headers.rb
Expand Up @@ -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.
Expand Down
11 changes: 9 additions & 2 deletions test/headers_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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}, '')
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac1c305

Please sign in to comment.