rickyxucn commented on code in PR #10515:
URL: https://github.com/apache/apisix/pull/10515#discussion_r1410322117


##########
apisix/plugins/brotli.lua:
##########
@@ -0,0 +1,195 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local ngx_header = ngx.header
+local req_http_version = ngx.req.http_version
+local str_sub = string.sub
+local ipairs = ipairs
+local tonumber = tonumber
+local type = type
+local brotli = require("brotli")
+
+
+local schema = {
+    type = "object",
+    properties = {
+        types = {
+            anyOf = {
+                {
+                    type = "array",
+                    minItems = 1,
+                    items = {
+                        type = "string",
+                        minLength = 1,
+                    },
+                },
+                {
+                    enum = {"*"}
+                }
+            },
+            default = {"text/html"}
+        },
+        min_length = {
+            type = "integer",
+            minimum = 1,
+            default = 20,
+        },
+        mode = {
+            type = "integer",
+            minimum = 0,
+            maximum = 2,
+            default = 0,
+            -- 0: MODE_GENERIC (default),
+            -- 1: MODE_TEXT (for UTF-8 format text input)
+            -- 2: MODE_FONT (for WOFF 2.0)
+        },
+        comp_level = {
+            type = "integer",
+            minimum = 0,
+            maximum = 11,
+            default = 11,
+        },
+        lgwin = {
+            type = "integer",
+            default = 24,
+            enum = {0,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24},
+        },
+        lgblock = {
+            type = "integer",
+            default = 0,
+            enum = {0,16,17,18,19,20,21,22,23,24},
+        },
+        http_version = {
+            enum = {1.1, 1.0},
+            default = 1.1,
+        },
+        vary = {
+            type = "boolean",
+        }
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 996,
+    name = "brotli",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function create_brotli_compressor(mode, comp_level, lgwin, lgblock)
+    local options = {
+        mode = mode,
+        quality = comp_level,
+        lgwin = lgwin,
+        lgblock = lgblock,
+    }
+    return brotli.compressor:new(options)
+end
+
+
+local function brotli_compress(conf, ctx, body)
+    local compressor = create_brotli_compressor(conf.mode, conf.comp_level,
+                                                conf.lgwin, conf.lgblock)
+    if not compressor then
+        core.log.error("failed to create brotli compressor")
+        return
+    end
+
+    local chunk = compressor:compress(body)
+    local chunk_fin = compressor:finish()
+    return chunk .. chunk_fin
+end
+
+
+function _M.header_filter(conf, ctx)
+    local types = conf.types
+    local content_type = ngx_header["Content-Type"]
+    if not content_type then
+        -- Like Nginx, don't compress if Content-Type is missing
+        return
+    end
+
+    if type(types) == "table" then
+        local matched = false
+        local from = core.string.find(content_type, ";")
+        if from then
+            content_type = str_sub(content_type, 1, from - 1)
+        end
+
+        for _, ty in ipairs(types) do

Review Comment:
   `"*"` cannot match any MIME type.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to