-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.lua
More file actions
355 lines (303 loc) · 10.8 KB
/
router.lua
File metadata and controls
355 lines (303 loc) · 10.8 KB
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
-- Router: maps incoming (method, path) to OpenAPI operations.
-- Uses lua-resty-radixtree for high-performance path matching.
-- Converts OpenAPI path templates ({param}) to radixtree :param syntax.
local _M = {}
local radixtree = require("resty.radixtree")
local setmetatable = setmetatable
local tostring = tostring
local pairs = pairs
local ipairs = ipairs
local tab_insert = table.insert
local str_find = string.find
local sub_str = string.sub
local str_gsub = string.gsub
local str_byte = string.byte
local str_upper = string.upper
local str_lower = string.lower
local str_gmatch = string.gmatch
local SLASH = str_byte("/")
local HTTP_METHODS = {
GET = true, POST = true, PUT = true, DELETE = true,
PATCH = true, HEAD = true, OPTIONS = true, TRACE = true,
}
local _router_mt = { __index = _M }
-- Convert OpenAPI path template to radixtree format.
-- e.g. "/users/{id}/posts/{postId}" -> "/users/:id/posts/:postId"
local function convert_path(path_template)
return (str_gsub(path_template, "{([^}]+)}", ":_%1"))
end
-- Detect path templates whose segments mix a {var} with literal text or
-- multiple {var}s in the same segment, e.g. "/foo/{id}.json" or
-- "/baz/{name}.{ext}". radixtree's :param syntax cannot extract these
-- correctly because it consumes the entire `/`-bounded segment as one
-- variable; the literal suffix is silently dropped from both the captured
-- value and the param name. For such templates we fall back to a per-route
-- PCRE that re-extracts path params at match time.
local function has_mixed_segment(path_template)
for seg in str_gmatch(path_template, "/([^/]*)") do
local has_var = str_find(seg, "{", 1, true) ~= nil
if has_var then
-- a clean segment is exactly "{name}" with nothing else
if not (str_byte(seg, 1) == str_byte("{")
and str_byte(seg, #seg) == str_byte("}")
and (str_find(seg, "}", 2, true) == #seg)) then
return true
end
end
end
return false
end
local PCRE_META = {
["%"] = true, ["."] = true, ["*"] = true, ["+"] = true, ["?"] = true,
["("] = true, [")"] = true, ["["] = true, ["]"] = true, ["{"] = true,
["}"] = true, ["|"] = true, ["^"] = true, ["$"] = true, ["\\"] = true,
["/"] = true,
}
local function pcre_escape(s)
return (str_gsub(s, ".", function(c)
if PCRE_META[c] then return "\\" .. c end
end))
end
-- Build a PCRE pattern + ordered name list that extracts path params from
-- a request path matching `path_template`. Used as a fallback when
-- has_mixed_segment(path_template) is true.
local function build_param_pcre(path_template)
local names = {}
local out = {}
local i = 1
while i <= #path_template do
local lb = str_find(path_template, "{", i, true)
if not lb then
tab_insert(out, pcre_escape(sub_str(path_template, i)))
break
end
if lb > i then
tab_insert(out, pcre_escape(sub_str(path_template, i, lb - 1)))
end
local rb = str_find(path_template, "}", lb + 1, true)
if not rb then
tab_insert(out, pcre_escape(sub_str(path_template, lb)))
break
end
tab_insert(names, sub_str(path_template, lb + 1, rb - 1))
tab_insert(out, "([^/]+?)")
i = rb + 1
end
return "^" .. table.concat(out) .. "$", names
end
-- Extract param names from {param} in path template.
local function extract_param_names(path_template)
local names = {}
for name in str_gmatch(path_template, "{([^}]+)}") do
tab_insert(names, name)
end
return names
end
-- Collect and organize parameters for an operation.
local function collect_params(path_item, operation)
local all_params = {}
if path_item.parameters then
for _, p in ipairs(path_item.parameters) do
all_params[p.name .. ":" .. p["in"]] = p
end
end
if operation.parameters then
for _, p in ipairs(operation.parameters) do
all_params[p.name .. ":" .. p["in"]] = p
end
end
local by_loc = { path = {}, query = {}, header = {} }
for _, p in pairs(all_params) do
local loc = p["in"]
if by_loc[loc] then
tab_insert(by_loc[loc], p)
end
end
return by_loc
end
-- Find request body schema and content map.
local function find_body_info(operation)
if not operation.requestBody then
return nil, nil, false
end
local body_required = operation.requestBody.required or false
local content = operation.requestBody.content
if not content then
return nil, nil, body_required
end
local primary_schema
if content["application/json"] then
primary_schema = content["application/json"].schema
else
for ct, media in pairs(content) do
local ct_lower = str_lower(ct)
if ct == "*/*" or str_find(ct_lower, "json", 1, true) then
primary_schema = media.schema
break
end
end
if not primary_schema then
-- pick the first available schema as fallback
for _, media in pairs(content) do -- luacheck: ignore 512
primary_schema = media.schema
break
end
end
end
return primary_schema, content, body_required
end
-- Extract base paths from the servers array.
-- Returns a list of base path prefixes (empty string if none).
local function extract_base_paths(spec)
local servers = spec.servers
if not servers or #servers == 0 then
return { "" }
end
local bases = {}
for _, srv in ipairs(servers) do
local url = srv.url or ""
-- strip protocol+host if present, keep only the path portion
local path = url:match("^https?://[^/]*(/.*)$") or url
-- remove trailing slash
if #path > 1 and str_byte(path, #path) == SLASH then
path = sub_str(path, 1, #path - 1)
end
-- "/" means no prefix
if path == "/" then
path = ""
end
bases[#bases + 1] = path
end
if #bases == 0 then
return { "" }
end
return bases
end
-- Build a router from a compiled OpenAPI spec.
function _M.new(spec)
local radix_routes = {}
local route_metadata = {}
local paths = spec.paths
if not paths then
return setmetatable({ rx = nil, metadata = route_metadata }, _router_mt)
end
local base_paths = extract_base_paths(spec)
local route_id = 0
for path_template, path_item in pairs(paths) do
local param_names = extract_param_names(path_template)
local mixed = has_mixed_segment(path_template)
local param_pcre, pcre_names
if mixed then
param_pcre, pcre_names = build_param_pcre(path_template)
end
for method, operation in pairs(path_item) do
local m = str_upper(method)
if HTTP_METHODS[m] then
route_id = route_id + 1
local id = tostring(route_id)
local params = collect_params(path_item, operation)
local body_schema, body_content, body_required =
find_body_info(operation)
route_metadata[id] = {
path_template = path_template,
param_names = param_names,
param_pcre = param_pcre,
pcre_names = pcre_names,
base_paths = base_paths,
method = m,
operation = operation,
params = params,
body_schema = body_schema,
body_content = body_content,
body_required = body_required,
}
local route_paths = {}
for _, base in ipairs(base_paths) do
route_paths[#route_paths + 1] =
convert_path(base .. path_template)
end
tab_insert(radix_routes, {
paths = route_paths,
methods = { m },
metadata = id,
})
end
end
end
if #radix_routes == 0 then
return setmetatable({ rx = nil, metadata = route_metadata }, _router_mt)
end
local rx = radixtree.new(radix_routes)
return setmetatable({ rx = rx, metadata = route_metadata }, _router_mt)
end
-- Match an incoming request to a route.
function _M.match(self, method, path)
if not self.rx then
return nil, nil
end
method = str_upper(method)
-- strip query string if present
local qpos = str_find(path, "?", 1, true)
if qpos then
path = sub_str(path, 1, qpos - 1)
end
-- normalize: remove trailing slash (except root)
if #path > 1 and str_byte(path, #path) == SLASH then
path = sub_str(path, 1, #path - 1)
end
local matched = {}
local opts = {
method = method,
matched = matched,
}
local id = self.rx:match(path, opts)
if not id then
return nil, nil
end
local route = self.metadata[id]
if not route then
return nil, nil
end
-- extract path params from radixtree matched table
-- radixtree stores them with "_" prefix since we use :_paramName
local path_params = {}
for _, name in ipairs(route.param_names) do
path_params[name] = matched["_" .. name]
end
-- Fallback: when the template has mixed segments (e.g. "/foo/{id}.json"
-- or "/baz/{name}.{ext}"), radixtree can match the route but cannot
-- extract the variables (it consumes the whole `/`-bounded segment as
-- one param and silently drops the literal suffix). Re-extract the
-- params here using a per-route PCRE built from the template.
if route.param_pcre then
local ngx_re = require("ngx.re")
local re_match = ngx.re.match
local m
local bases = route.base_paths or { "" }
for _, base in ipairs(bases) do
local rel = path
if base ~= "" then
if str_find(path, base, 1, true) == 1 then
rel = sub_str(path, #base + 1)
if rel == "" then rel = "/" end
else
rel = nil
end
end
if rel then
local mm, err = re_match(rel, route.param_pcre, "jo")
if mm then m = mm; break end
end
end
if not m then
-- radixtree matched but our authoritative regex doesn't:
-- treat as no match so callers get a clean error.
return nil, nil
end
for i, name in ipairs(route.pcre_names or {}) do
path_params[name] = m[i]
end
end
return route, path_params
end
return _M