diff --git a/client.lua b/client.lua index 405e5bd..1c860c5 100644 --- a/client.lua +++ b/client.lua @@ -1,11 +1,26 @@ package.path = "./?/init.lua;./?.lua;"..package.path local net = require("lnet.tcp") 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) - print("Response: ",data) +multi:newThread("Download Test",function() + 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) + +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() \ No newline at end of file diff --git a/lnet/http.lua b/lnet/http.lua index 5580f36..e44c0c1 100644 --- a/lnet/http.lua +++ b/lnet/http.lua @@ -19,6 +19,7 @@ local base = _G local table = require("table") net.http = {} local _M = net.http +local default_block_size = 1024 ----------------------------------------------------------------------------- -- Program constants @@ -168,8 +169,16 @@ function metat.__index:receivebody(headers, sink, step) local mode = "default" -- connection close if t and t ~= "identity" then mode = "http-chunked" elseif base.tonumber(headers["content-length"]) then mode = "by-length" end - return self.try(ltn12.pump.all(socket.source(mode, self.c, length), - sink, step)) + local ret,lp + 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 function metat.__index:receive09body(status, sink, step) @@ -313,7 +322,6 @@ trequest = thread:newFunction(function(reqt) local headers -- ignore any 100-continue messages while code == 100 do - thread.yield() headers = h:receiveheaders() code, status = h:receivestatusline() end diff --git a/test.jpg b/test.jpg new file mode 100644 index 0000000..1cd0030 Binary files /dev/null and b/test.jpg differ