Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local start=os.clock()
- local string=string
- local lfs=require"lfs"
- function isdir(fn)
- return (lfs.attributes(fn, "mode") == 'directory')
- end
- function exists(fn)
- return lfs.attributes(fn, "mode")
- end
- local function findcorrect(path,what)
- local ok,a,b,c,d=pcall(lfs.dir,path)
- if not ok then return false,a end
- what=what:lower()
- local found=false
- for file in a,b,c,d do
- if file ~= "." and file ~= ".." then
- if file:lower()==what then
- found=file
- break
- end
- end
- end
- return found
- end
- ngx.status=ngx.HTTP_NOT_FOUND
- status = ngx.status
- local root=ngx.var.document_root
- local url=ngx.var.uri
- -- find it the easy way
- local reqpath,delim,reqfile=url:match("(.*)([/\\])(.+)")
- local path=ngx.var.document_root..reqpath--lfs.currentdir()
- local correct=findcorrect(path,reqfile)
- if correct then
- ngx.status=ngx.HTTP_MOVED_PERMANENTLY
- status = ngx.status
- local f = reqpath..delim..correct
- return ngx.redirect(f, ngx.HTTP_MOVED_PERMANENTLY)
- end
- -- nope, let's find it the hard way. ADD CACHING D:
- local nowpath=""
- for str in url:gmatch"[^/]+" do
- local dir=exists(root..nowpath..str)
- local setstr
- if not dir then
- setstr=findcorrect(root..nowpath,str)
- else
- setstr=str
- end
- if not setstr then nowpath=false break end
- nowpath=nowpath..'/'..setstr
- end
- if nowpath then
- ngx.status=ngx.HTTP_MOVED_PERMANENTLY
- status = ngx.status
- return ngx.redirect(nowpath, ngx.HTTP_MOVED_PERMANENTLY)
- end
- ngx.status=ngx.HTTP_NOT_FOUND
- status = ngx.status
- ngx.say(string.format("Sorry, couldn't find that file. I looked it for: %.2f\n", os.clock() - start))
- return ngx.exit(ngx.HTTP_NOT_FOUND)
Advertisement
Add Comment
Please, Sign In to add comment