-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathtest_pause02.c.lua
89 lines (71 loc) · 2.11 KB
/
test_pause02.c.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
local curl = require "lcurl"
-- How many times curl_multi_perform should be called before hitting of CURLPAUSE_CONT.
-- (including curl_multi_perform that causes WriteFunction to pause writes,
-- i.e. 1 means that CURLPAUSE_CONT will be performed immediately after pause.)
local WAIT_COUNT = 15
local SIZE = 10 * 1024
-- local RESOURCE_URL = "http://httpbin.org/bytes/" .. SIZE
local RESOURCE_URL = "http://127.0.0.1:7090/bytes/" .. SIZE
local State = {
PAUSE = 0, -- write function should return CURL_WRITEFUNC_PAUSE
WAIT = 1, -- waiting for CURLPAUSE_CONT
WRITE = 2, -- write function should perform write
WRITTEN = 3, -- write function have performed write
}
-- Current state
local state = State.PAUSE
-- Countdown to continue writes
local waitCount = 0
-- Received data and data size
local data, datasize = {}, 0
local function WriteFunction(str)
if state == State.PAUSE then
state = State.WAIT
waitCount = WAIT_COUNT
return curl.WRITEFUNC_PAUSE
end
if state == State.WAIT then
-- callback shouldn't be called in this state
print("WARNING: write-callback called in STATE_WAIT")
return curl.WRITEFUNC_PAUSE
end
if state == State.WRITE then
state = State.WRITTEN
end
datasize = datasize + #str
data[#data + 1] = str
end
local function perform(multi, easy)
while true do
local handles = multi:perform()
if state == State.WAIT then
waitCount = waitCount - 1
if waitCount == 0 then
state = State.WRITE
easy:pause(curl.PAUSE_CONT)
end
end
if state == State.WRITTEN then
state = State.PAUSE
end
if 0 == handles then
local h, ok, err = multi:info_read()
return not not ok, err
end
multi:wait()
end
end
local easy = curl.easy{
url = RESOURCE_URL,
accept_encoding = "gzip,deflate",
writefunction = WriteFunction,
}
local multi = curl.multi()
multi:add_handle(easy)
local ok, err = perform(multi, easy)
if ok then
print("OK: data retrieved successfully (" .. tostring(datasize) .. ")")
else
print("ERROR: data retrieve failed (" .. tostring(err) .. ")")
os.exit(1)
end