forked from bsm/lua-resty-http
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsimple.lua
371 lines (312 loc) · 7.73 KB
/
simple.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
local pairs = pairs
local type = type
local tonumber = tonumber
local tostring = tostring
local setmetatable = setmetatable
local encode_args = ngx.encode_args
local tcp = ngx.socket.tcp
local concat = table.concat
local insert = table.insert
local upper = string.upper
local lower = string.lower
local sub = string.sub
local sfind = string.find
local gmatch = string.gmatch
local gsub = string.gsub
local ipairs = ipairs
local rawset = rawset
local rawget = rawget
local min = math.min
local ngx = ngx
module(...)
_VERSION = "0.1.0"
--------------------------------------
-- LOCAL CONSTANTS --
--------------------------------------
local HTTP_1_1 = " HTTP/1.1\r\n"
local HTTP_1_0 = " HTTP/1.0\r\n"
local USER_AGENT = "Resty/HTTP-Simple " .. _VERSION .. " (Lua)"
-- canonical names for common headers
local common_headers = {
"Cache-Control",
"Content-Length",
"Content-Type",
"Date",
"ETag",
"Expires",
"Host",
"Location",
"User-Agent"
}
for _,key in ipairs(common_headers) do
rawset(common_headers, key, key)
rawset(common_headers, lower(key), key)
end
function normalize_header(key)
local val = common_headers[key]
if val then
return val
end
key = lower(key)
val = common_headers[lower(key)]
if val then
return val
end
-- normalize it ourselves. do not cache it as we could explode our memory usage
key = gsub(key, "^%l", upper)
key = gsub(key, "-%l", upper)
return key
end
--------------------------------------
-- LOCAL HELPERS --
--------------------------------------
local function _req_header(self, opts)
-- Initialize request
local req = {
upper(opts.method or "GET"),
" "
}
-- Append path
local path = opts.path
if type(path) ~= "string" then
path = "/"
elseif sub(path, 1, 1) ~= "/" then
path = "/" .. path
end
insert(req, path)
-- Normalize query string
if type(opts.query) == "table" then
opts.query = encode_args(opts.query)
end
-- Append query string
if type(opts.query) == "string" then
insert(req, "?" .. opts.query)
end
-- Close first line
if opts.version == 1 then
insert(req, HTTP_1_1)
else
insert(req, HTTP_1_0)
end
-- Normalize headers
opts.headers = opts.headers or {}
local headers = {}
for k,v in pairs(opts.headers) do
headers[normalize_header(k)] = v
end
if opts.body then
headers['Content-Length'] = #opts.body
end
if not headers['Host'] then
headers['Host'] = self.host
end
if not headers['User-Agent'] then
headers['User-Agent'] = USER_AGENT
end
if not headers['Accept'] then
headers['Accept'] = "*/*"
end
if version == 0 and not headers['Connection'] then
headers['Connection'] = "Keep-Alive"
end
-- Append headers
for key, values in pairs(headers) do
if type(values) ~= "table" then
values = {values}
end
key = tostring(key)
for _, value in pairs(values) do
insert(req, key .. ": " .. tostring(value) .. "\r\n")
end
end
-- Close headers
insert(req, "\r\n")
return concat(req)
end
local function _parse_headers(sock)
local headers = {}
local mode = nil
repeat
local line = sock:receive()
for key, val in gmatch(line, "([%w%-]+)%s*:%s*(.+)") do
key = normalize_header(key)
if headers[key] then
local delimiter = ", "
if key == "Set-Cookie" then
delimiter = "; "
end
headers[key] = headers[key] .. delimiter .. tostring(val)
else
headers[key] = tostring(val)
end
end
until sfind(line, "^%s*$")
return headers, nil
end
local function _receive_length(sock, length)
local chunks = {}
local chunk, err = sock:receive(length)
if not chunk then
return nil, err
end
return chunk, nil
end
local function _receive_chunked(sock, maxsize)
local chunks = {}
local size = 0
local done = false
repeat
local str, err = sock:receive("*l")
if not str then
return nil, err
end
local length = tonumber(str, 16)
if not length then
return nil, "unable to read chunksize"
end
size = size + length
if maxsize and size > maxsize then
return nil, 'exceeds maxsize'
end
if length > 0 then
local str, err = sock:receive(length)
if not str then
return nil, err
end
insert(chunks, str)
else
done = true
end
-- read the \r\n
sock:receive(2)
until done
return concat(chunks), nil
end
local function _receive_all(sock, maxsize)
-- we read maxsize +1 for the corner case where the upstream wants to write
-- exactly maxsize bytes
local arg = maxsize and (maxsize + 1) or "*a"
local chunk, err, partial = sock:receive(arg)
if maxsize then
-- if we didn't get an error, it means that the upstream still had data to write
-- which means it exceeded maxsize
if not err then
return nil, 'exceeds maxsize'
else
-- you read to closed in this situation so, if upstream did not close
-- then its an error
if err ~= "closed" then
return nil, err
else
-- this seems odd but is correct, bcs of how ngx_lua
-- handled the rror case, which is actually a success
-- in this scenerio
chunk = partial
end
end
end
-- in the case of reading all til closed, closed is not a "valid" error
if not chunk then
return nil, err
end
return chunk, nil
end
local function _receive(self, sock)
local line, err = sock:receive()
if not line then
return nil, err
end
local status = tonumber(sub(line, 10, 12))
local headers, err = _parse_headers(sock)
if not headers then
return nil, err
end
local maxsize = self.opts.maxsize
local length = tonumber(headers["Content-Length"])
local body
local err
local keepalive = true
if length then
if maxsize and length > maxsize then
body, err = nil, 'exceeds maxsize'
else
body, err = _receive_length(sock, length)
end
else
local encoding = headers["Transfer-Encoding"]
if encoding and lower(encoding) == "chunked" then
body, err = _receive_chunked(sock, maxsize)
else
body, err = _receive_all(sock, maxsize)
keepalive = false
end
end
if not body then
if err then
return nil, err
end
keepalive = false
end
if keepalive then
local connection = headers["Connection"]
connection = connection and lower(connection) or nil
if connection then
if connection == "close" then
keepalive = false
end
else
if self.opts.version == 0 then
keepalive = false
end
end
end
if keepalive then
sock:setkeepalive()
else
sock:close()
end
return { status = status, headers = headers, body = body }
end
--------------------------------------
-- PUBLIC API --
--------------------------------------
function request(host, port, opts)
opts = opts or {}
local sock, err = tcp()
if not sock then
return nil, err
end
sock:settimeout(opts.timeout or 5000)
local rc, err = sock:connect(host, port)
if not rc then
return nil, err
end
local version = opts.version
if version then
if version ~= 0 and version ~= 1 then
return nil, "unknown HTTP version"
end
else
opts.version = 1
end
local self = {
host = host,
port = port,
sock = sock,
opts = opts
}
-- Build and send request header
local header = _req_header(self, opts)
local bytes, err = sock:send(header)
if not bytes then
return nil, err
end
-- Send the body if there is one
if opts and type(opts.body) == "string" then
local bytes, err = sock:send(opts.body)
if not bytes then
return nil, err
end
end
return _receive(self, sock)
end