83549f33859591c31c087e8bd9ed99607a5a3e2d
[project/luci.git] / libs / httpd / luasrc / httpd / handler / file.lua
1 module("luci.httpd.handler.file", package.seeall)
2 require("luci.httpd.module")
3 require("luci.fs")
4 require("ltn12")
5
6 Simple = luci.util.class(luci.httpd.module.Handler)
7 Response = luci.httpd.module.Response
8
9 function Simple.__init__(self, docroot)
10         luci.httpd.module.Handler.__init__(self)
11         self.docroot = docroot
12 end
13
14 function Simple.handle(self, request, sourcein, sinkerr)
15         local uri  = request.env.PATH_INFO
16         local file = self.docroot .. uri:gsub("%.%./", "")
17         local stat = luci.fs.stat(file)
18
19         if stat then
20                 if stat.type == "regular" then
21                         return Response(200, {["Content-Length"] = stat.size}), ltn12.source.file(io.open(file))
22                 else
23                         return self:failure(403, "Unable to transmit " .. stat.type .. " " .. uri)
24                 end
25         else
26                 return self:failure(404, "No such file: " .. uri)
27         end
28 end