1 -- Copyright 2009 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
5 local nixio = require "nixio"
6 local httpc = require "luci.httpclient"
7 local ltn12 = require "luci.ltn12"
9 local print, tonumber, require, unpack = print, tonumber, require, unpack
11 module "luci.httpclient.receiver"
13 local function prepare_fd(target)
14 -- Open fd for appending
15 local oflags = nixio.open_flags("wronly", "creat")
16 local file, code, msg = nixio.open(target, oflags)
18 return file, code, msg
22 local stat, code, msg = file:lock("tlock")
24 return stat, code, msg
32 local function splice_async(sock, pipeout, pipein, file, cb)
34 local smode = nixio.splice_flags("move", "more", "nonblock")
36 -- Set pipe non-blocking otherwise we might end in a deadlock
37 local stat, code, msg = pipein:setblocking(false)
39 stat, code, msg = pipeout:setblocking(false)
42 return stat, code, msg
47 {fd=sock, events=nixio.poll_flags("in")}
51 {fd=file, events=nixio.poll_flags("out")}
55 local active -- Older splice implementations sometimes don't detect EOS
62 nixio.poll(pollsock, 15000)
64 stat, code, msg = nixio.splice(sock, pipeout, ssize, smode)
66 return stat, code, msg
77 nixio.poll(pollfile, 15000)
79 stat, code, msg = nixio.splice(pipein, file, ssize, smode)
81 return stat, code, msg
92 -- We did not splice any data, maybe EOS, fallback to default
104 local function splice_sync(sock, pipeout, pipein, file, cb)
105 local os = require "os"
107 local smode = nixio.splice_flags("move", "more")
110 -- This is probably the only forking http-client ;-)
111 local pid, code, msg = nixio.fork()
113 return pid, code, msg
119 stat, code = nixio.splice(sock, pipeout, ssize, smode)
120 until not stat or stat == 0
124 os.exit(stat or code)
130 stat, code, msg = nixio.splice(pipein, file, ssize, smode)
134 until not stat or stat == 0
142 return stat, code, msg
144 pid, msg, code = nixio.wait(pid)
145 if msg == "exited" then
149 return nil, code, nixio.strerror(code)
152 return nil, -0x11, "broken pump"
158 function request_to_file(uri, target, options, cbs)
159 options = options or {}
161 options.headers = options.headers or {}
162 local hdr = options.headers
163 local file, code, msg
166 file, code, msg = prepare_fd(target)
168 return file, code, msg
171 local off = file:tell()
175 hdr.Range = hdr.Range or ("bytes=" .. off .. "-")
179 local code, resp, buffer, sock = httpc.request_raw(uri, options)
185 return code, resp, buffer
186 elseif hdr.Range and code ~= 206 then
187 -- We wanted a part but we got the while file
192 return nil, -4, code, resp
193 elseif not hdr.Range and code ~= 200 then
194 -- We encountered an error
199 return nil, -4, code, resp
202 if cbs.on_header then
203 local stat = {cbs.on_header(file, code, resp)}
204 if stat[1] == false then
211 file = file and stat[2]
216 return nil, -5, "no target given"
219 local chunked = resp.headers["Transfer-Encoding"] == "chunked"
222 -- Write the buffer to file
223 file:writeall(buffer)
226 if not options.splice or not sock:is_socket() or chunked then
230 -- This is a plain TCP socket and there is no encoding so we can splice
232 local pipein, pipeout, msg = nixio.pipe()
236 return pipein, pipeout, msg
240 -- Adjust splice values
242 local smode = nixio.splice_flags("move", "more")
244 -- Splicing 512 bytes should never block on a fresh pipe
245 local stat, code, msg = nixio.splice(sock, pipeout, 512, smode)
250 -- Now do the real splicing
251 local cb = cbs.on_write
252 if options.splice == "asynchronous" then
253 stat, code, msg = splice_async(sock, pipeout, pipein, file, cb)
254 elseif options.splice == "synchronous" then
255 stat, code, msg = splice_sync(sock, pipeout, pipein, file, cb)
260 if stat == false then
264 return stat, code, msg
267 local src = chunked and httpc.chunksource(sock) or sock:blocksource()
268 local snk = file:sink()
271 src = ltn12.source.chain(src, function(chunk)
277 -- Fallback to read/write
278 stat, code, msg = ltn12.pump.all(src, snk)
282 return stat and true, code, msg