Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables --
- pageCache = {}
- -- File sizes.
- kilobytes = 1024
- megabytes = 1024*1024
- gigabytes = 1024*1024*1024
- terabytes = 1024*1024*1024*1024
- KB = kilobytes
- MB = megabytes
- GB = gigabytes
- TB = terabytes
- -- Configuration --
- defaultBrowserLocation = "browser"
- cacheLocation = "internetCache"
- cacheEnabled = fs.getFreeSpace("") > 1*MB
- maxPageCacheSize = 20*MB
- local oget, opost, orequest
- if http then
- if not http.Modified then
- oget = http.get
- opost = http.post
- orequest = http.request
- end
- end
- function serializeCacheLocation(url, postData, headers)
- return cacheLocation.."/"..tostring(string.sub(url or "none", 6, -1).."/"..(string.sub(postData or "none", 1, 20) or "none").."/"..(string.sub(textutils.serialize(headers or {}) or "none", 1, 20) or "none"))
- end
- -- This function adds a page to the page cache.
- function addToCache(url, postData, headers, handle)
- local findStr = serializeCacheLocation(url, postData, headers)
- fs.makeDir(cacheLocation)
- fs.makeDir(cacheLocation.."/"..string.sub(url or "none", 6, -1))
- fs.makeDir(cacheLocation.."/"..string.sub(url or "none", 6, -1).."/"..(string.sub(postData or "none", 1, 20) or "none"))
- local file = fs.open(findStr, "w")
- if file then
- file.writeLine(handle.getResponseCode() or "100")
- file.write(handle.readAll())
- file.close()
- return true
- end
- end
- function pullFromCache(url, postData, headers)
- local findStr = serializeCacheLocation(url, postData, headers)
- local file = fs.open(findStr, "r")
- if file then
- --os.queueEvent( "http_success", url, file )
- file.responseCode = tonumber(file.readLine())
- file.getResponseCode = function()
- return file.responseCode
- end
- return file
- end
- return false
- end
- -- Override the final http functions with ones that pull files from the cache.
- if http then
- if oget and cacheEnabled then
- function request(url, postData, headers)
- local url, file = pullFromCache(url, postData, headers)
- if file then
- os.queueEvent( "http_success", url, file )
- end
- orequest(url or "", postData, headers)
- end
- function get(url, headers)
- if not string.find(url, "pastebin.com") then
- local url1, file = pullFromCache(url, nil, headers)
- if file then
- return file
- end
- end
- local handle, err = oget(url, headers)
- if handle then
- addToCache(url, nil, headers, handle)
- end
- return pullFromCache(url, nil, headers)
- end
- function post(url, postData, headers)
- local url1, file = pullFromCache(url, postData, headers)
- if file then
- return file
- end
- local handle, err = opost(url, postData, headers)
- if handle then
- pcall(addToCache, url, postData, headers, handle)
- end
- return handle, err
- end
- http.get = get
- http.post = post
- http.request = request
- end
- end
- function resetCache()
- for k, v in ipairs(fs.list(cacheLocation)) do
- fs.delete(cacheLocation.."/"..v)
- end
- end
- function overwriteFileRaw(contents, destination)
- local file, err = fs.open(destination, "w")
- if file then
- file.write(contents)
- file.close()
- return true
- end
- return false, err
- end
- -- Overwrites a file with the contents you give as the first argument.
- function overwriteFile(contents, destination)
- local pcallSuccess, returnCondition, errorString = pcall(overwriteFileRaw, contents, destination)
- if pcallSuccess then
- return returnCondition, errorString
- end
- return false, returnCondition
- end
- function pullFileRaw(url, header, post)
- if http then
- local handle, str
- if not post then
- handle, str = http.get(url, header), ""
- else
- handle, str = http.post(url, post, header), ""
- end
- if handle then
- str = handle.readAll()
- handle.close()
- end
- return str
- end
- return false, "Http API is not enabled"
- end
- -- Downloads a file from the internet.
- -- The url is the file to download.
- function pullFile(url, header, post)
- local pcallSuccess, returnCondition, errorString = pcall(pullFileRaw, url, header, post)
- if pcallSuccess then
- return returnCondition, errorString
- end
- return false, returnCondition
- end
- -- Downloads a file from the internet.
- -- The url is the file to download, and the destination is what file will be overwritten.
- function downloadFileRaw(url, destination, header, post)
- if http then
- local handle, str
- if not post then
- handle, str = http.get(url, header), ""
- else
- handle, str = http.post(url, post, header), ""
- end
- if handle then
- str = handle.readAll()
- handle.close()
- return overwriteFile(str, destination)
- end
- return str
- end
- return false, "Http API is not enabled"
- end
- -- Downloads a file from the internet.
- -- The url is the file to download, and the destination is what file will be overwritten.
- function downloadFile(url, destination, header, post)
- local pcallSuccess, returnCondition, errorString = pcall(downloadFileRaw, url, destination, header, post)
- if pcallSuccess then
- return returnCondition, errorString
- end
- return false, returnCondition
- end
- -- Opens the default web browser.
- function openInternetBrowser(url)
- if not fs.exists(defaultBrowserLocation) then
- shell.run("pastebin get VDUGPdiA "..defaultBrowserLocation)
- end
- shell.run(defaultBrowserLocation, url)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement