http fixes

This commit is contained in:
kepler155c@gmail.com 2017-10-08 20:03:01 -04:00
parent a625b52bad
commit 955f11042b
4 changed files with 66 additions and 48 deletions

View File

@ -1,5 +1,4 @@
local synchronized = require('sync')
local Util = require('util')
local Util = require('util')
local fs = _G.fs
@ -51,9 +50,7 @@ function urlfs.open(node, fn, fl)
local c = node.cache
if not c then
synchronized(node.url, function()
c = Util.download(node.url)
end)
c = Util.httpGet(node.url)
if c then
node.cache = c
node.size = #c

View File

@ -6,41 +6,53 @@ local fs = _G.fs
local http = _G.http
local os = _G.os
-- fix broken http get
local syncLocks = { }
if not http._patched then
-- fix broken http get
local syncLocks = { }
local function sync(obj, fn)
local key = tostring(obj)
if syncLocks[key] then
local cos = tostring(coroutine.running())
table.insert(syncLocks[key], cos)
repeat
local _, co = os.pullEvent('sync_lock')
until co == cos
else
syncLocks[key] = { }
local function sync(obj, fn)
local key = tostring(obj)
if syncLocks[key] then
local cos = tostring(coroutine.running())
table.insert(syncLocks[key], cos)
repeat
local _, co = os.pullEvent('sync_lock')
until co == cos
else
syncLocks[key] = { }
end
local s, m = pcall(fn)
local co = table.remove(syncLocks[key], 1)
if co then
os.queueEvent('sync_lock', co)
else
syncLocks[key] = nil
end
if not s then
error(m)
end
end
local s, m = pcall(fn)
local co = table.remove(syncLocks[key], 1)
if co then
os.queueEvent('sync_lock', co)
else
syncLocks[key] = nil
end
if not s then
error(m)
-- todo -- completely replace http.get with function that
-- checks for success on permanent redirects (minecraft 1.75 bug)
http._patched = http.get
function http.get(url, headers)
local s, m
sync(url, function()
s, m = http._patched(url, headers)
end)
return s, m
end
end
local function loadUrl(url)
local c
sync(url, function()
local h = http.get(url)
if h then
c = h.readAll()
h.close()
end
end)
local h = http.get(url)
if h then
c = h.readAll()
h.close()
end
if c and #c > 0 then
return c
end

View File

@ -1,5 +1,11 @@
local Util = { }
local fs = _G.fs
local http = _G.http
local os = _G.os
local term = _G.term
local textutils = _G.textutils
function Util.tryTimed(timeout, f, ...)
local c = os.clock()
repeat
@ -12,7 +18,7 @@ end
function Util.tryTimes(attempts, f, ...)
local result
for i = 1, attempts do
for _ = 1, attempts do
result = { f(...) }
if result[1] then
return unpack(result)
@ -72,11 +78,11 @@ end
function Util.getVersion()
local version
if _CC_VERSION then
version = tonumber(_CC_VERSION:gmatch('[%d]+%.?[%d][%d]', '%1')())
if _G._CC_VERSION then
version = tonumber(_G._CC_VERSION:gmatch('[%d]+%.?[%d][%d]', '%1')())
end
if not version and _HOST then
version = tonumber(_HOST:gmatch('[%d]+%.?[%d][%d]', '%1')())
if not version and _G._HOST then
version = tonumber(_G._HOST:gmatch('[%d]+%.?[%d][%d]', '%1')())
end
return version or 1.7
@ -351,13 +357,18 @@ function Util.loadTable(fname)
end
--[[ loading and running functions ]] --
function Util.download(url, filename)
local h = http.get(url)
if not h then
error('Failed to download ' .. url)
function Util.httpGet(url, headers)
local h, msg = http.get(url, headers)
if h then
local contents = h.readAll()
h.close()
return contents
end
local contents = h.readAll()
h.close()
return h, msg
end
function Util.download(url, filename)
local contents = Util.httpGet(url)
if not contents then
error('Failed to download ' .. url)
end
@ -418,8 +429,8 @@ function Util.toBytes(n)
return tostring(n)
end
function Util.insertString(os, is, pos)
return os:sub(1, pos - 1) .. is .. os:sub(pos)
function Util.insertString(str, istr, pos)
return str:sub(1, pos - 1) .. istr .. str:sub(pos)
end
function Util.split(str, pattern)

View File

@ -111,7 +111,6 @@ device.wireless_modem.open(999)
print('discovery: listening on port 999')
Event.on('modem_message', function(e, s, sport, id, info, distance)
debug(info)
if sport == 999 and tonumber(id) and type(info) == 'table' then
if not network[id] then
network[id] = { }
@ -151,7 +150,6 @@ end
-- every 10 seconds, send out this computer's info
Event.onInterval(10, function()
debug('timer')
sendInfo()
for _,c in pairs(_G.network) do
local elapsed = os.clock()-c.timestamp