changes to ngx.arg[1] not getting reflected in final response

vamshi nginx-forum at nginx.us
Fri Jun 27 17:34:15 UTC 2014


header_filter_by_lua '
                ngx.header.content_length = nil
                ngx.header.set_cookie = nil

                if ngx.header.location then
                    local _location = ngx.header.location
                    _location = ngx.escape_uri(_location)
                    _location = "http://10.0.9.44/?_redir_=" .. _location
                    ngx.header.location = _location
                end
            ';

            body_filter_by_lua '

                local escUri = function (m)
                    local _esc = "href=\\"http://10.0.9.44/?_redir_=" ..
ngx.escape_uri(m[1]) .. "\\""
                    print(_esc)
                    return _esc
                end

                local chunk, eof = ngx.arg[1], ngx.arg[2]
                local buffered = ngx.ctx.buffered
                if not buffered then
                    buffered = {}
                    ngx.ctx.buffered = buffered
                end

                if chunk ~= "" then
                    buffered[#buffered + 1] = chunk
                    ngx.arg[1] = nil
                end

                if eof then
                    local whole = table.concat(buffered)
                    ngx.ctx.buffered = nil
                    local newStr, n, err = ngx.re.gsub(whole,
"href=\\"(.*)\\"", escUri, "i")
                    ngx.arg[1] = whole
                    print(whole)
                end
            ';

Debug Logs: 

2014/06/27 22:51:34 [debug] 9059#0: *1 http output filter "/?"
2014/06/27 22:51:34 [debug] 9059#0: *1 http copy filter: "/?"
2014/06/27 22:51:34 [debug] 9059#0: *1 lua body filter for user lua code,
uri "/"
2014/06/27 22:51:34 [debug] 9059#0: *1 lua fetching existing ngx.ctx table
for the current request
2014/06/27 22:51:34 [debug] 9059#0: *1 lua fetching existing ngx.ctx table
for the current request
2014/06/27 22:51:34 [debug] 9059#0: *1 lua compiling gsub regex
"href="(.*)"" with options "i" (compile once: 0) (dfa mode: 0) (jit mode:
0)
2014/06/27 22:51:34 [notice] 9059#0: *1 [lua] body_filter_by_lua:5:
href="http://10.0.9.44/?_redir_=http%3a%2f%2fwww.google.co.in%2f%3fgfe_rd%3dcr%26amp%3bei%3dHqitU86qKubV8gfRzYDoAQ"
while sending to client, client: 10.0.9.44, server: 127.0.0.1, request: "GET
/?_redir_=www.google.com HTTP/1.1", upstream: "http://173.194.36.52:80/",
host: "10.0.9.44"
2014/06/27 22:51:34 [debug] 9059#0: *1 lua allocate new chainlink and new
buf of size 261, cl:086B49B0
2014/06/27 22:51:34 [notice] 9059#0: *1 [lua] body_filter_by_lua:26:
<HTML><HEAD><meta http-equiv="content-type"
content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A
HREF="http://www.google.co.in/?gfe_rd=cr&ei=HqitU86qKubV8gfRzYDoAQ">here</A>.^M
</BODY></HTML>^M
 while sending to client, client: 10.0.9.44, server: 127.0.0.1, request:
"GET /?_redir_=www.google.com HTTP/1.1", upstream:
"http://173.194.36.52:80/", host: "10.0.9.44"
2014/06/27 22:51:34 [debug] 9059#0: *1 lua capture body filter, uri "/"


As you can see, print(_esc) show that the URL was successfully URLencoded.
Yet, the print(whole) line does not reflect the gsub()

What could be issue here ?

-Vamshi

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,251248,251248#msg-251248



More information about the nginx mailing list