Merge pull request #278 from nmav/ocserv
[project/luci.git] / libs / httpclient / luasrc / httpclient / receiver.lua
index 0311090..4f08e93 100644 (file)
@@ -17,7 +17,7 @@ local nixio = require "nixio"
 local httpc = require "luci.httpclient"
 local ltn12 = require "luci.ltn12"
 
-local print, tonumber, require = print, tonumber, require
+local print, tonumber, require, unpack = print, tonumber, require, unpack
 
 module "luci.httpclient.receiver"
 
@@ -114,13 +114,12 @@ end
 
 local function splice_sync(sock, pipeout, pipein, file, cb)
        local os = require "os"
-       local posix = require "posix"
        local ssize = 65536
        local smode = nixio.splice_flags("move", "more")
        local stat
        
        -- This is probably the only forking http-client ;-)
-       local pid, code, msg = posix.fork()
+       local pid, code, msg = nixio.fork()
        if not pid then
                return pid, code, msg
        elseif pid == 0 then
@@ -149,11 +148,11 @@ local function splice_sync(sock, pipeout, pipein, file, cb)
                file:close()
                
                if not stat then
-                       posix.kill(pid)
-                       posix.wait(pid)
+                       nixio.kill(pid, 15)
+                       nixio.wait(pid)
                        return stat, code, msg
                else
-                       pid, msg, code = posix.wait(pid)
+                       pid, msg, code = nixio.wait(pid)
                        if msg == "exited" then
                                if code == 0 then
                                        return true
@@ -172,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 false, nil, nil, unpack(stat, 2)
+                       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