Skip to content

Commit

Permalink
fix+style: fix utils.encode_args failing some tests
Browse files Browse the repository at this point in the history
Also fix a bunch of style issues.
  • Loading branch information
thibaultcha committed Dec 1, 2015
1 parent 6d467c4 commit 6274cc4
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 157 deletions.
3 changes: 1 addition & 2 deletions kong/core/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ return {
access = {
before = function()
ngx.ctx.KONG_ACCESS_START = ngx.now()
ngx.ctx.api, ngx.ctx.upstream_url, ngx.ctx.upstream_host = resolver.execute(ngx.var.request_uri, ngx.req.get_headers())
ngx.ctx.api, ngx.ctx.upstream_url, ngx.var.upstream_host = resolver.execute(ngx.var.request_uri, ngx.req.get_headers())
end,
-- Only executed if the `resolver` module found an API and allows nginx to proxy it.
after = function()
Expand All @@ -65,7 +65,6 @@ return {
-- Set the `$upstream_url` and `$upstream_host` variables for the `proxy_pass` nginx
-- directive in kong.yml.
ngx.var.upstream_url = upstream_url
ngx.var.upstream_host = ngx.ctx.upstream_host
end
},
header_filter = {
Expand Down
1 change: 1 addition & 0 deletions kong/core/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local cache = require "kong.tools.database_cache"
local stringy = require "stringy"
local constants = require "kong.constants"
local responses = require "kong.tools.responses"

local table_insert = table.insert
local string_match = string.match
local string_find = string.find
Expand Down
4 changes: 2 additions & 2 deletions kong/tools/http_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ local function with_body(method)
else
headers["content-type"] = "application/x-www-form-urlencoded"
if type(body) == "table" then
body = ngx.encode_args(body)
body = ngx.encode_args(body, true)
end
end

Expand All @@ -75,7 +75,7 @@ local function without_body(method)
if not headers then headers = {} end

if querystring then
url = string.format("%s?%s", url, ngx.encode_args(querystring))
url = string.format("%s?%s", url, ngx.encode_args(querystring, true))
end

return http_call {
Expand Down
35 changes: 2 additions & 33 deletions kong/tools/ngx_stub.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
-- Monkeypatches the global `ngx` table.

local reg = require "rex_pcre"
local utils = require "kong.tools.utils"

-- DICT Proxy
-- https://github.com/bsm/fakengx/blob/master/fakengx.lua
Expand Down Expand Up @@ -125,37 +126,5 @@ _G.ngx = {
encode_base64 = function(str)
return string.format("base64_%s", str)
end,
-- Builds a querystring from a table, separated by `&`
-- @param `tab` The key/value parameters
-- @param `key` The parent key if the value is multi-dimensional (optional)
-- @return `querystring` A string representing the built querystring
encode_args = function(tab, key)
local query = {}
local keys = {}

for k in pairs(tab) do
keys[#keys+1] = k
end

table.sort(keys)

for _, name in ipairs(keys) do
local value = tab[name]
if key then
name = string.format("%s[%s]", tostring(key), tostring(name))
end
if type(value) == "table" then
query[#query+1] = ngx.encode_args(value, name)
else
value = tostring(value)
if value ~= "" then
query[#query+1] = string.format("%s=%s", name, value)
else
query[#query+1] = name
end
end
end

return table.concat(query, "&")
end
encode_args = utils.encode_args
}
26 changes: 19 additions & 7 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ function _M.random_string()
return uuid():gsub("-", "")
end

local function encode_args_value(key, value)
key = url.escape(key)
--- URL escape and format key and value
-- An obligatory url.unescape pass must be done to prevent double-encoding
-- already encoded values (which contain a '%' character that `url.escape` escapes)
local function encode_args_value(key, value, raw)
if not raw then
key = url.unescape(key)
key = url.escape(key)
end
if value ~= nil then
return string_format("%s=%s", key, url.escape(value))
if not raw then
value = url.unescape(value)
value = url.escape(value)
end
return string_format("%s=%s", key, value)
else
return key
end
Expand All @@ -42,7 +52,7 @@ end
-- @see https://github.com/Mashape/kong/issues/749
-- @param[type=table] args A key/value table containing the query args to encode
-- @treturn string A valid querystring (without the prefixing '?')
function _M.encode_args(args)
function _M.encode_args(args, raw)
local query = {}
local keys = {}

Expand All @@ -56,14 +66,16 @@ function _M.encode_args(args)
local value = args[key]
if type(value) == "table" then
for _, sub_value in ipairs(value) do
query[#query+1] = encode_args_value(key, sub_value)
query[#query+1] = encode_args_value(key, sub_value, raw)
end
elseif value == true then
query[#query+1] = encode_args_value(key)
query[#query+1] = encode_args_value(key, nil, raw)
elseif value ~= false and value ~= nil then
value = tostring(value)
if value ~= "" then
query[#query+1] = encode_args_value(key, value)
query[#query+1] = encode_args_value(key, value, raw)
elseif raw then
query[#query+1] = key
end
end
end
Expand Down
Loading

0 comments on commit 6274cc4

Please sign in to comment.