-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(resp-tranformer) handle already existing headers
Now add will only add header/json if it does not exist, config `append` added to support adding new value to existing header/json. If header does not exist, a new one will be added. config `replace added to replace the value of existing header/json with new value
- Loading branch information
Shashi Ranjan
committed
Dec 24, 2015
1 parent
88f2930
commit 7b7d388
Showing
12 changed files
with
786 additions
and
232 deletions.
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.