diff --git a/.travis.yml b/.travis.yml index 2222d3813c0d..95bc904c87ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ addons: - perl - etcd - luarocks + - cmake cache: directories: diff --git a/COPYRIGHT b/COPYRIGHT index 6edefda66bbb..e519faf64b32 100644 --- a/COPYRIGHT +++ b/COPYRIGHT @@ -46,3 +46,94 @@ https://github.com/knyar/nginx-lua-prometheus/blob/master/README.md Licensed under MIT license. %%%%%%%%% +rapidjson + +https://github.com/Tencent/rapidjson + +Tencent is pleased to support the open source community by making RapidJSON available. + +Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + +If you have downloaded a copy of the RapidJSON binary from Tencent, please note that the RapidJSON binary is licensed under the MIT License. +If you have downloaded a copy of the RapidJSON source code from Tencent, please note that RapidJSON source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of RapidJSON into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within RapidJSON. To avoid the problematic JSON license in your own projects, it's sufficient to exclude the bin/jsonchecker/ directory, as it's the only code under the JSON license. +A copy of the MIT License is included in this file. + +Other dependencies and licenses: + +Open Source Software Licensed Under the BSD License: +-------------------------------------------------------------------- + +The msinttypes r29 +Copyright (c) 2006-2013 Alexander Chemeris +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +* Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Open Source Software Licensed Under the JSON License: +-------------------------------------------------------------------- + +json.org +Copyright (c) 2002 JSON.org +All Rights Reserved. + +JSON_checker +Copyright (c) 2002 JSON.org +All Rights Reserved. + + +Terms of the JSON License: +--------------------------------------------------- + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +The Software shall be used for Good, not Evil. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +Terms of the MIT License: +-------------------------------------------------------------------- + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +%%%%%%%%% + +lua-rapidjson + +https://github.com/xpol/lua-rapidjson + +MIT License + +Copyright (C) 2015 Xpol Wan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +%%%%%%%%% diff --git a/apisix-0.4-0.rockspec b/apisix-0.4-0.rockspec index 075fceb817fc..c3a68f87afad 100644 --- a/apisix-0.4-0.rockspec +++ b/apisix-0.4-0.rockspec @@ -20,7 +20,8 @@ dependencies = { "lua-resty-etcd = 0.5", "lua-resty-balancer = 0.02rc5", "lua-resty-ngxvar = 0.2", - "lua-resty-jit-uuid = 0.0.7" + "lua-resty-jit-uuid = 0.0.7", + "rapidjson = 0.5.2-1", } build = { diff --git a/logs/placehold.txt b/logs/placehold.txt deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/lua/apisix/core.lua b/lua/apisix/core.lua index 7e1f65af0005..ecd293cbbb02 100644 --- a/lua/apisix/core.lua +++ b/lua/apisix/core.lua @@ -6,7 +6,6 @@ return { table = require("apisix.core.table"), request = require("apisix.core.request"), response = require("apisix.core.response"), - typeof = require("apisix.core.typeof"), lrucache = require("apisix.core.lrucache"), schema = require("apisix.core.schema"), ctx = require("apisix.core.ctx"), diff --git a/lua/apisix/core/schema.lua b/lua/apisix/core/schema.lua index fd79e579d301..5facc5b06a42 100644 --- a/lua/apisix/core/schema.lua +++ b/lua/apisix/core/schema.lua @@ -1,22 +1,24 @@ --- todo: change to https://github.com/iresty/ljsonschema - -local typeof = require("apisix.core.typeof") -local tostring = tostring -local pairs = pairs +local rapidjson_schema_validator = require('rapidjson').SchemaValidator +local rapidjson_schema_doc = require('rapidjson').SchemaDocument +local rapidjson_doc = require('rapidjson').Document local _M = {version = 0.1} +local schema_sd = {} -function _M.check_args(args, schema) - for k, v in pairs(schema) do - if not typeof[v](args[k]) then - return nil, "args." .. k .. " expect " .. v .. " value but got: [" - .. tostring(args[k]) .. "]" - end +-- You can follow this document to write schema: +-- https://github.com/Tencent/rapidjson/blob/master/bin/draft-04/schema +-- rapidjson not supported `format` in draft-04 yet +function _M.check_args(schema, json) + local sd = schema_sd[schema] + if not sd then + sd = rapidjson_schema_doc(schema) + schema_sd[schema] = sd end - - return true + local validator = rapidjson_schema_validator(sd) + local d = rapidjson_doc(json) + return validator:validate(d) end return _M diff --git a/lua/apisix/core/typeof.lua b/lua/apisix/core/typeof.lua deleted file mode 100644 index 7eac53909244..000000000000 --- a/lua/apisix/core/typeof.lua +++ /dev/null @@ -1,122 +0,0 @@ --- TODO: need to support more restriction rules - -local INFINITE_POS = math.huge -local INFINITE_NEG = -INFINITE_POS -local type = type -local floor = math.floor -local rawequal = rawequal - -local function typeof(cmp, arg) - return cmp == type(arg) -end - -local function typeofNil(...) - return typeof('nil', ...) -end - -local function typeofBoolean(...) - return typeof('boolean', ...) -end - -local function typeofString(...) - return typeof('string', ...) -end - -local function typeofNumber(...) - return typeof('number', ...) -end - -local function typeofFunction(...) - return typeof('function', ...) -end - -local function typeofTable(...) - return typeof('table', ...) -end - -local function typeofThread(...) - return typeof('thread', ...) -end - -local function typeofUserdata(...) - return typeof('userdata', ...) -end - -local function typeofFinite(arg) - return type(arg) == 'number' and (arg < INFINITE_POS and arg > INFINITE_NEG) -end - -local function typeofUnsigned(arg) - return type(arg) == 'number' and (arg < INFINITE_POS and arg >= 0) -end - -local function typeofInt(arg) - return typeofFinite(arg) and rawequal(floor(arg), arg) -end - -local function typeofInt8(arg) - return typeofInt(arg) and arg >= -128 and arg <= 127 -end - -local function typeofInt16(arg) - return typeofInt(arg) and arg >= -32768 and arg <= 32767 -end - -local function typeofInt32(arg) - return typeofInt(arg) and arg >= -2147483648 and arg <= 2147483647 -end - -local function typeofUInt(arg) - return typeofUnsigned(arg) and rawequal(floor(arg), arg) -end - -local function typeofUInt8(arg) - return typeofUInt(arg) and arg <= 255 -end - -local function typeofUInt16(arg) - return typeofUInt(arg) and arg <= 65535 -end - -local function typeofUInt32(arg) - return typeofUInt(arg) and arg <= 4294967295 -end - -local function typeofNaN(arg) - return arg ~= arg -end - -local function typeofNon(arg) - return arg == nil or arg == false or arg == 0 or arg == '' or arg ~= arg -end - - -local _M = { - version = 0.1, - ['nil'] = typeofNil, - ['boolean'] = typeofBoolean, - ['string'] = typeofString, - ['number'] = typeofNumber, - ['function'] = typeofFunction, - ['table'] = typeofTable, - ['thread'] = typeofThread, - ['userdata'] = typeofUserdata, - ['finite'] = typeofFinite, - ['unsigned'] = typeofUnsigned, - ['int'] = typeofInt, - ['int8'] = typeofInt8, - ['int16'] = typeofInt16, - ['int32'] = typeofInt32, - ['uint'] = typeofUInt, - ['uint8'] = typeofUInt8, - ['uint16'] = typeofUInt16, - ['uint32'] = typeofUInt32, - ['nan'] = typeofNaN, - ['non'] = typeofNon, - -- alias - ['Nil'] = typeofNil, - ['Function'] = typeofFunction -} - - -return _M diff --git a/lua/apisix/plugins/example-plugin.lua b/lua/apisix/plugins/example-plugin.lua index 577943e8a52e..1fe4cf9bbda9 100644 --- a/lua/apisix/plugins/example-plugin.lua +++ b/lua/apisix/plugins/example-plugin.lua @@ -1,11 +1,17 @@ local core = require("apisix.core") --- TODO: need a more powerful way to define the schema -local args_schema = { - i = "int", - s = "string", - t = "table", +-- You can follow this document to write schema: +-- https://github.com/Tencent/rapidjson/blob/master/bin/draft-04/schema +-- rapidjson not supported `format` in draft-04 yet +local schema = { + type = "object", + properties = { + i = {type = "number", minimum = 0}, + s = {type = "string"}, + t = {type = "array", minItems = 1}, + }, + required = {"i"} } @@ -19,7 +25,8 @@ local _M = { function _M.check_args(conf) - local ok, err = core.schema.check_args(conf, args_schema) + local ok, err = core.schema.check_args(schema, conf) + if not ok then return false, err end diff --git a/t/example-plugin.t b/t/example-plugin.t index c705735f385d..510eb33e906a 100644 --- a/t/example-plugin.t +++ b/t/example-plugin.t @@ -6,29 +6,98 @@ run_tests; __DATA__ -=== TEST 1: check arguments +=== TEST 1: sanity --- config location /t { content_by_lua_block { local plugin = require("apisix.plugins.example-plugin") - local ok, err = plugin.check_args({i = 1, s = "s", t = {}}) + local ok, err = plugin.check_args({i = 1, s = "s", t = {1}}) if not ok then - ngx.say("failed to check args: ", err) + ngx.say(err) end - ok, err = plugin.check_args({s = "s", t = {}}) + ngx.say("done") + } + } +--- request +GET /t +--- response_body +done + + + +=== TEST 2: missing args +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.example-plugin") + + local ok, err = plugin.check_args({s = "s", t = {1}}) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- request +GET /t +--- response_body +invalid "required" in docuement at pointer "#" +done + + + +=== TEST 3: small then minimum +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.example-plugin") + local ok, err = plugin.check_args({i = -1, s = "s", t = {1, 2}}) if not ok then - ngx.say("failed to check args: ", err) + ngx.say(err) end - ok, err = plugin.check_args({i = 1, s = 3, t = {}}) + ngx.say("done") + } + } +--- request +GET /t +--- response_body +invalid "minimum" in docuement at pointer "#/i" +done + + + +=== TEST 4: wrong type of string +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.example-plugin") + local ok, err = plugin.check_args({i = 1, s = 123, t = {1}}) if not ok then - ngx.say("failed to check args: ", err) + ngx.say(err) end - ok, err = plugin.check_args({i = 1, s = "s", t = ""}) + ngx.say("done") + } + } +--- request +GET /t +--- response_body +invalid "type" in docuement at pointer "#/s" +done + + + +=== TEST 5: the size of array < minItems +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.example-plugin") + local ok, err = plugin.check_args({i = 1, s = '123', t = {}}) if not ok then - ngx.say("failed to check args: ", err) + ngx.say(err) end ngx.say("done") @@ -37,14 +106,12 @@ __DATA__ --- request GET /t --- response_body -failed to check args: args.i expect int value but got: [nil] -failed to check args: args.s expect string value but got: [3] -failed to check args: args.t expect table value but got: [] +invalid "type" in docuement at pointer "#/t" done -=== TEST 2: load plugins +=== TEST 6: load plugins --- config location /t { content_by_lua_block { @@ -76,7 +143,7 @@ rewrite(): plugin rewrite phase -=== TEST 3: filter plugins +=== TEST 7: filter plugins --- config location /t { content_by_lua_block {