-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.lua
74 lines (62 loc) · 1.58 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
local _M = {}
-- Makes a request, but instead of reading the entire body into the memory,
-- it reads in chunks, and invokes res_callback with response and invokes
-- data_callback with each chunk of body read.
-- if res_callback or data_callback returns false, it stops reading body.
function _M.request_uri_stream(httpc, uri, params, res_callback, data_callback)
if not params then
params = {}
end
local parsed_uri, err = httpc:parse_uri(uri)
if err or not parsed_uri then
return nil, err
end
local scheme, host, port, path = unpack(parsed_uri)
if not params.path then
params.path = path
end
local c
c, err = httpc:connect(host, port)
if err or not c then
return nil, err
end
if scheme == "https" then
local verify = true
if params.ssl_verify == false then
verify = false
end
local ok
ok, err = httpc:ssl_handshake(nil, host, verify)
if err or not ok then
return nil, err
end
end
local res
res, err = httpc:request(params)
if err or not res then
return nil, err
end
local read_body = true
if type(res_callback) == "function" then
if res_callback(res) == false then -- specifically false, not just falsy
read_body = false
end
end
if read_body and type(data_callback) == "function" then
local chunk;
repeat
chunk, err = res.body_reader(8192)
if err then
break
end
if chunk then
if data_callback(chunk) == false then
break
end
end
until not chunk
end
httpc:set_keepalive()
return res, err
end
return _M