function _M.header_filter()

in apisix/plugins/brotli.lua [155:227]


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 content_encoded = ngx_header["Content-Encoding"]
    if content_encoded then
        
        return
    end

    local types = conf.types
    local content_type = ngx_header["Content-Type"]
    if not content_type then
        
        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
        
    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

    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

    ctx.brotli_matched = true
    ctx.compressor = compressor
    core.response.clear_header_as_body_modified()
    core.response.add_header("Content-Encoding", "br")
end