openresty / openresty/lua-nginx-module

Should I use body_filter_by_lua_block to perform filter on the entire response body?

Open
#1,813 13 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

The question is general in nature, but I work with version openresty/1.17.8.2

I need to filter (search and replace some strings) in the response body possibly arriving from upstream...
From docs, I understand that the response body received in ngx.arg[1] can arrive split into multiple parts i.e. body_filter_by_lua_block is called multiple times with those parts.

I found many simple usage examples that suggest it exactly done like in the example below... so I wonder if I am missing something and they actually can work correctly after all?

Namely, it would seem that filtering and using the string.gsub cannot possibly work correctly, as long as there is the possibility that an instance of 'will-be-replaced-text' is actually split somewhere in the middle between the calls. It would not be matched when ngx.arg[1] is given as subject string to string.gsub

Example:
This example would produce response exactly like the original without substituting anything.

Response body:
This is a will-be-replaced-text, and it is split in an unlucky place for me...

Immagine it arrive split in 2 parts like this:

  1. This is a will-be-repla
  2. ced-text, and it is split in an unlucky place for me...

body_filter_by_lua_block {
ngx.arg[1] = string.gsub(ngx.arg[1], 'will-be-replaced-text', 'replace-with-some-other-text')
}

I know that in my case the response body is small enough not to cause memory problems for me...
Is there a way to ensure that I receive the whole response body in 1 piece?
Or is there any other way to correctly search and replace something the response body?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the body_filter_by_lua_block documentation and the ngx.arg[1] behavior described in the issue, then review the response-body chunking example. Done means clarifying whether whole-response filtering is supported and documenting a correct approach for replacements that span chunks.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx
Domain
backend
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.