rtomayko / rack-cache

Real HTTP Caching for Ruby Web Apps
http://rtomayko.github.io/rack-cache/
Other
822 stars 124 forks source link

Cache-Control: public in response when Cache-Control: no-cache in request #114

Closed ip2k closed 8 years ago

ip2k commented 9 years ago

When I

use Rack::Cache,
  :verbose          => true,
  :allow_reload     => true,
  :allow_revalidate => true

before do
  expires 300, :public
end

in my Sinatra app and send it a request like:

curl -vvv 'http://127.0.0.1:4567/foo' -H 'Cache-Control: no-cache' -o /dev/null

it responds with Cache-Control: public, max-age=300 and X-Rack-Cache: reload, store.

I've hacked around this by doing:

before do
  expires 300, :public unless request.env['HTTP_CACHE_CONTROL'] == 'no-cache'
end

which responds with no Cache-Control header, and X-Rack-Cache: reload, which is what I would think should happen without my hack. It might be also nice to have an option to NOT store responses when a request specifies Cache-Control: no-cache

Thanks!

mezis commented 8 years ago

@ip2k: I believe this behaviour is actually correct. Cache-Control: no-cache in the request asks the server to provide an uncached response, which it does (as mentioned by X-Rack-Cache: reload). The Cache-Control: public in the response specifies that the client may cache the response.

In other words, the fact you asked for your data not to come from the cache has no bearing on whether the response will be cached, or can be cached.

Unless I'm missing something or misreading RFC2616?

grosser commented 8 years ago

sounds good to me ... thx for digging into the RFCs ... I know it's dirty work :D