Skip to content

Commit 201cf23

Browse files
authored
change: remove useless uri and uris (#45)
1 parent 0c7d66b commit 201cf23

File tree

7 files changed

+18
-195
lines changed

7 files changed

+18
-195
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ The attributes of each element may contain these:
8686

8787
|name |option |description|example|
8888
|:-------- |:--------|:-----------|:-----|
89-
|paths |required|A list of client request uri. The default is a full match, but if the end of the path is `*`, it means that this is a prefix path. For example `/foo*`, it'll match `/foo/bar` or `/foo/glo/grey` etc.|{"/", "/aa", "/bb"}|
89+
|paths |required|A list of client request path. The default is a full match, but if the end of the path is `*`, it means that this is a prefix path. For example `/foo*`, it'll match `/foo/bar` or `/foo/glo/grey` etc.|{"/", "/aa", "/bb"}|
9090
|hosts |option |A list of client request host, not only supports normal domain name, but also supports wildcard name.|{"foo.com", "*.bar.com"}|
91-
|uris |option |A list of client request uris, not only supports static uri, but also supports prefix uri.|{"/foo", "/bar/*"}|
9291
|remote_addrs|option |A list of client remote address(IPv4 and IPv6), and we can use CIDR format, eg `192.168.1.0/24`.|{"127.0.0.1", "192.0.0.0/8", "::1", "fe80::/32"}|
9392
|methods |option |A list of method name. Here is full valid method list: "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT" and "TRACE".|{"GET", "POST"}|
9493
|vars |option |A list of `{var, operator, val}`. For example: {{var, operator, val}, {var, operator, val}, ...}, `{"arg_name", "==", "json"}` means the value of argument `name` expect to `json`. Here is the full [Operator List](#operator-list).|{{"arg_name", "==", "json"}, {"arg_age", ">", 18}}|
@@ -175,7 +174,7 @@ local rx = radix.new({
175174
* `method`: optional, method name of client request.
176175
* `host`: optional, client request host.
177176
* `remote_addr`: optional, client remote address like `192.168.1.100`.
178-
* `uri`: optional, client request uri.
177+
* `paths`: optional, a list of client request path.
179178
* `vars`: optional, a Lua table to fetch variable, default value is `ngx.var` to fetch Ningx builtin variable.
180179

181180
Matchs the route by `method`, `path` and `host` etc, and return `metadata` if successful.
@@ -195,7 +194,6 @@ local metadata = rx:match(ngx.var.uri, {...})
195194
* `method`: optional, method name of client request.
196195
* `host`: optional, client request host.
197196
* `remote_addr`: optional, client remote address like `192.168.1.100`.
198-
* `uri`: optional, client request uri.
199197
* `vars`: optional, a Lua table to fetch variable, default value is `ngx.var` to fetch Ningx builtin variable.
200198

201199
Matchs the route by `method`, `path` and `host` etc, and call `handler` function if successful.

benchmark/match-parameter.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ ngx.update_time()
1313
local start_time = ngx.now()
1414

1515
local res
16-
local uri = "/user/gordon"
16+
local path = "/user/gordon"
1717
for _ = 1, match_times do
18-
res = rx:match(uri)
18+
res = rx:match(path)
1919
end
2020

2121
ngx.update_time()

benchmark/match-prefix.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ ngx.update_time()
1313
local start_time = ngx.now()
1414

1515
local res
16-
local uri = "/" .. ngx.md5(500) .. "/a"
16+
local path = "/" .. ngx.md5(500) .. "/a"
1717
for _ = 1, match_times do
18-
res = rx:match(uri)
18+
res = rx:match(path)
1919
end
2020

2121
ngx.update_time()

benchmark/match-static.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ ngx.update_time()
1313
local start_time = ngx.now()
1414

1515
local res
16-
local uri = "/" .. ngx.md5(500)
16+
local path = "/" .. ngx.md5(500)
1717
for _ = 1, match_times do
18-
res = rx:match(uri)
18+
res = rx:match(path)
1919
end
2020

2121
ngx.update_time()

lib/resty/radixtree.lua

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -261,30 +261,6 @@ function pre_insert_route(self, path, route)
261261
route_opts.hosts = {is_wildcard, host}
262262
end
263263

264-
local uris = route.uris
265-
if type(uris) == "table" and #uris > 0 then
266-
route_opts.uris = {}
267-
for _, uri in ipairs(uris) do
268-
local is_wildcard = false
269-
if uri and uri:sub(#uri, -1) == '*' then
270-
is_wildcard = true
271-
uri = uri:sub(1, -2)
272-
end
273-
274-
insert_tab(route_opts.uris, is_wildcard)
275-
insert_tab(route_opts.uris, uri)
276-
end
277-
278-
elseif type(uris) == "string" then
279-
local is_wildcard = false
280-
if uris and uris:sub(#uris, -1) == '*' then
281-
is_wildcard = true
282-
uris = uris:sub(1, -2)
283-
end
284-
285-
route_opts.uris = {is_wildcard, uris}
286-
end
287-
288264
route_opts.path_org = path
289265

290266
local pos = string.find(path, ':', 1, true)
@@ -400,24 +376,6 @@ local function match_host(route_host_is_wildcard, route_host, request_host)
400376
end
401377

402378

403-
local function match_uri(route_uri_is_wildcard, route_uri, request_uri)
404-
if type(request_uri) ~= "string" or #route_uri > #request_uri then
405-
return false
406-
end
407-
408-
if not route_uri_is_wildcard then
409-
return route_uri == request_uri
410-
end
411-
412-
local i = request_uri:find(route_uri, 1, true)
413-
if i ~= 1 then
414-
return false
415-
end
416-
417-
return true
418-
end
419-
420-
421379
local tmp = {}
422380
local lru_pat, err = lrucache.new(1000)
423381
if not lru_pat then
@@ -534,14 +492,15 @@ end
534492

535493
local function match_route_opts(route, opts, ...)
536494
local method = opts.method
495+
local opts_matched_exists = (opts.matched ~= nil)
537496
if route.method ~= 0 then
538497
if not method or type(METHODS[method]) ~= "number" or
539498
bit.band(route.method, METHODS[method]) == 0 then
540499
return false
541500
end
542501
end
543502

544-
if opts.matched ~= nil then
503+
if opts_matched_exists then
545504
opts.matched._method = method
546505
end
547506

@@ -570,8 +529,12 @@ local function match_route_opts(route, opts, ...)
570529
if reverse_host then
571530
for i = 1, #hosts, 2 do
572531
if match_host(hosts[i], hosts[i + 1], reverse_host) then
573-
if opts.matched ~= nil then
574-
opts.matched._host = hosts[i + 1]:reverse()
532+
if opts_matched_exists then
533+
if hosts[i] then
534+
opts.matched._host = (hosts[i + 1] .. "*"):reverse()
535+
else
536+
opts.matched._host = hosts[i + 1]:reverse()
537+
end
575538
end
576539
matched = true
577540
break
@@ -585,22 +548,6 @@ local function match_route_opts(route, opts, ...)
585548
end
586549
end
587550

588-
if route.uris then
589-
local matched = false
590-
local uris = route.uris
591-
for i = 1, #uris, 2 do
592-
if match_uri(uris[i], uris[i + 1], opts.uri) then
593-
matched = true
594-
break
595-
end
596-
end
597-
598-
log_info("uris match: ", matched)
599-
if not matched then
600-
return false
601-
end
602-
end
603-
604551
if route.vars then
605552
local vars = opts.vars or ngx_var
606553
if type(vars) ~= "table" then

t/parameter.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ matched: {}
186186
},
187187
})
188188
189-
local opts = {matched = {}, method = "GET", uri = "/bb/cc/xx", host = "foo.com"}
189+
local opts = {matched = {}, method = "GET", host = "aa.bar.com"}
190190
local meta = rx:match("/bb/cc/xx", opts)
191191
ngx.say("match meta: ", meta)
192192
ngx.say("matched: ", json.encode(opts.matched))
@@ -199,4 +199,4 @@ GET /t
199199
[error]
200200
--- response_body
201201
match meta: metadata /asf
202-
matched: {"_path":"\/bb\/cc\/*",":ext":"xx","_method":"GET","_host":"foo.com"}
202+
matched: {"_path":"\/bb\/cc\/*",":ext":"xx","_method":"GET","_host":"*.bar.com"}

t/uri.t

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)