]> git.sesse.net Git - vlc/blobdiff - share/lua/intf/http.lua
use vlc.input.item() where appropriate
[vlc] / share / lua / intf / http.lua
index dde018e88765c841f8d51acf9884b6bf3751f772..c48e6bda243b490140922289a389031158b9ecac 100644 (file)
@@ -1,7 +1,7 @@
 --[==========================================================================[
  http.lua: HTTP interface module for VLC
 --[==========================================================================[
- Copyright (C) 2007 the VideoLAN team
+ Copyright (C) 2007-2009 the VideoLAN team
  $Id$
 
  Authors: Antoine Cellerier <dionoea at videolan dot org>
@@ -31,8 +31,6 @@ Configuration options:
 --]==========================================================================]
 
 
-require "httpd"
-require "acl"
 require "common"
 
 vlc.msg.info("Lua HTTP interface")
@@ -40,6 +38,19 @@ vlc.msg.info("Lua HTTP interface")
 open_tag = "<?vlc"
 close_tag = "?>"
 
+-- TODO: use internal VLC mime lookup function for mimes not included here
+local mimes = {
+    txt = "text/plain",
+    html = "text/html",
+    xml = "text/xml",
+    js = "text/javascript",
+    css = "text/css",
+    png = "image/png",
+    jpg = "image/jpeg",
+    jpeg = "image/jpeg",
+    ico = "image/x-icon",
+}
+
 function escape(s)
     return (string.gsub(s,"([%^%$%%%.%[%]%*%+%-%?])","%%%1"))
 end
@@ -68,11 +79,12 @@ function process_raw(filename)
     --]]
     return assert(loadstring(code,filename))
 end
+
 function process(filename)
-    local mtime = 0    -- vlc.fd.stat(filename).modification_time
+    local mtime = 0    -- vlc.net.stat(filename).modification_time
     local func = false -- process_raw(filename)
     return function(...)
-        local new_mtime = vlc.fd.stat(filename).modification_time
+        local new_mtime = vlc.net.stat(filename).modification_time
         if new_mtime ~= mtime then
             -- Re-read the file if it changed
             if mtime == 0 then
@@ -122,7 +134,39 @@ function dirlisting(url,listing,acl_)
 </body>
 </html>]]
     end
-    return h:file_new(url,"text/html",nil,nil,acl_,callback,nil)
+    return h:file(url,"text/html",nil,nil,acl_,callback,nil)
+end
+
+-- FIXME: Experimental art support. Needs some cleaning up.
+function callback_art(data, request)
+    local art = function(data, request)
+        local item = vlc.item()
+        local metas = item:metas()
+        local filename = vlc.strings.decode_uri(string.gsub(metas["artwork_url"],"file://",""))
+        local size = vlc.net.stat(filename).size
+        local ext = string.match(filename,"%.([^%.]-)$")
+        local raw = io.open(filename):read("*a")
+        local content = [[Content-Type: ]]..mimes[ext]..[[
+
+Content-Length: ]]..size..[[
+
+
+]]..raw..[[
+
+]]
+        return content
+    end
+
+    local ok, content = pcall(art, data, request)
+    if not ok then
+        return [[Status: 404
+Content-Type: text/plain
+Content-Length: 5
+
+Error
+]]
+    end
+    return content
 end
 
 function file(h,path,url,acl_,mime)
@@ -151,15 +195,15 @@ function file(h,path,url,acl_,mime)
         end
         return table.concat(page)
     end
-    return h:file_new(url or path,mime,nil,nil,acl_,callback,nil)
+    return h:file(url or path,mime,nil,nil,acl_,callback,nil)
 end
 
 function rawfile(h,path,url,acl_)
     local filename = path
-    local mtime = 0    -- vlc.fd.stat(filename).modification_time
+    local mtime = 0    -- vlc.net.stat(filename).modification_time
     local page = false -- io.open(filename):read("*a")
     local callback = function(data,request)
-        local new_mtime = vlc.fd.stat(filename).modification_time
+        local new_mtime = vlc.net.stat(filename).modification_time
         if mtime ~= new_mtime then
             -- Re-read the file if it changed
             if mtime == 0 then
@@ -172,15 +216,15 @@ function rawfile(h,path,url,acl_)
         end
         return page
     end
