From 18b72fab9b4b5c1ef72003685f9913f047b9c9ac Mon Sep 17 00:00:00 2001 From: David Ortiz Date: Thu, 12 Jul 2018 18:01:03 +0200 Subject: [PATCH] policy/conditional: add first version of Engine This is just a first very basic version of the conditional engine. --- .../src/apicast/policy/conditional/engine.lua | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 gateway/src/apicast/policy/conditional/engine.lua diff --git a/gateway/src/apicast/policy/conditional/engine.lua b/gateway/src/apicast/policy/conditional/engine.lua new file mode 100644 index 000000000..2d3acc056 --- /dev/null +++ b/gateway/src/apicast/policy/conditional/engine.lua @@ -0,0 +1,28 @@ +local _M = {} + +local value_of = { + request_method = function() return ngx.req.get_method() end, + request_host = function() return ngx.var.host end, + request_path = function() return ngx.var.uri end +} + +function _M.evaluate(expression) + local match_attr = ngx.re.match(expression, [[^([\w]+)$]], 'oj') + + if match_attr then + return value_of[match_attr[1]]() + end + + local match_attr_and_value = ngx.re.match(expression, [[^([\w]+) == "([\w/]+)"$]], 'oj') + + if not match_attr_and_value then + return nil, 'Error while parsing the condition' + end + + local entity = match_attr_and_value[1] + local value = match_attr_and_value[2] + + return value_of[entity]() == value +end + +return _M