summaryrefslogtreecommitdiffhomepage
path: root/libs/httpclient/luasrc
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2009-07-24 18:23:59 +0000
committerSteven Barth <steven@midlink.org>2009-07-24 18:23:59 +0000
commit43aca0def5b0faeeec0bf3a54f1019c83a5ef112 (patch)
tree8e284b15b01d9051efd486389a31a3e49e2a821a /libs/httpclient/luasrc
parent4d75b511579ab4d833783318451a79327ce756ce (diff)
Merge from fonosfera
Diffstat (limited to 'libs/httpclient/luasrc')
-rw-r--r--libs/httpclient/luasrc/httpclient/receiver.lua43
1 files changed, 30 insertions, 13 deletions
diff --git a/libs/httpclient/luasrc/httpclient/receiver.lua b/libs/httpclient/luasrc/httpclient/receiver.lua
index cac96cd95..4f08e93fe 100644
--- a/libs/httpclient/luasrc/httpclient/receiver.lua
+++ b/libs/httpclient/luasrc/httpclient/receiver.lua
@@ -171,44 +171,61 @@ function request_to_file(uri, target, options, cbs)
cbs = cbs or {}
options.headers = options.headers or {}
local hdr = options.headers
+ local file, code, msg
- local file, code, msg = prepare_fd(target)
- if not file then
- return file, code, msg
- end
-
- local off = file:tell()
+ if target then
+ file, code, msg = prepare_fd(target)
+ if not file then
+ return file, code, msg
+ end
- -- Set content range
- if off > 0 then
- hdr.Range = hdr.Range or ("bytes=" .. off .. "-")
+ local off = file:tell()
+
+ -- Set content range
+ if off > 0 then
+ hdr.Range = hdr.Range or ("bytes=" .. off .. "-")
+ end
end
local code, resp, buffer, sock = httpc.request_raw(uri, options)
if not code then
-- No success
- file:close()
+ if file then
+ file:close()
+ end
return code, resp, buffer
elseif hdr.Range and code ~= 206 then
-- We wanted a part but we got the while file
sock:close()
- file:close()
+ if file then
+ file:close()
+ end
return nil, -4, code, resp
elseif not hdr.Range and code ~= 200 then
-- We encountered an error
sock:close()
- file:close()
+ if file then
+ file:close()
+ end
return nil, -4, code, resp
end
if cbs.on_header then
local stat = {cbs.on_header(file, code, resp)}
if stat[1] == false then
- file:close()
+ if file then
+ file:close()
+ end
sock:close()
return unpack(stat)
+ elseif stat[2] then
+ file = file and stat[2]
end
end
+
+ if not file then
+ return nil, -5, "no target given"
+ end
local chunked = resp.headers["Transfer-Encoding"] == "chunked"
local stat