Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cache = {}
- if fs.exists("/.http_cache") then
- local stat, err = pcall(fs.delete, "/.http_cache")
- if not stat then
- error("Error clearing \".http_cache\".", 2)
- end
- end
- local stat, err = pcall(fs.makeDir, "/.http_cache")
- if not stat then
- error("Error making \".http_cache\".", 2)
- end
- local function makeFileSafe(url)
- local url = string.gsub(url, "http%:%/%/", "")
- url = string.gsub(url, "[%/%\\%?%%%*%:%|%\"%<%>%.% ]", "_")
- return url
- end
- local function basicGet(url, data, cacheTimeout)
- cacheTimeout = cacheTimeout or 300 -- cache lifetime in seconds
- local fileSafeUrl = makeFileSafe(url)
- if not data then -- Don't lookup the cache on POST requests
- -- Cache lookup
- if (cache[fileSafeUrl]) then
- if (cache[fileSafeUrl][1] == url) then
- if fs.exists(fs.combine("/.http_cache", fileSafeUrl)) and (cache[fileSafeUrl][3] >= (os.clock() - cache[fileSafeUrl][2])) then
- local stat, ret = pcall(fs.open, fs.combine("/.http_cache", fileSafeUrl), "r")
- if stat then
- ret.getResponseCode = function()
- return cache[fileSafeUrl][4]
- end
- return ret
- end
- else
- pcall(fs.delete, fs.combine("/.http_cache", fileSafeUrl))
- cache[fileSafeUrl] = nil
- end
- end
- end
- end
- -- Network request
- local req = {}
- http.request(url, data)
- while true do
- local e, rUrl, handle = os.pullEvent()
- if (e == "http_success") and (url == rUrl) then
- req = handle
- break
- elseif (e == "http_failure") and (url == rUrl) then
- return
- end
- end
- -- Cache writeback
- local rCode = req.getResponseCode()
- local data = req.readAll()
- req.close()
- local localHandle = fs.open(fs.combine("/.http_cache", fileSafeUrl), "w")
- localHandle.write(data)
- localHandle.close()
- cache[fileSafeUrl] = {
- url,
- os.clock(),
- cacheTimeout,
- rCode,
- }
- local h = fs.open(fs.combine("/.http_cache", fileSafeUrl), "r")
- h.getResponseCode = function()
- return cache[fileSafeUrl][4]
- end
- return h
- end
- http.get = function(url, cacheTimeout)
- return basicGet(url, nil, cacheTimeout)
- end
- http.post = function(url, data, cacheTimeout)
- return basicGet(url, data, cacheTimeout)
- end
- http.clearCache = function(url)
- if url then
- if cache[makeFileSafe(url)] then
- cache[makeFileSafe(url)] = nil
- end
- if fs.exists(fs.combine("/.http_cache", makeFileSafe(url))) then
- local stat, err = pcall(fs.delete, fs.combine("/.http_cache", makeFileSafe(url)))
- if not stat then
- error("Error deleting cache file.", 2)
- end
- end
- else
- if fs.exists("/.http_cache") then
- local stat, err = pcall(fs.delete, "/.http_cache")
- if not stat then
- error("Error clearing \".http_cache\".", 2)
- end
- end
- local stat, err = pcall(fs.makeDir, "/.http_cache")
- if not stat then
- error("Error making \".http_cache\".", 2)
- end
- cache = {}
- end
- end
- http.selectiveClearCache = function() -- Only clear the entries that have timed out
- local l = {}
- for i,v in pairs(cache) do
- if not (v[3] >= (os.clock() - v[2])) then
- pcall(fs.delete, fs.combine("/.http_cache", i))
- table.insert(l, i)
- end
- end
- for i=1, #l do
- cache[l[i]] = nil
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement