* Preliminary module implementation for Luci HTTPD
[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 file = self.docroot .. request.env.REQUEST_URI:gsub("../", "")
16 local size = luci.fs.stat(file, "size")
17 if size then
18 return Response(200, {["Content-Length"] = size}), ltn12.source.file(io.open(file))
19 else
20 return Response(404)
21 end
22 end