http/https working async now

This commit is contained in:
Ryan Ward 2021-07-17 23:33:20 -04:00
parent 36ae77e98a
commit 808c73cf93
3 changed files with 30 additions and 7 deletions

View File

@ -1,11 +1,26 @@
package.path = "./?/init.lua;./?.lua;"..package.path package.path = "./?/init.lua;./?.lua;"..package.path
local net = require("lnet.tcp") local net = require("lnet.tcp")
local multi, thread = require("multi"):init() local multi, thread = require("multi"):init()
local client = net.newCastedClient("Test")--net:newTCPClient("localhost",12345)
client:send("Test!") local http = require("lnet.http")
client.OnDataRecieved(function(c,data) multi:newThread("Download Test",function()
print("Response: ",data) local data = http.request("http://zjcdn.mangafox.me/store/manga/14765/01-001.0/compressed/t001.jpg")
local file = io.open("test.jpg","wb")
file:write(data)
file:flush()
file:close()
os.exit()
end) end)
multi:newThread("Timer",function()
end)
-- local client = net.newCastedClient("Test")--net:newTCPClient("localhost",12345)
-- client:send("Test!")
-- client.OnDataRecieved(function(c,data)
-- print("Response: ",data)
-- end)
multi:mainloop() multi:mainloop()

View File

@ -19,6 +19,7 @@ local base = _G
local table = require("table") local table = require("table")
net.http = {} net.http = {}
local _M = net.http local _M = net.http
local default_block_size = 1024
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
@ -168,8 +169,16 @@ function metat.__index:receivebody(headers, sink, step)
local mode = "default" -- connection close local mode = "default" -- connection close
if t and t ~= "identity" then mode = "http-chunked" if t and t ~= "identity" then mode = "http-chunked"
elseif base.tonumber(headers["content-length"]) then mode = "by-length" end elseif base.tonumber(headers["content-length"]) then mode = "by-length" end
return self.try(ltn12.pump.all(socket.source(mode, self.c, length), local ret,lp
sink, step)) lp = length%default_block_size
for i=1,math.floor(length/default_block_size) do
thread.yield()
ret = self.try(ltn12.pump.step(socket.source(mode, self.c, default_block_size), sink, step))
end
if lp~=0 then
ret = self.try(ltn12.pump.step(socket.source(mode, self.c, lp), sink, step))
end
return ret
end end
function metat.__index:receive09body(status, sink, step) function metat.__index:receive09body(status, sink, step)
@ -313,7 +322,6 @@ trequest = thread:newFunction(function(reqt)
local headers local headers
-- ignore any 100-continue messages -- ignore any 100-continue messages
while code == 100 do while code == 100 do
thread.yield()
headers = h:receiveheaders() headers = h:receiveheaders()
code, status = h:receivestatusline() code, status = h:receivestatusline()
end end

BIN
test.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 KiB