--- LuaDist URL fetch functions -- Peter Drahos, LuaDist Project, 2008 module ("dist.fetch", package.seeall) local config = require "dist.config" local sys = require "dist.sys" local curl = require "luacurl" --- Fetch file from URL. -- @param src sting: URL to fetch file from. -- @param dest sting: Destination to save download to or nil for temp directory. -- @param callback function: Callback function that will provide download progress feedback. function(src, downloaded, total) -- @return path, err: Path the file was downloaded or nil and error message on failure. function download(src, dest, callback) assert(type(src)=="string", "download argument 'src' is not a string.") assert(not dest or type(dest)=="string", "download argument 'dest' is not a string.") assert(not callback or type(callback)=="function", "download argument 'callback' is not a function.") dest = dest or config.temp local file = src:match("([^/]+)$") if not file then return nil, "Could not determine filename." end if not sys.isDir(dest) then return nil, "Destination directory does not exist." end dest = dest .. "/" .. file local c = assert(curl.new(), "Could not initialize Curl.") local file = assert(io.open(dest, "wb")) assert(c:setopt(curl.OPT_WRITEFUNCTION, function (stream, buffer) stream:write(buffer) return string.len(buffer) end), "Could not initialize Curl write function.") assert(c:setopt(curl.OPT_WRITEDATA, file)) if callback then assert(c:setopt(curl.OPT_PROGRESSFUNCTION, function (_, dltotal, dlnow, uptotal, upnow) cballback(src, dlnow, dltotoal) end), "Could not initialize Curl download progress function.") end if config.verbose then assert(c:setopt(curl.OPT_VERBOSE, true)) end -- assert(c:setopt(curl.OPT_NOPROGRESS, true), "Could not set Curl parameter OPT_NOPROGRESS.") assert(c:setopt(curl.OPT_BUFFERSIZE, 2^13), "Could not set Curl parameter OPT_BUFFERSIZE.") assert(c:setopt(curl.OPT_HTTPHEADER, "Connection: Keep-Alive", "Accept-Language: en-us"), "Could not set Curl parameter OPT_HTTPHEADER.") assert(c:setopt(curl.OPT_URL, src), "Could not set Curl parameter OPT_URL.") assert(c:setopt(curl.OPT_CONNECTTIMEOUT, 15), "Could not set Curl parameter OPT_CONNECTTIMEOUT.") local ok, err = c:perform() file:close() if not ok then return false, err end return dest end --- Directly get file contents from URL. -- @param src string: URL of the file to load. -- @return text, err: Contents of the URL file or nil and error message on failure. function get(src) local c = assert(curl.new(), "Could not initialize Curl.") local file = "" assert(c:setopt(curl.OPT_WRITEFUNCTION, function (stream, buffer) file = file .. buffer return string.len(buffer) end)); -- assert(c:setopt(curl.OPT_WRITEDATA, nil)) -- assert(c:setopt(curl.OPT_NOPROGRESS, true)) assert(c:setopt(curl.OPT_BUFFERSIZE, 2^13), "Could not set Curl parameter OPT_BUFFERSIZE.") assert(c:setopt(curl.OPT_HTTPHEADER, "Connection: Keep-Alive", "Accept-Language: en-us"), "Could not set Curl parameter OPT_HTTPHEADER.") assert(c:setopt(curl.OPT_URL, src), "Could not set Curl parameter OPT_URL.") assert(c:setopt(curl.OPT_CONNECTTIMEOUT, 15), "Could not set Curl parameter OPT_CONNECTTIMEOUT.") local ok, err = c:perform() if ok then return file end return nil, err end