b57b5586a21554ff2b001d7d86d526a4cf14788f
[project/luci.git] / libs / lucid-http / luasrc / lucid / http / DirectoryPublisher.lua
1 --[[
2 LuCId HTTP-Slave
3 (c) 2009 Steven Barth <steven@midlink.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12 ]]--
13
14 local ipairs, require, tostring, type = ipairs, require, tostring, type
15 local file = require "luci.lucid.http.handler.file"
16 local srv = require "luci.lucid.http.server"
17
18 module "luci.lucid.http.DirectoryPublisher"
19
20
21 --- Prepare a directory publisher and assign it to a given Virtual Host.
22 -- @param server HTTP daemon object
23 -- @param config publisher configuration
24 function factory(server, config)
25         config.domain = config.domain or ""
26         local vhost = server:get_vhosts()[config.domain] 
27         if not vhost then
28                 vhost = srv.VHost()
29                 server:set_vhost(config.domain, vhost)
30         end
31
32         local handler = file.Simple(config.name, config.physical, config)
33         if config.read then
34                 for _, r in ipairs(config.read) do
35                         if r:sub(1,1) == ":" then
36                                 handler:restrict({interface = r:sub(2)})
37                         else
38                                 handler:restrict({user = r})
39                         end
40                 end
41         end
42         
43         if type(config.virtual) == "table" then
44                 for _, v in ipairs(config.virtual) do
45                         vhost:set_handler(v, handler)
46                 end
47         else
48                 vhost:set_handler(config.virtual, handler)
49         end
50 end