kwanhur commented on issue #5451:
URL: https://github.com/apache/apisix/issues/5451#issuecomment-1068909668


   Solution updated.
   
   Properties table adds one `filters`
   
   | name    | description                              | type  | default    |
   | ------- | ---------------------------------------- | ----- | ---------- |
   | filters | filter expressions, include key attributes how to filter | array 
| `[]` empty |
   
   filter key attributes
   
   | name    | description                              | type   | default   |
   | ------- | ---------------------------------------- | ------ | --------- |
   | expr    | match expression, adapters to 
[lua-resty-expr](https://github.com/api7/lua-resty-expr) , only on header | map 
   | {}        |
   | kind    | regex or subfilter mode                  | string | subfilter |
   | options | regex options, detail see 
[here](https://github.com/openresty/lua-nginx-module#ngxrematch) | string | ""  
      |
   | scope   | regex substitute once or global          | string | "once"    |
   | replace | regex substitution content               | string | ""        |
   | pattern | match pattern on response body, regex or fixed string both ok | 
string | ""        |
   
   Main steps:
   
   step 1: at `header_filter` `body_filter` phase,  if `filters` empty, then 
back to original body-replaced logic(backward compatibly); else then goto step 
2.
   
   step 2: at `header_filter` phase, foreach `filters` and eval `expr` ok or 
not.
   
   - `ctx.filter_matches` to store every filter expr result[true|false]
   - `ctx.filter_matched` to store total filters result(use to check response 
body need to filter or not)
   
   step 3: at `body_filter` phase,
   
   1. if `ctx.filter_matched` true and `ngx.arg[2]` false, then buffer the 
response body into `ctx.filter_body`. 
   2. if `ctx.filter_matched` true and `ngx.arg[2]` true, then foreach 
`filters` to filter, pseudo-code:
      ```lua
      if ctx.filter_matched and ngx.arg[2] then
        for i, filter in ipairs(conf.filters) do
          if ctx.filter_matched[i] then
            if filter.scope == "once" then
              ctx.body = ngx.re.sub(ctx.body,filter.pattern, filter.replace, 
filter.options)
            else
              ctx.body = ngx.re.gsub(ctx.body,filter.pattern, filter.replace, 
filter.options)
            end
          end
        end
        
        ngx.arg[1] = ctx.body
      end
      ```
   
      ​
   
   Q1:  it seems `filter.kind` is redundant, since step 1.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to