I have very strange bug with this.
local str = ngx.arg[1]
local script = "<script>alert('Hello');</script></body>"
ngx.arg[1] = ngx.re.gsub(str,'</body>',script)
So when i proxy site like nginx.com(here config)
location = / {
proxy_pass http://nginx.com/;
proxy_set_header Host "nginx.com";
proxy_ignore_headers Expires Cache-Control;
header_filter_by_lua 'ngx.header.content_length = nil';
body_filter_by_lua_file conf/filter.lua;
}
location ~ \.(png|jpg|jpeg|gif|js|css|ico|bmp)$ {
rewrite ^ http://nginx.com/$request_uri? permanent;
}
Than i see result in browser, but when i try to proxy something like google.co.uk or some other sites.
location = / {
proxy_pass http://www.google.co.uk/;
proxy_set_header Host "www.google.co.uk";
proxy_ignore_headers Expires Cache-Control;
header_filter_by_lua 'ngx.header.content_length = nil';
body_filter_by_lua_file conf/filter.lua;
}
location ~ \.(png|jpg|jpeg|gif|js|css|ico|bmp)$ {
rewrite ^ http://www.google.co.uk/$request_uri? permanent;
}
Ain't see changes in html from browsers(Chrome,SrWare,Firefox,IE,Opera) but when i load page from curl or wget i see that html has changed.
So i decided(maybe mistake) that webbrowser ignore(how?) html from openresty and load from origin source. But how to deal with that?
Thanks.
On Saturday, November 10, 2012 3:14:25 AM UTC+4, agentzh wrote:
Hello!
On Fri, Nov 9, 2012 at 2:05 PM, Guilherme wrote:
> I'm trying to make string replacement in body_filter_by_lua, but the
> replaced content is being truncated. The mainly purpose of this is to
> append, dynamically (per website), some _javascript_s in response body.
>
>
> body_filter_by_lua
> '
> replacestr = "<script>sdsdsdsdsd</script></head>"
> ngx.arg[1] = ngx.re.sub(ngx.arg[1],"</head>", replacestr)
> ';
>
> The original html version:
>
> <html>
> <head>
> </head>
> <body>
> test
> </body>
> </html>
>
> After body_filter_by_lua:
>
> <html>
> <head>
> <script>sdsdsdsd
>
> It looks like body_filter_by_lua is truncating in 16bytes.
>
Does your original response contain a Content-Length response header?
If yes, then you should clear it first in header_filter_by_lua,
because you change the response body length.
You can try something like this:
location = /test.html {
header_filter_by_lua '
ngx.header.content_length = nil
';
body_filter_by_lua
'
replacestr = "<script>sdsdsdsdsd</script></head>"
ngx.arg[1] = ngx.re.sub(ngx.arg[1],"</head>", replacestr)
';
}
Best regards,
-agentzh