Skip to content
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

bugfix: should not save the runtime data of plugin into etcd. #1910

Merged
merged 7 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions apisix/plugins/echo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ local schema = {
auth_value = {
description = "auth value",
type = "string"
}
},
},
anyOf = {
{required = {"before_body"}},
{required = {"body"}},
{required = {"after_body"}}
},
minProperties = 1
minProperties = 1,
additionalProperties = false,
}

local plugin_name = "echo"
Expand All @@ -62,27 +63,17 @@ local _M = {
schema = schema,
}

function _M.check_schema(conf)
if conf.headers then
conf.headers_arr = {}

for field, value in pairs(conf.headers) do
if type(field) == 'string'
and (type(value) == 'string' or type(value) == 'number') then
if #field == 0 then
return false, 'invalid field length in header'
end
core.table.insert(conf.headers_arr, field)
core.table.insert(conf.headers_arr, value)
else
return false, 'invalid type as header value'
end
end
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end

return core.schema.check(schema, conf)
return true
end


function _M.body_filter(conf, ctx)
if conf.body then
ngx.arg[1] = conf.body
Expand All @@ -98,6 +89,7 @@ function _M.body_filter(conf, ctx)
ngx.arg[2] = true
end


function _M.access(conf, ctx)
local value = core.request.header(ctx, "Authorization")

Expand All @@ -107,13 +99,33 @@ function _M.access(conf, ctx)

end


function _M.header_filter(conf, ctx)
if conf.headers_arr then
local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
ngx.header[conf.headers_arr[i]] = conf.headers_arr[i+1]
if not conf.headers then
return
end

if not conf.headers_arr then
conf.headers_arr = {}

for field, value in pairs(conf.headers) do
if type(field) == 'string'
and (type(value) == 'string' or type(value) == 'number') then
if #field == 0 then
return false, 'invalid field length in header'
end
core.table.insert(conf.headers_arr, field)
core.table.insert(conf.headers_arr, value)
else
return false, 'invalid type as header value'
end
end
end

local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
ngx.header[conf.headers_arr[i]] = conf.headers_arr[i+1]
end
end

return _M
70 changes: 43 additions & 27 deletions apisix/plugins/proxy-rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ local schema = {
},
},
minProperties = 1,
additionalProperties = false,
}


Expand All @@ -88,32 +89,35 @@ function _M.check_schema(conf)
end
end

--reform header from object into array, so can avoid use pairs, which is NYI
if conf.headers then
conf.headers_arr = {}
-- check headers
if not conf.headers then
return true
end

for field, value in pairs(conf.headers) do
if type(field) == 'string'
and (type(value) == 'string' or type(value) == 'number') then
if #field == 0 then
return false, 'invalid field length in header'
end

core.log.info("header field: ", field)

if not core.utils.validate_header_field(field) then
return false, 'invalid field character in header'
end
if not core.utils.validate_header_value(value) then
return false, 'invalid value character in header'
end
core.table.insert(conf.headers_arr, field)
core.table.insert(conf.headers_arr, value)
else
return false, 'invalid type as header value'
end
for field, value in pairs(conf.headers) do
if type(field) ~= 'string' then
return false, 'invalid type as header field'
end

if type(value) ~= 'string' and type(value) ~= 'number' then
return false, 'invalid type as header value'
end

if #field == 0 then
return false, 'invalid field length in header'
end

core.log.info("header field: ", field)

if not core.utils.validate_header_field(field) then
return false, 'invalid field character in header'
end

if not core.utils.validate_header_value(value) then
return false, 'invalid value character in header'
end
end

return true
end

Expand Down Expand Up @@ -172,12 +176,24 @@ function _M.rewrite(conf, ctx)
ctx.var.upstream_uri = upstream_uri
end

if conf.headers_arr then
local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
ngx.req.set_header(conf.headers_arr[i], conf.headers_arr[i+1])
if not conf.headers then
return
end

-- reform header from object into array, so can avoid use pairs,
-- which is NYI
if not conf.headers_arr then
conf.headers_arr = {}

for field, value in pairs(conf.headers) do
core.table.insert_tail(conf.headers_arr, field, value)
end
end

local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
ngx.req.set_header(conf.headers_arr[i], conf.headers_arr[i+1])
end
end

end -- do
Expand Down
39 changes: 24 additions & 15 deletions apisix/plugins/response-rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ local schema = {
}
},
minProperties = 1,
additionalProperties = false,
}


Expand All @@ -63,22 +64,19 @@ function _M.check_schema(conf)
return false, err
end

--reform header from object into array, so can avoid use pairs, which is NYI
if conf.headers then
conf.headers_arr = {}

for field, value in pairs(conf.headers) do
if type(field) == 'string'
and (type(value) == 'string' or type(value) == 'number') then
if #field == 0 then
return false, 'invalid field length in header'
end
core.table.insert(conf.headers_arr, field)
core.table.insert(conf.headers_arr, value)
else
if type(field) ~= 'string' then
return false, 'invalid type as header field'
end

if type(value) ~= 'string' and type(value) ~= 'number' then
return false, 'invalid type as header value'
end

if #field == 0 then
return false, 'invalid field length in header'
end
end
end

Expand Down Expand Up @@ -119,12 +117,23 @@ function _M.header_filter(conf, ctx)
ngx.header.content_encoding = nil
end

if conf.headers_arr then
local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
ngx.header[conf.headers_arr[i]] = conf.headers_arr[i+1]
if not conf.headers then
return
end

--reform header from object into array, so can avoid use pairs, which is NYI
if not conf.headers_arr then
conf.headers_arr = {}

for field, value in pairs(conf.headers) do
core.table.insert_tail(conf.headers_arr, field, value)
end
end

local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
ngx.header[conf.headers_arr[i]] = conf.headers_arr[i+1]
end
end

end -- do
Expand Down
104 changes: 104 additions & 0 deletions t/lib/json_sort.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

local type = type
local concat = table.concat
local tostring = tostring
local ngx_null = ngx.null
local gsub = string.gsub
local sort = table.sort
local pairs = pairs
local ipairs = ipairs


local _M = {}


local meta_chars = {
["\t"] = "\\t",
["\\"] = "\\\\",
['"'] = '\\"',
["\r"] = "\\r",
["\n"] = "\\n",
}


local function encode_str(s)
return gsub(s, '["\\\r\n\t]', meta_chars)
end


local function is_arr(t)
local exp = 1
for k, _ in pairs(t) do
if k ~= exp then
return nil
end
exp = exp + 1
end
return exp - 1
end


local encode
function encode (v)
if v == nil or v == ngx_null then
return "null"
end

local typ = type(v)
if typ == 'string' then
return '"' .. encode_str(v) .. '"'
end

if typ == 'number' or typ == 'boolean' then
return tostring(v)
end

if typ == 'table' then
local n = is_arr(v)
if n then
local bits = {}
for i, elem in ipairs(v) do
bits[i] = encode(elem)
end
return "[" .. concat(bits, ",") .. "]"
end

local keys = {}
local i = 0
for key, _ in pairs(v) do
i = i + 1
keys[i] = key
end
sort(keys)

local bits = {}
i = 0
for _, key in ipairs(keys) do
i = i + 1
bits[i] = encode(key) .. ":" .. encode(v[key])
end
return "{" .. concat(bits, ",") .. "}"
end

return '"<' .. typ .. '>"'
end
_M.encode = encode


return _M
Loading