1 --[==========================================================================[
2 http.lua: HTTP interface module for VLC
3 --[==========================================================================[
4 Copyright (C) 2007 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 a 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 --]==========================================================================]
38 vlc.msg.err("Lua HTTP interface")
44 return (string.gsub(s,"([%^%$%%%.%[%]%*%+%-%?])","%%%1"))
47 function process_raw(filename)
48 local input = io.open(filename):read("*a")
49 -- find the longest [===[ or ]=====] type sequence and make sure that
50 -- we use one that's longer.
52 for str2 in string.gmatch(input,"[%[%]]=*[%[%]]") do
53 if #str < #str2 then str = str2 end
55 str=string.rep("=",#str-1)
58 <?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.
60 local code0 = string.gsub(input,escape(close_tag)," print(["..str.."[")
61 local code1 = string.gsub(code0,escape(open_tag),"]"..str.."]) ")
62 local code = "print(["..str.."["..code1.."]"..str.."])"
63 --[[ Uncomment to debug
64 if string.match(filename,"vlm_cmd.xml$") then
69 return assert(loadstring(code,filename))
71 function process(filename)
72 vlc.msg.dbg("Loading `"..filename.."'")
73 local mtime = 0 -- vlc.fd.stat(filename).modification_time
74 local func = false -- process_raw(filename)
76 local new_mtime = vlc.fd.stat(filename).modification_time
77 if new_mtime ~= mtime then
78 -- Re-read the file if it changed
79 vlc.msg.dbg("Reloading `"..filename.."'")
80 func = process_raw(filename)
88 function callback_error(path,url,msg)
89 local url = url or "<page unknown>"
90 return [[<html xmlns="http://www.w3.org/1999/xhtml">
92 <title>Error loading ]]..url..[[</title>
95 <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>
97 <a href="http://www.videolan.org/">VideoLAN</a><br/>
98 <a href="http://www.lua.org/manual/5.1/">Lua 5.1 Reference Manual</a>
104 function dirlisting(url,listing)
106 for _,f in ipairs(listing) do
107 if not string.match(f,"^%.") then
108 table.insert(list,"<li><a href='"..f.."'>"..f.."</a></li>")
111 list = table.concat(list)
112 local function callback()
113 return [[<html xmlns="http://www.w3.org/1999/xhtml">
115 <title>Directory listing ]]..url..[[</title>
118 <h1>Directory listing ]]..url..[[</h1><ul>]]..list..[[</ul>
122 return h:file_new(url,"text/html",nil,nil,nil,callback,nil)
125 function file(h,path,url,acl_,mime)
126 local generate_page = process(path)
127 local callback = function(data,request)
128 -- FIXME: I'm sure that we could define a real sandbox
131 local function pageprint(...)
132 for i=1,select("#",...) do
134 table.insert(page,tostring(select(i,...)))
136 table.insert(page," "..tostring(select(i,...)))
140 _G._GET = parse_url_request(request)
141 local oldprint = print
143 local ok, msg = pcall(generate_page)
147 return callback_error(path,url,msg)
149 return table.concat(page)
151 return h:file_new(url or path,mime,nil,nil,acl_,callback,nil)
154 function rawfile(h,path,url,acl_)
155 local filename = path
156 vlc.msg.dbg("Loading `"..filename.."'")
157 local mtime = 0 -- vlc.fd.stat(filename).modification_time
158 local page = false -- io.open(filename):read("*a")
159 local callback = function(data,request)
160 local new_mtime = vlc.fd.stat(filename).modification_time
161 if mtime ~= new_mtime then
162 -- Re-read the file if it changed
163 vlc.msg.dbg("Reloading `"..filename.."'")
164 page = io.open(filename):read("*a")
169 return h:file_new(url or path,nil,nil,nil,acl_,callback,nil)
172 function parse_url_request(request)
173 if not request then return {} end
175 for k,v in string.gmatch(request,"([^=&]+)=?([^=&]*)") do
176 local k_ = vlc.decode_uri(k)
177 local v_ = vlc.decode_uri(v)
183 local function find_datadir(name)
184 local list = vlc.datadir_list(name)
185 for _, l in ipairs(list) do
186 local s = vlc.fd.stat(l)
191 error("Unable to find the `"..name.."' directory.")
193 http_dir = config.dir or find_datadir("http-lua")
196 local oldpath = package.path
197 package.path = http_dir.."/?.lua"
198 local ok, err = pcall(require,"custom")
200 vlc.msg.warn("Couldn't load "..http_dir.."/custom.lua")
202 vlc.msg.dbg("Loaded "..http_dir.."/custom.lua")
204 package.path = oldpath
211 js = "text/javascript",
213 ico = "image/x-icon",
215 local function load_dir(dir,root,parent_acl)
216 local root = root or "/"
217 local has_index = false
218 if string.match(dir,"/old") then
221 local my_acl = parent_acl
223 local af = dir.."/.hosts"
224 local s = vlc.fd.stat(af)
225 if s and s.type == "file" then
227 my_acl = acl.new(false)
231 local d = vlc.fd.opendir(dir)
232 for _,f in ipairs(d) do
233 if not string.match(f,"^%.") then
234 local s = vlc.fd.stat(dir.."/"..f)
235 if s.type == "file" then
237 if f == "index.html" then
243 local ext = string.match(f,"%.([^%.]-)$")
244 local mime = mimes[ext]
246 if mime and string.match(mime,"^text/") then
247 table.insert(files,file(h,dir.."/"..f,url,my_acl and my_acl.private,mime))
249 table.insert(files,rawfile(h,dir.."/"..f,url,my_acl and my_acl.private))
251 elseif s.type == "dir" then
252 if f == "dialogs" then -- FIXME
254 load_dir(dir.."/"..f,root..f.."/",my_acl)
259 if not has_index and not config.no_index then
260 -- print("Adding index for", root)
261 table.insert(files,dirlisting(root,d,my_acl and my_acl.private))
265 local u = vlc.net.url_parse( config.host or "localhost:8080" )
266 h = httpd.new(u.host,u.port)
269 while not die do die = vlc.lock_and_wait() end -- everything happens in callbacks
271 -- FIXME: We shouldn't need to do this ourselves.
273 getmetatable(files[i]).__gc(files[i])