-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcurl_http.lua
78 lines (71 loc) · 1.72 KB
/
curl_http.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
local curl = require('libcurl')
local ffi = require('ffi')
local function httpsget(url, headers, retry_times)
local t = {}
local h_strs = nil
if headers then
h_strs = {}
local i = 0
for k, v in pairs(headers) do
table.insert(h_strs, k .. ': ' .. v)
end
end
local e = assert(curl.easy{
url = url,
failonerror = true,
httpheader = h_strs,
accept_encoding = 'gzip, deflate',
ssl_verifyhost = false,
ssl_verifypeer = false,
timeout = 60,
writefunction = function(data, size)
if size == 0 then return 0 end
table.insert(t, ffi.string(data, size))
return size
end
})
local res, err, ecode
for i = 1, retry_times do
t = {}
--print('attempt ' .. i)
res, err, ecode = e:perform()
if res then break end
end
e:close()
if not res then return nil, err, ecode end
return table.concat(t)
end
function url_encode(str)
if str then
str = string.gsub(str, '\n', '\r\n')
str =
string.gsub(
str,
'([^%w:/%-%_%.%~])',
function(c)
return string.format('%%%02X', string.byte(c))
end
)
end
return str
end
--- Convert HTTP arguments to a URL-encoded request body.
-- @param arguments (table) the arguments to convert
-- @return (string) a request body created from the URL-encoded arguments
function params(arguments)
local body = nil
for key, value in pairs(arguments) do
if body then
body = body .. '&'
else
body = ''
end
body = body .. url_encode(key) .. '=' .. url_encode(value)
end
return body or ''
end
return {
httpsget = httpsget,
params = params,
url_encode = url_encode
}