summaryrefslogtreecommitdiff
path: root/tex/context/base/luat-crl.lua
blob: 1298327650bc27cdaf910a222c3382ffe29c8a43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- filename : luat-crl.lua
-- comment  : companion to luat-lib.tex
-- author   : Hans Hagen, PRAGMA-ADE, Hasselt NL
-- copyright: PRAGMA ADE / ConTeXt Development Team
-- license  : see context related readme files

if not versions then versions = { } end versions['luat-crl'] = 1.001
if not curl     then curl     = { } end

curl.cachepath = caches.setpath(texmf.instance,"curl")

curl.cached = { }

function curl.fetch(protocol, name)
    local cachename = curl.cachepath .. "/" .. file.robustname(name)
    cachename = cachename:gsub("[\\/]", io.fileseparator)
    if not curl.cached[name] then
        if not io.exists(cachename) then
            curl.cached[name] = cachename
            local command = "curl --silent --create-dirs --output " .. cachename .. " " .. protocol .. "://" .. name
            os.execute(command)
        end
        if io.exists(cachename) then
            curl.cached[name] = cachename
        else
            curl.cached[name] = ""
        end
    end
    return curl.cached[name]
end

function input.finders.curl(instance,protocol,filename)
    local foundname = curl.fetch(protocol, filename)
    return input.finders.generic(instance,protocol,foundname,filetype)
end
function input.openers.curl(instance,protocol,filename)
    return input.openers.generic(instance,protocol,filename)
end
function input.loaders.curl(instance,protocol,filename)
    return input.loaders.generic(instance,protocol,filename)
end

-- todo: metamethod

function curl.install(protocol)
    input.finders[protocol] = function (instance,filename,filetype) return input.finders.curl(instance,protocol,filename) end
    input.openers[protocol] = function (instance,filename)          return input.openers.curl(instance,protocol,filename) end
    input.loaders[protocol] = function (instance,filename)          return input.loaders.curl(instance,protocol,filename) end
end

curl.install('http')
curl.install('https')
curl.install('ftp')