Skip to content

Commit

Permalink
Merge pull request #88 from hamishforbes/master
Browse files Browse the repository at this point in the history
Bugfix: Multiple cache-control headers should be concatenated
  • Loading branch information
pintsized committed Jul 30, 2014
2 parents bfb217f + 1305280 commit 98579a0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/ledge/ledge.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local type = type
local next = next
local table = table
local ngx = ngx
local tbl_concat = table.concat

module(...)

Expand Down Expand Up @@ -1467,10 +1468,14 @@ function save_to_cache(self, res)
}

-- Also don't cache any headers marked as Cache-Control: (no-cache|no-store|private)="header".
if res.header["Cache-Control"] and res.header["Cache-Control"]:find("=") then
local cc = res.header["Cache-Control"]
if type(cc) == "table" then
cc = tbl_concat(cc, ", ")
end
if cc and cc:find("=") then
local patterns = { "no%-cache", "no%-store", "private" }
for _,p in ipairs(patterns) do
for h in res.header["Cache-Control"]:gmatch(p .. "=\"?([%a-]+)\"?") do
for h in cc:gmatch(p .. "=\"?([%a-]+)\"?") do
table.insert(uncacheable_headers, h)
end
end
Expand Down
10 changes: 8 additions & 2 deletions lib/ledge/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ local rawset = rawset
local rawget = rawget
local tonumber = tonumber
local error = error
local type = type
local ngx = ngx
local tbl_concat = table.concat

module(...)

Expand Down Expand Up @@ -102,9 +104,13 @@ end
function ttl(self)
-- Header precedence is Cache-Control: s-maxage=NUM, Cache-Control: max-age=NUM,
-- and finally Expires: HTTP_TIMESTRING.
if self.header["Cache-Control"] then
local cc = self.header["Cache-Control"]
if cc then
if type(cc) == "table" then
cc = tbl_concat(cc, ", ")
end
local max_ages = {}
for max_age in ngx.re.gmatch(self.header["Cache-Control"],
for max_age in ngx.re.gmatch(cc,
"(s\\-maxage|max\\-age)=(\\d+)",
"io") do
max_ages[max_age[1]] = max_age[2]
Expand Down
68 changes: 68 additions & 0 deletions t/19-multiple_headers.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use Test::Nginx::Socket;
use Cwd qw(cwd);

plan tests => 6;

my $pwd = cwd();

$ENV{TEST_LEDGE_REDIS_DATABASE} ||= 1;

our $HttpConfig = qq{
lua_package_path "$pwd/../lua-resty-rack/lib/?.lua;$pwd/lib/?.lua;;";
init_by_lua "
ledge_mod = require 'ledge.ledge'
ledge = ledge_mod:new()
ledge:config_set('redis_database', $ENV{TEST_LEDGE_REDIS_DATABASE})
";
};

run_tests();

__DATA__
=== TEST 1: Multiple cache-control response headers, miss
--- http_config eval: $::HttpConfig
--- config
location /cache {
content_by_lua '
ledge:run()
';
}
location /__ledge_origin {
content_by_lua '
ngx.header["Cache-Control"] = { "public", "max-age=3600"}
ngx.say("TEST 1")
';
}
--- request
GET /cache
--- response_headers_like
X-Cache: MISS from .*
--- response_headers
Cache-Control: public, max-age=3600
--- response_body
TEST 1
=== TEST 1b: Multiple cache-control response headers, hit
--- http_config eval: $::HttpConfig
--- config
location /cache {
content_by_lua '
ledge:run()
';
}
location /__ledge_origin {
content_by_lua '
ngx.header["Cache-Control"] = { "public", "max-age=3600"}
ngx.say("TEST 2")
';
}
--- request
GET /cache
--- response_headers_like
X-Cache: HIT from .*
--- response_headers
Cache-Control: public, max-age=3600
--- response_body
TEST 1

0 comments on commit 98579a0

Please sign in to comment.