-    return h:file_new(url or path,nil,nil,nil,acl_,callback,nil)
+    return h:file(url or path,nil,nil,nil,acl_,callback,nil)
 end
 
 function parse_url_request(request)
     if not request then return {} end
-    t = {}
+    local t = {}
     for k,v in string.gmatch(request,"([^=&]+)=?([^=&]*)") do
-        local k_ = vlc.decode_uri(k)
-        local v_ = vlc.decode_uri(v)
+        local k_ = vlc.strings.decode_uri(k)
+        local v_ = vlc.strings.decode_uri(v)
         if t[k_] ~= nil then
             local t2
             if type(t[k_]) ~= "table" then
@@ -199,9 +243,9 @@ function parse_url_request(request)
 end
 
 local function find_datadir(name)
-    local list = vlc.datadir_list(name)
+    local list = vlc.misc.datadir_list(name)
     for _, l in ipairs(list) do
-        local s = vlc.fd.stat(l)
+        local s = vlc.net.stat(l)
         if s then
             return l
         end
@@ -215,39 +259,30 @@ do
     package.path = http_dir.."/?.lua"
     local ok, err = pcall(require,"custom")
     if not ok then
-        vlc.msg.warn("Couldn't load "..http_dir.."/custom.lua")
+        vlc.msg.warn("Couldn't load "..http_dir.."/custom.lua",err)
     else
         vlc.msg.dbg("Loaded "..http_dir.."/custom.lua")
     end
     package.path = oldpath
 end
 local files = {}
-local mimes = {
-    txt = "text/plain",
-    html = "text/html",
-    xml = "text/xml",
-    js = "text/javascript",
-    css = "text/css",
-    png = "image/png",
-    ico = "image/x-icon",
-}
 local function load_dir(dir,root,parent_acl)
     local root = root or "/"
     local has_index = false
     local my_acl = parent_acl
     do
         local af = dir.."/.hosts"
-        local s = vlc.fd.stat(af)
+        local s = vlc.net.stat(af)
         if s and s.type == "file" then
             -- We found an acl
-            my_acl = acl.new(false)
+            my_acl = vlc.acl(false)
             my_acl:load_file(af)
         end
     end
-    local d = vlc.fd.opendir(dir)
+    local d = vlc.net.opendir(dir)
     for _,f in ipairs(d) do
         if not string.match(f,"^%.") then
-            local s = vlc.fd.stat(dir.."/"..f)
+            local s = vlc.net.stat(dir.."/"..f)
             if s.type == "file" then
                 local url
                 if f == "index.html" then
@@ -260,9 +295,9 @@ local function load_dir(dir,root,parent_acl)
                 local mime = mimes[ext]
                 -- print(url,mime)
                 if mime and string.match(mime,"^text/") then
-                    table.insert(files,file(h,dir.."/"..f,url,my_acl and my_acl.private,mime))
+                    table.insert(files,file(h,dir.."/"..f,url,my_acl,mime))
                 else
-                    table.insert(files,rawfile(h,dir.."/"..f,url,my_acl and my_acl.private))
+                    table.insert(files,rawfile(h,dir.."/"..f,url,my_acl))
                 end
             elseif s.type == "dir" then
                 load_dir(dir.."/"..f,root..f.."/",my_acl)
@@ -271,18 +306,15 @@ local function load_dir(dir,root,parent_acl)
     end
     if not has_index and not config.no_index then
         -- print("Adding index for", root)
-        table.insert(files,dirlisting(root,d,my_acl and my_acl.private))
+        table.insert(files,dirlisting(root,d,my_acl))
     end
+    return my_acl
 end
 
-local u = vlc.net.url_parse( config.host or "localhost:8080" )
-h = httpd.new(u.host,u.port)
-load_dir( http_dir )
+local u = vlc.net.url_parse( config.host or "0.0.0.0:8080" )
+h = vlc.httpd(u.host,u.port)
+local root_acl = load_dir( http_dir )
+local a = h:handler("/art",nil,nil,root_acl,callback_art,nil)
 
-while not die do die = vlc.lock_and_wait() end -- everything happens in callbacks
+while not vlc.misc.lock_and_wait() do end -- everything happens in callbacks
 
--- FIXME: We shouldn't need to do this ourselves.
-for i=1,#files do
-    getmetatable(files[i]).__gc(files[i])
-    files[i] = nil
-end