I have Openresty configured as a reverse proxy and need to modify some of the proxied HTML. I'm using lua-gumbo to parse and modify the HTML. When developing the script I created a local variable containing the HTML code and was able to modify the code properly. When I updated the code to parse the proxied HTML via ngx.arg[1] and use body_filter_by_lua_file the code stopped working. I understand that body_filter_by_lua has certain API function limitations in this phase. What workarounds can I implement to run this code and modify proxied HTML on the fly?
My source code and subsequent error message are below:
local gumbo = require "gumbo"
local document = assert(gumbo.parse(ngx.arg[1]))
local Links = document:getElementsByTagName("a")
for i, element in ipairs(Links) do
local Attr = element:getAttribute("href")
if Attr then
element:setAttribute("href", UpdatedHrefValue)
end
end
ngx.say(document.documentElement.outerHTML, "\n")
====================================error.log===============================================================================
2014/10/20 17:04:30 [error] 19449#0: *67 failed to run body_filter_by_lua*: coroutine.wrap:15: API disabled in the context of (unknown)
stack traceback:
[C]: in function 'create'
coroutine.wrap:15: in function 'walk'
/opt/openresty/lualib/gumbo/dom/Element.lua:50: in function 'getElementsByTagName'
/opt/openresty/nginx/conf/abc.lua:30: in function 'main'
/opt/openresty/nginx/conf/abc.lua:44: in function </opt/openresty/nginx/conf/abc.lua:1>, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
2014/10/20 17:04:30 [error] 19449#0: *67 failed to run body_filter_by_lua*: coroutine.wrap:15: API disabled in the context of (unknown)
stack traceback:
[C]: in function 'create'
coroutine.wrap:15: in function 'walk'
/opt/openresty/lualib/gumbo/dom/Element.lua:50: in function 'getElementsByTagName'
/opt/openresty/nginx/conf/abc.lua:30: in function 'main'
/opt/openresty/nginx/conf/abc.lua:44: in function </opt/openresty/nginx/conf/abc.lua:1>, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"