Annotation of embedaddon/axTLS/www/lua/download.lua, revision 1.1

1.1     ! misho       1: #!/usr/local/bin/lua
        !             2: 
        !             3: require"luasocket"
        !             4: 
        !             5: function receive (connection)
        !             6:        connection:settimeout(0)
        !             7:        local s, status = connection:receive (2^10)
        !             8:        if status == "timeout" then
        !             9:                coroutine.yield (connection)
        !            10:        end
        !            11:        return s, status
        !            12: end
        !            13: 
        !            14: function download (host, file, outfile)
        !            15:        --local f = assert (io.open (outfile, "w"))
        !            16:        local c = assert (socket.connect (host, 80))
        !            17:        c:send ("GET "..file.." HTTP/1.0\r\n\r\n")
        !            18:        while true do
        !            19:                local s, status = receive (c)
        !            20:                --f:write (s)
        !            21:                if status == "closed" then
        !            22:                        break
        !            23:                end
        !            24:        end
        !            25:        c:close()
        !            26:        --f:close()
        !            27: end
        !            28: 
        !            29: local threads = {}
        !            30: function get (host, file, outfile)
        !            31:        print (string.format ("Downloading %s from %s to %s", file, host, outfile))
        !            32:        local co = coroutine.create (function ()
        !            33:                return download (host, file, outfile)
        !            34:        end)
        !            35:        table.insert (threads, co)
        !            36: end
        !            37: 
        !            38: function dispatcher ()
        !            39:        while true do
        !            40:                local n = table.getn (threads)
        !            41:                if n == 0 then
        !            42:                        break
        !            43:                end
        !            44:                local connections = {}
        !            45:                for i = 1, n do
        !            46:                        local status, res = coroutine.resume (threads[i])
        !            47:                        if not res then
        !            48:                                table.remove (threads, i)
        !            49:                                break
        !            50:                        else
        !            51:                                table.insert (connections, res)
        !            52:                        end
        !            53:                end
        !            54:                if table.getn (connections) == n then
        !            55:                        socket.select (connections)
        !            56:                end
        !            57:        end
        !            58: end
        !            59: 
        !            60: local url = arg[1]
        !            61: if not url then
        !            62:        print (string.format ("usage: %s url [times]", arg[0]))
        !            63:        os.exit()
        !            64: end
        !            65: local times = arg[2] or 5
        !            66: 
        !            67: url = string.gsub (url, "^http.?://", "")
        !            68: local _, _, host, file = string.find (url, "^([^/]+)(/.*)")
        !            69: local _, _, fn = string.find (file, "([^/]+)$")
        !            70: 
        !            71: for i = 1, times do
        !            72:        get (host, file, fn..i)
        !            73: end
        !            74: 
        !            75: dispatcher ()

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>