Skip to content

Commit

Permalink
fix(brotli-plugin): skip brotli compression for upstream compressed r…
Browse files Browse the repository at this point in the history
…esponse (#10740)
  • Loading branch information
SilentEntity authored Jan 12, 2024
1 parent c11782a commit 781e8f6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apisix/plugins/brotli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ function _M.header_filter(conf, ctx)
return
end

local content_encoded = ngx_header["Content-Encoding"]
if content_encoded then
-- Don't compress if Content-Encoding is present in upstream data
return
end

local types = conf.types
local content_type = ngx_header["Content-Type"]
if not content_type then
Expand Down
6 changes: 6 additions & 0 deletions docs/en/latest/plugins/brotli.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ sudo sh -c "echo /usr/local/brotli/lib >> /etc/ld.so.conf.d/brotli.conf"
sudo ldconfig
```

:::caution

If the upstream is returning a compressed response, then the Brotli plugin won't be able to compress it.

:::

## Attributes

| Name | Type | Required | Default | Valid values | Description |
Expand Down
7 changes: 7 additions & 0 deletions t/lib/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,11 @@ function _M.clickhouse_logger_server()
end


function _M.mock_compressed_upstream_response()
local s = "compressed_response"
ngx.header['Content-Encoding'] = 'gzip'
ngx.say(s)
end


return _M
65 changes: 65 additions & 0 deletions t/plugin/brotli.t
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,68 @@ passed
}
--- response_body
ok
=== TEST 30: mock upstream compressed response
--- 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": "/mock_compressed_upstream_response",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
},
"plugins": {
"brotli": {
"types": "*"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 31: hit - skip brotli compression of compressed upstream response
--- config
location /t {
content_by_lua_block {
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port
.. "/mock_compressed_upstream_response"
local httpc = http.new()
local req_body = ("abcdf01234"):rep(1024)
local res, err = httpc:request_uri(uri,
{method = "POST", headers = {["Accept-Encoding"] = "gzip, br"}, body = req_body})
if not res then
ngx.say(err)
return
end
if res.headers["Content-Encoding"] == 'gzip' then
ngx.say("ok")
end
}
}
--- request
GET /t
--- more_headers
Accept-Encoding: gzip, br
Vary: upstream
Content-Type: text/html
--- response_body
ok

0 comments on commit 781e8f6

Please sign in to comment.