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 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 --]==========================================================================]
38 vlc.msg.info("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 local mtime = 0 -- vlc.fd.stat(filename).modification_time
73 local func = false -- process_raw(filename)
75 local new_mtime = vlc.fd.stat(filename).modification_time
76 if new_mtime ~= mtime then
77 -- Re-read the file if it changed
79 vlc.msg.dbg("Loading `"..filename.."'")
81 vlc.msg.dbg("Reloading `"..filename.."'")
83 func = process_raw(filename)
91 function callback_error(path,url,msg)
92 local url = url or "<page unknown>"
93 return [[<html xmlns="http://www.w3.org/1999/xhtml">
95 <title>Error loading ]]..url..[[</title>
98 <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>
100 <a href="http://www.videolan.org/">VideoLAN</a><br/>
101 <a href="http://www.lua.org/manual/5.1/">Lua 5.1 Reference Manual</a>
107 function dirlisting(url,listing,acl_)
109 for _,f in ipairs(listing) do
110 if not string.match(f,"^%.") then
111 table.insert(list,"<li><a href='"..f.."'>"..f.."</a></li>")
114 list = table.concat(list)
115 local function callback()
116 return [[<html xmlns="http://www.w3.org/1999/xhtml">
118 <title>Directory listing ]]..url..[[</title>
121 <h1>Directory listing ]]..url..[[</h1><ul>]]..list..[[</ul>
125 return h:file_new(url,"text/html",nil,nil,acl_,callback,nil)
128 function file(h,path,url,acl_,mime)
129 local generate_page = process(path)
130 local callback = function(data,request)
131 -- FIXME: I'm sure that we could define a real sandbox
134 local function pageprint(...)
135 for i=1,select("#",...) do
137 table.insert(page,tostring(select(i,...)))
139 table.insert(page," "..tostring(select(i,...)))
143 _G._GET = parse_url_request(request)
144 local oldprint = print
146 local ok, msg = pcall(generate_page)
150 return callback_error(path,url,msg)
152 return table.concat(page)
154 return h:file_new(url or path,mime,nil,nil,acl_,callback,nil)
157 function rawfile(h,path,url,acl_)
158 local filename = path
159 local mtime = 0 -- vlc.fd.stat(filename).modification_time
160 local page = false -- io.open(filename):read("*a")
161 local callback = function(data,request)
162 local new_mtime = vlc.fd.stat(filename).modification_time
163 if mtime ~= new_mtime then
164 -- Re-read the file if it changed
166 vlc.msg.dbg("Loading `"..filename.."'")
168 vlc.msg.dbg("Reloading `"..filename.."'")
170 page = io.open(filename):read("*a")
175 return h:file_new(url or path,nil,nil,nil,acl_,callback,nil)
178 function parse_url_request(request)
179 if not request then return {} end
181 for k,v in string.gmatch(request,"([^=&]+)=?([^=&]*)") do
182 local k_ = vlc.decode_uri(k)
183 local v_ = vlc.decode_uri(v)
186 if type(t[k_]) ~= "table" then
188 table.insert(t2,t[k_])
201 local function find_datadir(name)
202 local list = vlc.datadir_list(name)
203 for _, l in ipairs(list) do
204 local s = vlc.fd.stat(l)
209 error("Unable to find the `"..name.."' directory.")
211 http_dir = config.dir or find_datadir("http")
214 local oldpath = package.path
215 package.path = http_dir.."/?.lua"
216 local ok, err = pcall(require,"custom")
218 vlc.msg.warn("Couldn't load "..http_dir.."/custom.lua")
220 vlc.msg.dbg("Loaded "..http_dir.."/custom.lua")
222 package.path = oldpath
229 js = "text/javascript",
232 ico = "image/x-icon",
234 local function load_dir(dir,root,parent_acl)
235 local root = root or "/"
236 local has_index = false
237 local my_acl = parent_acl
239 local af = dir.."/.hosts"
240 local s = vlc.fd.stat(af)
241 if s and s.type == "file" then
243 my_acl = acl.new(false)
247 local d = vlc.fd.opendir(dir)
248 for _,f in ipairs(d) do
249 if not string.match(f,"^%.") then
250 local s = vlc.fd.stat(dir.."/"..f)
251 if s.type == "file" then
253 if f == "index.html" then
259 local ext = string.match(f,"%.([^%.]-)$")
260 local mime = mimes[ext]
262 if mime and string.match(mime,"^text/") then
263 table.insert(files,file(h,dir.."/"..f,url,my_acl and my_acl.private,mime))
265 table.insert(files,rawfile(h,dir.."/"..f,url,my_acl and my_acl.private))
267 elseif s.type == "dir" then
268 load_dir(dir.."/"..f,root..f.."/",my_acl)
272 if not has_index and not config.no_index then
273 -- print("Adding index for", root)
274 table.insert(files,dirlisting(root,d,my_acl and my_acl.private))
278 local u = vlc.net.url_parse( config.host or "localhost:8080" )
279 h = httpd.new(u.host,u.port)
282 while not die do die = vlc.lock_and_wait() end -- everything happens in callbacks
284 -- FIXME: We shouldn't need to do this ourselves.
286 getmetatable(files[i]).__gc(files[i])