Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: support advanced matching based on POST form #5399

Closed
bzp2010 opened this issue Nov 2, 2021 · 6 comments · Fixed by #5409
Closed

Proposal: support advanced matching based on POST form #5399

bzp2010 opened this issue Nov 2, 2021 · 6 comments · Fixed by #5409
Assignees
Labels

Comments

@bzp2010
Copy link
Contributor

bzp2010 commented Nov 2, 2021

Proposal

Background

Currently, advanced matching only uses URL query data when matching on request content, and does not support advanced matching with fields in POST form as conditions.

Objectives

Support for POST form as matching conditions by adding new matching key.

Solutions

Determine the added matching key prefix

I think we can add a new post_arg_ prefix as a POST form data prefix, this is similar to the arg_ used in the query parameter currently provided by openresty.

Modify the mt metatable in ctx.lua and add a new key processor

The main purpose of the current modification is to handle POST form submissions with Content-Type x-www-form-urlencoded.

Use ngx.req.read_body() and fetch data from ngx.req.get_post_args() according to read requirements in the metatable to achieve complete on-demand inert loading with the help of the metatable feature. The currently used lua-resty-expr can automatically get the target value and compare it with the meta table based on the expression.

current arg_:

apisix/apisix/core/ctx.lua

Lines 167 to 176 in 042ce5c

elseif core_str.has_prefix(key, "arg_") then
local arg_key = sub_str(key, 5)
local args = request.get_uri_args()[arg_key]
if args then
if type(args) == "table" then
val = args[1]
else
val = args
end
end

demo:

if core_str.has_prefix(key,"post_arg_") then
  ngx.req.read_body()
  local args, err = ngx.req.get_post_args()
  loacl post_arg_key = sub_str(key, 10)
  val = args[post_arg_key]

Pre-fetching modifications

  1. apisix/core/ctx.lua: Add new key processor
  2. t/*: Add test case
  3. Add documents for new function
  4. Optional: Add support for its configuration to the Dashboard

Other

What are your ideas?

@bzp2010 bzp2010 self-assigned this Nov 2, 2021
@bzp2010 bzp2010 added the discuss label Nov 2, 2021
@tokers
Copy link
Contributor

tokers commented Nov 2, 2021

+1, Proposal LGTM.

@tzssangglass
Copy link
Member

+1, useful

@jagerzhang
Copy link
Contributor

请教下,这个新增特性是否支持content-type=application/json的POST请求中的body过滤?
目前我们是通过 serverless-pre来实现的,如果这个特性能支持,那就更优雅了。

@tzssangglass
Copy link
Member

not support yet, here:

if request.header(nil, "Content-Type") == "application/x-www-form-urlencoded" then

json is nested and complicated.

@jagerzhang
Copy link
Contributor

@tzssangglass 那还是只能用serverless解决body字段路由的问题了~ 希望可以支持下。。。

@tzssangglass
Copy link
Member

@tzssangglass 那还是只能用serverless解决body字段路由的问题了~ 希望可以支持下。。。

can you submit a new issue about support this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants