Skip to content

Commit fb21466

Browse files
authored
fix(file-logger): incorrectly attempts to decompress uncompressed response body (#13100)
1 parent dd6305c commit fb21466

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

apisix/plugins/gzip.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function _M.header_filter(conf, ctx)
160160
core.log.error("failed to set gzip: ", err)
161161
return
162162
end
163-
163+
ctx.gzip_matched = true
164164
if conf.vary then
165165
core.response.add_header("Vary", "Accept-Encoding")
166166
end

apisix/utils/log-util.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ function _M.collect_body(conf, ctx)
399399
end
400400

401401
local response_encoding = ngx_header["Content-Encoding"]
402-
if not response_encoding then
402+
if not response_encoding or ctx.gzip_matched then
403403
ctx.resp_body = final_body
404404
return
405405
end

t/plugin/file-logger2.t

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,63 @@ contain with target
514514
--- response_body
515515
contain target body hits with expr
516516
skip unconcern body
517+
518+
519+
520+
=== TEST 15: collecting response body gzipped by APISIX
521+
--- config
522+
location /t {
523+
content_by_lua_block {
524+
local core = require("apisix.core")
525+
local t = require("lib.test_admin").test
526+
local code, body = t('/apisix/admin/routes/1',
527+
ngx.HTTP_PUT,
528+
[[{
529+
"plugins": {
530+
"file-logger": {
531+
"path": "file-with-uncompressed-resp-body.log",
532+
"include_resp_body": true
533+
},
534+
"gzip": {
535+
"min_length": 10
536+
}
537+
},
538+
"upstream": {
539+
"nodes": {
540+
"127.0.0.1:1982": 1
541+
},
542+
"type": "roundrobin"
543+
},
544+
"uri": "/hello"
545+
}]]
546+
)
547+
548+
if code >= 300 then
549+
ngx.status = code
550+
ngx.say(body)
551+
return
552+
end
553+
554+
local code = t("/hello", ngx.HTTP_GET, nil, nil, {['Accept-Encoding']='gzip'})
555+
local fd, err = io.open("file-with-uncompressed-resp-body.log", 'r')
556+
local msg
557+
558+
if not fd then
559+
core.log.error("failed to open file: file-with-uncompressed-resp-body.log, error info: ", err)
560+
return
561+
end
562+
563+
-- note only for first line
564+
msg = fd:read()
565+
566+
local new_msg = core.json.decode(msg)
567+
ngx.status = code
568+
if new_msg.response ~= nil and new_msg.response.body == "hello world\n" then
569+
ngx.say('contain with target')
570+
end
571+
}
572+
}
573+
--- no_error_log
574+
try decode compressed data err: inflate gzip err: INFLATE: data error
575+
--- response_body
576+
contain with target

0 commit comments

Comments
 (0)