Forum: NGINX changes to ngx.argnot getting reflected in final response

2974d09ac2541e892966b762aad84943?d=identicon&s=25 vamshi (Guest)
on 2014-06-27 19:34
(Received via mailing list)
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....
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&amp;ei=HqitU86q...
</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
2974d09ac2541e892966b762aad84943?d=identicon&s=25 jdewald (Guest)
on 2014-07-02 01:46
(Received via mailing list)
vamshi Wrote:
-------------------------------------------------------
>                 end
>
>                 end
> ...
>
>
> 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

gsub is going to return the results of the substitution, not do it
inline.
You should be outputting/assigning newStr not whole.

Cheers,
Josh

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,251248,251425#msg-251425
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.