1 --[==========================================================================[
2 http.lua: HTTP interface module for VLC
3 --[==========================================================================[
4 Copyright (C) 2007-2009 the VideoLAN team
7 Authors: Antoine Cellerier <dionoea at videolan dot org>
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.
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.
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 --]==========================================================================]
24 --[==========================================================================[
25 Configuration options:
26 * host: A host to listen on.
27 * dir: Directory to use as the http interface's root.
28 * no_error_detail: If set, do not print the Lua error message when generating
30 * no_index: If set, don't build directory indexes
31 --]==========================================================================]
36 vlc.msg.info("Lua HTTP interface")
41 -- TODO: use internal VLC mime lookup function for mimes not included here
46 js = "text/javascript",
55 return (string.gsub(s,"([%^%$%%%.%[%]%*%+%-%?])","%%%1"))
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.
63 for str2 in string.gmatch(input,"[%[%]]=*[%[%]]") do
64 if #str < #str2 then str = str2 end
66 str=string.rep("=",#str-1)
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.
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
80 return assert(loadstring(code,filename))
83 function process(filename)
84 local mtime = 0 -- vlc.net.stat(filename).modification_time
85 local func = false -- process_raw(filename)
87 local new_mtime = vlc.net.stat(filename).modification_time
88 if new_mtime ~= mtime then
89 -- Re-read the file if it changed
91 vlc.msg.dbg("Loading `"..filename.."'")
93 vlc.msg.dbg("Reloading `"..filename.."'")
95 func = process_raw(filename)
103 function callback_error(path,url,msg)
104 local url = url or "<page unknown>"
105 return [[<html xmlns="http://www.w3.org/1999/xhtml">
107 <title>Error loading ]]..url..[[</title>
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>
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>
119 function dirlisting(url,listing,acl_)
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>")
126 list = table.concat(list)
127 local function callback()
128 return [[<html xmlns="http://www.w3.org/1999/xhtml">
130 <title>Directory listing ]]..url..[[</title>
133 <h1>Directory listing ]]..url..[[</h1><ul>]]..list..[[</ul>
137 return h:file(url,"text/html",nil,nil,acl_,callback,nil)
140 -- FIXME: Experimental art support. Needs some cleaning up.
141 function callback_art(data, request)
142 local art = function(data, request)
143 local metas = vlc.input.metas()
144 local filename = vlc.strings.decode_uri(string.gsub(metas["artwork_url"],"file://",""))
145 local size = vlc.net.stat(filename).size
146 local ext = string.match(filename,"%.([^%.]-)$")
147 local raw = io.open(filename):read("*a")
148 local content = [[Content-Type: ]]..mimes[ext]..[[
150 Content-Length: ]]..size..[[
159 local ok, content = pcall(art, data, request)
162 Content-Type: text/plain
171 function file(h,path,url,acl_,mime)
172 local generate_page = process(path)
173 local callback = function(data,request)
174 -- FIXME: I'm sure that we could define a real sandbox
177 local function pageprint(...)
178 for i=1,select("#",...) do
180 table.insert(page,tostring(select(i,...)))
182 table.insert(page," "..tostring(select(i,...)))
186 _G._GET = parse_url_request(request)
187 local oldprint = print
189 local ok, msg = pcall(generate_page)
193 return callback_error(path,url,msg)
195 return table.concat(page)
197 return h:file(url or path,mime,nil,nil,acl_,callback,nil)
200 function rawfile(h,path,url,acl_)
201 local filename = path
202 local mtime = 0 -- vlc.net.stat(filename).modification_time
203 local page = false -- io.open(filename):read("*a")
204 local callback = function(data,request)
205 local new_mtime = vlc.net.stat(filename).modification_time
206 if mtime ~= new_mtime then
207 -- Re-read the file if it changed
209 vlc.msg.dbg("Loading `"..filename.."'")
211 vlc.msg.dbg("Reloading `"..filename.."'")
213 page = io.open(filename):read("*a")
218 return h:file(url or path,nil,nil,nil,acl_,callback,nil)
221 function parse_url_request(request)
222 if not request then return {} end
224 for k,v in string.gmatch(request,"([^=&]+)=?([^=&]*)") do
225 local k_ = vlc.strings.decode_uri(k)
226 local v_ = vlc.strings.decode_uri(v)
229 if type(t[k_]) ~= "table" then
231 table.insert(t2,t[k_])
244 local function find_datadir(name)
245 local list = vlc.misc.datadir_list(name)
246 for _, l in ipairs(list) do
247 local s = vlc.net.stat(l)
252 error("Unable to find the `"..name.."' directory.")
254 http_dir = config.dir or find_datadir("http")
257 local oldpath = package.path
258 package.path = http_dir.."/?.lua"
259 local ok, err = pcall(require,"custom")
261 vlc.msg.warn("Couldn't load "..http_dir.."/custom.lua",err)
263 vlc.msg.dbg("Loaded "..http_dir.."/custom.lua")
265 package.path = oldpath
268 local function load_dir(dir,root,parent_acl)
269 local root = root or "/"
270 local has_index = false
271 local my_acl = parent_acl
273 local af = dir.."/.hosts"
274 local s = vlc.net.stat(af)
275 if s and s.type == "file" then
277 my_acl = vlc.acl(false)
281 local d = vlc.net.opendir(dir)
282 for _,f in ipairs(d) do
283 if not string.match(f,"^%.") then
284 local s = vlc.net.stat(dir.."/"..f)
285 if s.type == "file" then
287 if f == "index.html" then
293 local ext = string.match(f,"%.([^%.]-)$")
294 local mime = mimes[ext]
296 if mime and string.match(mime,"^text/") then
297 table.insert(files,file(h,dir.."/"..f,url,my_acl,mime))
299 table.insert(files,rawfile(h,dir.."/"..f,url,my_acl))
301 elseif s.type == "dir" then
302 load_dir(dir.."/"..f,root..f.."/",my_acl)
306 if not has_index and not config.no_index then
307 -- print("Adding index for", root)
308 table.insert(files,dirlisting(root,d,my_acl))
313 local u = vlc.net.url_parse( config.host or "0.0.0.0:8080" )
314 h = vlc.httpd(u.host,u.port)
315 local root_acl = load_dir( http_dir )
316 local a = h:handler("/art",nil,nil,root_acl,callback_art,nil)
318 while not vlc.misc.lock_and_wait() do end -- everything happens in callbacks