-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.lua
181 lines (162 loc) · 4.02 KB
/
requests.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
-- luacheck: ignore
local _fail = fail
local insert, concat = table.insert, table.concat
local lower, match = string.lower, string.match
local requests = {}
requests.__index = requests
setmetatable(requests, {
__call = function(self, ...)
return self.new(...)
end
})
--[[
--default timeout: 60s
local response, status, header = request--[[.get]] {
url = "", -- or {scheme = "", userinfo = "", host = "", port = "", query = "" --[[or {}]], fragment = ""
body = "", -- or {aa = 1}
headers = {},
}
--]]
requests._uri = function(url, encoder)
local result = {}
insert(result, assert(url.scheme, "URL scheme required"))
insert(result, ":")
local scheme = url.scheme
if scheme == "http" or scheme == "https" then
insert(result, "//")
end
if url.userinfo then
insert(result, url.userinfo)
insert(result, "@")
end
insert(result, assert(url.host, "URL host required"))
if url.port then
insert(result, ":")
insert(result, url.port)
end
if url.path then
local path = url.path
if not match(path, "^/") then
insert(result, "/")
end
insert(result, path)
end
if url.query then
local query = url.query
if type(query) == "table" then
query = encoder(query)
end
if not match(query, "^%?") then
insert(result, "?")
end
insert(result, query)
end
if url.fragment then
local fragment = url.fragment
if type(fragment) == "table" then
fragment = encoder(fragment)
end
if not match(fragment, "^#") then
insert(result, "#")
end
insert(result, fragment)
end
return concat(result)
end
requests._encoder = {
["ngx_lua"] = ngx.encode_args,
["lua-resty-http"] = ngx.encode_args,
}
requests._socket = {
["ngx_lua"] = function(data, encoder) -- https://github.com/leafo/lapis/blob/master/lapis/nginx/http.moon
-- encode url
local url = data.url
if type(url) == "table" then
url = requests._uri(url, encoder)
end
-- encode headers
-- (pass)
-- encode body
local body = data.body
if type(body) == "table" then
body = encoder(body)
end
-- make request
local result = ngx.location.capture("/proxy", {
method = data.method,
body = body,
ctx = {
headers = data.headers
},
vars = {
_url = url
}
})
return result.body, result.status, result.header
end,
["lua-resty-http"] = function(data)
-- encode url
local url = data.url
if type(url) == "table" then
url = requests._uri(url, encoder)
end
-- encode headers
-- (pass)
-- encode body
local body = data.body
if type(body) == "table" then
body = encoder(body)
end
local http = require("resty.http")
local httpc = http.new()
local result, err = httpc:request_uri(data.url, {
method = data.method,
body = data.body,
headers = data.headers,
keepalive_timeout = 60,
keepalive_pool = 10
})
return result and (result.body, result.status, result.header) or (_fail, err)
end,
["lua-resty-http"] = function(data)
-- encode url
local url = data.url
if type(url) == "table" then
url = requests._uri(url, encoder)
end
-- encode headers
-- (pass)
-- encode body
local body = data.body
if type(body) == "table" then
body = encoder(body)
end
local http = require("resty.http")
local httpc = http.new()
local result, err = httpc:request_uri(data.url, {
method = data.method,
body = data.body,
headers = data.headers,
keepalive_timeout = 60,
keepalive_pool = 10
})
return result and (result.body, result.status, result.header) or (_fail, err)
end
}
function requests.new(socket)
local self = setmetatable({}, requests)
if not socket then
self.socket = socket
else
self.socket = requests._detect()
end
if not self.socket then
return _fail, "unable to detect socket"
end
return self
end
function requests._detect()
if ngx then
end
end
return requests