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

bug: response-rewrite filters.regex does not apply when content-encoding: gzip #10562

Closed
JoonaHa opened this issue Nov 28, 2023 · 9 comments · Fixed by #10637
Closed

bug: response-rewrite filters.regex does not apply when content-encoding: gzip #10562

JoonaHa opened this issue Nov 28, 2023 · 9 comments · Fixed by #10637
Assignees

Comments

@JoonaHa
Copy link

JoonaHa commented Nov 28, 2023

Current Behavior

I have an APISIX route that uses proxy-rewrite to proxy users to specific endpoint. The endpoint response is a JSON payload with content headers Content-Type: application/json and content-encoding: gzip.

When I add response-rewrite plugin with filters.regex APISIX drops the content-encoding: gzip header. When I add the header back manually using response-rewrite's headers.add functionality, the filters.regex replacement is not applied.

Here is my route configuration with placeholder urls:

{
    "id":"testroute",
    "methods":[
        "GET"
    ],
    "plugins":{
        "proxy-rewrite":{
            "uri":"/specific/path",
            "host":"example.org"
        },
        "response-rewrite":{
            "headers":{
                "add":[
                    "content-encoding: gzip"
                ]
            },
            "filters":[
                {
                    "regex":"conformsTo",
                    "scope":"global",
                    "replace":"replacedvalue"
                }
            ],
            "vars":[
                [
                    "status",
                    "==",
                    200
                ]
            ]
        }
    },
    "upstream":{
        "type":"roundrobin",
        "scheme":"https",
        "nodes":{
            "example.org:443":1
        }
    },
    "uri":"/testroute"
}

Expected Behavior

Upstream's content-encoding: gzip header is passed normally trough APISIX and response-rewrite's filters.regex is applied to the response JSON.

Error Logs

No response

Steps to Reproduce

  1. Have an endpoint which responds with headers: Content-Type: application/json and content-encoding: gzip
  2. Make a APISIX route with proxy-rewrite and response-rewrite
  3. See if response-rewrite is applied with correct encoding headers.

Environment

APISIX Docker image apache/apisix:3.7.0-debian

@yuweizzz
Copy link
Contributor

In my options, when upstream return content-encoding means the body has been compressed, so the filters.regex may not work it the compressed body, manually using response-rewrite's headers.add should not work.

@shreemaan-abhishek shreemaan-abhishek added the checking check first if this issue occurred label Nov 29, 2023
@shreemaan-abhishek
Copy link
Contributor

this isn't a bug, but gzip encoded data isn't supported for rewrite.

@shreemaan-abhishek shreemaan-abhishek added feature-request and removed checking check first if this issue occurred labels Nov 29, 2023
@JoonaHa
Copy link
Author

JoonaHa commented Nov 29, 2023

Thank you for checking. I would have presumed that Apisix would handle different kind of content-encoding settings under the hood, so this feature would be much appreciated.

@yuweizzz
Copy link
Contributor

I want to try it, assign this issue to me?

@JoonaHa
Copy link
Author

JoonaHa commented Nov 29, 2023

For a workaround I used proxy-rewrite's headers to set Accept-Encoding to an empty value so the upstream server doesn't respond with an compressed payload.

{
   "id":"testroute",
   "methods":[
      "GET"
   ],
   "plugins":{
      "proxy-rewrite":{
         "uri":"/specific/path",
         "host":"example.org",
         "headers":{
            "Accept-Encoding": ""
         }
      },
      "response-rewrite":{
         "filters":[
            {
               "regex":"conformsTo",
               "scope":"global",
               "replace":"replacedvalue"
            }
         ],
         "vars":[
            [
               "status",
               "==",
               200
            ]
         ]
      }
   },
   "upstream":{
      "type":"roundrobin",
      "scheme":"https",
      "nodes":{
         "example.org:443":1
      }
   },
   "uri":"/testroute"
}

@yuweizzz
Copy link
Contributor

use "Accept-Encoding": "" tell upstream don't compress the payload, it need to work with proxy-rewrite, the another way is disable compression on upstream, it doesn't need a new header.

@yuweizzz
Copy link
Contributor

Do you think we still need to support encoded data in response-rewrite? @shreemaan-abhishek @JoonaHa

@JoonaHa
Copy link
Author

JoonaHa commented Nov 29, 2023

@yuweizzz By default APISIX seems to accepts gzip encoded data so the ideal situation would be for response-rewrite to also support different encoding methods.
Or at least mention the limitations of response-rewrite in the documentation.

@monkeyDluffy6017
Copy link
Contributor

@JoonaHa please help to review #10637

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

Successfully merging a pull request may close this issue.

4 participants