mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 04:28:20 +01:00
http.request: possibility to defer body receiving if sink is not specified
In this case, the first returned value is not `1`, but a function, taking two optional arguments: `sink` and `step`. If the function is run without a `sink`, it simply closes the handle, ignoring the body.
This commit is contained in:
parent
1fad162690
commit
0278463377
30
src/http.lua
30
src/http.lua
@ -368,8 +368,16 @@ end
|
|||||||
local code, status = h:receivestatusline()
|
local code, status = h:receivestatusline()
|
||||||
-- if it is an HTTP/0.9 server, simply get the body and we are done
|
-- if it is an HTTP/0.9 server, simply get the body and we are done
|
||||||
if not code then
|
if not code then
|
||||||
h:receive09body(status, nreqt.sink, nreqt.step)
|
if nreqt.sink then
|
||||||
return 1, 200
|
h:receive09body(status, nreqt.sink, nreqt.step)
|
||||||
|
return 1, 200
|
||||||
|
else
|
||||||
|
return socket.protect(function(sink, step)
|
||||||
|
if sink then
|
||||||
|
return h:receive09body(status, sink, step or nreqt.step)
|
||||||
|
end
|
||||||
|
end), 200
|
||||||
|
end
|
||||||
elseif code == 408 then
|
elseif code == 408 then
|
||||||
return 1, code
|
return 1, code
|
||||||
end
|
end
|
||||||
@ -387,11 +395,23 @@ end
|
|||||||
return tredirect(reqt, headers.location)
|
return tredirect(reqt, headers.location)
|
||||||
end
|
end
|
||||||
-- here we are finally done
|
-- here we are finally done
|
||||||
|
local receivebody
|
||||||
if shouldreceivebody(nreqt, code) then
|
if shouldreceivebody(nreqt, code) then
|
||||||
h:receivebody(headers, nreqt.sink, nreqt.step)
|
if nreqt.sink then
|
||||||
|
h:receivebody(headers, nreqt.sink, nreqt.step)
|
||||||
|
else
|
||||||
|
receivebody = socket.protect(function(sink, step)
|
||||||
|
local res, err
|
||||||
|
if sink then
|
||||||
|
res, err = h:receivebody(headers, sink, step or nreqt.step)
|
||||||
|
end
|
||||||
|
h:close()
|
||||||
|
return res, err
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
h:close()
|
if not receivebody then h:close() end
|
||||||
return 1, code, headers, status
|
return receivebody or 1, code, headers, status
|
||||||
end
|
end
|
||||||
|
|
||||||
-- turns an url and a body into a generic request
|
-- turns an url and a body into a generic request
|
||||||
|
Loading…
Reference in New Issue
Block a user