shreemaan-abhishek commented on code in PR #10515:
URL: https://github.com/apache/apisix/pull/10515#discussion_r1418351046


##########
apisix/plugins/brotli.lua:
##########
@@ -0,0 +1,241 @@
+--
+-- 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_re_gmatch = ngx.re.gmatch
+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 is_loaded, brotli = pcall(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 = 6,
+            -- follow the default value from ngx_brotli brotli_comp_level
+        },
+        lgwin = {
+            type = "integer",
+            default = 19,
+            -- follow the default value from ngx_brotli brotli_window
+            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 check_accept_encoding(ctx)
+    local accept_encoding = core.request.header(ctx, "Accept-Encoding")
+    -- no Accept-Encoding
+    if not accept_encoding then
+        return false
+    end
+
+    -- single Accept-Encoding
+    if accept_encoding == "*" or accept_encoding == "br" then
+        return true
+    end
+
+    -- multi Accept-Encoding
+    local iterator, err = ngx_re_gmatch(accept_encoding,
+                                        [[([a-z\*]+)(;q=)?([0-9.]*)?]], "jo")
+    if not iterator then
+        core.log.error("gmatch failed, error: ", err)
+        return false
+    end
+
+    local captures
+    while true do
+        captures, err = iterator()
+        if not captures then
+            break
+        end
+        if err then
+            core.log.error("iterator failed, error: ", err)
+            return false
+        end
+        if (captures[1] == "br" or captures[1] == "*") and
+           (not captures[2] or captures[3] ~= "0") then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+function _M.header_filter(conf, ctx)
+    if not is_loaded then
+        core.log.error("please check the brotli library")
+        return
+    end
+
+    local allow_encoding = check_accept_encoding(ctx)
+    if not allow_encoding then
+        return
+    end
+
+    local types = conf.types
+    local content_type = ngx_header["Content-Type"]
+    if not content_type then

Review Comment:
   how about checking content-type before `check_accept_encoding`?



##########
apisix/plugins/brotli.lua:
##########
@@ -0,0 +1,241 @@
+--
+-- 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_re_gmatch = ngx.re.gmatch
+local ngx_header = ngx.header
+local req_http_version = ngx.req.http_version

Review Comment:
   ditto



##########
apisix/plugins/brotli.lua:
##########
@@ -0,0 +1,241 @@
+--
+-- 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_re_gmatch = ngx.re.gmatch
+local ngx_header = ngx.header

Review Comment:
   consider using `core.req.header`



##########
apisix/plugins/brotli.lua:
##########
@@ -0,0 +1,241 @@
+--
+-- 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_re_gmatch = ngx.re.gmatch
+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 is_loaded, brotli = pcall(require, "brotli")
+
+
+local schema = {
+    type = "object",
+    properties = {
+        types = {
+            anyOf = {
+                {
+                    type = "array",
+                    minItems = 1,
+                    items = {
+                        type = "string",
+                        minLength = 1,
+                    },
+                },
+                {
+                    enum = {"*"}

Review Comment:
   what does this mean?
   ```
                       enum = {"*"}
   ```



-- 
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