]> git.sesse.net Git - vlc/blob - share/lua/intf/http.lua
Lua: remove httpd ACL support
[vlc] / share / lua / intf / http.lua
1 --[==========================================================================[
2  http.lua: HTTP interface module for VLC
3 --[==========================================================================[
4  Copyright (C) 2007-2009 the VideoLAN team
5  $Id$
6
7  Authors: Antoine Cellerier <dionoea at videolan dot org>
8
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 --]==========================================================================]
23
24 --[==========================================================================[
25 Configuration options:
26  * dir: Directory to use as the http interface's root.
27  * no_error_detail: If set, do not print the Lua error message when generating
28                     a page fails.
29  * no_index: If set, don't build directory indexes
30 --]==========================================================================]
31
32
33 require "common"
34
35 vlc.msg.info("Lua HTTP interface")
36
37 open_tag = "<?vlc"
38 close_tag = "?>"
39
40 -- TODO: use internal VLC mime lookup function for mimes not included here
41 local mimes = {
42     txt = "text/plain",
43     json = "text/plain",
44     html = "text/html",
45     xml = "text/xml",
46     js = "text/javascript",
47     css = "text/css",
48     png = "image/png",
49     jpg = "image/jpeg",
50     jpeg = "image/jpeg",
51     ico = "image/x-icon",
52 }
53
54 function escape(s)
55     return (string.gsub(s,"([%^%$%%%.%[%]%*%+%-%?])","%%%1"))
56 end
57
58 function process_raw(filename)
59     local input = io.open(filename):read("*a")
60     -- find the longest [===[ or ]=====] type sequence and make sure that
61     -- we use one that's longer.
62     local str="X"
63     for str2 in string.gmatch(input,"[%[%]]=*[%[%]]") do
64         if #str < #str2 then str = str2 end
65     end
66     str=string.rep("=",#str-1)
67
68     --[[ FIXME:
69     <?xml version="1.0" encoding="charset" standalone="yes" ?> is still a problem. The closing '?>' needs to be printed using '?<?vlc print ">" ?>' to prevent a parse error.
70     --]]
71     local code0 = string.gsub(input,escape(close_tag)," print(["..str.."[")
72     local code1 = string.gsub(code0,escape(open_tag),"]"..str.."]) ")
73     local code = "print(["..str.."["..code1.."]"..str.."])"
74     --[[ Uncomment to debug
75     if string.match(filename,"vlm_cmd.xml$") then
76     io.write(code)
77     io.write("\n")
78     end
79     --]]
80     return assert(loadstring(code,filename))
81 end
82
83 function process(filename)
84     local mtime = 0    -- vlc.net.stat(filename).modification_time
85     local func = false -- process_raw(filename)
86     return function(...)
87         local new_mtime = vlc.net.stat(filename).modification_time
88         if new_mtime ~= mtime then
89             -- Re-read the file if it changed
90             if mtime == 0 then
91                 vlc.msg.dbg("Loading `"..filename.."'")
92             else
93                 vlc.msg.dbg("Reloading `"..filename.."'")
94             end
95             func = process_raw(filename)
96             mtime = new_mtime
97         end
98         return func(...)
99     end
100 end
101
102
103 function callback_error(path,url,msg)
104     local url = url or "&lt;page unknown&gt;"
105     return  [[<html xmlns="http://www.w3.org/1999/xhtml">
106 <head>
107 <title>Error loading ]]..url..[[</title>
108 </head>
109 <body>
110 <h1>Error loading ]]..url..[[</h1><pre>]]..(config.no_error_detail and "Remove configuration option `no_error_detail' on the server to get more information." or tostring(msg))..[[</pre>
111 <p>
112 <a href="http://www.videolan.org/">VideoLAN</a><br/>
113 <a href="http://www.lua.org/manual/5.1/">Lua 5.1 Reference Manual</a>
114 </p>
115 </body>
116 </html>]]
117 end
118
119 function dirlisting(url,listing)
120     local list = {}
121     for _,f in ipairs(listing) do
122         if not string.match(f,"^%.") then
123             table.insert(list,"<li><a href='"..f.."'>"..f.."</a></li>")
124         end
125     end
126     list = table.concat(list)
127     local function callback()
128         return [[<html xmlns="http://www.w3.org/1999/xhtml">
129 <head>
130 <title>Directory listing ]]..url..[[</title>
131 </head>
132 <body>
133 <h1>Directory listing ]]..url..[[</h1><ul>]]..list..[[</ul>
134 </body>
135 </html>]]
136     end
137     return h:file(url,"text/html",nil,password,callback,nil)
138 end
139
140 -- FIXME: Experimental art support. Needs some cleaning up.
141 function callback_art(data, request, args)
142     local art = function(data, request)
143         local num = nil
144         if args ~= nil then
145             num = string.gmatch(args, "item=(.*)")
146             if num ~= nil then
147                 num = num()
148             end
149         end
150         local item
151         if num == nil then
152             item = vlc.input.item()
153         else
154             item = vlc.playlist.get(num).item
155         end
156         local metas = item:metas()
157         local filename = vlc.strings.decode_uri(string.gsub(metas["artwork_url"],"file://",""))
158         local size = vlc.net.stat(filename).size
159         local ext = string.match(filename,"%.([^%.]-)$")
160         local raw = io.open(filename):read("*a")
161         local content = [[Content-Type: ]]..mimes[ext]..[[
162
163 Content-Length: ]]..size..[[
164
165
166 ]]..raw..[[
167
168 ]]
169         return content
170     end
171
172     local ok, content = pcall(art, data, request)
173     if not ok then
174         return [[Status: 404
175 Content-Type: text/plain
176 Content-Length: 5
177
178 Error
179 ]]
180     end
181     return content
182 end
183
184 function file(h,path,url,mime)
185     local generate_page = process(path)
186     local callback = function(data,request)
187         -- FIXME: I'm sure that we could define a real sandbox
188         -- redefine print
189         local page = {}
190         local function pageprint(...)
191             for i=1,select("#",...) do
192                 if i== 1 then
193                     table.insert(page,tostring(select(i,...)))
194                 else
195                     table.insert(page," "..tostring(select(i,...)))
196                 end
197             end
198         end
199         _G._GET = parse_url_request(request)
200         local oldprint = print
201         print = pageprint
202         local ok, msg = pcall(generate_page)
203         -- reset
204         print = oldprint
205         if not ok then
206             return callback_error(path,url,msg)
207         end
208         return table.concat(page)
209     end
210     return h:file(url or path,mime,nil,password,callback,nil)
211 end
212
213 function rawfile(h,path,url)
214     local filename = path
215     local mtime = 0    -- vlc.net.stat(filename).modification_time
216     local page = false -- io.open(filename):read("*a")
217     local callback = function(data,request)
218         local new_mtime = vlc.net.stat(filename).modification_time
219         if mtime ~= new_mtime then
220             -- Re-read the file if it changed
221             if mtime == 0 then
222                 vlc.msg.dbg("Loading `"..filename.."'")
223             else
224                 vlc.msg.dbg("Reloading `"..filename.."'")
225             end
226             page = io.open(filename,"rb"):read("*a")
227             mtime = new_mtime
228         end
229         return page
230     end
231     return h:file(url or path,nil,nil,password,callback,nil)
232 end
233
234 function parse_url_request(request)
235     if not request then return {} end
236     local t = {}
237     for k,v in string.gmatch(request,"([^=&]+)=?([^=&]*)") do
238         local k_ = vlc.strings.decode_uri(k)
239         local v_ = vlc.strings.decode_uri(v)
240         if t[k_] ~= nil then
241             local t2
242             if type(t[k_]) ~= "table" then
243                 t2 = {}
244                 table.insert(t2,t[k_])
245                 t[k_] = t2
246             else
247                 t2 = t[k_]
248             end
249             table.insert(t2,v_)
250         else
251             t[k_] = v_
252         end
253     end
254     return t
255 end
256
257 local function find_datadir(name)
258     local list = vlc.config.datadir_list(name)
259     for _, l in ipairs(list) do
260         local s = vlc.net.stat(l)
261         if s then
262             return l
263         end
264     end
265     error("Unable to find the `"..name.."' directory.")
266 end
267 http_dir = config.dir or find_datadir("http")
268
269 do
270     local oldpath = package.path
271     package.path = http_dir.."/?.lua"
272     local ok, err = pcall(require,"custom")
273     if not ok then
274         vlc.msg.warn("Couldn't load "..http_dir.."/custom.lua",err)
275     else
276         vlc.msg.dbg("Loaded "..http_dir.."/custom.lua")
277     end
278     package.path = oldpath
279 end
280 local files = {}
281 local function load_dir(dir,root)
282     local root = root or "/"
283     local has_index = false
284     local d = vlc.net.opendir(dir)
285     for _,f in ipairs(d) do
286         if not string.match(f,"^%.") then
287             local s = vlc.net.stat(dir.."/"..f)
288             if s.type == "file" then
289                 local url
290                 if f == "index.html" then
291                     url = root
292                     has_index = true
293                 else
294                     url = root..f
295                 end
296                 local ext = string.match(f,"%.([^%.]-)$")
297                 local mime = mimes[ext]
298                 -- print(url,mime)
299                 if mime and string.match(mime,"^text/") then
300                     table.insert(files,file(h,dir.."/"..f,url,mime))
301                 else
302                     table.insert(files,rawfile(h,dir.."/"..f,url))
303                 end
304             elseif s.type == "dir" then
305                 load_dir(dir.."/"..f,root..f.."/")
306             end
307         end
308     end
309     if not has_index and not config.no_index then
310         -- print("Adding index for", root)
311         table.insert(files,dirlisting(root,d))
312     end
313 end
314
315 if config.host then
316     vlc.msg.err("\""..config.host.."\" HTTP host ignored")
317     local port = string.match(config.host, ":(%d+)[^]]*$")
318     vlc.msg.info("Pass --http-host=IP "..(port and "and --http-port="..port.." " or "").."on the command line instead.")
319 end
320
321 password = vlc.var.inherit(nil,"http-password")
322 h = vlc.httpd()
323 local a = h:handler("/art",nil,password,callback_art,nil)