function _M.header_filter()

in apisix/plugins/gzip.lua [103:167]


function _M.header_filter(conf, ctx)
    if not is_apisix_or then
        core.log.error("need to build APISIX-Runtime to support setting gzip")
        return 501
    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

    local buffers = conf.buffers

    core.log.info("set gzip with buffers: ", buffers.number, " ", buffers.size,
                  ", level: ", conf.comp_level)

    local ok, err = response.set_gzip({
        buffer_num = buffers.number,
        buffer_size = buffers.size,
        compress_level = conf.comp_level,
    })
    if not ok then
        core.log.error("failed to set gzip: ", err)
        return
    end

    if conf.vary then
        core.response.add_header("Vary", "Accept-Encoding")
    end
end