This is an automated email from the ASF dual-hosted git repository.
nic-6443 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 5911c60006 fix(log-util): decode empty and size-capped compressed
response bodies (#14232)
5911c60006 is described below
commit 5911c6000633c9db00fe33dc2402ee0c24767490
Author: Nic <[email protected]>
AuthorDate: Fri Nov 20 11:12:03 2026 +0800
fix(log-util): decode empty and size-capped compressed response bodies
(#14232)
* fix(log-util): decode empty and size-capped compressed response bodies
An empty body labelled with a Content-Encoding is no longer passed to the
decoder, so 204, 304 and Content-Length: 0 responses log an empty body
without a decode warning. A buffered body that reached max_resp_body_bytes
is decoded as a possibly cut stream: the plaintext decoded so far is kept,
capped at max_resp_body_bytes, instead of logging the raw compressed bytes.
gzip and br decoders both support this through an optional max_output.
response-rewrite no longer reports a decode error for an empty compressed
body.
* fix(log-util): cap the decoded body of every compressed response
Pass max_resp_body_bytes to the decoder for every compressed response, not
only when the buffer reached it, so the logged body never exceeds the limit
and a highly compressed body is not inflated without bound.
---
apisix/plugins/response-rewrite.lua | 2 +-
apisix/utils/content-decode.lua | 27 ++-
apisix/utils/gzip.lua | 14 +-
apisix/utils/log-util.lua | 6 +-
docs/en/latest/plugins/clickhouse-logger.md | 2 +-
docs/en/latest/plugins/elasticsearch-logger.md | 2 +-
docs/en/latest/plugins/file-logger.md | 2 +-
docs/en/latest/plugins/http-logger.md | 2 +-
docs/en/latest/plugins/kafka-logger.md | 2 +-
docs/en/latest/plugins/loggly.md | 2 +-
docs/en/latest/plugins/loki-logger.md | 2 +-
docs/en/latest/plugins/rocketmq-logger.md | 2 +-
docs/en/latest/plugins/skywalking-logger.md | 2 +-
docs/en/latest/plugins/sls-logger.md | 2 +-
docs/en/latest/plugins/syslog.md | 2 +-
docs/en/latest/plugins/tcp-logger.md | 2 +-
docs/en/latest/plugins/tencent-cloud-cls.md | 2 +-
docs/en/latest/plugins/udp-logger.md | 2 +-
docs/zh/latest/plugins/clickhouse-logger.md | 2 +-
docs/zh/latest/plugins/elasticsearch-logger.md | 2 +-
docs/zh/latest/plugins/http-logger.md | 2 +-
docs/zh/latest/plugins/kafka-logger.md | 2 +-
docs/zh/latest/plugins/loki-logger.md | 2 +-
docs/zh/latest/plugins/rocketmq-logger.md | 2 +-
docs/zh/latest/plugins/skywalking-logger.md | 2 +-
docs/zh/latest/plugins/syslog.md | 2 +-
t/plugin/file-logger2.t | 288 +++++++++++++++++++++++++
t/plugin/response-rewrite3.t | 59 +++++
28 files changed, 408 insertions(+), 32 deletions(-)
diff --git a/apisix/plugins/response-rewrite.lua
b/apisix/plugins/response-rewrite.lua
index df4fa889d4..0df10eedce 100644
--- a/apisix/plugins/response-rewrite.lua
+++ b/apisix/plugins/response-rewrite.lua
@@ -271,7 +271,7 @@ function _M.body_filter(conf, ctx)
end
local err
- if ctx.response_encoding ~= nil then
+ if ctx.response_encoding ~= nil and body ~= "" then
local decoder =
content_decode.dispatch_decoder(ctx.response_encoding)
if not decoder then
core.log.error("filters may not work as expected ",
diff --git a/apisix/utils/content-decode.lua b/apisix/utils/content-decode.lua
index 6768c65b2b..5453c4d8cb 100644
--- a/apisix/utils/content-decode.lua
+++ b/apisix/utils/content-decode.lua
@@ -18,13 +18,23 @@ local pcall = pcall
local inflate_gzip = require("apisix.utils.gzip").inflate_gzip
local str_buffer = require("string.buffer")
local is_br_libs_loaded, brotli = pcall(require, "brotli")
+local _M = {}
+
+
+-- Every decoder takes (data, max_output). With max_output set, data may be a
+-- cut prefix of the stream: decoding stops without an error where data ends or
+-- once max_output bytes are decoded, and returns at most max_output bytes.
+local function gzip_decode(data, max_output)
+ return inflate_gzip(data, nil, nil, max_output, max_output ~= nil)
+end
+
+
local content_decode_funcs = {
- gzip = inflate_gzip,
+ gzip = gzip_decode,
}
-local _M = {}
-local function brotli_stream_decode(read_inputs, write_outputs)
+local function brotli_stream_decode(read_inputs, write_outputs, partial)
-- read 64k data per times
local read_size = 64 * 1024
local decompressor = brotli.decompressor:new()
@@ -36,6 +46,8 @@ local function brotli_stream_decode(read_inputs,
write_outputs)
ok, res = pcall(function()
return decompressor:decompress(chunk)
end)
+ elseif partial then
+ return true, nil
else
ok, res = pcall(function()
return decompressor:finish()
@@ -51,11 +63,14 @@ local function brotli_stream_decode(read_inputs,
write_outputs)
end
-local function brotli_decode(data)
+local function brotli_decode(data, max_output)
local inputs = str_buffer.new():set(data)
local outputs = str_buffer.new()
local read_inputs = function(size)
+ if max_output and #outputs >= max_output then
+ return nil
+ end
local data = inputs:get(size)
if data == "" then
return nil
@@ -67,12 +82,12 @@ local function brotli_decode(data)
return outputs:put(data)
end
- local ok, err = brotli_stream_decode(read_inputs, write_outputs)
+ local ok, err = brotli_stream_decode(read_inputs, write_outputs,
max_output ~= nil)
if not ok then
return nil, "brotli decode err: " .. err
end
- return outputs:get()
+ return outputs:get(max_output)
end
if is_br_libs_loaded then
diff --git a/apisix/utils/gzip.lua b/apisix/utils/gzip.lua
index a3a630a415..229232bff6 100644
--- a/apisix/utils/gzip.lua
+++ b/apisix/utils/gzip.lua
@@ -29,15 +29,20 @@ local _M = {}
-- running total crosses it, so an oversized payload is never fully buffered;
-- the third return value flags that case. Bytes left after the last member are
-- fed to a further inflate, which rejects them.
-function _M.inflate_gzip(data, buf_size, opts, max_output)
+-- With partial set, data may be a cut prefix of the stream: running out of
+-- input or reaching max_output ends inflating without an error, and what was
+-- inflated until then is returned, at most max_output bytes of it.
+function _M.inflate_gzip(data, buf_size, opts, max_output, partial)
buf_size = buf_size or DEFAULT_CHUNK
local outputs = str_buffer.new()
local written = 0
local exceeded = false
+ local drained = false
local pos = 1
local read_inputs = function(size)
if pos > #data then
+ drained = true
return nil
end
local chunk = str_sub(data, pos, pos + size - 1)
@@ -47,9 +52,13 @@ function _M.inflate_gzip(data, buf_size, opts, max_output)
local write_outputs = function(chunk)
if max_output then
+ local room = max_output - written
written = written + #chunk
if written > max_output then
exceeded = true
+ if partial then
+ outputs:put(str_sub(chunk, 1, room))
+ end
return nil, "output size limit exceeded"
end
end
@@ -68,6 +77,9 @@ function _M.inflate_gzip(data, buf_size, opts, max_output)
local ok, err = zlib.inflate(read_inputs, write_outputs, buf_size,
stream, inbuf, outbuf)
if not ok then
+ if partial and (exceeded or drained) then
+ return outputs:get()
+ end
if exceeded then
return nil, "inflated data is greater than the maximum size "
.. max_output .. " allowed", true
diff --git a/apisix/utils/log-util.lua b/apisix/utils/log-util.lua
index e31f5256f3..01dfce59b8 100644
--- a/apisix/utils/log-util.lua
+++ b/apisix/utils/log-util.lua
@@ -436,7 +436,7 @@ function _M.collect_body(conf, ctx)
end
local response_encoding = ngx_header["Content-Encoding"]
- if not response_encoding or ctx.gzip_matched then
+ if not response_encoding or ctx.gzip_matched or final_body == ""
then
ctx.resp_body = final_body
return
end
@@ -449,7 +449,9 @@ function _M.collect_body(conf, ctx)
return
end
- local decoded_body, err = decoder(final_body)
+ -- the buffer may have been cut at the limit, so it is decoded as a
+ -- possibly cut stream, and the decoded body is held to the same
limit
+ local decoded_body, err = decoder(final_body, max_resp_body_bytes)
if err ~= nil then
core.log.warn("try decode compressed data err: ", err)
ctx.resp_body = final_body
diff --git a/docs/en/latest/plugins/clickhouse-logger.md
b/docs/en/latest/plugins/clickhouse-logger.md
index 1d7df6e327..f5aaf2f2bc 100644
--- a/docs/en/latest/plugins/clickhouse-logger.md
+++ b/docs/en/latest/plugins/clickhouse-logger.md
@@ -53,7 +53,7 @@ The `clickhouse-logger` Plugin pushes request and response
logs to [ClickHouse](
| include_resp_body | boolean | False | false |
| If `true`, include the response body in the log.
|
| include_resp_body_expr | array | False | |
| An array of one or more conditions in the form of [APISIX
expressions](https://github.com/api7/lua-resty-expr). Used when
`include_resp_body` is `true`. Response body is only logged when the
expressions evaluate to `true`.
|
| max_req_body_bytes | integer | False | 524288 | >= 1
| Maximum request body size in bytes to include in the log. If the
request body exceeds this value, it will be truncated. Available in APISIX from
3.16.0.
|
-| max_resp_body_bytes | integer | False | 524288 | >= 1
| Maximum response body size in bytes to include in the log. If
the response body exceeds this value, it will be truncated. Available in APISIX
from 3.16.0.
|
+| max_resp_body_bytes | integer | False | 524288 | >= 1
| Maximum response body size in bytes to include in the log. If
the response body exceeds this value, it will be truncated. For a compressed
response, the decoded body is also truncated to this size. Available in APISIX
from 3.16.0.
|
| name | string | False | "clickhouse logger" |
| Unique identifier of the Plugin for the batch processor. If you
use [Prometheus](./prometheus.md) to monitor APISIX metrics, the name is
exported in `apisix_batch_process_entries`.
|
| batch_max_size | integer | False | 1000 |
greater than 0 | The number of log entries allowed in one batch. Once
reached, the batch will be sent to ClickHouse. Setting this parameter to `1`
means immediate processing.
|
| inactive_timeout | integer | False | 5 |
greater than 0 | The maximum time in seconds to wait for new logs before
sending the batch to the logging service. The value should be smaller than
`buffer_duration`.
|
diff --git a/docs/en/latest/plugins/elasticsearch-logger.md
b/docs/en/latest/plugins/elasticsearch-logger.md
index b1c92896cd..053e13f273 100644
--- a/docs/en/latest/plugins/elasticsearch-logger.md
+++ b/docs/en/latest/plugins/elasticsearch-logger.md
@@ -55,7 +55,7 @@ The `elasticsearch-logger` Plugin pushes request and response
logs in batches to
| max_req_body_bytes | integer | False | 524288 | >=1 | Request
bodies within this size will be logged, if the size exceeds the configured
value it will be truncated before logging. |
| include_resp_body | boolean | False | false | |
If true, include the response body in the log. |
| include_resp_body_expr | array[array] | False | | |
An array of one or more conditions in the form of
[lua-resty-expr](https://github.com/api7/lua-resty-expr). Used when the
`include_resp_body` is true. Response body would only be logged when the
expressions configured here evaluate to true. |
-| max_resp_body_bytes | integer | False | 524288 | >=1 | Response
bodies within this size will be logged, if the size exceeds the configured
value it will be truncated before logging. |
+| max_resp_body_bytes | integer | False | 524288 | >=1 | Response
bodies within this size will be logged, if the size exceeds the configured
value it will be truncated before logging. For a compressed response, the
decoded body is also truncated to this size. |
NOTE: `encrypt_fields = {"auth.password", "headers"}` is also defined in the
schema, which means that these fields will be stored encrypted in etcd. See
[encrypted storage fields](../plugin-develop.md#encrypted-storage-fields).
diff --git a/docs/en/latest/plugins/file-logger.md
b/docs/en/latest/plugins/file-logger.md
index 183ee55b62..f6630e5572 100644
--- a/docs/en/latest/plugins/file-logger.md
+++ b/docs/en/latest/plugins/file-logger.md
@@ -52,7 +52,7 @@ The `file-logger` Plugin is used to push log streams to a
specific location.
| max_req_body_bytes | integer | False | Request bodies within this size will
be logged, if the size exceeds the configured value it will be truncated before
logging. |
| include_resp_body | boolean | False | When set to `true` includes
the response body in the log file.
|
| include_resp_body_expr | array | False | When the `include_resp_body`
attribute is set to `true`, use this to filter based on
[lua-resty-expr](https://github.com/api7/lua-resty-expr). If present, only logs
the response into file if the expression evaluates to `true`. |
-| max_resp_body_bytes | integer | False | Response bodies within this size
will be logged, if the size exceeds the configured value it will be truncated
before logging. |
+| max_resp_body_bytes | integer | False | Response bodies within this size
will be logged, if the size exceeds the configured value it will be truncated
before logging. For a compressed response, the decoded body is also truncated
to this size. |
| match | array[array] | False | Logs will be recorded when the rule
matching is successful if the option is set. See
[lua-resty-expr](https://github.com/api7/lua-resty-expr#operator-list) for a
list of available expressions. |
### Example of default log format
diff --git a/docs/en/latest/plugins/http-logger.md
b/docs/en/latest/plugins/http-logger.md
index 23ec615642..f046f5b6f9 100644
--- a/docs/en/latest/plugins/http-logger.md
+++ b/docs/en/latest/plugins/http-logger.md
@@ -49,7 +49,7 @@ The `http-logger` Plugin pushes request and response logs as
JSON objects to HTT
| include_resp_body | boolean | False | false |
| If true, include the response body in the log.
|
| include_resp_body_expr | array | False | |
| An array of one or more conditions in the form of
[lua-resty-expr](https://github.com/api7/lua-resty-expr) expressions. Used when
`include_resp_body` is true. Response body is only logged when the expressions
configured here evaluate to true. |
| max_req_body_bytes | integer | False | 524288 | greater than or
equal to 1 | Maximum request body size in bytes to include in the log. If the
request body exceeds this value, it will be truncated.
|
-| max_resp_body_bytes | integer | False | 524288 | greater than or
equal to 1 | Maximum response body size in bytes to include in the log. If the
response body exceeds this value, it will be truncated.
|
+| max_resp_body_bytes | integer | False | 524288 | greater than or
equal to 1 | Maximum response body size in bytes to include in the log. If the
response body exceeds this value, it will be truncated. For a compressed
response, the decoded body is also truncated to this size.
|
| concat_method | string | False | `json` | `json` or `new_line`
| Method to concatenate logs. When set to `json`, use `json.encode` for all
pending logs. When set to `new_line`, also use `json.encode` but use the
newline character `\n` to concatenate lines.
|
| ssl_verify | boolean | False | false |
| If true, verify the server's SSL certificate.
|
diff --git a/docs/en/latest/plugins/kafka-logger.md
b/docs/en/latest/plugins/kafka-logger.md
index 9548b428af..67429da437 100644
--- a/docs/en/latest/plugins/kafka-logger.md
+++ b/docs/en/latest/plugins/kafka-logger.md
@@ -65,7 +65,7 @@ It might take some time to receive the log data. It will be
automatically sent a
| max_req_body_bytes | integer | False | 524288 | >=1
| Maximum request body size in
bytes to push to Kafka. If the size exceeds the configured value, the body will
be truncated before being pushed.
|
| include_resp_body | boolean | False | false |
[false, true] | When set to `true` includes
the response body in the log.
|
| include_resp_body_expr | array | False | |
| Filter for when the
`include_resp_body` attribute is set to `true`. Response body is only logged
when the expression set here evaluates to `true`. See
[lua-resty-expr](https://github.com/api7/lua-resty-expr) for more.
|
-| max_resp_body_bytes | integer | False | 524288 | >=1
| Maximum response body size in
bytes to push to Kafka. If the size exceeds the configured value, the body will
be truncated before being pushed.
|
+| max_resp_body_bytes | integer | False | 524288 | >=1
| Maximum response body size in
bytes to push to Kafka. If the size exceeds the configured value, the body will
be truncated before being pushed. For a compressed response, the decoded body
is also truncated to this size.
[...]
| cluster_name | integer | False | 1 |
[1,...] | Name of the cluster. Used
when there are two or more Kafka clusters. Only works if the `producer_type`
attribute is set to `async`.
|
| producer_batch_num | integer | False | 200 |
[1,...] | `batch_num` parameter in
[lua-resty-kafka](https://github.com/doujiang24/lua-resty-kafka). Merges
messages and sends them in batches. Unit is message count.
|
| producer_batch_size | integer | False | 1048576 |
[0,...] | `batch_size` parameter in
[lua-resty-kafka](https://github.com/doujiang24/lua-resty-kafka) in bytes.
|
diff --git a/docs/en/latest/plugins/loggly.md b/docs/en/latest/plugins/loggly.md
index 344a72c1bc..8682a767eb 100644
--- a/docs/en/latest/plugins/loggly.md
+++ b/docs/en/latest/plugins/loggly.md
@@ -50,7 +50,7 @@ When the maximum batch size is exceeded, the data in the
queue is pushed to Logg
| max_req_body_bytes | integer | False | 524288 | Request bodies within this
size will be logged, if the size exceeds the configured value it will be
truncated before logging. |
| include_resp_body | boolean | False | false | When set to
`true` includes the response body in the log.
|
| include_resp_body_expr | array | False | | When the
`include_resp_body` attribute is set to `true`, use this to filter based on
[lua-resty-expr](https://github.com/api7/lua-resty-expr). If present, only logs
the response if the expression evaluates to `true`. |
-| max_resp_body_bytes | integer | False | 524288 | Response bodies within this
size will be logged, if the size exceeds the configured value it will be
truncated before logging. |
+| max_resp_body_bytes | integer | False | 524288 | Response bodies within this
size will be logged, if the size exceeds the configured value it will be
truncated before logging. For a compressed response, the decoded body is also
truncated to this size. |
This Plugin supports using batch processors to aggregate and process entries
(logs/data) in a batch. This avoids the need for frequently submitting the
data. The batch processor submits data every `5` seconds or when the data in
the queue reaches `1000`. See [Batch
Processor](../batch-processor.md#configurations) for more information or
setting your custom configuration.
diff --git a/docs/en/latest/plugins/loki-logger.md
b/docs/en/latest/plugins/loki-logger.md
index f2d835efe2..cbc9759d6c 100644
--- a/docs/en/latest/plugins/loki-logger.md
+++ b/docs/en/latest/plugins/loki-logger.md
@@ -60,7 +60,7 @@ When enabled, the Plugin will serialize the request context
information to [JSON
| max_req_body_bytes | integer | False | 524288 | >=1 | Request bodies within
this size will be logged, if the size exceeds the configured value it will be
truncated before logging. |
| include_resp_body | boolean | False | false | | If true, include the
response body in the log. |
| include_resp_body_expr | array[array] | False | | | An array of one or
more conditions in the form of
[lua-resty-expr](https://github.com/api7/lua-resty-expr). Used when the
`include_resp_body` is true. Response body would only be logged when the
expressions configured here evaluate to true. |
-| max_resp_body_bytes | integer | False | 524288 | >=1 | Response bodies
within this size will be logged, if the size exceeds the configured value it
will be truncated before logging. |
+| max_resp_body_bytes | integer | False | 524288 | >=1 | Response bodies
within this size will be logged, if the size exceeds the configured value it
will be truncated before logging. For a compressed response, the decoded body
is also truncated to this size. |
This Plugin supports using batch processors to aggregate and process entries
(logs/data) in a batch. This avoids the need for frequently submitting the
data. The batch processor submits data every `5` seconds or when the data in
the queue reaches `1000`. See [Batch
Processor](../batch-processor.md#configurations) for more information or
setting your custom configuration.
diff --git a/docs/en/latest/plugins/rocketmq-logger.md
b/docs/en/latest/plugins/rocketmq-logger.md
index 02aa93d341..7de83a62e9 100644
--- a/docs/en/latest/plugins/rocketmq-logger.md
+++ b/docs/en/latest/plugins/rocketmq-logger.md
@@ -55,7 +55,7 @@ The `rocketmq-logger` Plugin pushes request and response logs
as JSON objects to
| include_resp_body | boolean | False | false |
| If true, include the response body in the log.
|
| include_resp_body_expr | array | False | |
| An array of one or more conditions in the form of
[lua-resty-expr](https://github.com/api7/lua-resty-expr) expressions. Used when
`include_resp_body` is true. Response body is only logged when the expressions
configured here evaluate to true.
|
| max_req_body_bytes | integer | False | 524288 | greater
than or equal to 1 | Maximum request body size in bytes to include in the log.
If the request body exceeds this value, it will be truncated. Available in
APISIX from 3.16.0.
|
-| max_resp_body_bytes | integer | False | 524288 | greater
than or equal to 1 | Maximum response body size in bytes to include in the log.
If the response body exceeds this value, it will be truncated. Available in
APISIX from 3.16.0.
|
+| max_resp_body_bytes | integer | False | 524288 | greater
than or equal to 1 | Maximum response body size in bytes to include in the log.
If the response body exceeds this value, it will be truncated. For a compressed
response, the decoded body is also truncated to this size. Available in APISIX
from 3.16.0.
|
| batch_max_size | integer | False | 1000 | greater
than 0 | The number of log entries allowed in one batch. Once reached,
the batch will be sent to the logging service. Setting this parameter to 1
means immediate processing.
|
| inactive_timeout | integer | False | 5 | greater
than 0 | The maximum time in seconds to wait for new logs before sending
the batch to the logging service. The value should be smaller than
`buffer_duration`.
|
| buffer_duration | integer | False | 60 | greater
than 0 | The maximum time in seconds from the earliest entry allowed
before sending the batch to the logging service.
|
diff --git a/docs/en/latest/plugins/skywalking-logger.md
b/docs/en/latest/plugins/skywalking-logger.md
index 19d099fb67..26a6072bc6 100644
--- a/docs/en/latest/plugins/skywalking-logger.md
+++ b/docs/en/latest/plugins/skywalking-logger.md
@@ -54,7 +54,7 @@ If there is an existing tracing context, it sets up the
trace-log correlation au
| max_req_body_bytes | integer | False | 524288 | >=1 | Request
bodies within this size will be logged, if the size exceeds the configured
value it will be truncated before logging. |
| include_resp_body | boolean | False | false | If true,
include the response body in the log. |
| include_resp_body_expr | array[array] | False | | An array of
one or more conditions in the form of
[lua-resty-expr](https://github.com/api7/lua-resty-expr). Used when the
`include_resp_body` is true. Response body would only be logged when the
expressions configured here evaluate to true. |
-| max_resp_body_bytes | integer | False | 524288 | >=1 | Response
bodies within this size will be logged, if the size exceeds the configured
value it will be truncated before logging. |
+| max_resp_body_bytes | integer | False | 524288 | >=1 | Response
bodies within this size will be logged, if the size exceeds the configured
value it will be truncated before logging. For a compressed response, the
decoded body is also truncated to this size. |
This Plugin supports using batch processors to aggregate and process entries
(logs/data) in a batch. This avoids the need for frequently submitting the
data. The batch processor submits data every `5` seconds or when the data in
the queue reaches `1000`. See [Batch
Processor](../batch-processor.md#configurations) for more information or
setting your custom configuration.
diff --git a/docs/en/latest/plugins/sls-logger.md
b/docs/en/latest/plugins/sls-logger.md
index ccf2ca9fd3..2d6b5cc4b7 100644
--- a/docs/en/latest/plugins/sls-logger.md
+++ b/docs/en/latest/plugins/sls-logger.md
@@ -52,7 +52,7 @@ It might take some time to receive the log data. It will be
automatically sent a
| max_req_body_bytes | False | Request bodies within this size will be logged,
if the size exceeds the configured value it will be truncated before logging. |
| include_resp_body | No | When set to `true` includes the response body in
the log.
|
| include_resp_body_expr | No | Filter for when the `include_resp_body`
attribute is set to `true`. Response body is only logged when the expression
set here evaluates to `true`. See
[lua-resty-expr](https://github.com/api7/lua-resty-expr) for more.
|
-| max_resp_body_bytes | False | Response bodies within this size will be
logged, if the size exceeds the configured value it will be truncated before
logging. |
+| max_resp_body_bytes | False | Response bodies within this size will be
logged, if the size exceeds the configured value it will be truncated before
logging. For a compressed response, the decoded body is also truncated to this
size. |
| name | False | Unique identifier for the batch processor. If
you use Prometheus to monitor APISIX metrics, the name is exported in
`apisix_batch_process_entries`.
|
NOTE: `encrypt_fields = {"access_key_secret"}` is also defined in the schema,
which means that the field will be stored encrypted in etcd. See [encrypted
storage fields](../plugin-develop.md#encrypted-storage-fields).
diff --git a/docs/en/latest/plugins/syslog.md b/docs/en/latest/plugins/syslog.md
index 47d9941d2b..19446ef1d8 100644
--- a/docs/en/latest/plugins/syslog.md
+++ b/docs/en/latest/plugins/syslog.md
@@ -53,7 +53,7 @@ The `syslog` Plugin pushes request and response logs as JSON
objects to syslog s
| include_resp_body | boolean | False | false |
| If true, include the response body in the log.
|
| include_resp_body_expr | array | False | |
| An array of one or more conditions in the form of
[lua-resty-expr](https://github.com/api7/lua-resty-expr) expressions. Used when
`include_resp_body` is true. Response body is only logged when the expressions
configured here evaluate to true. |
| max_req_body_bytes | integer | False | 524288 | greater than or
equal to 1 | Maximum request body size in bytes to include in the log. If the
request body exceeds this value, it will be truncated. Available in APISIX from
3.16.0. |
-| max_resp_body_bytes | integer | False | 524288 | greater than or
equal to 1 | Maximum response body size in bytes to include in the log. If the
response body exceeds this value, it will be truncated. Available in APISIX
from 3.16.0. |
+| max_resp_body_bytes | integer | False | 524288 | greater than or
equal to 1 | Maximum response body size in bytes to include in the log. If the
response body exceeds this value, it will be truncated. For a compressed
response, the decoded body is also truncated to this size. Available in APISIX
from 3.16.0. |
| name | string | False | `sys logger` |
| Unique identifier of the Plugin for the batch processor. If you use
Prometheus to monitor APISIX metrics, the name is exported in
`apisix_batch_process_entries`.
|
| batch_max_size | integer | False | 1000 | greater than 0
| The number of log entries allowed in one batch. Once reached, the batch
will be sent to the logging service. Setting this parameter to 1 means
immediate processing. |
| inactive_timeout | integer | False | 5 | greater than 0
| The maximum time in seconds to wait for new logs before sending the
batch to the logging service. The value should be smaller than
`buffer_duration`.
|
diff --git a/docs/en/latest/plugins/tcp-logger.md
b/docs/en/latest/plugins/tcp-logger.md
index a7696cf4d9..a7137dffe2 100644
--- a/docs/en/latest/plugins/tcp-logger.md
+++ b/docs/en/latest/plugins/tcp-logger.md
@@ -52,7 +52,7 @@ This plugin also allows to push logs as a batch to your
external TCP server. It
| max_req_body_bytes | integer | False | 524288 | >=1 | Request
bodies within this size will be logged, if the size exceeds the configured
value it will be truncated before logging. |
| include_resp_body | boolean | No | false | [false, true] | When set
to `true` includes the response body in the log.
|
| include_resp_body_expr | array | No | | | Filter
for when the `include_resp_body` attribute is set to `true`. Response body is
only logged when the expression set here evaluates to `true`. See
[lua-resty-expr](https://github.com/api7/lua-resty-expr) for more.
|
-| max_resp_body_bytes | integer | False | 524288 | >=1 | Response
bodies within this size will be logged, if the size exceeds the configured
value it will be truncated before logging. |
+| max_resp_body_bytes | integer | False | 524288 | >=1 | Response
bodies within this size will be logged, if the size exceeds the configured
value it will be truncated before logging. For a compressed response, the
decoded body is also truncated to this size. |
This Plugin supports using batch processors to aggregate and process entries
(logs/data) in a batch. This avoids the need for frequently submitting the
data. The batch processor submits data every `5` seconds or when the data in
the queue reaches `1000`. See [Batch
Processor](../batch-processor.md#configurations) for more information or
setting your custom configuration.
diff --git a/docs/en/latest/plugins/tencent-cloud-cls.md
b/docs/en/latest/plugins/tencent-cloud-cls.md
index ada24bc5b4..7227989992 100644
--- a/docs/en/latest/plugins/tencent-cloud-cls.md
+++ b/docs/en/latest/plugins/tencent-cloud-cls.md
@@ -47,7 +47,7 @@ The `tencent-cloud-cls` Plugin uses [TencentCloud
CLS](https://cloud.tencent.com
| max_req_body_bytes | integer | False | 524288 | >=1 | Request bodies within
this size will be logged, if the size exceeds the configured value it will be
truncated before logging. |
| include_resp_body | boolean | No | false | [false, true] | When set
to `true` includes the response body in the log.
|
| include_resp_body_expr | array | No | | | Filter
for when the `include_resp_body` attribute is set to `true`. Response body is
only logged when the expression set here evaluates to `true`. See
[lua-resty-expr](https://github.com/api7/lua-resty-expr) for more.
|
-| max_resp_body_bytes | integer | False | 524288 | >=1 | Response bodies
within this size will be logged, if the size exceeds the configured value it
will be truncated before logging. |
+| max_resp_body_bytes | integer | False | 524288 | >=1 | Response bodies
within this size will be logged, if the size exceeds the configured value it
will be truncated before logging. For a compressed response, the decoded body
is also truncated to this size. |
| global_tag | object | No | | | kv pairs
in JSON,send with each log.
|
| log_format | object | No | | | Log format
declared as key-value pairs in JSON. Values support strings and nested objects
(up to five levels deep; deeper fields are truncated). Within strings,
[APISIX](../apisix-variable.md) or
[NGINX](http://nginx.org/en/docs/varindex.html) variables can be referenced by
prefixing with `$`. |
| log_format_extra | object | No | | |
Extra log fields **added on top of** the default log entry, keeping every
default field instead of replacing them (unlike `log_format`). Same value
syntax as `log_format`. Ignored when `log_format` is set. |
diff --git a/docs/en/latest/plugins/udp-logger.md
b/docs/en/latest/plugins/udp-logger.md
index db89eedd1f..20134e1a2c 100644
--- a/docs/en/latest/plugins/udp-logger.md
+++ b/docs/en/latest/plugins/udp-logger.md
@@ -50,7 +50,7 @@ This plugin also allows to push logs as a batch to your
external UDP server. It
| max_req_body_bytes | integer | False | 524288 | >=1 | Request bodies within
this size will be logged, if the size exceeds the configured value it will be
truncated before logging. |
| include_resp_body | boolean | No | false | [false, true] | When set
to `true` includes the response body in the log.
|
| include_resp_body_expr | array | No | | | Filter
for when the `include_resp_body` attribute is set to `true`. Response body is
only logged when the expression set here evaluates to `true`. See
[lua-resty-expr](https://github.com/api7/lua-resty-expr) for more.
|
-| max_resp_body_bytes | integer | False | 524288 | >=1 | Response bodies
within this size will be logged, if the size exceeds the configured value it
will be truncated before logging. |
+| max_resp_body_bytes | integer | False | 524288 | >=1 | Response bodies
within this size will be logged, if the size exceeds the configured value it
will be truncated before logging. For a compressed response, the decoded body
is also truncated to this size. |
This Plugin supports using batch processors to aggregate and process entries
(logs/data) in a batch. This avoids the need for frequently submitting the
data. The batch processor submits data every `5` seconds or when the data in
the queue reaches `1000`. See [Batch
Processor](../batch-processor.md#configurations) for more information or
setting your custom configuration.
diff --git a/docs/zh/latest/plugins/clickhouse-logger.md
b/docs/zh/latest/plugins/clickhouse-logger.md
index 0806519e58..bacba84ce0 100644
--- a/docs/zh/latest/plugins/clickhouse-logger.md
+++ b/docs/zh/latest/plugins/clickhouse-logger.md
@@ -52,7 +52,7 @@ description: clickhouse-logger 插件将请求和响应日志批量推送到 Cli
| include_resp_body | boolean | False | false |
| 若为 `true`,则在日志中包含响应体。
|
| include_resp_body_expr | array | False | |
| 当 `include_resp_body` 为 `true` 时使用的过滤条件数组,以 [APISIX
表达式](https://github.com/api7/lua-resty-expr) 形式表示。仅当表达式求值为 `true` 时才记录响应体。
|
| max_req_body_bytes | integer | False | 524288 | >= 1
| 日志中包含的最大请求体大小(字节)。超出此值的请求体将被截断。APISIX 3.16.0 起可用。
|
-| max_resp_body_bytes | integer | False | 524288 | >= 1
| 日志中包含的最大响应体大小(字节)。超出此值的响应体将被截断。APISIX 3.16.0 起可用。
|
+| max_resp_body_bytes | integer | False | 524288 | >= 1
| 日志中包含的最大响应体大小(字节)。超出此值的响应体将被截断。对于压缩响应,解压后的响应体同样截断到该大小。APISIX 3.16.0
起可用。
|
| name | string | False | "clickhouse logger" |
| 批处理器中插件的唯一标识符。如果使用 [Prometheus](./prometheus.md) 监控 APISIX 指标,该名称将在
`apisix_batch_process_entries` 中导出。
|
| batch_max_size | integer | False | 1000 | 大于 0
| 单批允许的日志条目数。达到此数量后,批次将被发送至 ClickHouse。设置为 `1` 表示立即处理。
|
| inactive_timeout | integer | False | 5 | 大于 0
| 在发送批次到日志服务之前等待新日志的最长时间(秒)。该值应小于 `buffer_duration`。
|
diff --git a/docs/zh/latest/plugins/elasticsearch-logger.md
b/docs/zh/latest/plugins/elasticsearch-logger.md
index 45ba4d2c12..8ef48c91e7 100644
--- a/docs/zh/latest/plugins/elasticsearch-logger.md
+++ b/docs/zh/latest/plugins/elasticsearch-logger.md
@@ -55,7 +55,7 @@ description: elasticsearch-logger Plugin 将请求和响应日志批量推送到
| max_req_body_bytes | integer | 否 | 524288 | >=1 |
记录请求主体的最大字节数。如果请求主体超过此值,则会在记录前截断。在 APISIX 3.16.0 中可用。 |
| include_resp_body | boolean | 否 | false | | 如果为
true,则将响应主体包含在日志中。 |
| include_resp_body_expr | array[array] | 否 | | | 一个或多个条件的数组,形式为
[lua-resty-expr](https://github.com/api7/lua-resty-expr)。在 `include_resp_body`
为 true 时使用。仅当此处配置的表达式计算结果为 true 时,才会记录响应主体。 |
-| max_resp_body_bytes | integer | 否 | 524288 | >=1 |
记录响应主体的最大字节数。如果响应主体超过此值,则会在记录前截断。在 APISIX 3.16.0 中可用。 |
+| max_resp_body_bytes | integer | 否 | 524288 | >=1 |
记录响应主体的最大字节数。如果响应主体超过此值,则会在记录前截断。对于压缩响应,解压后的响应体同样截断到该大小。在 APISIX 3.16.0 中可用。 |
注意:schema 中还定义了 `encrypt_fields = {"auth.password",
"headers"}`,这意味着这些字段将会被加密存储在 etcd 中。具体参考 [加密存储字段](../plugin-develop.md#加密存储字段)。
diff --git a/docs/zh/latest/plugins/http-logger.md
b/docs/zh/latest/plugins/http-logger.md
index 81aefaae4c..d0ec2526a0 100644
--- a/docs/zh/latest/plugins/http-logger.md
+++ b/docs/zh/latest/plugins/http-logger.md
@@ -49,7 +49,7 @@ description: http-logger 插件将请求和响应日志以 JSON 对象批量推
| include_resp_body | boolean | False | false | |
若为 true,则在日志中包含响应体。
|
| include_resp_body_expr | array | False | | |
[lua-resty-expr](https://github.com/api7/lua-resty-expr) 表达式数组。当
`include_resp_body` 为 true 时使用,仅当此处表达式求值为 true 时才记录响应体。
|
| max_req_body_bytes | integer | False | 524288 | 大于等于 1 |
日志中记录的最大请求体字节数。超出该值的请求体将被截断。
|
-| max_resp_body_bytes | integer | False | 524288 | 大于等于 1 |
日志中记录的最大响应体字节数。超出该值的响应体将被截断。
|
+| max_resp_body_bytes | integer | False | 524288 | 大于等于 1 |
日志中记录的最大响应体字节数。超出该值的响应体将被截断。对于压缩响应,解压后的响应体同样截断到该大小。
|
| concat_method | string | False | `json` | `json` 或 `new_line` |
日志的拼接方式。设为 `json` 时对所有待发日志使用 `json.encode`;设为 `new_line` 时也使用
`json.encode`,但用换行符 `\n` 拼接各行。
|
| ssl_verify | boolean | False | false | |
若为 true,则验证服务器的 SSL 证书。
|
diff --git a/docs/zh/latest/plugins/kafka-logger.md
b/docs/zh/latest/plugins/kafka-logger.md
index 61fd64b859..47fe9eb73f 100644
--- a/docs/zh/latest/plugins/kafka-logger.md
+++ b/docs/zh/latest/plugins/kafka-logger.md
@@ -62,7 +62,7 @@ description: kafka-logger 插件将请求和响应日志作为 JSON 对象批量
| max_req_body_bytes | integer | 否 | 524288 | >=1
| 允许推送到 Kafka
的最大请求体大小(字节)。如果超过该值,请求体在推送前会被截断。
|
| include_resp_body | boolean | 否 | false |
[false, true] | 设置为 `true` 时,在日志中包含响应体。
|
| include_resp_body_expr | array | 否 | |
| 当 `include_resp_body` 设置为
`true` 时的过滤条件。只有当此处设置的表达式计算结果为 `true` 时,才会记录响应体。详情请参考
[lua-resty-expr](https://github.com/api7/lua-resty-expr)。
|
-| max_resp_body_bytes | integer | 否 | 524288 | >=1
| 允许推送到 Kafka
的最大响应体大小(字节)。如果超过该值,响应体在推送前会被截断。
|
+| max_resp_body_bytes | integer | 否 | 524288 | >=1
| 允许推送到 Kafka
的最大响应体大小(字节)。如果超过该值,响应体在推送前会被截断。对于压缩响应,解压后的响应体同样截断到该大小。
|
| cluster_name | integer | 否 | 1 |
[1,...] | Kafka 集群的名称,在有两个或多个 Kafka
集群时使用。仅当 `producer_type` 设置为 `async` 时有效。
|
| producer_batch_num | integer | 否 | 200 |
[1,...] | 对应
[lua-resty-kafka](https://github.com/doujiang24/lua-resty-kafka) 中的 `batch_num`
参数,聚合消息批量提交,单位为消息条数。
|
| producer_batch_size | integer | 否 | 1048576 |
[0,...] | 对应
[lua-resty-kafka](https://github.com/doujiang24/lua-resty-kafka) 中的
`batch_size` 参数,单位为字节。
|
diff --git a/docs/zh/latest/plugins/loki-logger.md
b/docs/zh/latest/plugins/loki-logger.md
index e90657e62c..a8e9978344 100644
--- a/docs/zh/latest/plugins/loki-logger.md
+++ b/docs/zh/latest/plugins/loki-logger.md
@@ -59,7 +59,7 @@ description: loki-logger 插件通过 Loki HTTP API /loki/api/v1/push
将请求
| max_req_body_bytes | integer | 否 | 524288 | >=1 |
记录请求正文的最大字节数。如果请求正文超过此值,则会在记录前截断。|
| include_resp_body | boolean | 否 | false | | 如果为 true,则将响应正文包含在日志中。|
| include_resp_body_expr | array[array] | 否 | | | 一个或多个
[lua-resty-expr](https://github.com/api7/lua-resty-expr) 形式条件的数组。在
`include_resp_body` 为 true 时使用。仅当此处配置的表达式计算结果为 true 时,才会记录响应正文。|
-| max_resp_body_bytes | integer | 否 | 524288 | >=1 |
记录响应正文的最大字节数。如果响应正文超过此值,则会在记录前截断。|
+| max_resp_body_bytes | integer | 否 | 524288 | >=1 |
记录响应正文的最大字节数。如果响应正文超过此值,则会在记录前截断。对于压缩响应,解压后的响应体同样截断到该大小。|
该插件支持使用批处理器对条目(日志/数据)进行批量聚合和处理,避免了频繁提交数据的需求。批处理器每隔 `5` 秒或当队列中的数据达到 `1000`
时提交数据。有关更多信息或设置自定义配置,请参阅 [批处理器](../batch-processor.md#configuration)。
diff --git a/docs/zh/latest/plugins/rocketmq-logger.md
b/docs/zh/latest/plugins/rocketmq-logger.md
index 0149a56405..305d4244e2 100644
--- a/docs/zh/latest/plugins/rocketmq-logger.md
+++ b/docs/zh/latest/plugins/rocketmq-logger.md
@@ -55,7 +55,7 @@ description: rocketmq-logger 插件将请求和响应日志以 JSON 对象批量
| include_resp_body | boolean | False | false |
| 若为 true,则在日志中包含响应体。
|
| include_resp_body_expr | array | False | |
| [lua-resty-expr](https://github.com/api7/lua-resty-expr) 表达式数组。当
`include_resp_body` 为 true 时使用,仅当此处表达式求值为 true 时才记录响应体。
|
| max_req_body_bytes | integer | False | 524288 | 大于等于 1
| 日志中记录的最大请求体字节数。超出该值的请求体将被截断。在 APISIX 3.16.0 版本中可用。
|
-| max_resp_body_bytes | integer | False | 524288 | 大于等于 1
| 日志中记录的最大响应体字节数。超出该值的响应体将被截断。在 APISIX 3.16.0 版本中可用。
|
+| max_resp_body_bytes | integer | False | 524288 | 大于等于 1
| 日志中记录的最大响应体字节数。超出该值的响应体将被截断。对于压缩响应,解压后的响应体同样截断到该大小。在 APISIX 3.16.0
版本中可用。
|
| batch_max_size | integer | False | 1000 | 大于 0
| 每批次允许的最大日志条目数。达到该值后,批次将被发送至日志服务。设置为 1 表示立即处理。
|
| inactive_timeout | integer | False | 5 | 大于 0
| 在将批次发送至日志服务前等待新日志的最长时间(秒)。该值应小于 `buffer_duration`。
|
| buffer_duration | integer | False | 60 | 大于 0
| 发送批次前允许最早条目存在的最长时间(秒)。
|
diff --git a/docs/zh/latest/plugins/skywalking-logger.md
b/docs/zh/latest/plugins/skywalking-logger.md
index 24bf2609fe..dcef643212 100644
--- a/docs/zh/latest/plugins/skywalking-logger.md
+++ b/docs/zh/latest/plugins/skywalking-logger.md
@@ -52,7 +52,7 @@ description: skywalking-logger 将请求和响应日志作为 JSON 对象批量
| max_req_body_bytes | integer | 否 | 524288 | >=1 |
在此大小范围内的请求主体将被记录,如果大小超过配置值则截断后记录。|
| include_resp_body | boolean | 否 | false | | 如果为 true,则将响应主体包含在日志中。|
| include_resp_body_expr | array[array] | 否 | | | 一个或多个条件的数组,形式为
[lua-resty-expr](https://github.com/api7/lua-resty-expr)。在 `include_resp_body`
为 true 时使用。仅当此处配置的表达式计算结果为 true 时,才会记录响应主体。|
-| max_resp_body_bytes | integer | 否 | 524288 | >=1 |
在此大小范围内的响应主体将被记录,如果大小超过配置值则截断后记录。|
+| max_resp_body_bytes | integer | 否 | 524288 | >=1 |
在此大小范围内的响应主体将被记录,如果大小超过配置值则截断后记录。对于压缩响应,解压后的响应体同样截断到该大小。|
该插件支持使用批处理器来聚合并批量处理条目(日志/数据)。这样可以避免插件频繁地提交数据,默认设置情况下批处理器会每 `5` 秒钟或队列中的数据达到
`1000` 条时提交数据,如需了解批处理器相关参数设置,请参考 [Batch-Processor](../batch-processor.md#配置)。
diff --git a/docs/zh/latest/plugins/syslog.md b/docs/zh/latest/plugins/syslog.md
index eb3eb03f0d..176768644e 100644
--- a/docs/zh/latest/plugins/syslog.md
+++ b/docs/zh/latest/plugins/syslog.md
@@ -53,7 +53,7 @@ description: syslog 插件将请求和响应日志以 JSON 对象批量推送到
| include_resp_body | boolean | False | false |
| 若为 true,则在日志中包含响应体。
|
| include_resp_body_expr | array | False | |
| [lua-resty-expr](https://github.com/api7/lua-resty-expr) 表达式数组。当
`include_resp_body` 为 true 时使用,仅当此处表达式求值为 true 时才记录响应体。
|
| max_req_body_bytes | integer | False | 524288 | 大于等于 1
| 日志中记录的最大请求体字节数。超出该值的请求体将被截断。在 APISIX 3.16.0 版本中可用。
|
-| max_resp_body_bytes | integer | False | 524288 | 大于等于 1
| 日志中记录的最大响应体字节数。超出该值的响应体将被截断。在 APISIX 3.16.0 版本中可用。
|
+| max_resp_body_bytes | integer | False | 524288 | 大于等于 1
| 日志中记录的最大响应体字节数。超出该值的响应体将被截断。对于压缩响应,解压后的响应体同样截断到该大小。在 APISIX 3.16.0 版本中可用。
|
| name | string | False | `sys logger` |
| 批处理器中插件的唯一标识符。若使用 Prometheus 监控 APISIX 指标,该名称将以
`apisix_batch_process_entries` 导出。
|
| batch_max_size | integer | False | 1000 | 大于 0
| 每批次允许的最大日志条目数。达到该值后,批次将被发送至日志服务。设置为 1 表示立即处理。
|
| inactive_timeout | integer | False | 5 | 大于 0
| 在将批次发送至日志服务前等待新日志的最长时间(秒)。该值应小于 `buffer_duration`。
|
diff --git a/t/plugin/file-logger2.t b/t/plugin/file-logger2.t
index 8cbaabd727..daf6ec8c9b 100644
--- a/t/plugin/file-logger2.t
+++ b/t/plugin/file-logger2.t
@@ -28,6 +28,48 @@ add_block_preprocessor(sub {
$block->set_value("response_body", "passed\n");
}
}
+
+ if (!defined $block->http_config) {
+ my $http_config = <<_EOC_;
+ server {
+ listen 11463;
+ location ~ ^/compressed/(.+)\$ {
+ content_by_lua_block {
+ local gzip = require("apisix.utils.gzip")
+ local text = string.rep("hello world\\n", 10000)
+ local stored = string.rep("0123456789", 200)
+ local bodies = {
+ ["text-gzip"] = {"gzip", gzip.deflate_gzip(text)},
+ ["stored-gzip"] = {"gzip", gzip.deflate_gzip(stored, nil,
{level = 0})},
+ ["not-gzip"] = {"gzip", "not a gzip stream"},
+ }
+ local ok, brotli = pcall(require, "brotli")
+ if ok then
+ local numbers = {}
+ for i = 1, 20000 do
+ numbers[i] = i
+ end
+ bodies["text-br"] = {"br",
brotli:compress(table.concat(numbers, " "))}
+ bodies["not-br"] = {"br", "not a brotli stream"}
+ end
+
+ local case = ngx.var[1]
+ if case == "empty" or case == "204" or case == "304" or case
== "empty-br" then
+ ngx.status = tonumber(case) or 200
+ ngx.header["Content-Encoding"] = case == "empty-br" and
"br" or "gzip"
+ ngx.header["Content-Length"] = 0
+ ngx.send_headers()
+ return
+ end
+
+ ngx.header["Content-Encoding"] = bodies[case][1]
+ ngx.print(bodies[case][2])
+ }
+ }
+ }
+_EOC_
+ $block->set_value("http_config", $http_config);
+ }
});
@@ -574,3 +616,249 @@ skip unconcern body
try decode compressed data err: inflate gzip err: INFLATE: data error
--- response_body
contain with target
+
+
+
+=== TEST 16: set routes to upstreams sending compressed bodies
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+ local gzip = require("apisix.utils.gzip")
+ local stored = string.rep("0123456789", 200)
+ local exact = #gzip.deflate_gzip(stored, nil, {level = 0})
+
+ local routes = {
+ {uri = "/compressed/empty"},
+ {uri = "/compressed/204"},
+ {uri = "/compressed/304"},
+ {uri = "/compressed/text-gzip", max = 100},
+ {uri = "/compressed/stored-gzip", max = 100},
+ {uri = "/compressed/stored-gzip-exact", upstream =
"/compressed/stored-gzip",
+ max = exact},
+ {uri = "/compressed/not-gzip"},
+ {uri = "/compressed/text-gzip-under", upstream =
"/compressed/text-gzip",
+ max = 10000},
+ {uri = "/compressed/empty-br"},
+ {uri = "/compressed/text-br", max = 5000},
+ {uri = "/compressed/text-br-under", upstream =
"/compressed/text-br",
+ max = 50000},
+ {uri = "/compressed/not-br"},
+ }
+ for i, route in ipairs(routes) do
+ local code, body = t("/apisix/admin/routes/" .. i,
ngx.HTTP_PUT,
+ core.json.encode({
+ uri = route.uri,
+ plugins = {
+ ["file-logger"] = {
+ path = "file-logger-compressed.log",
+ include_resp_body = true,
+ max_resp_body_bytes = route.max,
+ },
+ ["proxy-rewrite"] = {
+ uri = route.upstream or route.uri,
+ },
+ },
+ upstream = {
+ nodes = {["127.0.0.1:11463"] = 1},
+ type = "roundrobin",
+ },
+ })
+ )
+ if code >= 300 then
+ ngx.status = code
+ ngx.say(body)
+ return
+ end
+ end
+ ngx.say("passed")
+ }
+ }
+
+
+
+=== TEST 17: empty bodies labelled gzip are logged as empty
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+ for _, case in ipairs({"empty", "204", "304"}) do
+ local code = t("/compressed/" .. case, ngx.HTTP_GET)
+ local fd = assert(io.open("file-logger-compressed.log"))
+ local last
+ for line in fd:lines() do
+ last = line
+ end
+ fd:close()
+ local resp = core.json.decode(last).response
+ ngx.say(code, " ", resp.headers["content-encoding"], " [",
resp.body, "]")
+ end
+ }
+ }
+--- response_body
+200 gzip []
+204 gzip []
+304 gzip []
+--- no_error_log
+try decode compressed data err
+
+
+
+=== TEST 18: a gzip body cut at the limit is logged as the plaintext decoded
from it
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+ local plaintexts = {
+ ["text-gzip"] = string.rep("hello world\n", 10000),
+ ["stored-gzip"] = string.rep("0123456789", 200),
+ }
+ for _, case in ipairs({"text-gzip", "stored-gzip"}) do
+ t("/compressed/" .. case, ngx.HTTP_GET)
+ local fd = assert(io.open("file-logger-compressed.log"))
+ local last
+ for line in fd:lines() do
+ last = line
+ end
+ fd:close()
+ local body = core.json.decode(last).response.body
+ ngx.say(case, " ", #body, " ", body ==
string.sub(plaintexts[case], 1, #body))
+ end
+ }
+ }
+--- response_body
+text-gzip 100 true
+stored-gzip 85 true
+--- no_error_log
+try decode compressed data err
+
+
+
+=== TEST 19: a complete gzip body exactly at the limit is decoded in full
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+ t("/compressed/stored-gzip-exact", ngx.HTTP_GET)
+ local fd = assert(io.open("file-logger-compressed.log"))
+ local last
+ for line in fd:lines() do
+ last = line
+ end
+ fd:close()
+ local body = core.json.decode(last).response.body
+ ngx.say(body == string.rep("0123456789", 200))
+ }
+ }
+--- response_body
+true
+--- no_error_log
+try decode compressed data err
+
+
+
+=== TEST 20: a body below the limit that fails to decode is logged as received
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+ t("/compressed/not-gzip", ngx.HTTP_GET)
+ local fd = assert(io.open("file-logger-compressed.log"))
+ local last
+ for line in fd:lines() do
+ last = line
+ end
+ fd:close()
+ ngx.say(core.json.decode(last).response.body)
+ }
+ }
+--- response_body
+not a gzip stream
+--- error_log
+try decode compressed data err: inflate gzip err
+
+
+
+=== TEST 21: the plaintext of a gzip body under the limit is held to the limit
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+ local plain = string.rep("hello world\n", 10000)
+ t("/compressed/text-gzip-under", ngx.HTTP_GET)
+ local fd = assert(io.open("file-logger-compressed.log"))
+ local last
+ for line in fd:lines() do
+ last = line
+ end
+ fd:close()
+ local body = core.json.decode(last).response.body
+ ngx.say(#body, " ", body == string.sub(plain, 1, #body))
+ }
+ }
+--- response_body
+10000 true
+--- no_error_log
+try decode compressed data err
+
+
+
+=== TEST 22: br bodies that are empty, cut at the limit or decoding past it
are logged without error
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+ local numbers = {}
+ for i = 1, 20000 do
+ numbers[i] = i
+ end
+ local plain = table.concat(numbers, " ")
+ for _, case in ipairs({"empty-br", "text-br", "text-br-under"}) do
+ t("/compressed/" .. case, ngx.HTTP_GET)
+ local fd = assert(io.open("file-logger-compressed.log"))
+ local last
+ for line in fd:lines() do
+ last = line
+ end
+ fd:close()
+ local body = core.json.decode(last).response.body
+ ngx.say(case, " ", #body, " ", body == string.sub(plain, 1,
#body))
+ end
+ }
+ }
+--- response_body
+empty-br 0 true
+text-br 5000 true
+text-br-under 50000 true
+--- no_error_log
+try decode compressed data err
+
+
+
+=== TEST 23: a br body below the limit that fails to decode is logged as
received
+--- config
+ location /t {
+ content_by_lua_block {
+ local core = require("apisix.core")
+ local t = require("lib.test_admin").test
+ t("/compressed/not-br", ngx.HTTP_GET)
+ local fd = assert(io.open("file-logger-compressed.log"))
+ local last
+ for line in fd:lines() do
+ last = line
+ end
+ fd:close()
+ ngx.say(core.json.decode(last).response.body)
+ }
+ }
+--- response_body
+not a brotli stream
+--- error_log
+try decode compressed data err: brotli decode err
diff --git a/t/plugin/response-rewrite3.t b/t/plugin/response-rewrite3.t
index dc43074f21..bc4e69e46f 100644
--- a/t/plugin/response-rewrite3.t
+++ b/t/plugin/response-rewrite3.t
@@ -52,6 +52,13 @@ add_block_preprocessor(sub {
ngx.say(s)
}
}
+ location /gzip_empty {
+ content_by_lua_block {
+ ngx.header['Content-Encoding'] = "gzip"
+ ngx.header['Content-Length'] = 0
+ ngx.send_headers()
+ }
+ }
}
server {
@@ -675,3 +682,55 @@ Content-Encoding: br
X-Server-id: 3
X-Server-status: on
Content-Type:
+
+
+
+=== TEST 23: set route with filters to an upstream sending an empty gzip body
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/gzip_empty",
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:11451": 1
+ },
+ "type": "roundrobin"
+ },
+ "plugins": {
+ "response-rewrite": {
+ "filters": [
+ {
+ "regex": "hello",
+ "replace": "test"
+ }
+ ]
+ }
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body
+passed
+
+
+
+=== TEST 24: an empty gzip body is passed through without a decode error
+--- request
+GET /gzip_empty
+--- response_body
+--- response_headers
+Content-Encoding:
+--- no_error_log
+filters may not work as expected