function _M.body_filter()

in apisix/plugins/response-rewrite.lua [252:303]


function _M.body_filter(conf, ctx)
    if not ctx.response_rewrite_matched then
        return
    end

    if conf.filters then

        local body = core.response.hold_body_chunk(ctx)
        if not body then
            return
        end

        local err
        if ctx.response_encoding ~= nil then
            local decoder = content_decode.dispatch_decoder(ctx.response_encoding)
            if not decoder then
                core.log.error("filters may not work as expected ",
                               "due to unsupported compression encoding type: ",
                               ctx.response_encoding)
                return
            end
            body, err = decoder(body)
            if err ~= nil then
                core.log.error("filters may not work as expected: ", err)
                return
            end
        end

        for _, filter in ipairs(conf.filters) do
            if filter.scope == "once" then
                body, _, err = re_sub(body, filter.regex, filter.replace, filter.options)
            else
                body, _, err = re_gsub(body, filter.regex, filter.replace, filter.options)
            end
            if err ~= nil then
                core.log.error("regex \"" .. filter.regex .. "\" substitutes failed:" .. err)
            end
        end

        ngx.arg[1] = body
        return
    end

    if conf.body then
        ngx.arg[2] = true
        if conf.body_base64 then
            ngx.arg[1] = ngx.decode_base64(conf.body)
        else
            ngx.arg[1] = conf.body
        end
    end
end