fix: check for config before rewriting

This commit is contained in:
Iain Learmonth 2025-05-17 16:09:42 +01:00
parent b7cf5a5084
commit 5fe0739635
2 changed files with 42 additions and 38 deletions

View file

@ -52,22 +52,24 @@ local function rewrite_body(body)
return body return body
end end
if ngx.ctx.rewriting then if ngx.ctx.jasima_config then
local chunk = ngx.arg[1] if ngx.ctx.rewriting then
local eof = ngx.arg[2] 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 if #ngx.ctx.buffered > 5 * 1024 * 1024 and not eof then
-- Don't just consume memory forever -- Don't just consume memory forever
ngx.arg[1] = rewrite_body(ngx.ctx.buffered) -- We still do our best ngx.arg[1] = rewrite_body(ngx.ctx.buffered) -- We still do our best
ngx.ctx.rewriting = false ngx.ctx.rewriting = false
return return
end end
if eof then if eof then
ngx.arg[1] = rewrite_body(ngx.ctx.buffered) ngx.arg[1] = rewrite_body(ngx.ctx.buffered)
else else
ngx.arg[1] = nil ngx.arg[1] = nil
end
end end
end end

View file

@ -1,29 +1,31 @@
local utils = require "utils" local utils = require "utils"
if ngx.ctx.jasima_config.rewrite_disable then if ngx.ctx.jasima_config then
return if ngx.ctx.jasima_config.rewrite_disable then
end return
end
if ngx.header["Content-Type"] then if ngx.header["Content-Type"] then
local content_type = ngx.header["Content-Type"]:lower() local content_type = ngx.header["Content-Type"]:lower()
if content_type:find("text/html") or if content_type:find("text/html") or
content_type:find("text/css") or content_type:find("text/css") or
content_type:find("text/xml") or content_type:find("text/xml") or
content_type:find("application/javascript") or content_type:find("application/javascript") or
content_type:find("application/json") or content_type:find("application/json") or
content_type:find("application/rss%+xml") or content_type:find("application/rss%+xml") or
content_type:find("application/atom%+xml") or content_type:find("application/atom%+xml") or
content_type:find("application/vnd%.mpegurl") or content_type:find("application/vnd%.mpegurl") or
content_type:find("application/x%-mpegurl") then content_type:find("application/x%-mpegurl") then
ngx.log(ngx.DEBUG, "Enabling rewrite due to content type " .. content_type) ngx.log(ngx.DEBUG, "Enabling rewrite due to content type " .. content_type)
ngx.ctx.rewriting = true ngx.ctx.rewriting = true
ngx.header["Content-Length"] = nil -- We're rewriting the body so this has the wrong value if set ngx.header["Content-Length"] = nil -- We're rewriting the body so this has the wrong value if set
end end
end end
if ngx.header["Location"] then if ngx.header["Location"] then
if ngx.ctx.jasima_pool_map then if ngx.ctx.jasima_pool_map then
local location = ngx.header["Location"] local location = ngx.header["Location"]
ngx.header["Location"] = location:gsub("//([%a%d%.-]+%.[%a%d-]+)/+", utils.get_mirror) ngx.header["Location"] = location:gsub("//([%a%d%.-]+%.[%a%d-]+)/+", utils.get_mirror)
end
end end
end end