diff --git a/src/lua/header_filter.lua b/src/lua/header_filter.lua index 6dcfeef..f1735f2 100644 --- a/src/lua/header_filter.lua +++ b/src/lua/header_filter.lua @@ -1,3 +1,4 @@ +-- Determine if we rewrite the body of this type of content if ngx.header["Content-Type"] then local content_type = ngx.header["Content-Type"] if content_type:find("text/html") or @@ -12,3 +13,20 @@ if ngx.header["Content-Type"] then ngx.header["Content-Length"] = nil end end + +-- Rewrite any redirects from the origin to point at a mirror +if ngx.header["Location"] then + if ngx.ctx.jasima_pool_map and not ngx.ctx.jasima_config.rewrite_disable then + local location = ngx.header["Location"] + for from, to in pairs(ngx.ctx.jasima_pool_map) do + if ngx.ctx.jasima_config.rewrite_case_insensitive then + local pattern = ngx.re.escape(from) + location = ngx.re.gsub(location, pattern, to, "ijo") + else + local pattern = from:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") -- escape Lua patterns + location = location:gsub(pattern, to) + end + end + ngx.header["Location"] = location + end +end \ No newline at end of file