Skip to content

Commit

Permalink
hotfix(alf_serializer) do not include JSON body in params
Browse files Browse the repository at this point in the history
JSON bodies need to be present in the text field but not included in
`postData.params`.

This also avoids reading the request body if not configured so in the
plugin.

Sadly there is no unit test for this part of the ALF serializer.

Fix #762
  • Loading branch information
thibaultcha committed Dec 3, 2015
1 parent b4cc76a commit bef5870
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions kong/plugins/mashape-analytics/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ local ALFSerializer = require "kong.plugins.log-serializers.alf"
local ngx_now = ngx.now
local ngx_log = ngx.log
local ngx_log_ERR = ngx.ERR
local string_find = string.find
local pcall = pcall

local ALF_BUFFERS = {} -- buffers per-api

Expand All @@ -33,29 +35,36 @@ end
function AnalyticsHandler:access(conf)
AnalyticsHandler.super.access(self)

-- Retrieve and keep in memory the bodies for this request
ngx.ctx.analytics = {
req_body = "",
res_body = "",
req_post_args = {}
}
local req_body = ""
local res_body = ""
local req_post_args = {}

ngx.req.read_body()

local status, res = pcall(ngx.req.get_post_args)
if not status then
if res == "requesty body in temp file not supported" then
ngx_log(ngx_log_ERR, "[mashape-analytics] cannot read request body from temporary file. Try increasing the client_body_buffer_size directive.")
else
ngx_log(ngx_log_ERR, res)
if conf.log_body then
ngx.req.read_body()
req_body = ngx.req.get_body_data()

local headers = ngx.req.get_headers()
local content_type = headers["content-type"]
if content_type and string_find(content_type:lower(), "application/x-www-form-urlencoded", nil, true) then
local status, res = pcall(ngx.req.get_post_args)
if not status then
if res == "requesty body in temp file not supported" then
ngx_log(ngx_log_ERR, "[mashape-analytics] cannot read request body from temporary file. Try increasing the client_body_buffer_size directive.")
else
ngx_log(ngx_log_ERR, res)
end
else
req_post_args = res
end
end
else
ngx.ctx.analytics.req_post_args = res
end

if conf.log_body then
ngx.ctx.analytics.req_body = ngx.req.get_body_data()
end
-- keep in memory the bodies for this request
ngx.ctx.analytics = {
req_body = req_body,
res_body = res_body,
req_post_args = req_post_args
}
end

function AnalyticsHandler:body_filter(conf)
Expand Down

0 comments on commit bef5870

Please sign in to comment.