1f3e9480206019644abb5cbe5f7d7004fedf998f
[project/luci.git] / libs / httpd / luasrc / httpd / FileHandler.lua
1 module("luci.httpd.FileHandler", package.seeall)
2 require("luci.util")
3 require("luci.fs")
4 require("ltn12")
5
6 SimpleHandler = luci.util.class(luci.httpd.Handler)
7
8 function SimpleHandler.__init__(self, docroot)
9 luci.httpd.Handler.__init__(self)
10 self.docroot = docroot
11 end
12
13 function SimpleHandler.handle(self, request)
14 local response = luci.httpd.Response()
15 local f = self.docroot .. "/" .. request.request_uri:gsub("%.%./", "")
16 request.error:write("Requested " .. f .. "\n")
17 local s = luci.fs.stat(f, "size")
18 if s then
19 response:addheader("Content-Length", s)
20 response:setsource(ltn12.source.file(io.open(f)))
21 else
22 response:setstatus(404)
23 end
24 end