feat(gzip): support special * to match any type (#4817)

This commit is contained in:
RocFang 2021-08-16 16:13:52 +08:00 committed by GitHub
parent ca5ea1ffa4
commit e244940ea6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 20 deletions

View File

@ -21,17 +21,25 @@ local req_http_version = ngx.req.http_version
local str_sub = string.sub local str_sub = string.sub
local ipairs = ipairs local ipairs = ipairs
local tonumber = tonumber local tonumber = tonumber
local type = type
local schema = { local schema = {
type = "object", type = "object",
properties = { properties = {
types = { types = {
type = "array", anyOf = {
minItems = 1, {
items = { type = "array",
type = "string", minItem = 1,
minLength = 1, items = {
type = "string",
minLength = 1,
},
},
{
enum = {"*"}
}
}, },
default = {"text/html"} default = {"text/html"}
}, },
@ -110,11 +118,15 @@ function _M.header_filter(conf, ctx)
end end
local matched = false local matched = false
for _, ty in ipairs(types) do if type(types) == "table" then
if content_type == ty then for _, ty in ipairs(types) do
matched = true if content_type == ty then
break matched = true
break
end
end end
else
matched = true
end end
if not matched then if not matched then
return return

View File

@ -37,15 +37,15 @@ The `gzip` plugin dynamically set the gzip behavior of Nginx.
## Attributes ## Attributes
| Name | Type | Requirement | Default | Valid | Description | | Name | Type | Requirement | Default | Valid | Description |
| --------- | ------------- | ----------- | ---------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- | | --------------------------------------| ------------| -------------- | -------- | --------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| types | array | optional | ["text/html"] | | dynamically set the `gzip_types` directive | | types | array[string] or "*" | optional | ["text/html"] | | dynamically set the `gzip_types` directive, special value `"*"` matches any MIME type |
| min_length | integer | optional | 20 | >= 1 | dynamically set the `gzip_min_length` directive | | min_length | integer | optional | 20 | >= 1 | dynamically set the `gzip_min_length` directive |
| comp_level | integer | optional | 1 | [1, 9] | dynamically set the `gzip_comp_level` directive | | comp_level | integer | optional | 1 | [1, 9] | dynamically set the `gzip_comp_level` directive |
| http_version | number | optional | 1.1 | 1.1, 1.0 | dynamically set the `gzip_http_version` directive | | http_version | number | optional | 1.1 | 1.1, 1.0 | dynamically set the `gzip_http_version` directive |
| buffers.number | integer | optional | 32 | >= 1 | dynamically set the `gzip_buffers` directive | | buffers.number | integer | optional | 32 | >= 1 | dynamically set the `gzip_buffers` directive |
| buffers.size | integer | optional | 4096 | >= 1 | dynamically set the `gzip_buffers` directive | | buffers.size | integer | optional | 4096 | >= 1 | dynamically set the `gzip_buffers` directive |
| vary | boolean | optional | false | | dynamically set the `gzip_vary` directive | | vary | boolean | optional | false | | dynamically set the `gzip_vary` directive |
## How To Enable ## How To Enable

51
t/plugin/gzip.t vendored
View File

@ -395,7 +395,54 @@ Content-Encoding: gzip
=== TEST 15: vary === TEST 15: match all types
--- 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": "/echo",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
},
"plugins": {
"gzip": {
"types": "*"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 16: hit
--- request
POST /echo
0123456789
012345678
--- more_headers
Accept-Encoding: gzip
Content-Type: video/3gpp
--- response_headers
Content-Encoding: gzip
=== TEST 17: vary
--- config --- config
location /t { location /t {
content_by_lua_block { content_by_lua_block {
@ -429,7 +476,7 @@ passed
=== TEST 16: hit === TEST 18: hit
--- request --- request
POST /echo POST /echo
0123456789 0123456789