mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-16 10:51:55 -04:00
require("internet").request now returns a function object
The function object can be called to read from the stream, same usage as before But the object also exposes the request handle userdata, to allow calling close for example: ``` local request = require("internet").request("http://www.google.com") print(request()) -- same as before request:close() -- clean up resources now, as opposed to waiting for gc ``` closes #2255
This commit is contained in:
parent
afff699dff
commit
d8471278ba
@ -11,10 +11,10 @@ function internet.request(url, data, headers)
|
|||||||
checkArg(2, data, "string", "table", "nil")
|
checkArg(2, data, "string", "table", "nil")
|
||||||
checkArg(3, headers, "table", "nil")
|
checkArg(3, headers, "table", "nil")
|
||||||
|
|
||||||
local inet = component.internet
|
if not component.isAvailable("internet") then
|
||||||
if not inet then
|
|
||||||
error("no primary internet card found", 2)
|
error("no primary internet card found", 2)
|
||||||
end
|
end
|
||||||
|
local inet = component.internet
|
||||||
|
|
||||||
local post
|
local post
|
||||||
if type(data) == "string" then
|
if type(data) == "string" then
|
||||||
@ -31,23 +31,35 @@ function internet.request(url, data, headers)
|
|||||||
error(reason, 2)
|
error(reason, 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
return function()
|
return setmetatable(
|
||||||
while true do
|
{
|
||||||
local data, reason = request.read()
|
["()"] = "function():string -- Tries to read data from the socket stream and return the read byte array.",
|
||||||
if not data then
|
close = setmetatable({},
|
||||||
request.close()
|
{
|
||||||
if reason then
|
__call = request.close,
|
||||||
error(reason, 2)
|
__tostring = function() return "function() -- closes the connection" end
|
||||||
else
|
})
|
||||||
return nil -- eof
|
},
|
||||||
|
{
|
||||||
|
__call = function()
|
||||||
|
while true do
|
||||||
|
local data, reason = request.read()
|
||||||
|
if not data then
|
||||||
|
request.close()
|
||||||
|
if reason then
|
||||||
|
error(reason, 2)
|
||||||
|
else
|
||||||
|
return nil -- eof
|
||||||
|
end
|
||||||
|
elseif #data > 0 then
|
||||||
|
return data
|
||||||
end
|
end
|
||||||
elseif #data > 0 then
|
-- else: no data, block
|
||||||
return data
|
os.sleep(0)
|
||||||
end
|
end
|
||||||
-- else: no data, block
|
end,
|
||||||
os.sleep(0)
|
__index = request,
|
||||||
end
|
})
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user