Skip to content

Commit

Permalink
fix(ai-proxy): double-gzipping responses when status is not 200 (#12493)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1fb8be5)
  • Loading branch information
tysoekong authored and github-actions[bot] committed Feb 1, 2024
1 parent 1f89c9f commit ff90be9
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions kong/plugins/ai-proxy/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ function _M:header_filter(conf)

local new_response_string, err = ai_driver.from_format(response_body, conf.model, route_type)
if err then
kong.ctx.plugin.ai_parser_error = true

ngx.status = 500
local message = {
error = {
Expand All @@ -66,21 +68,24 @@ end

function _M:body_filter(conf)
if not kong.ctx.shared.skip_response_transformer then
-- all errors MUST be checked and returned in header_filter
-- we should receive a replacement response body from the same thread

local original_request = kong.ctx.plugin.parsed_response or kong.response.get_raw_body()
local deflated_request = kong.ctx.plugin.parsed_response or kong.response.get_raw_body()
if deflated_request then
local is_gzip = kong.response.get_header("Content-Encoding") == "gzip"
if is_gzip then
deflated_request = kong_utils.deflate_gzip(deflated_request)
if (kong.response.get_status() == 200) or (kong.ctx.plugin.ai_parser_error) then
-- all errors MUST be checked and returned in header_filter
-- we should receive a replacement response body from the same thread

local original_request = kong.ctx.plugin.parsed_response
local deflated_request = kong.ctx.plugin.parsed_response
if deflated_request then
local is_gzip = kong.response.get_header("Content-Encoding") == "gzip"
if is_gzip then
deflated_request = kong_utils.deflate_gzip(deflated_request)
end

kong.response.set_raw_body(deflated_request)
end
kong.response.set_raw_body(deflated_request)
end

-- call with replacement body, or original body if nothing changed
ai_shared.post_request(conf, original_request)
-- call with replacement body, or original body if nothing changed
ai_shared.post_request(conf, original_request)
end
end
end

Expand Down

0 comments on commit ff90be9

Please sign in to comment.