-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[fix/response-transformer] fix for issue #393 #822
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
local stringy = require "stringy" | ||
local cjson = require "cjson" | ||
|
||
local table_insert = table.insert | ||
local pcall = pcall | ||
local string_find = string.find | ||
local unpack = unpack | ||
local type = type | ||
|
||
local _M = {} | ||
|
||
local function read_json_body(body) | ||
if body then | ||
local status, res = pcall(cjson.decode, body) | ||
if status then | ||
return res | ||
end | ||
end | ||
end | ||
|
||
local function append_value(current_value, value) | ||
local current_value_type = type(current_value) | ||
|
||
if current_value_type == "string" then | ||
return {current_value, value} | ||
elseif current_value_type == "table" then | ||
table_insert(current_value, value) | ||
return current_value | ||
else | ||
return {value} | ||
end | ||
end | ||
|
||
local function iter(config_array) | ||
return function(config_array, i, previous_name, previous_value) | ||
i = i + 1 | ||
local current_pair = config_array[i] | ||
if current_pair == nil then -- n + 1 | ||
return nil | ||
end | ||
local current_name, current_value = unpack(stringy.split(current_pair, ":")) | ||
return i, current_name, current_value | ||
end, config_array, 0 | ||
end | ||
|
||
function _M.is_json_body(content_type) | ||
return content_type and string_find(content_type:lower(), "application/json", nil, true) | ||
end | ||
|
||
function _M.transform_json_body(conf, buffered_data) | ||
local json_body = read_json_body(buffered_data) | ||
if json_body == nil then return end | ||
|
||
-- remove key:value to body | ||
for _, name in iter(conf.remove.json) do | ||
json_body[name] = nil | ||
end | ||
|
||
-- replace key:value to body | ||
for _, name, value in iter(conf.replace.json) do | ||
local v = cjson.encode(value) | ||
if stringy.startswith(v, "\"") and stringy.endswith(v, "\"") then | ||
v = v:sub(2, v:len() - 1):gsub("\\\"", "\"") -- To prevent having double encoded quotes | ||
end | ||
if json_body[name] then | ||
json_body[name] = v | ||
end | ||
end | ||
|
||
-- add new key:value to body | ||
for _, name, value in iter(conf.add.json) do | ||
local v = cjson.encode(value) | ||
if stringy.startswith(v, "\"") and stringy.endswith(v, "\"") then | ||
v = v:sub(2, v:len() - 1):gsub("\\\"", "\"") -- To prevent having double encoded quotes | ||
end | ||
if not json_body[name] then | ||
json_body[name] = v | ||
end | ||
end | ||
|
||
-- append new key:value or value to existing key | ||
for _, name, value in iter(conf.append.json) do | ||
local v = cjson.encode(value) | ||
if stringy.startswith(v, "\"") and stringy.endswith(v, "\"") then | ||
v = v:sub(2, v:len() - 1):gsub("\\\"", "\"") -- To prevent having double encoded quotes | ||
end | ||
json_body[name] = append_value(json_body[name],v) | ||
end | ||
|
||
return cjson.encode(json_body) | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
local stringy = require "stringy" | ||
|
||
local table_insert = table.insert | ||
local unpack = unpack | ||
local type = type | ||
local string_find = string.find | ||
|
||
local _M = {} | ||
|
||
local function iter(config_array) | ||
return function(config_array, i, previous_header_name, previous_header_value) | ||
i = i + 1 | ||
local header_to_test = config_array[i] | ||
if header_to_test == nil then -- n + 1 | ||
return nil | ||
end | ||
local header_to_test_name, header_to_test_value = unpack(stringy.split(header_to_test, ":")) | ||
return i, header_to_test_name, header_to_test_value | ||
end, config_array, 0 | ||
end | ||
|
||
local function append_value(current_value, value) | ||
local current_value_type = type(current_value) | ||
|
||
if current_value_type == "string" then | ||
return {current_value, value} | ||
elseif current_value_type == "table" then | ||
table_insert(current_value, value) | ||
return current_value | ||
else | ||
return {value} | ||
end | ||
end | ||
|
||
local function is_json_body(content_type) | ||
return content_type and string_find(content_type:lower(), "application/json", nil, true) | ||
end | ||
|
||
local function is_body_transform_set(conf) | ||
return #conf.add.json > 0 or #conf.remove.json > 0 or #conf.replace.json > 0 or #conf.append.json > 0 | ||
end | ||
|
||
--- | ||
-- # Example: | ||
-- ngx.headers = header_filter.transform_headers(conf, ngx.headers) | ||
-- We run transformations in following order: remove, replace, add, append. | ||
-- @param[type=table] conf Plugin configuration. | ||
-- @param[type=table] ngx_headers Table of headers, that should be `ngx.headers` | ||
-- @return table A table containing the new headers. | ||
function _M.transform_headers(conf, ngx_headers) | ||
-- remove headers | ||
for _, header_name, header_value in iter(conf.remove.headers) do | ||
ngx_headers[header_name] = nil | ||
end | ||
|
||
-- replace headers | ||
for _, header_name, header_value in iter(conf.replace.headers) do | ||
if ngx_headers[header_name] ~= nil then | ||
ngx_headers[header_name] = header_value | ||
end | ||
end | ||
|
||
-- add headers | ||
for _, header_name, header_value in iter(conf.add.headers) do | ||
if ngx_headers[header_name] == nil then | ||
ngx_headers[header_name] = header_value | ||
end | ||
end | ||
|
||
-- append headers | ||
for _, header_name, header_value in iter(conf.append.headers) do | ||
ngx_headers[header_name] = append_value(ngx_headers[header_name], header_value) | ||
end | ||
|
||
-- Removing the content-length header because the body is going to change | ||
if is_body_transform_set(conf) and is_json_body(ngx_headers["content-type"]) then | ||
ngx_headers["content-length"] = nil | ||
end | ||
end | ||
|
||
return _M |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: (2 spaces here)