apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
14.47k stars 2.52k forks source link

bug: Response-rewrite plug-in BUG #8890

Closed flylan closed 1 year ago

flylan commented 1 year ago

Current Behavior

When the upstream service is nginx+php, the plug-in body filters will return garbled code

Expected Behavior

The body can display normally without garbled code

Error Logs

no error logs

Steps to Reproduce

The response-rewrite plug-in in the document is set to httpbin upstream, and forwarding is normal. However, if I set the upstream to nginx+php and configure the body filter rule, the body will return garbled code

with https://dog.ceo/api/breeds/image/random The link is an example. The dog.ceo is a third-party system. Each request will randomly return a picture of a dog. The normal response is as follows:

image

I added the following rules in Apisix: curl -s "http://127.0.0.1:9180/apisix/admin/routes/1000" \ -H "X-API-KEY: admin-key" -X PUT -d ' { "host": "dog.ceo", "uri": "/*", "plugins": { "response-rewrite": { "filters": [{ "regex": "message", "scope": "global", "replace": "message_rewrites" }] } }, "upstream": { "type": "roundrobin", "scheme": "https", "nodes": { "dog.ceo:443": 1 } } }'

The local binding domain name dog.ceo is 127.0.0.1, and the request is made through postman https://dog.ceo/api/breeds/image/random

image

When I turn off Accept-Encoding, it can display normally

image

Environment

flylan commented 1 year ago

I debugged the Response-rewrite plug-in and found the problem in the core.response.hold_body_chunk method, when I configure the upstream as httpbin, the body data obtained by this method is normal. However, when I configure the upstream as dog.ceo, the data obtained by this method is garbled, and I cannot fix this problem by myself

image

flylan commented 1 year ago

I tried to set the upstream as the local nginx-php service, but the problem remained. This plug-in can rewrite the response header normally without garbled code, but it cannot rewrite the response body normally

soulbird commented 1 year ago

According to your description, the response received by APISIX from the upstream is compressed by gzip, which makes the response-rewrite plugin unable to perform text recognition and replacement. You can turn off the Accept-Encoding request header as you describe, or let the upstream send the uncompressed content

flylan commented 1 year ago

According to your description, the response received by APISIX from the upstream is compressed by gzip, which makes the response-rewrite plugin unable to perform text recognition and replacement. You can turn off the Accept-Encoding request header as you describe, or let the upstream send the uncompressed content

OK。There is no garbled code after removing Accept-Encoding in the M. rewrite() process

image