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, unpack = print, tonumber, require, unpack
22 module "luci.httpclient.receiver"
24 local function prepare_fd(target)
25 -- Open fd for appending
26 local oflags = nixio.open_flags("wronly", "creat")
27 local file, code, msg = nixio.open(target, oflags)
29 return file, code, msg
33 local stat, code, msg = file:lock("tlock")
35 return stat, code, msg
43 local function splice_async(sock, pipeout, pipein, file, cb)
45 local smode = nixio.splice_flags("move", "more", "nonblock")
47 -- Set pipe non-blocking otherwise we might end in a deadlock
48 local stat, code, msg = pipein:setblocking(false)
50 stat, code, msg = pipeout:setblocking(false)
53 return stat, code, msg
58 {fd=sock, events=nixio.poll_flags("in")}
62 {fd=file, events=nixio.poll_flags("out")}
66 local active -- Older splice implementations sometimes don't detect EOS
73 nixio.poll(pollsock, 15000)
75 stat, code, msg = nixio.splice(sock, pipeout, ssize, smode)
77 return stat, code, msg
88 nixio.poll(pollfile, 15000)
90 stat, code, msg = nixio.splice(pipein, file, ssize, smode)
92 return stat, code, msg
103 -- We did not splice any data, maybe EOS, fallback to default
115 local function splice_sync(sock, pipeout, pipein, file, cb)
116 local os = require "os"
118 local smode = nixio.splice_flags("move", "more")
121 -- This is probably the only forking http-client ;-)
122 local pid, code, msg = nixio.fork()
124 return pid, code, msg
130 stat, code = nixio.splice(sock, pipeout, ssize, smode)
131 until not stat or stat == 0
135 os.exit(stat or code)
141 stat, code, msg = nixio.splice(pipein, file, ssize, smode)
145 until not stat or stat == 0
153 return stat, code, msg
155 pid, msg, code = nixio.wait(pid)
156 if msg == "exited" then
160 return nil, code, nixio.strerror(code)
163 return nil, -0x11, "broken pump"
169 function request_to_file(uri, target, options, cbs)
170 options = options or {}
172 options.headers = options.headers or {}
173 local hdr = options.headers
174 local file, code, msg
177 file, code, msg = prepare_fd(target)
179 return file, code, msg
182 local off = file:tell()
186 hdr.Range = hdr.Range or ("bytes=" .. off .. "-")
190 local code, resp, buffer, sock = httpc.request_raw(uri, options)
196 return code, resp, buffer
197 elseif hdr.Range and code ~= 206 then
198 -- We wanted a part but we got the while file
203 return nil, -4, code, resp
204 elseif not hdr.Range and code ~= 200 then
205 -- We encountered an error
210 return nil, -4, code, resp
213 if cbs.on_header then
214 local stat = {cbs.on_header(file, code, resp)}
215 if stat[1] == false then
222 file = file and stat[2]
227 return nil, -5, "no target given"
230 local chunked = resp.headers["Transfer-Encoding"] == "chunked"
233 -- Write the buffer to file
234 file:writeall(buffer)
237 if not options.splice or not sock:is_socket() or chunked then
241 -- This is a plain TCP socket and there is no encoding so we can splice
243 local pipein, pipeout, msg = nixio.pipe()
247 return pipein, pipeout, msg
251 -- Adjust splice values
253 local smode = nixio.splice_flags("move", "more")
255 -- Splicing 512 bytes should never block on a fresh pipe
256 local stat, code, msg = nixio.splice(sock, pipeout, 512, smode)
261 -- Now do the real splicing
262 local cb = cbs.on_write
263 if options.splice == "asynchronous" then
264 stat, code, msg = splice_async(sock, pipeout, pipein, file, cb)
265 elseif options.splice == "synchronous" then
266 stat, code, msg = splice_sync(sock, pipeout, pipein, file, cb)
271 if stat == false then
275 return stat, code, msg
278 local src = chunked and httpc.chunksource(sock) or sock:blocksource()
279 local snk = file:sink()
282 src = ltn12.source.chain(src, function(chunk)
288 -- Fallback to read/write
289 stat, code, msg = ltn12.pump.all(src, snk)
293 return stat and true, code, msg