Skip to content

Commit

Permalink
Only cache status 200 and requests without authorization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 7, 2020
1 parent f4e1feb commit 74d0822
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/async/http/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Cache < ::Protocol::HTTP::Middleware
CACHE_CONTROL = 'cache-control'
SET_COOKIE = 'set-cookie'
CONTENT_TYPE = 'content-type'
AUTHORIZATION = 'authorization'

class Response < ::Protocol::HTTP::Response
def initialize(response, body)
Expand Down Expand Up @@ -115,6 +116,10 @@ def cachable?(request)
return false
end

if request.headers[AUTHORIZATION]
return false
end

# We only support caching GET and HEAD requests:
if request.method == 'GET' || request.method == 'HEAD'
return true
Expand All @@ -125,6 +130,10 @@ def cachable?(request)
end

def wrap(request, key, response)
if response.status != 200
return false
end

if body = response.body
if length = body.length
# Don't cache responses bigger than 128Kb:
Expand Down

0 comments on commit 74d0822

Please sign in to comment.