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


   Above the conversation, I got these information:
   
   Target: support specify content-type to filter, substitute response body by 
matching specific regex(fixed string as well) with replacement.
   > regex mode supports to replace all or once, and case-sensitive or not
   
   Solution: 
   Properties table
   | name | description | type | default |
   | -----  | ----------- | ----  | ------- |
   | filter_type | which response content-type matched | array | "text/plain" |
   | filter_pattern | pattern to match response body, could be regex | string | 
|
   | filter_replace | substitution content | string | |
   | filter_options | options for regex, detail see 
[here](https://github.com/openresty/lua-nginx-module#ngxrematch) | string | |
   | filter_kind | regex or completely substitute mode, range [1,2] 
1-completely 2-regex | integer | 1 |
   | filter_scope | regex substitute once or global, range [1,2], 1-once 
2-global | integer | 2 |
   
   Main steps:
   * step 1: check `conf.filter_kind`, equals to 1 then fallback to completely 
substitute, otherwise goto regex substitute(both in header_filter and 
body_filter).
   * step 2: at `header_filter` phase, check match `conf.filter_type` or not, 
if not then return, otherwise then mark `ctx.response_rewrite_matched` true.
   * step 3: at `body_filter` phase, `conf.filter_pattern` empty or if not 
`ctx.response_rewrite_matched` then return, otherwise `check 
conf.filter_scope`: scope 1 use `ngx.re.sub`, scope 2 use `ngx.re.gsub`.
   * step 4. substitution: 
   ```lua
   local body = ngx.arg[1]
   ngx.arg[1] = sub(conf.filter_pattern, conf.filter_replace, 
conf.filter_options)
   ```
   * step 5: mark eof
   ```lua
   ngx.arg[2] = true
   ```
   
   Any advices welcome :-).


-- 
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