location / {
body_filter_by_lua_block {
local zlib = require "zlib"
if not ngx.ctx.test then
local ctx = {}
ctx.z_inflated = zlib.inflate()
ctx.z_deflated = zlib.deflate()
ngx.ctx.test = ctx
end
local inflated
ngx.log(ngx.ERR, "ngx.arg[1] size is " .. (ngx.arg[1] and #ngx.arg[1] or "nil"))
ngx.log(ngx.ERR, string.format("arg[2] is %s", tostring(ngx.arg[2])))
if ngx.arg[2] == false then
inflated = ngx.ctx.test.z_inflated(ngx.arg[1])
local begin_op, end_op
begin_op, end_op = string.find(inflated, "<head>")
if begin_op then
local res = string.sub(inflated,1,begin_op-1) .. "<head><script>/test.js</script>" .. string.sub(inflated, end_op+1)
ngx.log(ngx.ERR, res)
ngx.arg[1] = ngx.ctx.test.z_deflated(res)
-- ngx.arg[1] = res
else
ngx.arg[1] = ngx.ctx.test.z_deflated(inflated)
-- ngx.arg[1] = inflated
end
else
return
end
ngx.log(ngx.ERR, "inflated size is " .. (inflated and #inflated or "nil")
}