rickyxucn commented on code in PR #10515: URL: https://github.com/apache/apisix/pull/10515#discussion_r1411756533
########## 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 + if content_type == ty then + matched = true + break + end + end + + if not matched then + return + end + end + + local content_length = tonumber(ngx_header["Content-Length"]) + if content_length then + local min_length = conf.min_length + if content_length < min_length then + return + end + -- Like Nginx, don't check min_length if Content-Length is missing + end + + local http_version = req_http_version() + if http_version < conf.http_version then + return + end + + if conf.vary then + core.response.add_header("Vary", "Accept-Encoding") + end + + ctx.brotli_matched = true + core.response.clear_header_as_body_modified() + core.response.add_header("Content-Encoding", "br") +end + + +function _M.body_filter(conf, ctx) + if ctx.brotli_matched then + local body = core.response.hold_body_chunk(ctx) Review Comment: For large files, the following error will be reported: ` 2023/12/01 16:04:48 [error] 24291#24291: *1347 failed to run body_filter_by_lua*: not enough memory while sending to client, client: 10.38.87.16, server: _, request: "GET /data/2g.html HTTP/1.1", upstream: "http://127.0.0.1:30001/data/2g.html", host: "xx.xx.xx.xx:9080" ` The following code works well on my machine: ```lua function _M.header_filter(conf, ctx) ... ctx.content_length = tonumber(ngx_header["Content-Length"]) end function _M.body_filter(conf, ctx) if ctx.brotli_matched then if not ctx.compressor then ctx.compressor = brotli.compressor:new { mode = conf.mode, quality = conf.comp_level, lgwin = conf.lgwin, lgblock = conf.lgblock, } if not ctx.compressor then core.log.error("failed to create brotli compressor") return end end ctx.content_length = ctx.content_length - #ngx.arg[1] local chunk = ctx.compressor:compress(ngx.arg[1]) if ctx.content_length <= 0 then local chunk_fin = ctx.compressor:finish() chunk = chunk .. chunk_fin end ngx.arg[1] = chunk end end ``` -- 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]
