Fix. socket callback get correct cURLv3 easy object.
parent
0ba125dfc3
commit
273212fb93
|
@ -27,9 +27,11 @@ local FLAGS = {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
local trace = function() end or print
|
local trace = true
|
||||||
|
|
||||||
local FILES, CONTEXT = {}, {}
|
trace = trace and print or function() end
|
||||||
|
|
||||||
|
local CONTEXT = {}
|
||||||
|
|
||||||
function create_curl_context(sockfd)
|
function create_curl_context(sockfd)
|
||||||
local context = {
|
local context = {
|
||||||
|
@ -58,7 +60,7 @@ function add_download(url, num)
|
||||||
writefunction = file;
|
writefunction = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILES[handle] = file
|
handle.data = file
|
||||||
|
|
||||||
curl_handle:add_handle(handle)
|
curl_handle:add_handle(handle)
|
||||||
fprintf(stderr, "Added download %s -> %s\n", url, filename);
|
fprintf(stderr, "Added download %s -> %s\n", url, filename);
|
||||||
|
@ -66,14 +68,14 @@ end
|
||||||
|
|
||||||
function check_multi_info()
|
function check_multi_info()
|
||||||
while true do
|
while true do
|
||||||
local easy, ok, err = curl_handle:info_read()
|
local easy, ok, err = curl_handle:info_read(true)
|
||||||
if not easy then curl_handle:close() error(err) end
|
if not easy then curl_handle:close() error(err) end
|
||||||
if easy == 0 then break end
|
if easy == 0 then break end
|
||||||
|
|
||||||
local context = CONTEXT[e]
|
local context = CONTEXT[e]
|
||||||
if context then destroy_curl_context(context) end
|
if context then destroy_curl_context(context) end
|
||||||
local file = FILES[easy]
|
local file = assert(easy.data)
|
||||||
if file then FILES[easy] = nil, file:close() end
|
file:close()
|
||||||
local done_url = easy:getinfo_effective_url()
|
local done_url = easy:getinfo_effective_url()
|
||||||
easy:close()
|
easy:close()
|
||||||
if ok then
|
if ok then
|
||||||
|
@ -116,31 +118,28 @@ function start_timeout(timeout_ms)
|
||||||
timeout:stop():start(timeout_ms, 0, on_timeout)
|
timeout:stop():start(timeout_ms, 0, on_timeout)
|
||||||
end
|
end
|
||||||
|
|
||||||
local handle_socket = function(...)
|
function handle_socket(easy, s, action)
|
||||||
local ok, err = pcall(handle_socket_impl, ...)
|
local ok, err = pcall(function()
|
||||||
|
-- calls by curl --
|
||||||
|
trace("CURL::SOCKET", easy, s, ACTION_NAMES[action] or action)
|
||||||
|
|
||||||
|
local curl_context = CONTEXT[easy] or create_curl_context(s)
|
||||||
|
CONTEXT[easy] = curl_context
|
||||||
|
|
||||||
|
assert(curl_context.sockfd == s)
|
||||||
|
|
||||||
|
if action == curl.POLL_IN then
|
||||||
|
curl_context.poll_handle:start(uv.READABLE, curl_perform)
|
||||||
|
elseif action == curl.POLL_OUT then
|
||||||
|
curl_context.poll_handle:start(uv.WRITABLE, curl_perform)
|
||||||
|
elseif action == curl.POLL_REMOVE then
|
||||||
|
CONTEXT[easy] = nil
|
||||||
|
destroy_curl_context(curl_context)
|
||||||
|
end
|
||||||
|
end)
|
||||||
if not ok then uv.defer(function() error(err) end) end
|
if not ok then uv.defer(function() error(err) end) end
|
||||||
end
|
end
|
||||||
|
|
||||||
function handle_socket_impl(easy, s, action)
|
|
||||||
-- calls by curl --
|
|
||||||
|
|
||||||
trace("CURL::SOCKET", easy, s, ACTION_NAMES[action] or action)
|
|
||||||
|
|
||||||
local curl_context = CONTEXT[easy] or create_curl_context(s)
|
|
||||||
CONTEXT[easy] = curl_context
|
|
||||||
|
|
||||||
assert(curl_context.sockfd == s)
|
|
||||||
|
|
||||||
if action == curl.POLL_IN then
|
|
||||||
curl_context.poll_handle:start(uv.READABLE, curl_perform)
|
|
||||||
elseif action == curl.POLL_OUT then
|
|
||||||
curl_context.poll_handle:start(uv.WRITABLE, curl_perform)
|
|
||||||
elseif action == curl.POLL_REMOVE then
|
|
||||||
CONTEXT[easy] = nil
|
|
||||||
destroy_curl_context(curl_context)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
timeout = uv.timer()
|
timeout = uv.timer()
|
||||||
|
|
||||||
curl_handle = curl.multi{
|
curl_handle = curl.multi{
|
||||||
|
|
|
@ -571,6 +571,61 @@ function Multi:info_read(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function wrap_callback(...)
|
||||||
|
local n = select("#", ...)
|
||||||
|
local fn, ctx, has_ctx
|
||||||
|
if n >= 2 then
|
||||||
|
has_ctx, fn, ctx = true, assert(...)
|
||||||
|
else
|
||||||
|
fn = assert(...)
|
||||||
|
if type(fn) ~= "function" then
|
||||||
|
has_ctx, fn, ctx = true, assert(fn.socket), fn
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if has_ctx then
|
||||||
|
return function(...) return fn(ctx, ...) end
|
||||||
|
end
|
||||||
|
return function(...) return fn(...) end
|
||||||
|
end
|
||||||
|
|
||||||
|
function wrap_socketfunction(self, cb)
|
||||||
|
return function(h, ...)
|
||||||
|
local e = self._easy[h]
|
||||||
|
if e then return cb(e, ...) end
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local setopt_socketfunction = wrap_function("setopt_socketfunction")
|
||||||
|
function Multi:setopt_socketfunction(...)
|
||||||
|
local cb = wrap_callback(...)
|
||||||
|
|
||||||
|
return setopt_socketfunction(wrap_socketfunction(self, cb))
|
||||||
|
end
|
||||||
|
|
||||||
|
local setopt = wrap_function("setopt")
|
||||||
|
function Multi:setopt(k, v)
|
||||||
|
if type(k) == 'table' then
|
||||||
|
local t = k
|
||||||
|
|
||||||
|
local socketfunction = t.socketfunction or t[curl.OPT_SOCKETFUNCTION]
|
||||||
|
if socketfunction then
|
||||||
|
t = clone(t)
|
||||||
|
local fn = wrap_socketfunction(self, socketfunction)
|
||||||
|
if t.socketfunction then t.socketfunction = fn end
|
||||||
|
if t[curl.OPT_SOCKETFUNCTION] then t[curl.OPT_SOCKETFUNCTION] = fn end
|
||||||
|
end
|
||||||
|
|
||||||
|
return setopt(self, t)
|
||||||
|
end
|
||||||
|
|
||||||
|
if k == curl.OPT_SOCKETFUNCTION then
|
||||||
|
return self:setopt_httppost(wrap_socketfunction(v))
|
||||||
|
end
|
||||||
|
|
||||||
|
return setopt(self, k, v)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue