Merge from fonosfera
This commit is contained in:
parent
4d75b51157
commit
43aca0def5
1 changed files with 30 additions and 13 deletions
|
@ -171,45 +171,62 @@ function request_to_file(uri, target, options, cbs)
|
||||||
cbs = cbs or {}
|
cbs = cbs or {}
|
||||||
options.headers = options.headers or {}
|
options.headers = options.headers or {}
|
||||||
local hdr = options.headers
|
local hdr = options.headers
|
||||||
|
local file, code, msg
|
||||||
|
|
||||||
local file, code, msg = prepare_fd(target)
|
if target then
|
||||||
if not file then
|
file, code, msg = prepare_fd(target)
|
||||||
return file, code, msg
|
if not file then
|
||||||
end
|
return file, code, msg
|
||||||
|
end
|
||||||
|
|
||||||
local off = file:tell()
|
local off = file:tell()
|
||||||
|
|
||||||
-- Set content range
|
-- Set content range
|
||||||
if off > 0 then
|
if off > 0 then
|
||||||
hdr.Range = hdr.Range or ("bytes=" .. off .. "-")
|
hdr.Range = hdr.Range or ("bytes=" .. off .. "-")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local code, resp, buffer, sock = httpc.request_raw(uri, options)
|
local code, resp, buffer, sock = httpc.request_raw(uri, options)
|
||||||
if not code then
|
if not code then
|
||||||
-- No success
|
-- No success
|
||||||
file:close()
|
if file then
|
||||||
|
file:close()
|
||||||
|
end
|
||||||
return code, resp, buffer
|
return code, resp, buffer
|
||||||
elseif hdr.Range and code ~= 206 then
|
elseif hdr.Range and code ~= 206 then
|
||||||
-- We wanted a part but we got the while file
|
-- We wanted a part but we got the while file
|
||||||
sock:close()
|
sock:close()
|
||||||
file:close()
|
if file then
|
||||||
|
file:close()
|
||||||
|
end
|
||||||
return nil, -4, code, resp
|
return nil, -4, code, resp
|
||||||
elseif not hdr.Range and code ~= 200 then
|
elseif not hdr.Range and code ~= 200 then
|
||||||
-- We encountered an error
|
-- We encountered an error
|
||||||
sock:close()
|
sock:close()
|
||||||
file:close()
|
if file then
|
||||||
|
file:close()
|
||||||
|
end
|
||||||
return nil, -4, code, resp
|
return nil, -4, code, resp
|
||||||
end
|
end
|
||||||
|
|
||||||
if cbs.on_header then
|
if cbs.on_header then
|
||||||
local stat = {cbs.on_header(file, code, resp)}
|
local stat = {cbs.on_header(file, code, resp)}
|
||||||
if stat[1] == false then
|
if stat[1] == false then
|
||||||
file:close()
|
if file then
|
||||||
|
file:close()
|
||||||
|
end
|
||||||
sock:close()
|
sock:close()
|
||||||
return unpack(stat)
|
return unpack(stat)
|
||||||
|
elseif stat[2] then
|
||||||
|
file = file and stat[2]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not file then
|
||||||
|
return nil, -5, "no target given"
|
||||||
|
end
|
||||||
|
|
||||||
local chunked = resp.headers["Transfer-Encoding"] == "chunked"
|
local chunked = resp.headers["Transfer-Encoding"] == "chunked"
|
||||||
local stat
|
local stat
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue