diff --git a/src/lua/body_filter.lua b/src/lua/body_filter.lua index 3acd201..5cf2033 100644 --- a/src/lua/body_filter.lua +++ b/src/lua/body_filter.lua @@ -52,22 +52,24 @@ local function rewrite_body(body) return body end -if ngx.ctx.rewriting then - local chunk = ngx.arg[1] - local eof = ngx.arg[2] +if ngx.ctx.jasima_config then + if ngx.ctx.rewriting then + local chunk = ngx.arg[1] + local eof = ngx.arg[2] - ngx.ctx.buffered = (ngx.ctx.buffered or "") .. (chunk or "") + ngx.ctx.buffered = (ngx.ctx.buffered or "") .. (chunk or "") - if #ngx.ctx.buffered > 5 * 1024 * 1024 and not eof then - -- Don't just consume memory forever - ngx.arg[1] = rewrite_body(ngx.ctx.buffered) -- We still do our best - ngx.ctx.rewriting = false - return - end + if #ngx.ctx.buffered > 5 * 1024 * 1024 and not eof then + -- Don't just consume memory forever + ngx.arg[1] = rewrite_body(ngx.ctx.buffered) -- We still do our best + ngx.ctx.rewriting = false + return + end - if eof then - ngx.arg[1] = rewrite_body(ngx.ctx.buffered) - else - ngx.arg[1] = nil + if eof then + ngx.arg[1] = rewrite_body(ngx.ctx.buffered) + else + ngx.arg[1] = nil + end end end diff --git a/src/lua/header_filter.lua b/src/lua/header_filter.lua index d7c3d17..fba237b 100644 --- a/src/lua/header_filter.lua +++ b/src/lua/header_filter.lua @@ -1,29 +1,31 @@ local utils = require "utils" -if ngx.ctx.jasima_config.rewrite_disable then - return -end +if ngx.ctx.jasima_config then + if ngx.ctx.jasima_config.rewrite_disable then + return + end -if ngx.header["Content-Type"] then - local content_type = ngx.header["Content-Type"]:lower() - if content_type:find("text/html") or - content_type:find("text/css") or - content_type:find("text/xml") or - content_type:find("application/javascript") or - content_type:find("application/json") or - content_type:find("application/rss%+xml") or - content_type:find("application/atom%+xml") or - content_type:find("application/vnd%.mpegurl") or - content_type:find("application/x%-mpegurl") then - ngx.log(ngx.DEBUG, "Enabling rewrite due to content type " .. content_type) - ngx.ctx.rewriting = true - ngx.header["Content-Length"] = nil -- We're rewriting the body so this has the wrong value if set - end -end - -if ngx.header["Location"] then - if ngx.ctx.jasima_pool_map then - local location = ngx.header["Location"] - ngx.header["Location"] = location:gsub("//([%a%d%.-]+%.[%a%d-]+)/+", utils.get_mirror) + if ngx.header["Content-Type"] then + local content_type = ngx.header["Content-Type"]:lower() + if content_type:find("text/html") or + content_type:find("text/css") or + content_type:find("text/xml") or + content_type:find("application/javascript") or + content_type:find("application/json") or + content_type:find("application/rss%+xml") or + content_type:find("application/atom%+xml") or + content_type:find("application/vnd%.mpegurl") or + content_type:find("application/x%-mpegurl") then + ngx.log(ngx.DEBUG, "Enabling rewrite due to content type " .. content_type) + ngx.ctx.rewriting = true + ngx.header["Content-Length"] = nil -- We're rewriting the body so this has the wrong value if set + end + end + + if ngx.header["Location"] then + if ngx.ctx.jasima_pool_map then + local location = ngx.header["Location"] + ngx.header["Location"] = location:gsub("//([%a%d%.-]+%.[%a%d-]+)/+", utils.get_mirror) + end end end