From dd403799f6fe6d2c949022b2f98dc4f1ae0172bb Mon Sep 17 00:00:00 2001 From: RocFang Date: Fri, 13 Aug 2021 09:40:10 +0800 Subject: [PATCH 1/2] feat(gzip): support single string type and * to match any type --- apisix/plugins/gzip.lua | 31 +++++++++++++++------ docs/en/latest/plugins/gzip.md | 18 ++++++------ t/plugin/gzip.t | 51 ++++++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 19 deletions(-) diff --git a/apisix/plugins/gzip.lua b/apisix/plugins/gzip.lua index a2263221be0d..3eb89d543e32 100644 --- a/apisix/plugins/gzip.lua +++ b/apisix/plugins/gzip.lua @@ -21,17 +21,26 @@ local req_http_version = ngx.req.http_version local str_sub = string.sub local ipairs = ipairs local tonumber = tonumber +local type = type local schema = { type = "object", properties = { types = { - type = "array", - minItems = 1, - items = { - type = "string", - minLength = 1, + anyOf = { + { + type = "array", + minItem = 1, + items = { + type = "string", + minLength = 1, + }, + }, + { + type = "string", + minLength = 1, + } }, default = {"text/html"} }, @@ -110,10 +119,16 @@ function _M.header_filter(conf, ctx) end local matched = false - for _, ty in ipairs(types) do - if content_type == ty then + if type(types) == "table" then + for _, ty in ipairs(types) do + if content_type == ty then + matched = true + break + end + end + else + if content_type == types or types == "*" then matched = true - break end end if not matched then diff --git a/docs/en/latest/plugins/gzip.md b/docs/en/latest/plugins/gzip.md index d94721571d96..734136296a4b 100644 --- a/docs/en/latest/plugins/gzip.md +++ b/docs/en/latest/plugins/gzip.md @@ -37,15 +37,15 @@ This plugin requires APISIX to run on [APISIX-OpenResty](../how-to-build.md#step ## Attributes -| Name | Type | Requirement | Default | Valid | Description | -| --------- | ------------- | ----------- | ---------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- | -| types | array | optional | ["text/html"] | | dynamically set the `gzip_types` 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 | -| 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.size | integer | optional | 4096 | >= 1 | dynamically set the `gzip_buffers` directive | -| vary | boolean | optional | false | | dynamically set the `gzip_vary` directive | +| Name | Type | Requirement | Default | Valid | Description | +| --------------------------------------| ------------| -------------- | -------- | --------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| types | array[string]/string | optional | ["text/html"] | | dynamically set the `gzip_types` 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 | +| 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.size | integer | optional | 4096 | >= 1 | dynamically set the `gzip_buffers` directive | +| vary | boolean | optional | false | | dynamically set the `gzip_vary` directive | ## How To Enable diff --git a/t/plugin/gzip.t b/t/plugin/gzip.t index f29214934c8a..a1737e623ac2 100644 --- a/t/plugin/gzip.t +++ b/t/plugin/gzip.t @@ -395,7 +395,7 @@ Content-Encoding: gzip -=== TEST 15: vary +=== TEST 15: match all types --- config location /t { content_by_lua_block { @@ -412,7 +412,7 @@ Content-Encoding: gzip }, "plugins": { "gzip": { - "vary": true + "types": "*" } } }]] @@ -436,6 +436,53 @@ POST /echo 012345678 --- more_headers Accept-Encoding: gzip +Content-Type: text/html +--- response_headers +Content-Encoding: gzip + + + +=== TEST 17: vary +--- 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": { + "vary": true + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } +} +--- response_body +passed + + + +=== TEST 18: hit +--- request +POST /echo +0123456789 +012345678 +--- more_headers +Accept-Encoding: gzip Vary: upstream Content-Type: text/html --- response_headers From 0ec4deb8df1331fec2a90def2faa9e7713633a9e Mon Sep 17 00:00:00 2001 From: RocFang Date: Fri, 13 Aug 2021 11:49:16 +0800 Subject: [PATCH 2/2] fix: strictly limit single string types to "*" --- apisix/plugins/gzip.lua | 7 ++----- docs/en/latest/plugins/gzip.md | 2 +- t/plugin/gzip.t | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/apisix/plugins/gzip.lua b/apisix/plugins/gzip.lua index 3eb89d543e32..ac7128e6ceda 100644 --- a/apisix/plugins/gzip.lua +++ b/apisix/plugins/gzip.lua @@ -38,8 +38,7 @@ local schema = { }, }, { - type = "string", - minLength = 1, + enum = {"*"} } }, default = {"text/html"} @@ -127,9 +126,7 @@ function _M.header_filter(conf, ctx) end end else - if content_type == types or types == "*" then - matched = true - end + matched = true end if not matched then return diff --git a/docs/en/latest/plugins/gzip.md b/docs/en/latest/plugins/gzip.md index 734136296a4b..d844c8e12c52 100644 --- a/docs/en/latest/plugins/gzip.md +++ b/docs/en/latest/plugins/gzip.md @@ -39,7 +39,7 @@ This plugin requires APISIX to run on [APISIX-OpenResty](../how-to-build.md#step | Name | Type | Requirement | Default | Valid | Description | | --------------------------------------| ------------| -------------- | -------- | --------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | -| types | array[string]/string | 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 | | 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 | diff --git a/t/plugin/gzip.t b/t/plugin/gzip.t index a1737e623ac2..aea85e1c1d95 100644 --- a/t/plugin/gzip.t +++ b/t/plugin/gzip.t @@ -436,7 +436,7 @@ POST /echo 012345678 --- more_headers Accept-Encoding: gzip -Content-Type: text/html +Content-Type: video/3gpp --- response_headers Content-Encoding: gzip