rewrite_by_lua '


function file_exists(path)
	local file = io.open(path, "rb")
	if file then file:close() end
	return file ~= nil
end

function writeMsg(host,url,msg)

	if (file_exists("/var/tmp/count"))
	then
		file=io.open("/var/tmp/msg.txt","a");
		io.output(file);
		io.write(string.format("%s%s	---%s\\n",host,url,msg));
		io.close()
	end
end

function string.indexOf(s, pattern, init)
    init = init or 0
    local index = string.find(s, pattern, init, true)
    return index or -1;
end

function string.insert(str,index,insertStr)
    local first = string.sub(str,1,index-1)
    local last = string.sub(str,index,-1)
    local createStr = string.format("%s%s%s",first,insertStr,last)
    return createStr
end


local ngx = require "ngx";
local url = ngx.var.uri;
local host = ngx.var.host;
local agent = ngx.var.http_user_agent
local request_url = ngx.var.request_uri;
local request_method = ngx.var.request_method;
local scheme = ngx.var.scheme
local server_port = ngx.var.server_port

request_url = string.format(" url: %s %s://%s:%s%s",request_method,scheme,host,server_port,request_url);


if agent ~= nil then
	agent = string.lower(agent)
    local m, ret = ngx.re.match(agent, "android|webos|iphone|ipod")
    if m == nil then
		return
	end
end


if(url==nil)then
	return
end


url = string.lower(url);


if(url ~= nil and (string.indexOf(url,".txt")>=1 or
   string.indexOf(url,".xml")>=1 or
   string.indexOf(url,".css")>=1 or
   string.indexOf(url,".gif")>=1 or
   string.indexOf(url,".png")>=1 or
   string.indexOf(url,".jpg")>=1 or
   string.indexOf(url,".ico")>=1 or
   string.indexOf(url,".bmp")>=1))
then
	return;
end

ngx.update_time();
math.randomseed(tonumber(tostring(ngx.now()*1000):reverse():sub(1, 9)));
local rand = math.random(1,100);
writeMsg(host,request_url,rand);

';