From 8aeffac54c19b6fa58b909f9793cb4728fca3647 Mon Sep 17 00:00:00 2001 From: pluveto Date: Mon, 13 Feb 2023 15:46:11 +0800 Subject: [PATCH 01/29] feat: lua validate an expression against a schema --- lib/resty/router/router.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index be5c6db9..b3289e7e 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -97,5 +97,31 @@ function _M:get_fields() return out end +do + local routers = {} + -- validate an expression against a schema + -- @param expr the expression to validate + -- @param schema the schema to validate against + -- @return true if the expression is valid, (nil, error) otherwise + function _M.validate(schema, expr) + local r = routers[schema] + + if not r then + r = ffi_gc(clib.router_new(schema.schema), router_free) + routers[schema] = r + end + + local uuid = "00000000-0000-0000-0000-000000000000" + + local res, err = r:add_matcher(0, uuid, expr) + if not res then + return nil, "invalid expression: " .. err + end + + r:remove_matcher(uuid) + + return true + end +end return _M From c673fb239d1e125207dd61fdfb1219fba02d5649 Mon Sep 17 00:00:00 2001 From: pluveto Date: Mon, 13 Feb 2023 15:49:59 +0800 Subject: [PATCH 02/29] move uuid to outside --- lib/resty/router/router.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index b3289e7e..cda80698 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -99,6 +99,7 @@ end do local routers = {} + local default_uuid = "00000000-0000-0000-0000-000000000000" -- validate an expression against a schema -- @param expr the expression to validate -- @param schema the schema to validate against @@ -111,9 +112,8 @@ do routers[schema] = r end - local uuid = "00000000-0000-0000-0000-000000000000" - local res, err = r:add_matcher(0, uuid, expr) + local res, err = r:add_matcher(0, default_uuid, expr) if not res then return nil, "invalid expression: " .. err end From 91e3ed464d936dce44679787455c7bf446fa5f72 Mon Sep 17 00:00:00 2001 From: pluveto Date: Mon, 13 Feb 2023 16:04:54 +0800 Subject: [PATCH 03/29] refactor: perf improvement --- lib/resty/router/router.lua | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index cda80698..1b3e8a9e 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -100,6 +100,8 @@ end do local routers = {} local default_uuid = "00000000-0000-0000-0000-000000000000" + local default_priority = 0 + -- validate an expression against a schema -- @param expr the expression to validate -- @param schema the schema to validate against @@ -112,14 +114,19 @@ do routers[schema] = r end - - local res, err = r:add_matcher(0, default_uuid, expr) - if not res then - return nil, "invalid expression: " .. err + local errbuf = get_string_buf(ERR_BUF_MAX_LEN) + local errbuf_len = get_size_ptr() + errbuf_len[0] = ERR_BUF_MAX_LEN + + if clib.router_add_matcher(r, default_priority, default_uuid, expr, + errbuf, errbuf_len) == false then + return nil, "invalid expression: " .. ffi_string(errbuf, errbuf_len[0]) end - r:remove_matcher(uuid) - + if clib.router_remove_matcher(r, default_priority, default_uuid) == false then + return nil, "failed when call router_remove_matcher" + end + return true end end From 2d217afd95e757ca15d2bd0d807fa94a7dfd3048 Mon Sep 17 00:00:00 2001 From: pluveto Date: Mon, 13 Feb 2023 16:15:19 +0800 Subject: [PATCH 04/29] draft --- lib/resty/router/router.lua | 2 +- t/06-validate.t | 80 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 t/06-validate.t diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index 1b3e8a9e..a3e76fa9 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -124,7 +124,7 @@ do end if clib.router_remove_matcher(r, default_priority, default_uuid) == false then - return nil, "failed when call router_remove_matcher" + routers[schema] = nil end return true diff --git a/t/06-validate.t b/t/06-validate.t new file mode 100644 index 00000000..8182c9b3 --- /dev/null +++ b/t/06-validate.t @@ -0,0 +1,80 @@ +# vim:set ft= ts=4 sw=4 et: + +use Test::Nginx::Socket::Lua; +use Cwd qw(cwd); + +repeat_each(2); + +plan tests => repeat_each() * blocks() * 5; + +my $pwd = cwd(); + +our $HttpConfig = qq{ + lua_package_path "$pwd/lib/?.lua;;"; + lua_package_cpath "$pwd/target/debug/?.so;;"; +}; + +no_long_string(); +no_diff(); + +run_tests(); + +__DATA__ + +=== TEST 1: test valid schema + expr +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local schema = require("resty.router.schema") + local router = require("resty.router.router") + local context = require("resty.router.context") + + local s = schema.new() + + s:add_field("http.headers.foo", "String") + local expr = "http.headers.foo == \"bar\"" + local r, err = router.validate(s, expr) + + ngx.say(r) + ngx.say(err) + } + } +--- request +GET /t +--- response_body +true +nil +--- no_error_log +[error] +[warn] +[crit] + +=== TEST 1: test invalid schema + expr +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local schema = require("resty.router.schema") + local router = require("resty.router.router") + local context = require("resty.router.context") + + local s = schema.new() + + s:add_field("http.headers.foo", "String") + local expr = "http.headers.foo == 123" + local r, err = router.validate(s, expr) + + ngx.say(r) + ngx.say(err) + } + } +--- request +GET /t +--- response_body +false +nil +--- no_error_log +[error] +[warn] +[crit] \ No newline at end of file From d1c7694497186c6bb61b357cb1afee0467b09099 Mon Sep 17 00:00:00 2001 From: pluveto Date: Mon, 13 Feb 2023 16:32:46 +0800 Subject: [PATCH 05/29] tests: validate --- t/06-validate.t | 59 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/t/06-validate.t b/t/06-validate.t index 8182c9b3..ec9f1b5e 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -50,7 +50,7 @@ nil [warn] [crit] -=== TEST 1: test invalid schema + expr +=== TEST 2: test invalid schema + expr --- http_config eval: $::HttpConfig --- config location = /t { @@ -66,13 +66,66 @@ nil local r, err = router.validate(s, expr) ngx.say(r) - ngx.say(err) } } --- request GET /t --- response_body -false +nil +--- no_error_log +[error] +[warn] +[crit] + + +=== TEST 3: test invalid schema + invalid expr +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local schema = require("resty.router.schema") + local router = require("resty.router.router") + local context = require("resty.router.context") + + local s = schema.new() + + s:add_field("http.headers.foo", "String") + local expr = "== 123" + local r, err = router.validate(s, expr) + + ngx.say(r) + } + } +--- request +GET /t +--- response_body +nil +--- no_error_log +[error] +[warn] +[crit] + +=== TEST 4: test valid schema + invalid expr +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local schema = require("resty.router.schema") + local router = require("resty.router.router") + local context = require("resty.router.context") + + local s = schema.new() + + s:add_field("http.headers.foo", "String") + local expr = "== \"bar\"" + local r, err = router.validate(s, expr) + + ngx.say(r) + } + } +--- request +GET /t +--- response_body nil --- no_error_log [error] From c2021346725e3866977565823b877021f33e64e8 Mon Sep 17 00:00:00 2001 From: pluveto Date: Mon, 13 Feb 2023 17:02:10 +0800 Subject: [PATCH 06/29] update --- t/06-validate.t | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/t/06-validate.t b/t/06-validate.t index ec9f1b5e..cb6da133 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -28,11 +28,10 @@ __DATA__ content_by_lua_block { local schema = require("resty.router.schema") local router = require("resty.router.router") - local context = require("resty.router.context") local s = schema.new() - s:add_field("http.headers.foo", "String") + local expr = "http.headers.foo == \"bar\"" local r, err = router.validate(s, expr) @@ -57,21 +56,21 @@ nil content_by_lua_block { local schema = require("resty.router.schema") local router = require("resty.router.router") - local context = require("resty.router.context") local s = schema.new() - s:add_field("http.headers.foo", "String") + local expr = "http.headers.foo == 123" local r, err = router.validate(s, expr) ngx.say(r) + ngx.say(err) } } --- request GET /t ---- response_body -nil +--- response_body_like +^nil\ninvalid expression.*$ --- no_error_log [error] [warn] @@ -85,21 +84,21 @@ nil content_by_lua_block { local schema = require("resty.router.schema") local router = require("resty.router.router") - local context = require("resty.router.context") local s = schema.new() - s:add_field("http.headers.foo", "String") + local expr = "== 123" local r, err = router.validate(s, expr) ngx.say(r) + ngx.say(err) } } --- request GET /t ---- response_body -nil +--- response_body_like +^nil\ninvalid expression.*$ --- no_error_log [error] [warn] @@ -112,21 +111,21 @@ nil content_by_lua_block { local schema = require("resty.router.schema") local router = require("resty.router.router") - local context = require("resty.router.context") local s = schema.new() - s:add_field("http.headers.foo", "String") + local expr = "== \"bar\"" local r, err = router.validate(s, expr) ngx.say(r) + ngx.say(err) } } --- request GET /t ---- response_body -nil +--- response_body_like +^nil\ninvalid expression.*$ --- no_error_log [error] [warn] From f8b924bb600441c8a8e18540d466d769158f4578 Mon Sep 17 00:00:00 2001 From: pluveto Date: Mon, 13 Feb 2023 17:04:16 +0800 Subject: [PATCH 07/29] add newline --- t/06-validate.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/06-validate.t b/t/06-validate.t index cb6da133..c9bef7ce 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -129,4 +129,4 @@ GET /t --- no_error_log [error] [warn] -[crit] \ No newline at end of file +[crit] From e502d03a9df6581cc656f3b0961580cff5b65ed4 Mon Sep 17 00:00:00 2001 From: pluveto Date: Mon, 13 Feb 2023 17:07:12 +0800 Subject: [PATCH 08/29] rename test 2 --- t/06-validate.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/06-validate.t b/t/06-validate.t index c9bef7ce..37173dbb 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -49,7 +49,7 @@ nil [warn] [crit] -=== TEST 2: test invalid schema + expr +=== TEST 2: test type inconsistency (schema is String, expr is Int) --- http_config eval: $::HttpConfig --- config location = /t { From ab13472ea040c5a445dfd0b78319a9caa6724565 Mon Sep 17 00:00:00 2001 From: pluveto Date: Mon, 13 Feb 2023 17:37:30 +0800 Subject: [PATCH 09/29] regex tests --- lib/resty/router/router.lua | 2 +- t/06-validate.t | 96 +++++++++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 6 deletions(-) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index a3e76fa9..63dbad86 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -120,7 +120,7 @@ do if clib.router_add_matcher(r, default_priority, default_uuid, expr, errbuf, errbuf_len) == false then - return nil, "invalid expression: " .. ffi_string(errbuf, errbuf_len[0]) + return nil, ffi_string(errbuf, errbuf_len[0]) end if clib.router_remove_matcher(r, default_priority, default_uuid) == false then diff --git a/t/06-validate.t b/t/06-validate.t index 37173dbb..4aa9de63 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -70,7 +70,8 @@ nil --- request GET /t --- response_body_like -^nil\ninvalid expression.*$ +^nil +predicate type mismatch.*$ --- no_error_log [error] [warn] @@ -97,8 +98,15 @@ GET /t } --- request GET /t ---- response_body_like -^nil\ninvalid expression.*$ +--- response_body +nil + --> 1:1 + | +1 | == 123 + | ^--- + | + = expected term + --- no_error_log [error] [warn] @@ -124,9 +132,87 @@ GET /t } --- request GET /t ---- response_body_like -^nil\ninvalid expression.*$ +--- response_body +nil + --> 1:1 + | +1 | == "bar" + | ^--- + | + = expected term + --- no_error_log [error] [warn] [crit] + +=== TEST 5: valid regex +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local schema = require("resty.router.schema") + local router = require("resty.router.router") + + local s = schema.new() + s:add_field("http.headers.foo", "String") + + local expr + local r + local err + + expr = "http.headers.foo ~ \"/\\\\\\\\/*user$\"" + r, err = router.validate(s, expr) + ngx.say(r) + ngx.say(err) + } + } +--- request +GET /t +--- response_body +true +nil +--- no_error_log +[error] +[warn] +[crit] + +=== TEST 6: invalid regex +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local schema = require("resty.router.schema") + local router = require("resty.router.router") + + local s = schema.new() + s:add_field("http.headers.foo", "String") + + local expr + local r + local err + + expr = "http.headers.foo ~ \"([.\"" + r, err = router.validate(s, expr) + ngx.say(r) + ngx.say(err) + } + } +--- request +GET /t +--- response_body +nil + --> 1:1 + | +1 | http.headers.foo ~ "([." + | ^----------------------^ + | + = regex parse error: + ([. + ^ +error: unclosed character class + +--- no_error_log +[error] +[warn] +[crit] \ No newline at end of file From 6c0dac71338767e7d392c89af73cc6ff9943442e Mon Sep 17 00:00:00 2001 From: pluveto Date: Tue, 14 Feb 2023 10:05:12 +0800 Subject: [PATCH 10/29] fix test --- t/06-validate.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/06-validate.t b/t/06-validate.t index 4aa9de63..c48ef051 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -70,8 +70,8 @@ nil --- request GET /t --- response_body_like -^nil -predicate type mismatch.*$ +nil +Type mismatch between the LHS and RHS values of predicate --- no_error_log [error] [warn] From f2228d854dc687c17333273b5fb97525f1e0b822 Mon Sep 17 00:00:00 2001 From: pluveto Date: Tue, 14 Feb 2023 10:26:30 +0800 Subject: [PATCH 11/29] add test --- t/06-validate.t | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/t/06-validate.t b/t/06-validate.t index c48ef051..dd5fac6f 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -212,6 +212,46 @@ nil ^ error: unclosed character class +--- no_error_log +[error] +[warn] +[crit] + +=== TEST 6: invalid regex 2 +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local schema = require("resty.router.schema") + local router = require("resty.router.router") + + local s = schema.new() + s:add_field("http.headers.foo", "String") + + local expr + local r + local err + + expr = [[http.headers.foo ~ "/\\/*user$"]] + r, err = router.validate(s, expr) + ngx.say(r) + ngx.say(err) + } + } +--- request +GET /t +--- response_body +nil + --> 1:1 + | +1 | http.headers.foo ~ "/\\/*user$" + | ^-----------------------------^ + | + = regex parse error: + /\/*user$ + ^^ +error: unrecognized escape sequence + --- no_error_log [error] [warn] From 77131b753d8541c29a78ca31fa6fb123d721f4d8 Mon Sep 17 00:00:00 2001 From: pluveto Date: Tue, 14 Feb 2023 11:15:31 +0800 Subject: [PATCH 12/29] fix format --- t/06-validate.t | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/t/06-validate.t b/t/06-validate.t index dd5fac6f..b7f7f093 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -49,6 +49,7 @@ nil [warn] [crit] + === TEST 2: test type inconsistency (schema is String, expr is Int) --- http_config eval: $::HttpConfig --- config @@ -112,6 +113,7 @@ nil [warn] [crit] + === TEST 4: test valid schema + invalid expr --- http_config eval: $::HttpConfig --- config @@ -146,6 +148,7 @@ nil [warn] [crit] + === TEST 5: valid regex --- http_config eval: $::HttpConfig --- config @@ -157,12 +160,8 @@ nil local s = schema.new() s:add_field("http.headers.foo", "String") - local expr - local r - local err - - expr = "http.headers.foo ~ \"/\\\\\\\\/*user$\"" - r, err = router.validate(s, expr) + local expr = "http.headers.foo ~ \"/\\\\\\\\/*user$\"" + local r, err = router.validate(s, expr) ngx.say(r) ngx.say(err) } @@ -177,6 +176,7 @@ nil [warn] [crit] + === TEST 6: invalid regex --- http_config eval: $::HttpConfig --- config @@ -188,12 +188,8 @@ nil local s = schema.new() s:add_field("http.headers.foo", "String") - local expr - local r - local err - - expr = "http.headers.foo ~ \"([.\"" - r, err = router.validate(s, expr) + local expr = "http.headers.foo ~ \"([.\"" + local r, err = router.validate(s, expr) ngx.say(r) ngx.say(err) } @@ -217,6 +213,7 @@ error: unclosed character class [warn] [crit] + === TEST 6: invalid regex 2 --- http_config eval: $::HttpConfig --- config @@ -228,12 +225,8 @@ error: unclosed character class local s = schema.new() s:add_field("http.headers.foo", "String") - local expr - local r - local err - - expr = [[http.headers.foo ~ "/\\/*user$"]] - r, err = router.validate(s, expr) + local expr = [[http.headers.foo ~ "/\\/*user$"]] + local r, err = router.validate(s, expr) ngx.say(r) ngx.say(err) } @@ -255,4 +248,4 @@ error: unrecognized escape sequence --- no_error_log [error] [warn] -[crit] \ No newline at end of file +[crit] From fc3167d255a1253bc80159743229fdd81ae980dc Mon Sep 17 00:00:00 2001 From: pluveto Date: Tue, 14 Feb 2023 11:22:53 +0800 Subject: [PATCH 13/29] rawstring --- t/06-validate.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/06-validate.t b/t/06-validate.t index b7f7f093..08fccfeb 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -160,7 +160,7 @@ nil local s = schema.new() s:add_field("http.headers.foo", "String") - local expr = "http.headers.foo ~ \"/\\\\\\\\/*user$\"" + local expr = [[http.headers.foo ~ "/\\\\/*user$"]] local r, err = router.validate(s, expr) ngx.say(r) ngx.say(err) @@ -188,7 +188,7 @@ nil local s = schema.new() s:add_field("http.headers.foo", "String") - local expr = "http.headers.foo ~ \"([.\"" + local expr = [[http.headers.foo ~ "(["]] local r, err = router.validate(s, expr) ngx.say(r) ngx.say(err) From ec1f953113f3f19015b9c9362f64d68af02d380a Mon Sep 17 00:00:00 2001 From: pluveto Date: Tue, 14 Feb 2023 11:34:21 +0800 Subject: [PATCH 14/29] fix dot --- t/06-validate.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/06-validate.t b/t/06-validate.t index 08fccfeb..dc99e485 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -188,7 +188,7 @@ nil local s = schema.new() s:add_field("http.headers.foo", "String") - local expr = [[http.headers.foo ~ "(["]] + local expr = [[http.headers.foo ~ "([."]] local r, err = router.validate(s, expr) ngx.say(r) ngx.say(err) From 564fa4e6f72ed054778704de22178a87f13116c0 Mon Sep 17 00:00:00 2001 From: Zijing Zhang <50045289+pluveto@users.noreply.github.com> Date: Mon, 20 Mar 2023 16:22:57 +0800 Subject: [PATCH 15/29] Update lib/resty/router/router.lua Co-authored-by: Chrono --- lib/resty/router/router.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index 63dbad86..1cd656c7 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -98,9 +98,9 @@ function _M:get_fields() end do - local routers = {} - local default_uuid = "00000000-0000-0000-0000-000000000000" - local default_priority = 0 + local ROUTERS = {} + local DEFAULT_UUID = "00000000-0000-0000-0000-000000000000" + local DEFAULT_PRIORITY = 0 -- validate an expression against a schema -- @param expr the expression to validate From e8677b610b043408dfd0e263dbeca4181c4ad537 Mon Sep 17 00:00:00 2001 From: Zijing Zhang <50045289+pluveto@users.noreply.github.com> Date: Mon, 20 Mar 2023 16:24:08 +0800 Subject: [PATCH 16/29] Update router.lua --- lib/resty/router/router.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index 1cd656c7..f34e1552 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -98,7 +98,7 @@ function _M:get_fields() end do - local ROUTERS = {} + local routers = {} local DEFAULT_UUID = "00000000-0000-0000-0000-000000000000" local DEFAULT_PRIORITY = 0 @@ -118,12 +118,12 @@ do local errbuf_len = get_size_ptr() errbuf_len[0] = ERR_BUF_MAX_LEN - if clib.router_add_matcher(r, default_priority, default_uuid, expr, + if clib.router_add_matcher(r, DEFAULT_PRIORITY, DEFAULT_UUID, expr, errbuf, errbuf_len) == false then return nil, ffi_string(errbuf, errbuf_len[0]) end - if clib.router_remove_matcher(r, default_priority, default_uuid) == false then + if clib.router_remove_matcher(r, DEFAULT_PRIORITY, DEFAULT_UUID) == false then routers[schema] = nil end From b981f91cbd05d6d96291fbecdb2ea16444240fab Mon Sep 17 00:00:00 2001 From: Zijing Zhang <50045289+pluveto@users.noreply.github.com> Date: Sun, 23 Apr 2023 16:33:57 +0800 Subject: [PATCH 17/29] Update README.md --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 5b4e951a..453092c2 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Table of Contents * [remove\_matcher](#remove_matcher) * [execute](#execute) * [get\_fields](#get_fields) + * [validate](#validate) * [resty.router.context](#restyroutercontext) * [new](#new) * [add\_value](#add_value) @@ -144,6 +145,26 @@ actually used by the user supplied matchers. [Back to TOC](#table-of-contents) +### validate + +**syntax:** *isValid, err = r:validate(schema, expr)* + +**context:** *any* + +Validates an expression against a given schema. It checks if the expression complies with the schema's constraints and structure. + +**params:** + +- *expr*: The expression to validate. +- *schema*: The schema to validate against. + +**returns:** + +- *is_valid*: A boolean value, true if the expression is valid according to the schema, false otherwise. +- *err*: A string describing the error, if the expression is not valid. If the expression is valid, this value will be nil. + +[Back to TOC](#table-of-contents) + ## resty.router.context ### new From 8a6a4843489ad59f9645106b9d6e3862c9feeed3 Mon Sep 17 00:00:00 2001 From: Zijing Zhang <50045289+pluveto@users.noreply.github.com> Date: Sun, 23 Apr 2023 16:37:15 +0800 Subject: [PATCH 18/29] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 453092c2..856c2a17 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ actually used by the user supplied matchers. ### validate -**syntax:** *isValid, err = r:validate(schema, expr)* +**syntax:** *isValid, err = r.validate(schema, expr)* **context:** *any* From 42ab8fbe5005afa0e4b40fc5a85f36fd9db3fe6b Mon Sep 17 00:00:00 2001 From: Zijing Zhang <50045289+pluveto@users.noreply.github.com> Date: Sun, 23 Apr 2023 16:46:51 +0800 Subject: [PATCH 19/29] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 856c2a17..71e90a6a 100644 --- a/README.md +++ b/README.md @@ -147,11 +147,11 @@ actually used by the user supplied matchers. ### validate -**syntax:** *isValid, err = r.validate(schema, expr)* +**syntax:** *ok, err = r.validate(schema, expr)* **context:** *any* -Validates an expression against a given schema. It checks if the expression complies with the schema's constraints and structure. +Validates an expression against a given schema. **params:** @@ -161,7 +161,7 @@ Validates an expression against a given schema. It checks if the expression comp **returns:** - *is_valid*: A boolean value, true if the expression is valid according to the schema, false otherwise. -- *err*: A string describing the error, if the expression is not valid. If the expression is valid, this value will be nil. +- *err*: A string describing the error, if the expression is not valid. Otherwise, this value will be nil. [Back to TOC](#table-of-contents) From b5fe1e8d15ee96f3e0272c0c7962e4aef46590b7 Mon Sep 17 00:00:00 2001 From: Zijing Zhang <50045289+pluveto@users.noreply.github.com> Date: Sun, 23 Apr 2023 17:29:16 +0800 Subject: [PATCH 20/29] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 71e90a6a..b2053cb9 100644 --- a/README.md +++ b/README.md @@ -160,8 +160,7 @@ Validates an expression against a given schema. **returns:** -- *is_valid*: A boolean value, true if the expression is valid according to the schema, false otherwise. -- *err*: A string describing the error, if the expression is not valid. Otherwise, this value will be nil. +Returns `ok` when valid. If an error occurred, `false` and a string describing the error will be returned. [Back to TOC](#table-of-contents) From 9b86fb6d87a95cef6569ed7845dd5dbb0630cc04 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 24 Apr 2023 13:47:34 +0800 Subject: [PATCH 21/29] assert remove ok --- lib/resty/router/router.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index f34e1552..550498f9 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -117,16 +117,14 @@ do local errbuf = get_string_buf(ERR_BUF_MAX_LEN) local errbuf_len = get_size_ptr() errbuf_len[0] = ERR_BUF_MAX_LEN - + if clib.router_add_matcher(r, DEFAULT_PRIORITY, DEFAULT_UUID, expr, errbuf, errbuf_len) == false then return nil, ffi_string(errbuf, errbuf_len[0]) end - if clib.router_remove_matcher(r, DEFAULT_PRIORITY, DEFAULT_UUID) == false then - routers[schema] = nil - end - + assert(clib.router_remove_matcher(r, DEFAULT_PRIORITY, DEFAULT_UUID)) + return true end end From e553871e503a8e8fe6acfb32ad5f6f0d4c3ede20 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 24 Apr 2023 13:49:31 +0800 Subject: [PATCH 22/29] weak table --- lib/resty/router/router.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index 550498f9..76f14919 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -97,8 +97,8 @@ function _M:get_fields() return out end -do - local routers = {} +do + local ROUTERS = setmetatable({}, { __mode = "k" }) local DEFAULT_UUID = "00000000-0000-0000-0000-000000000000" local DEFAULT_PRIORITY = 0 @@ -107,11 +107,11 @@ do -- @param schema the schema to validate against -- @return true if the expression is valid, (nil, error) otherwise function _M.validate(schema, expr) - local r = routers[schema] + local r = ROUTERS[schema] if not r then r = ffi_gc(clib.router_new(schema.schema), router_free) - routers[schema] = r + ROUTERS[schema] = r end local errbuf = get_string_buf(ERR_BUF_MAX_LEN) From 866ba469689ba5d6d5bd77150505a4a653370ec3 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 24 Apr 2023 13:54:16 +0800 Subject: [PATCH 23/29] readme --- README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b2053cb9..6ac122d4 100644 --- a/README.md +++ b/README.md @@ -153,14 +153,10 @@ actually used by the user supplied matchers. Validates an expression against a given schema. -**params:** +Returns `true` when the expression is valid. -- *expr*: The expression to validate. -- *schema*: The schema to validate against. - -**returns:** - -Returns `ok` when valid. If an error occurred, `false` and a string describing the error will be returned. +If the expression is invalid, +`nil` and a string describing the reason will be returned. [Back to TOC](#table-of-contents) From 7983c9f506822cdd988f9fd428cff66ab491c188 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 8 May 2023 14:50:39 +0800 Subject: [PATCH 24/29] fix case to fit regex 1.8 --- t/06-validate.t | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/t/06-validate.t b/t/06-validate.t index dc99e485..d42211e6 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -214,7 +214,7 @@ error: unclosed character class [crit] -=== TEST 6: invalid regex 2 +=== TEST 6: Rust regex 1.8.x will not think the regex is invalid --- http_config eval: $::HttpConfig --- config location = /t { @@ -234,17 +234,8 @@ error: unclosed character class --- request GET /t --- response_body +true nil - --> 1:1 - | -1 | http.headers.foo ~ "/\\/*user$" - | ^-----------------------------^ - | - = regex parse error: - /\/*user$ - ^^ -error: unrecognized escape sequence - --- no_error_log [error] [warn] From 5c152387b30b1add8c7d0bfffdee3d75de1603bb Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 8 May 2023 15:26:23 +0800 Subject: [PATCH 25/29] minor style clean --- lib/resty/router/router.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index 76f14919..7166886c 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -97,6 +97,7 @@ function _M:get_fields() return out end + do local ROUTERS = setmetatable({}, { __mode = "k" }) local DEFAULT_UUID = "00000000-0000-0000-0000-000000000000" @@ -129,4 +130,5 @@ do end end + return _M From 4e51351419e0f73454ea6fda24cdb454691ed2ab Mon Sep 17 00:00:00 2001 From: Wangchong Zhou Date: Tue, 9 May 2023 16:27:05 +0800 Subject: [PATCH 26/29] chore(makefile): move step to install lualib into seperate target --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6ae96fbd..2244f0e3 100644 --- a/Makefile +++ b/Makefile @@ -23,9 +23,11 @@ build: target/release/libatc_router.$(SHLIB_EXT) target/release/libatc_router.a target/release/libatc_router.%: src/*.rs cargo build --release -install: build +install-lualib: $(INSTALL) -d $(DESTDIR)$(LUA_LIB_DIR)/resty/router/ $(INSTALL) -m 664 lib/resty/router/*.lua $(DESTDIR)$(LUA_LIB_DIR)/resty/router/ + +install: build install-lualib $(INSTALL) -m 775 target/release/libatc_router.$(SHLIB_EXT) $(DESTDIR)$(LUA_LIB_DIR)/libatc_router.$(SHLIB_EXT) clean: From ab7a1a7dc57b521dda1655aba1e2b117cf31f124 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 10 May 2023 10:21:54 +0800 Subject: [PATCH 27/29] typo fix --- t/06-validate.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/06-validate.t b/t/06-validate.t index d42211e6..720f79fa 100644 --- a/t/06-validate.t +++ b/t/06-validate.t @@ -214,7 +214,7 @@ error: unclosed character class [crit] -=== TEST 6: Rust regex 1.8.x will not think the regex is invalid +=== TEST 7: Rust regex 1.8.x will not think the regex is invalid --- http_config eval: $::HttpConfig --- config location = /t { From 4e82d60b34528ce65f38e1fb7e08ddc3be92d83c Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 10 May 2023 17:40:56 +0800 Subject: [PATCH 28/29] code clean --- lib/resty/router/router.lua | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/resty/router/router.lua b/lib/resty/router/router.lua index 7166886c..25965ee3 100644 --- a/lib/resty/router/router.lua +++ b/lib/resty/router/router.lua @@ -111,20 +111,16 @@ do local r = ROUTERS[schema] if not r then - r = ffi_gc(clib.router_new(schema.schema), router_free) + r = _M.new(schema, 1) ROUTERS[schema] = r end - local errbuf = get_string_buf(ERR_BUF_MAX_LEN) - local errbuf_len = get_size_ptr() - errbuf_len[0] = ERR_BUF_MAX_LEN - - if clib.router_add_matcher(r, DEFAULT_PRIORITY, DEFAULT_UUID, expr, - errbuf, errbuf_len) == false then - return nil, ffi_string(errbuf, errbuf_len[0]) + local ok, err = r:add_matcher(DEFAULT_PRIORITY, DEFAULT_UUID, expr) + if not ok then + return nil, err end - assert(clib.router_remove_matcher(r, DEFAULT_PRIORITY, DEFAULT_UUID)) + assert(r:remove_matcher(DEFAULT_UUID)) return true end From 57e50d12f8c110ad86d27de4a3627e1b83ef29b0 Mon Sep 17 00:00:00 2001 From: Datong Sun Date: Wed, 10 May 2023 18:33:25 +0800 Subject: [PATCH 29/29] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ac122d4..8a204c49 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ actually used by the user supplied matchers. ### validate -**syntax:** *ok, err = r.validate(schema, expr)* +**syntax:** *ok, err = router.validate(schema, expr)* **context:** *any*