* libs/httpd: Introduced keep-alive and pipelining support
[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.getfile(self, uri)
15         local file = self.docroot .. uri:gsub("%.%./", "")
16         local stat = luci.fs.stat(file)
17         
18         return file, stat
19 end
20
21 function Simple.handle_get(self, request, sourcein, sinkerr)
22         local file, stat = self:getfile(request.env.PATH_INFO)
23
24         if stat then
25                 if stat.type == "regular" then
26                         return Response(200, {["Content-Length"] = stat.size}), ltn12.source.file(io.open(file))
27                 else
28                         return self:failure(403, "Unable to transmit " .. stat.type .. " " .. request.env.PATH_INFO)
29                 end
30         else
31                 return self:failure(404, "No such file: " .. uri)
32         end
33 end
34
35 function Simple.handle_head(self, ...)
36         local response, sourceout = self:handle_get(...)
37         return response
38 end