f478fe850f5e89469a8b929aedee2e3291a344ee
[project/luci.git] / libs / httpclient / luasrc / httpclient / receiver.lua
1 --[[
2 LuCI - Lua Development Framework 
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5
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
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 require "nixio.util"
16 local nixio = require "nixio"
17 local httpclient = require "luci.httpclient"
18 local ltn12 = require "luci.ltn12"
19
20 local print = print
21
22 module "luci.httpclient.receiver"
23
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")
29                 if file then
30                         file:flush()
31                 end
32         end
33         if not file then
34                 return file, code, msg
35         end
36         
37         -- Acquire lock
38         local stat, code, msg = file:lock("ex", "nb")
39         if not stat then
40                 return stat, code, msg
41         end
42         
43         file:seek(0, "end") 
44         
45         return file
46 end
47
48
49 function request_to_file(uri, target, options, cbs)
50         options = options or {}
51         cbs = cbs or {}
52         options.headers = options.headers or {}
53         local hdr = options.headers
54         
55         local file, code, msg = prepare_fd(target)
56         if not file then
57                 return file, code, msg
58         end
59         
60         local off = file:tell()
61         
62         -- Set content range
63         if off > 0 then
64                 hdr.Range = hdr.Range or ("bytes=" .. off .. "-")  
65         end
66         
67         local code, resp, buffer, sock = httpclient.request_raw(uri, options)
68         if not code then
69                 -- No success
70                 file:close()
71                 return code, resp, buffer
72         elseif hdr.Range and code ~= 206 then
73                 -- We wanted a part but we got the while file
74                 sock:close()
75                 file:close()
76                 return nil, -4, code, resp
77         elseif not hdr.Range and code ~= 200 then
78                 -- We encountered an error
79                 sock:close()
80                 file:close()
81                 return nil, -4, code, resp
82         end
83         
84         if cbs.on_header then
85                 cbs.on_header(file, code, resp)
86         end
87
88         local chunked = resp.headers["Transfer-Encoding"] == "chunked"
89
90         -- Write the buffer to file
91         file:writeall(buffer)
92         print ("Buffered data: " .. #buffer .. " Byte")
93         
94         repeat
95                 if not sock:is_socket() or chunked then
96                         break
97                 end
98                 
99                 -- This is a plain TCP socket and there is no encoding so we can splice
100         
101                 local pipein, pipeout, msg = nixio.pipe()
102                 if not pipein then
103                         sock:close()
104                         file:close()
105                         return pipein, pipeout, msg
106                 end
107                 
108                 
109                 -- Disable blocking for the pipe otherwise we might end in a deadlock
110                 local stat, code, msg = pipein:setblocking(false)
111                 if stat then
112                         stat, code, msg = pipeout:setblocking(false)
113                 end
114                 if not stat then
115                         sock:close()
116                         file:close()
117                         return stat, code, msg
118                 end
119                 
120                 
121                 -- Adjust splice values
122                 local ssize = 65536
123                 local smode = nixio.splice_flags("move", "more", "nonblock")
124                 
125                 local stat, code, msg = nixio.splice(sock, pipeout, ssize, smode)
126                 if stat == nil then
127                         break
128                 end
129                 
130                 local pollsock = {
131                         {fd=sock, events=nixio.poll_flags("in")}
132                 }
133                 
134                 local pollfile = {
135                         {fd=file, events=nixio.poll_flags("out")}
136                 }
137                 
138                 local done
139                 
140                 repeat
141                         -- Socket -> Pipe
142                         repeat
143                                 nixio.poll(pollsock, 15000)
144                         
145                                 stat, code, msg = nixio.splice(sock, pipeout, ssize, smode)
146                                 if stat == nil then
147                                         sock:close()
148                                         file:close()
149                                         return stat, code, msg
150                                 elseif stat == 0 then
151                                         done = true
152                                         break
153                                 end
154                         until stat == false
155                         
156                         -- Pipe -> File
157                         repeat
158                                 nixio.poll(pollfile, 15000)
159                         
160                                 stat, code, msg = nixio.splice(pipein, file, ssize, smode)
161                                 if stat == nil then
162                                         sock:close()
163                                         file:close()
164                                         return stat, code, msg
165                                 end
166                         until stat == false
167                         
168                         if cbs.on_write then
169                                 cbs.on_write(file)
170                         end
171                 until done
172                 
173                 file:close()
174                 sock:close()
175                 return true
176         until true
177         
178         print "Warning: splice() failed, falling back to read/write mode"
179         
180         local src = chunked and httpclient.chunksource(sock) or sock:blocksource()
181         local snk = file:sink()
182         
183         if cbs.on_write then
184                 src = ltn12.source.chain(src, function(chunk)
185                         cbs.on_write(file)
186                         return chunk
187                 end)
188         end
189         
190         -- Fallback to read/write
191         local stat, code, msg = ltn12.pump.all(src, snk)
192         if stat then
193                 file:close()
194                 sock:close()
195         end
196         return stat, code, msg
197 end