2 LuCI - Lua Development Framework
4 Copyright 2009 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 local nixio = require "nixio"
17 local httpc = require "luci.httpclient"
18 local ltn12 = require "luci.ltn12"
20 local print, tonumber, require = print, tonumber, require
22 module "luci.httpclient.receiver"
24 local function prepare_fd(target)
25 -- Open fd for appending
26 local file, code, msg = nixio.open(target, "r+")
27 if not file and code == nixio.const.ENOENT then
28 file, code, msg = nixio.open(target, "w")
34 return file, code, msg
38 local stat, code, msg = file:lock("ex", "nb")
40 return stat, code, msg
48 local function splice_async(sock, pipeout, pipein, file, cb)
50 local smode = nixio.splice_flags("move", "more", "nonblock")
52 -- Set pipe non-blocking otherwise we might end in a deadlock
53 local stat, code, msg = pipein:setblocking(false)
55 stat, code, msg = pipeout:setblocking(false)
58 return stat, code, msg
63 {fd=sock, events=nixio.poll_flags("in")}
67 {fd=file, events=nixio.poll_flags("out")}
71 local active -- Older splice implementations sometimes don't detect EOS
78 nixio.poll(pollsock, 15000)
80 stat, code, msg = nixio.splice(sock, pipeout, ssize, smode)
82 return stat, code, msg
93 nixio.poll(pollfile, 15000)
95 stat, code, msg = nixio.splice(pipein, file, ssize, smode)
97 return stat, code, msg
108 -- We did not splice any data, maybe EOS, fallback to default
120 local function splice_sync(sock, pipeout, pipein, file, cb)
121 local os = require "os"
122 local posix = require "posix"
124 local smode = nixio.splice_flags("move", "more")
127 -- This is probably the only forking http-client ;-)
128 local pid, code, msg = posix.fork()
130 return pid, code, msg
136 stat, code = nixio.splice(sock, pipeout, ssize, smode)
137 until not stat or stat == 0
141 os.exit(stat or code)
147 stat, code, msg = nixio.splice(pipein, file, ssize, smode)
151 until not stat or stat == 0
159 return stat, code, msg
161 pid, msg, code = posix.wait(pid)
162 if msg == "exited" then
166 return nil, code, nixio.strerror(code)
169 return nil, -0x11, "broken pump"
175 function request_to_file(uri, target, options, cbs)
176 options = options or {}
178 options.headers = options.headers or {}
179 local hdr = options.headers
181 local file, code, msg = prepare_fd(target)
183 return file, code, msg
186 local off = file:tell()
190 hdr.Range = hdr.Range or ("bytes=" .. off .. "-")
193 local code, resp, buffer, sock = httpc.request_raw(uri, options)
197 return code, resp, buffer
198 elseif hdr.Range and code ~= 206 then
199 -- We wanted a part but we got the while file
202 return nil, -4, code, resp
203 elseif not hdr.Range and code ~= 200 then
204 -- We encountered an error
207 return nil, -4, code, resp
210 if cbs.on_header then
211 cbs.on_header(file, code, resp)
214 local chunked = resp.headers["Transfer-Encoding"] == "chunked"
217 -- Write the buffer to file
218 file:writeall(buffer)
221 if not options.splice or not sock:is_socket() or chunked then
225 -- This is a plain TCP socket and there is no encoding so we can splice
227 local pipein, pipeout, msg = nixio.pipe()
231 return pipein, pipeout, msg
235 -- Adjust splice values
237 local smode = nixio.splice_flags("move", "more")
239 -- Splicing 512 bytes should never block on a fresh pipe
240 local stat, code, msg = nixio.splice(sock, pipeout, 512, smode)
245 -- Now do the real splicing
246 local cb = cbs.on_write
247 if options.splice == "asynchronous" then
248 stat, code, msg = splice_async(sock, pipeout, pipein, file, cb)
249 elseif options.splice == "synchronous" then
250 stat, code, msg = splice_sync(sock, pipeout, pipein, file, cb)
255 if stat == false then
259 return stat, code, msg
262 local src = chunked and httpc.chunksource(sock) or sock:blocksource()
263 local snk = file:sink()
266 src = ltn12.source.chain(src, function(chunk)
272 -- Fallback to read/write
273 stat, code, msg = ltn12.pump.all(src, snk)
277 return stat and true, code, msg