-
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.
- Loading branch information
Showing
13 changed files
with
294 additions
and
9 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 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 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 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 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 |
---|---|---|
|
@@ -94,4 +94,4 @@ function Apis:delete(api_id) | |
return ok | ||
end | ||
|
||
return Apis | ||
return Apis |
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 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,92 @@ | ||
local utils = require "kong.tools.utils" | ||
local stringy = require "stringy" | ||
local cjson = require "cjson" | ||
|
||
local _M = {} | ||
|
||
local APPLICATION_JSON = "application/json" | ||
local CONTENT_TYPE = "content-type" | ||
|
||
local function get_content_type() | ||
local header_value = ngx.header[CONTENT_TYPE] | ||
if header_value then | ||
return stringy.strip(header_value):lower() | ||
end | ||
return nil | ||
end | ||
|
||
local function read_response_body() | ||
local chunk, eof = ngx.arg[1], ngx.arg[2] | ||
local buffered = ngx.ctx.buffered | ||
if not buffered then | ||
buffered = {} | ||
ngx.ctx.buffered = buffered | ||
end | ||
if chunk ~= "" then | ||
buffered[#buffered + 1] = chunk | ||
ngx.arg[1] = nil | ||
end | ||
if eof then | ||
local response_body = table.concat(buffered) | ||
return response_body | ||
end | ||
return nil | ||
end | ||
|
||
local function read_json_body() | ||
local body = read_response_body() | ||
if body then | ||
local status, res = pcall(cjson.decode, body) | ||
if status then | ||
return res | ||
end | ||
end | ||
return nil | ||
end | ||
|
||
local function set_json_body(json) | ||
local body = cjson.encode(json) | ||
ngx.arg[1] = body | ||
end | ||
|
||
local function iterate_and_exec(val, cb) | ||
if utils.table_size(val) > 0 then | ||
for _, entry in ipairs(val) do | ||
local parts = stringy.split(entry, ":") | ||
cb(parts[1], utils.table_size(parts) == 2 and parts[2] or nil) | ||
end | ||
end | ||
end | ||
|
||
function _M.execute(conf) | ||
if not conf then return end | ||
|
||
local is_json_body = get_content_type() == APPLICATION_JSON | ||
|
||
if (conf.add.json or conf.remove.json) and is_json_body then | ||
local json_body = read_json_body() | ||
if json_body then | ||
|
||
if conf.add.json then | ||
iterate_and_exec(conf.add.json, function(name, value) | ||
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] = v | ||
end) | ||
end | ||
|
||
if conf.remove.json then | ||
iterate_and_exec(conf.remove.json, function(name) | ||
json_body[name] = nil | ||
end) | ||
end | ||
|
||
set_json_body(json_body) | ||
end | ||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
local body_filter = require "kong.plugins.response_transformer.body_filter" | ||
local header_filter = require "kong.plugins.response_transformer.header_filter" | ||
|
||
local ResponseTransformerHandler = BasePlugin:extend() | ||
|
||
function ResponseTransformerHandler:new() | ||
ResponseTransformerHandler.super.new(self, "response_transformer") | ||
end | ||
|
||
function ResponseTransformerHandler:header_filter(conf) | ||
ResponseTransformerHandler.super.header_filter(self) | ||
header_filter.execute(conf) | ||
end | ||
|
||
function ResponseTransformerHandler:body_filter(conf) | ||
ResponseTransformerHandler.super.body_filter(self) | ||
body_filter.execute(conf) | ||
end | ||
|
||
ResponseTransformerHandler.PRIORITY = 800 | ||
|
||
return ResponseTransformerHandler |
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,66 @@ | ||
local utils = require "kong.tools.utils" | ||
local stringy = require "stringy" | ||
|
||
local _M = {} | ||
|
||
local APPLICATION_JSON = "application/json" | ||
local CONTENT_TYPE = "content-type" | ||
local CONTENT_LENGTH = "content-length" | ||
|
||
local function get_content_type() | ||
local header_value = ngx.header[CONTENT_TYPE] | ||
if header_value then | ||
return stringy.strip(header_value):lower() | ||
end | ||
return nil | ||
end | ||
|
||
local function iterate_and_exec(val, cb) | ||
if utils.table_size(val) > 0 then | ||
for _, entry in ipairs(val) do | ||
local parts = stringy.split(entry, ":") | ||
cb(parts[1], utils.table_size(parts) == 2 and parts[2] or nil) | ||
end | ||
end | ||
end | ||
|
||
function _M.execute(conf) | ||
if not conf then return end | ||
|
||
local is_json_body = get_content_type() == APPLICATION_JSON | ||
|
||
if conf.add then | ||
|
||
-- Add headers | ||
if conf.add.headers then | ||
iterate_and_exec(conf.add.headers, function(name, value) | ||
ngx.header[name] = value | ||
end) | ||
end | ||
|
||
-- Removing the header because the body is going to change | ||
if conf.add.json and is_json_body then | ||
ngx.header[CONTENT_LENGTH] = nil | ||
end | ||
|
||
end | ||
|
||
if conf.remove then | ||
|
||
-- Remove headers | ||
if conf.remove.headers then | ||
iterate_and_exec(conf.remove.headers, function(name, value) | ||
ngx.header[name] = nil | ||
end) | ||
end | ||
|
||
-- Removing the header because the body is going to change | ||
if conf.remove.json and is_json_body then | ||
ngx.header[CONTENT_LENGTH] = nil | ||
end | ||
|
||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
return { | ||
add = { type = "table", schema = { | ||
json = { type = "array" }, | ||
headers = { type = "array" } | ||
} | ||
}, | ||
remove = { type = "table", schema = { | ||
json = { type = "array" }, | ||
headers = { type = "array" } | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -123,4 +123,5 @@ describe("Request Transformer", function() | |
end) | ||
|
||
end) | ||
|
||
end) |
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,84 @@ | ||
local spec_helper = require "spec.spec_helpers" | ||
local http_client = require "kong.tools.http_client" | ||
local cjson = require "cjson" | ||
|
||
local STUB_GET_URL = spec_helper.PROXY_URL.."/get" | ||
local STUB_HEADERS_URL = spec_helper.PROXY_URL.."/response-headers" | ||
local STUB_POST_URL = spec_helper.PROXY_URL.."/post" | ||
|
||
describe("Response Transformer Plugin #proxy", function() | ||
|
||
setup(function() | ||
spec_helper.prepare_db() | ||
spec_helper.insert_fixtures { | ||
api = { | ||
{ name = "tests response_transformer", public_dns = "response.com", target_url = "http://httpbin.org" }, | ||
}, | ||
plugin_configuration = { | ||
{ | ||
name = "response_transformer", | ||
value = { | ||
add = { | ||
headers = {"x-added:true", "x-added2:true" }, | ||
json = {"newjsonparam:newvalue"} | ||
}, | ||
remove = { | ||
headers = { "x-to-remove" }, | ||
json = { "origin" } | ||
} | ||
}, | ||
__api = 1 | ||
} | ||
} | ||
} | ||
|
||
spec_helper.start_kong() | ||
end) | ||
|
||
teardown(function() | ||
spec_helper.stop_kong() | ||
end) | ||
|
||
describe("Test adding parameters", function() | ||
|
||
it("should add new headers", function() | ||
local body, status, headers = http_client.get(STUB_GET_URL, {}, {host = "response.com"}) | ||
assert.are.equal(200, status) | ||
assert.are.equal("true", headers["x-added"]) | ||
assert.are.equal("true", headers["x-added2"]) | ||
end) | ||
|
||
it("should add new parameters on GET", function() | ||
local response, status, headers = http_client.get("http://127.0.0.1:8100/get", {}, {host = "response.com"}) | ||
assert.are.equal(200, status) | ||
local body = cjson.decode(response) | ||
assert.are.equal("newvalue", body["newjsonparam"]) | ||
end) | ||
|
||
it("should add new parameters on POST", function() | ||
local response, status, headers = http_client.post("http://127.0.0.1:8100/post", {}, {host = "response.com"}) | ||
assert.are.equal(200, status) | ||
local body = cjson.decode(response) | ||
assert.are.equal("newvalue", body["newjsonparam"]) | ||
end) | ||
|
||
end) | ||
|
||
describe("Test removing parameters", function() | ||
|
||
it("should remove a header", function() | ||
local _, status, headers = http_client.get(STUB_HEADERS_URL, { ["x-to-remove"] = "true"}, {host = "response.com"}) | ||
assert.are.equal(200, status) | ||
assert.falsy(headers["x-to-remove"]) | ||
end) | ||
|
||
it("should remove a parameter on GET", function() | ||
local response, status, headers = http_client.get("http://127.0.0.1:8100/get", {}, {host = "response.com"}) | ||
assert.are.equal(200, status) | ||
local body = cjson.decode(response) | ||
assert.falsy(body.origin) | ||
end) | ||
|
||
end) | ||
|
||
end) |
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