From d8b0c2d04c59e8f70d848ea404c372ca047984cc Mon Sep 17 00:00:00 2001 From: YuanSheng Wang Date: Wed, 23 Oct 2019 12:59:22 +0800 Subject: [PATCH] change: removed router r3. (#725) --- .travis.yml | 1 - .travis/linux_runner.sh | 1 - Makefile | 11 - conf/config.yaml | 6 +- doc/architecture-design-cn.md | 6 +- doc/architecture-design.md | 6 +- doc/dev-manual-cn.md | 1 - doc/dev-manual.md | 1 - doc/doc-index.md | 1 - doc/install-dependencies.md | 2 +- doc/router-r3.md | 55 ---- lua/apisix/http/router/r3_host_uri.lua | 159 --------- lua/apisix/http/router/r3_sni.lua | 145 --------- lua/apisix/http/router/r3_uri.lua | 109 ------- lua/apisix/router.lua | 4 +- t/node/sanity-r3.t | 149 --------- t/router/r3-host-uri.t | 268 --------------- t/router/r3-sni.t | 434 ------------------------- t/router/radixtree-uri-host.t | 1 - t/router/radixtree-uri-multiple.t | 1 - t/router/radixtree-uri-sanity.t | 1 - 21 files changed, 8 insertions(+), 1354 deletions(-) delete mode 100644 doc/router-r3.md delete mode 100644 lua/apisix/http/router/r3_host_uri.lua delete mode 100644 lua/apisix/http/router/r3_sni.lua delete mode 100644 lua/apisix/http/router/r3_uri.lua delete mode 100644 t/node/sanity-r3.t delete mode 100644 t/router/r3-host-uri.t delete mode 100644 t/router/r3-sni.t diff --git a/.travis.yml b/.travis.yml index c7d55d78a996..46cd11822195 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,6 @@ addons: - cpanminus - build-essential - libncurses5-dev - - libpcre3-dev - libreadline-dev - libssl-dev - perl diff --git a/.travis/linux_runner.sh b/.travis/linux_runner.sh index ae7716012432..57aa4ff09cdb 100755 --- a/.travis/linux_runner.sh +++ b/.travis/linux_runner.sh @@ -18,7 +18,6 @@ export_or_prefix() { create_lua_deps() { WITHOUT_DASHBOARD=1 sudo luarocks make --lua-dir=${OPENRESTY_PREFIX}/luajit rockspec/apisix-master-0.rockspec --tree=deps --only-deps --local - sudo luarocks install --lua-dir=${OPENRESTY_PREFIX}/luajit lua-resty-libr3 --tree=deps --local echo "Create lua deps cache" sudo rm -rf build-cache/deps sudo cp -r deps build-cache/ diff --git a/Makefile b/Makefile index c8cd1c56b7ee..df636a08e842 100644 --- a/Makefile +++ b/Makefile @@ -37,17 +37,6 @@ ifeq ($(lj-releng-exist), not_exist) chmod a+x utils/lj-releng endif -### dev_r3: Create a development ENV for r3 -.PHONY: dev_r3 -dev_r3: -ifeq ($(UNAME),Darwin) - luarocks install --lua-dir=$(LUA_JIT_DIR) lua-resty-libr3 --tree=deps --local -else ifneq ($(LUAROCKS_VER),'luarocks 3.') - luarocks install lua-resty-libr3 --tree=deps --local -else - luarocks install --lua-dir=/usr/local/openresty/luajit lua-resty-libr3 --tree=deps --local -endif - ### check: Check Lua source code .PHONY: check diff --git a/conf/config.yaml b/conf/config.yaml index 13a5200b1e28..d49f93a90938 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -17,11 +17,7 @@ apisix: router: http: 'radixtree_uri' # radixtree_uri: match route by uri(base on radixtree) # radixtree_host_uri: match route by host + uri(base on radixtree) - # r3_uri: match route by uri(base on r3) - # r3_host_uri: match route by host + uri(base on r3) - - ssl: 'radixtree_sni' # r3_sni: match route by SNI(base on r3) - # radixtree_sni: match route by SNI(base on radixtree) + ssl: 'radixtree_sni' # radixtree_sni: match route by SNI(base on radixtree) # stream_proxy: # TCP/UDP proxy # tcp: # TCP proxy port list # - 9100 diff --git a/doc/architecture-design-cn.md b/doc/architecture-design-cn.md index 6adff64844ac..a746e6cf5281 100644 --- a/doc/architecture-design-cn.md +++ b/doc/architecture-design-cn.md @@ -328,17 +328,15 @@ APISIX 区别于其他 API 网关的一大特点是允许用户选择不同 Rout 在本地配置 `conf/config.yaml` 中设置最符合自身业务需求的路由。 * `apisix.router.http`: HTTP 请求路由。 - * `radixtree_uri`: (默认)只使用 `uri` 作为主索引。基于 `radix tree` 引擎,支持全量和深前缀匹配,更多见 [如何使用 router-radixtree](router-radixtree.md)。 + * `radixtree_uri`: (默认)只使用 `uri` 作为主索引。基于 `radixtree` 引擎,支持全量和深前缀匹配,更多见 [如何使用 router-radixtree](router-radixtree.md)。 * `绝对匹配`:完整匹配给定的 `uri` ,比如 `/foo/bar`,`/foo/glo`。 * `前缀匹配`:末尾使用 `*` 代表给定的 `uri` 是前缀匹配。比如 `/foo*`,则允许匹配 `/foo/`、`/foo/a`和`/foo/b`等。 * `匹配优先级`:优先尝试绝对匹配,若无法命中绝对匹配,再尝试前缀匹配。 * `任意过滤属性`:允许指定任何 Ningx 内置变量作为过滤条件,比如 uri 请求参数、请求头、cookie 等。 - * `r3_uri`: 只使用 `uri` 作为主索引(基于 r3 引擎)。基于 `r3` 的 trie tree 是支持正则匹配的,比如 `/foo/{:\w+}/{:\w+}`,更多见 [如何使用 router-r3](router-r3.md)。 - * `r3_host_uri`: 使用 `host + uri` 作为主索引(基于 r3 引擎),对当前请求会同时匹配 host 和 uri。 + * `radixtree_host_uri`: 使用 `host + uri` 作为主索引(基于 `radixtree` 引擎),对当前请求会同时匹配 host 和 uri,支持的匹配条件与 `radixtree_uri` 基本一致。 * `apisix.router.ssl`: SSL 加载匹配路由。 * `radixtree_sni`: (默认)使用 `SNI` (Server Name Indication) 作为主索引(基于 radixtree 引擎)。 - * `r3_sni`: 使用 `SNI` (Server Name Indication) 作为主索引(基于 r3 引擎)。 [返回目录](#目录) diff --git a/doc/architecture-design.md b/doc/architecture-design.md index cc1f2ff718a8..02c0fdf7ee6d 100644 --- a/doc/architecture-design.md +++ b/doc/architecture-design.md @@ -322,17 +322,15 @@ A distinguishing feature of APISIX from other API gateways is that it allows use Set the route that best suits your business needs in the local configuration `conf/config.yaml`. * `apisix.router.http`: HTTP Request Route。 - * `radixtree_uri`: (Default) only use `uri` as the primary index. Support for full and deep prefix matching based on the `radix tree` engine, see [How to use router-radixtree](router-radixtree.md). + * `radixtree_uri`: (Default) only use `uri` as the primary index. Support for full and deep prefix matching based on the `radixtree` engine, see [How to use router-radixtree](router-radixtree.md). * `Absolute match `: Complete match for the given `uri` , such as `/foo/bar`,`/foo/glo`. * `Prefix match`: Use `*` at the end to represent the given `uri` as a prefix match. For example, `/foo*` allows matching `/foo/`, `/foo/a` and `/foo/b`. * `match priority`: first try absolute match, if you can't hit absolute match, try prefix match. * `Any filter attribute`: Allows you to specify any Ningx built-in variable as a filter, such as uri request parameters, request headers, cookies, and so on. - * `r3_uri`: Use only `uri` as the primary index (based on the r3 engine). The trie tree based on `r3` supports regular matching, such as `/foo/{:\w+}/{:\w+}`, see [How to use router-r3](router-r3.md). - * `r3_host_uri`: Use `host + uri` as the primary index (based on the r3 engine), matching both host and uri for the current request. + * `radixtree_host_uri`: Use `host + uri` as the primary index (based on the `radixtree` engine), matching both host and uri for the current request. * `apisix.router.ssl`: SSL loads the matching route. * `radixtree_sni`: (Default) Use `SNI` (Server Name Indication) as the primary index (based on the radixtree engine). - * `r3_sni`: Use `SNI` (Server Name Indication) as the primary index (based on the r3 engine). [Back to top](#Table-of-contents) diff --git a/doc/dev-manual-cn.md b/doc/dev-manual-cn.md index 348af7b5db18..bdf3d6bd8e2d 100644 --- a/doc/dev-manual-cn.md +++ b/doc/dev-manual-cn.md @@ -83,7 +83,6 @@ Makefile rules: help: Show Makefile rules. dev: Create a development ENV - dev_r3: Create a development ENV for r3 check: Check Lua source code init: Initialize the runtime environment run: Start the apisix server diff --git a/doc/dev-manual.md b/doc/dev-manual.md index 20250255e52d..eca173c1dbba 100644 --- a/doc/dev-manual.md +++ b/doc/dev-manual.md @@ -83,7 +83,6 @@ Makefile rules: help: Show Makefile rules. dev: Create a development ENV - dev_r3: Create a development ENV for r3 check: Check Lua source code init: Initialize the runtime environment run: Start the apisix server diff --git a/doc/doc-index.md b/doc/doc-index.md index 893e418e7ce2..fc58590b1566 100644 --- a/doc/doc-index.md +++ b/doc/doc-index.md @@ -10,7 +10,6 @@ Reference document * [Health Check](health-check.md): Enable health check on the upstream node, and will automatically filter unhealthy nodes during load balancing to ensure system stability. * Router * [radixtree](router-radixtree.md) - * [r3](router-r3.md) * [Stand Alone Model](stand-alone.md): Supports to load route rules from local yaml file, it is more friendly such as under the kubernetes(k8s). * [Stream Proxy](stream-proxy.md) * [Admin API](admin-api-cn.md) diff --git a/doc/install-dependencies.md b/doc/install-dependencies.md index 706afe7a63f5..e9b0ecf36038 100644 --- a/doc/install-dependencies.md +++ b/doc/install-dependencies.md @@ -94,7 +94,7 @@ CentOS 6 TODO -Failed to compile `rapidjson`, and the compilation of `libr3` relies on later versions of `autoconf` and `pcre`, but the CentOS 6 comes with a lower version, will support CentOS 6 later. +Failed to compile `rapidjson`, but the CentOS 6 comes with a lower version, will support CentOS 6 later. Mac OSX ======= diff --git a/doc/router-r3.md b/doc/router-r3.md deleted file mode 100644 index e32408323e8f..000000000000 --- a/doc/router-r3.md +++ /dev/null @@ -1,55 +0,0 @@ -# libr3 - -### what's libr3? -[libr3](https://github.com/c9s/r3) is a high-performance path dispatching library. It compiles your route paths into a prefix tree (trie). - -APISIX using [lua-resty-libr3](https://github.com/iresty/lua-resty-libr3) as route dispatching library. - -### How to use libr3 in APISIX? -libr3 supports PCRE(Perl Compatible Regular Expressions), so you can use route flexibly. - -Let's take a look at a few examples and have an intuitive understanding. - -1. default regular expression - -`/blog/post/{id}` - -there is not regular expression included, and `[^/]+` will be the default -regular expression of `id`. - -So `/blog/post/{id}` is equivalent to `/blog/post/{id:[^/]+}`. - -2. match all uris - -`/{:.*}` - -`/` matches root uri, and `.*` matches any character (except for line terminators). - -`:` means is an anonymous match, for example the uri is `/blog/post/1`, the libr3 will return `[/blog/post/1]` if pattern is `/{:.*}`, and return `{"uri":"/blog/post/1"}` if pattern is `/{uri:.*}`. - -3. match number - -`/blog/post/{id:\d+}` - -for example the uri is `/blog/post/1`, libr3 will return `{"id":"1"}`. - -4. match characters - -`/blog/post/{name:\w+}` - -for example the uri is `/blog/post/foo`, libr3 will return `{"name":"foo"}`. - -5. match multiple uri segments - -`/blog/post/{name:\w+}/{id:\d+}` - -for example the uri is `/blog/post/foo/12`, libr3 will return `{"name":"foo", "id":"12"}`. - -`/blog/post/{:\w+}/{id:\d+}` - -for example the uri is `/blog/post/foo/12`, libr3 will return `{"1":"foo", "id":"12"}`. - -### How to filter route by Nginx builtin variable -libr3 can not support Nginx builtin variable, http headers and uri args now, -you can use [radixtree](route-radixtree.md) instead. - diff --git a/lua/apisix/http/router/r3_host_uri.lua b/lua/apisix/http/router/r3_host_uri.lua deleted file mode 100644 index 3c4b67b2bfcb..000000000000 --- a/lua/apisix/http/router/r3_host_uri.lua +++ /dev/null @@ -1,159 +0,0 @@ --- Copyright (C) Yuansheng Wang - -local require = require -local r3router = require("resty.r3") -local core = require("apisix.core") -local plugin = require("apisix.plugin") -local ipairs = ipairs -local type = type -local error = error -local str_reverse = string.reverse -local user_routes -local cached_version - - -local _M = {version = 0.2} - - local only_uri_routes = {} - local only_uri_router -local function create_only_uri_router() - local routes = plugin.api_routes() - - for _, route in ipairs(routes) do - if type(route) == "table" then - core.table.insert(only_uri_routes, { - path = route.uri, - handler = route.handler, - method = route.methods, - }) - end - end - - only_uri_router = r3router.new(only_uri_routes) - only_uri_router:compile() - return true -end - - - local host_uri_routes = {} - local host_uri_router -local function push_valid_route(route) - if type(route) ~= "table" then - return - end - - local host = route.value.host - if not host then - core.table.insert(only_uri_routes, { - path = route.value.uri, - method = route.value.methods, - remote_addr = route.value.remote_addr, - handler = function (params, api_ctx) - --[[ - If you need to get the parameters, you need to replace the first parameter - nil of dispatch2 with an empty table and open the following comment, but - this will affect performance. - --]] - -- api_ctx.matched_params = params - api_ctx.matched_route = route - end - }) - return - end - - host = str_reverse(host) - if host:sub(#host) == "*" then - host = host:sub(1, #host - 1) .. "{host_prefix}" - end - - core.log.info("route rule: ", host .. route.value.uri) - core.table.insert(host_uri_routes, { - path = "/" .. host .. route.value.uri, - method = route.value.methods, - remote_addr = route.value.remote_addr, - handler = function (params, api_ctx) - --[[ - If you need to get the parameters, you need to replace the first parameter - nil of dispatch2 with an empty table and open the following comment, but - this will affect performance. - --]] - -- api_ctx.matched_params = params - api_ctx.matched_route = route - end - }) - - return -end - -local function create_r3_router(routes) - core.table.clear(only_uri_routes) - core.table.clear(host_uri_routes) - - for _, route in ipairs(routes or {}) do - push_valid_route(route) - end - - create_only_uri_router() - - core.log.info("route items: ", - core.json.delay_encode(host_uri_routes, true)) - host_uri_router = r3router.new(host_uri_routes) - host_uri_router:compile() -end - - - local match_opts = {} -function _M.match(api_ctx) - if not cached_version or cached_version ~= user_routes.conf_version then - create_r3_router(user_routes.values) - cached_version = user_routes.conf_version - end - - if not host_uri_router then - core.log.error("failed to fetch valid `host+uri` router: ") - return core.response.exit(404) - end - - core.table.clear(match_opts) - match_opts.method = api_ctx.var.method - match_opts.remote_addr = api_ctx.var.remote_addr - - local host_uri = "/" .. str_reverse(api_ctx.var.host) .. api_ctx.var.uri - local ok = host_uri_router:dispatch2(nil, host_uri, match_opts, api_ctx) - if ok then - return true - end - - ok = only_uri_router:dispatch2(nil, api_ctx.var.uri, match_opts, api_ctx) - if ok then - return true - end - - core.log.info("not find any matched route") - return core.response.exit(404) -end - - -function _M.routes() - if not user_routes then - return nil, nil - end - - return user_routes.values, user_routes.conf_version -end - - -function _M.init_worker(filter) - local err - user_routes, err = core.config.new("/routes", { - automatic = true, - item_schema = core.schema.route, - filter = filter, - }) - if not user_routes then - error("failed to create etcd instance for fetching /routes : " .. err) - end -end - - -return _M diff --git a/lua/apisix/http/router/r3_sni.lua b/lua/apisix/http/router/r3_sni.lua deleted file mode 100644 index 569381e33bc4..000000000000 --- a/lua/apisix/http/router/r3_sni.lua +++ /dev/null @@ -1,145 +0,0 @@ --- Copyright (C) Yuansheng Wang - -local get_request = require("resty.core.base").get_request -local r3router = require("resty.r3") -local core = require("apisix.core") -local ngx_ssl = require("ngx.ssl") -local ffi = require("ffi") -local errmsg = ffi.new("char *[1]") -local C = ffi.C -local ipairs = ipairs -local type = type -local error = error -local ffi_string = ffi.string -local ssl - - -ffi.cdef[[ -int ngx_http_lua_ffi_cert_pem_to_der(const unsigned char *pem, - size_t pem_len, unsigned char *der, char **err); -int ngx_http_lua_ffi_ssl_set_der_certificate(void *r, - const char *data, size_t len, char **err); -]] - - -local _M = { - version = 0.1, - server_name = ngx_ssl.server_name, -} - - - local empty_tab = {} - local route_items -local function create_r3_router(ssl_items) - local ssl_items = ssl_items or empty_tab - - route_items = core.table.new(#ssl_items, 0) - local idx = 0 - - for _, ssl in ipairs(ssl_items) do - if type(ssl) == "table" then - local sni = ssl.value.sni:reverse() - if sni:sub(#sni) == "*" then - sni = sni:sub(1, #sni - 1) .. "{prefix:.*}" - end - - idx = idx + 1 - route_items[idx] = { - path = sni, - handler = function (params, api_ctx) - api_ctx.matched_ssl = ssl - end - } - end - end - - core.log.info("route items: ", core.json.delay_encode(route_items, true)) - local r3 = r3router.new(route_items) - r3:compile() - return r3 -end - - -local function set_pem_ssl_key(cert, pkey) - local r = get_request() - if r == nil then - return false, "no request found" - end - - ngx_ssl.clear_certs() - - local out = ffi.new("char [?]", #cert) - local rc = C.ngx_http_lua_ffi_cert_pem_to_der(cert, #cert, out, errmsg) - if rc < 1 then - return false, "failed to parse PEM cert: " .. ffi_string(errmsg[0]) - end - - local cert_der = ffi_string(out, rc) - local rc = C.ngx_http_lua_ffi_ssl_set_der_certificate(r, cert_der, - #cert_der, errmsg) - if rc ~= 0 then - return false, "failed to set DER cert: " .. ffi_string(errmsg[0]) - end - - out = ffi.new("char [?]", #pkey) - local rc = C.ngx_http_lua_ffi_priv_key_pem_to_der(pkey, #pkey, out, errmsg) - if rc < 1 then - return false, "failed to parse PEM priv key: " .. ffi_string(errmsg[0]) - end - - local pkey_der = ffi_string(out, rc) - - local rc = C.ngx_http_lua_ffi_ssl_set_der_private_key(r, pkey_der, - #pkey_der, errmsg) - if rc ~= 0 then - return false, "failed to set DER priv key: " .. ffi_string(errmsg[0]) - end - - return true -end - - -function _M.match_and_set(api_ctx) - local r3, err = core.lrucache.global("/ssl", ssl.conf_version, - create_r3_router, ssl.values) - if not r3 then - return false, "failed to fetch ssl router: " .. err - end - - local sni - sni, err = ngx_ssl.server_name() - if type(sni) ~= "string" then - return false, "failed to fetch SNI: " .. (err or "not found") - end - - core.log.debug("sni: ", sni) - local ok = r3:dispatch2(nil, sni:reverse(), nil, api_ctx) - if not ok then - core.log.warn("not found any valid sni configuration") - return false - end - - local matched_ssl = api_ctx.matched_ssl - core.log.info("debug: ", core.json.delay_encode(matched_ssl, true)) - ok, err = set_pem_ssl_key(matched_ssl.value.cert, matched_ssl.value.key) - if not ok then - return false, err - end - - return true -end - - -function _M.init_worker() - local err - ssl, err = core.config.new("/ssl", { - automatic = true, - item_schema = core.schema.ssl - }) - if not ssl then - error("failed to create etcd instance for fetching ssl: " .. err) - end -end - - -return _M diff --git a/lua/apisix/http/router/r3_uri.lua b/lua/apisix/http/router/r3_uri.lua deleted file mode 100644 index 38370539fe6f..000000000000 --- a/lua/apisix/http/router/r3_uri.lua +++ /dev/null @@ -1,109 +0,0 @@ --- Copyright (C) Yuansheng Wang - -local require = require -local r3router = require("resty.r3") -local core = require("apisix.core") -local plugin = require("apisix.plugin") -local ipairs = ipairs -local type = type -local error = error -local user_routes -local cached_version - - -local _M = {version = 0.1} - - - local uri_routes = {} - local uri_router -local function create_r3_router(routes) - routes = routes or {} - - local api_routes = plugin.api_routes() - core.table.clear(uri_routes) - - for _, route in ipairs(api_routes) do - if type(route) == "table" then - core.table.insert(uri_routes, { - path = route.uri, - handler = route.handler, - method = route.methods, - }) - end - end - - for _, route in ipairs(routes) do - if type(route) == "table" then - core.table.insert(uri_routes, { - path = route.value.uri, - method = route.value.methods, - host = route.value.host, - handler = function (params, api_ctx) - --[[ - If you need to get the parameters, you need to replace the first parameter - nil of dispatch2 with an empty table and open the following comment, but - this will affect performance. - --]] - -- api_ctx.matched_params = params - api_ctx.matched_route = route - end - }) - end - end - - core.log.info("route items: ", core.json.delay_encode(uri_routes, true)) - uri_router = r3router.new(uri_routes) - uri_router:compile() -end - - - local match_opts = {} -function _M.match(api_ctx) - if not cached_version or cached_version ~= user_routes.conf_version then - create_r3_router(user_routes.values) - cached_version = user_routes.conf_version - end - - if not uri_router then - core.log.error("failed to fetch valid `uri` router: ") - return core.response.exit(404) - end - - core.table.clear(match_opts) - match_opts.method = api_ctx.var.method - match_opts.host = api_ctx.var.host - match_opts.remote_addr = api_ctx.var.remote_addr - - local ok = uri_router:dispatch2(nil, api_ctx.var.uri, match_opts, api_ctx) - if not ok then - core.log.info("not find any matched route") - return core.response.exit(404) - end - - return true -end - - -function _M.routes() - if not user_routes then - return nil, nil - end - - return user_routes.values, user_routes.conf_version -end - - -function _M.init_worker(filter) - local err - user_routes, err = core.config.new("/routes", { - automatic = true, - item_schema = core.schema.route, - filter = filter, - }) - if not user_routes then - error("failed to create etcd instance for fetching /routes : " .. err) - end -end - - -return _M diff --git a/lua/apisix/router.lua b/lua/apisix/router.lua index 1ee584c0872b..e281820615fb 100644 --- a/lua/apisix/router.lua +++ b/lua/apisix/router.lua @@ -32,8 +32,8 @@ end function _M.http_init_worker() local conf = core.config.local_conf() - local router_http_name = "r3_uri" - local router_ssl_name = "r3_sni" + local router_http_name = "radixtree_uri" + local router_ssl_name = "radixtree_sni" if conf and conf.apisix and conf.apisix.router then router_http_name = conf.apisix.router.http or router_http_name diff --git a/t/node/sanity-r3.t b/t/node/sanity-r3.t deleted file mode 100644 index 0e9ba9626d6e..000000000000 --- a/t/node/sanity-r3.t +++ /dev/null @@ -1,149 +0,0 @@ -use t::APISIX 'no_plan'; - -repeat_each(1); -log_level('info'); -worker_connections(256); -no_root_location(); -no_shuffle(); - -sub read_file($) { - my $infile = shift; - open my $in, $infile - or die "cannot open $infile for reading: $!"; - my $cert = do { local $/; <$in> }; - close $in; - $cert; -} - -our $yaml_config = read_file("conf/config.yaml"); -$yaml_config =~ s/node_listen: 9080/node_listen: 1984/; -$yaml_config =~ s/enable_heartbeat: true/enable_heartbeat: false/; -$yaml_config =~ s/http: 'radixtree_uri'/ssl: 'r3_uri'/; - -run_tests(); - -__DATA__ - -=== TEST 1: set route(id: 1) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "methods": ["GET"], - "uri": "/hello", - "upstream": { - "nodes": { - "127.0.0.1:1980": 1 - }, - "type": "roundrobin" - } - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 2: /not_found ---- request -GET /not_found ---- yaml_config eval: $::yaml_config ---- error_code: 404 ---- response_body eval -qr/404 Not Found/ ---- no_error_log -[error] - - - -=== TEST 3: hit routes ---- request -GET /hello ---- yaml_config eval: $::yaml_config ---- response_body -hello world ---- no_error_log -[error] - - - -=== TEST 4: set route(id: 1) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "upstream": { - "nodes": { - "127.0.0.1:1980": 1 - }, - "type": "roundrobin" - }, - "uri": "/hello{:1?}" - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 5: hit routes:/hello ---- request -GET /hello ---- yaml_config eval: $::yaml_config ---- response_body -hello world ---- no_error_log -[error] - - - -=== TEST 6: hit routes: /hello1 ---- request -GET /hello1 ---- yaml_config eval: $::yaml_config ---- response_body -hello1 world ---- no_error_log -[error] - - - -=== TEST 7: hit routes: /hello2 ---- request -GET /hello2 ---- yaml_config eval: $::yaml_config ---- error_code: 404 ---- response_body eval -qr/404 Not Found/ ---- no_error_log -[error] diff --git a/t/router/r3-host-uri.t b/t/router/r3-host-uri.t deleted file mode 100644 index 573aa90b3b69..000000000000 --- a/t/router/r3-host-uri.t +++ /dev/null @@ -1,268 +0,0 @@ -use t::APISIX 'no_plan'; - -repeat_each(1); -log_level('info'); -worker_connections(256); -no_root_location(); -no_shuffle(); - -sub read_file($) { - my $infile = shift; - open my $in, $infile - or die "cannot open $infile for reading: $!"; - my $cert = do { local $/; <$in> }; - close $in; - $cert; -} - -our $yaml_config = read_file("conf/config.yaml"); -$yaml_config =~ s/node_listen: 9080/node_listen: 1984/; -$yaml_config =~ s/enable_heartbeat: true/enable_heartbeat: false/; -$yaml_config =~ s/http: 'radixtree_uri'/http: 'r3_host_uri'/; - -run_tests(); - -__DATA__ - -=== TEST 1: set route(id: 1) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "methods": ["GET"], - "upstream": { - "nodes": { - "127.0.0.1:1980": 1 - }, - "type": "roundrobin" - }, - "host": "foo.com", - "uri": "/hello" - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- yaml_config eval: $::yaml_config ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 2: /not_found ---- request -GET /not_found ---- yaml_config eval: $::yaml_config ---- error_code: 404 ---- response_body eval -qr/404 Not Found/ ---- no_error_log -[error] - - - -=== TEST 3: /not_found ---- request -GET /hello ---- yaml_config eval: $::yaml_config ---- error_code: 404 ---- response_body eval -qr/404 Not Found/ ---- no_error_log -[error] - - - -=== TEST 4: /not_found ---- request -GET /hello ---- yaml_config eval: $::yaml_config ---- more_headers -Host: not_found.com ---- error_code: 404 ---- response_body eval -qr/404 Not Found/ ---- no_error_log -[error] - - - -=== TEST 5: hit routes ---- request -GET /hello ---- yaml_config eval: $::yaml_config ---- more_headers -Host: foo.com ---- response_body -hello world ---- no_error_log -[error] - - - -=== TEST 6: hit routes ---- request -GET /hello ---- yaml_config eval: $::yaml_config ---- more_headers -Host: foo.com ---- response_body -hello world ---- error_log -moc.oof/hello - - - -=== TEST 7: set route(id: 1) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "methods": ["GET"], - "upstream": { - "nodes": { - "127.0.0.1:1981": 1 - }, - "type": "roundrobin" - }, - "uri": "/server_port" - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- yaml_config eval: $::yaml_config ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 8: /not_found ---- request -GET /hello ---- yaml_config eval: $::yaml_config ---- error_code: 404 ---- response_body eval -qr/404 Not Found/ ---- no_error_log -[error] - - - -=== TEST 9: hit routes ---- request -GET /server_port ---- yaml_config eval: $::yaml_config ---- more_headers -Host: anydomain.com ---- response_body_like eval -qr/1981/ ---- no_error_log -[error] - - - -=== TEST 10: set route(id: 2) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/2', - ngx.HTTP_PUT, - [[{ - "methods": ["GET"], - "upstream": { - "nodes": { - "127.0.0.1:1981": 1 - }, - "type": "roundrobin" - }, - "uri": "/hello" - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- yaml_config eval: $::yaml_config ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 11: /not_found ---- request -GET /hello2 ---- yaml_config eval: $::yaml_config ---- error_code: 404 ---- response_body eval -qr/404 Not Found/ ---- no_error_log -[error] - - - -=== TEST 12: hit routes ---- request -GET /hello ---- yaml_config eval: $::yaml_config ---- more_headers -Host: anydomain.com ---- response_body -hello world ---- no_error_log -[error] - - - -=== TEST 13: delete route(id: 2) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/2', - ngx.HTTP_DELETE - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- yaml_config eval: $::yaml_config ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] diff --git a/t/router/r3-sni.t b/t/router/r3-sni.t deleted file mode 100644 index d30c56182cd7..000000000000 --- a/t/router/r3-sni.t +++ /dev/null @@ -1,434 +0,0 @@ -use t::APISIX 'no_plan'; - -log_level('debug'); -no_root_location(); - -$ENV{TEST_NGINX_HTML_DIR} ||= html_dir(); - -sub read_file($) { - my $infile = shift; - open my $in, $infile - or die "cannot open $infile for reading: $!"; - my $cert = do { local $/; <$in> }; - close $in; - $cert; -} - -our $yaml_config = read_file("conf/config.yaml"); -$yaml_config =~ s/node_listen: 9080/node_listen: 1984/; -$yaml_config =~ s/enable_heartbeat: true/enable_heartbeat: false/; -$yaml_config =~ s/ssl: 'radixtree_sni'/ssl: 'r3_sni'/; - -run_tests; - -__DATA__ - -=== TEST 1: set ssl(sni: www.test.com) ---- config -location /t { - content_by_lua_block { - local core = require("apisix.core") - local t = require("lib.test_admin") - - local ssl_cert = t.read_file("conf/cert/apisix.crt") - local ssl_key = t.read_file("conf/cert/apisix.key") - local data = {cert = ssl_cert, key = ssl_key, sni = "www.test.com"} - - local code, body = t.test('/apisix/admin/ssl/1', - ngx.HTTP_PUT, - core.json.encode(data), - [[{ - "node": { - "value": { - "sni": "www.test.com" - }, - "key": "/apisix/ssl/1" - }, - "action": "set" - }]] - ) - - ngx.status = code - ngx.say(body) - } -} ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 2: set route(id: 1) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "upstream": { - "nodes": { - "127.0.0.1:1980": 1 - }, - "type": "roundrobin" - }, - "uri": "/hello" - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 3: client request ---- config -location /t { - content_by_lua_block { - -- etcd sync - ngx.sleep(0.2) - - do - local sock = ngx.socket.tcp() - - sock:settimeout(2000) - - local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock") - if not ok then - ngx.say("failed to connect: ", err) - return - end - - ngx.say("connected: ", ok) - - local sess, err = sock:sslhandshake(nil, "www.test.com", true) - if not sess then - ngx.say("failed to do SSL handshake: ", err) - return - end - - ngx.say("ssl handshake: ", type(sess)) - - local req = "GET /hello HTTP/1.0\r\nHost: www.test.com\r\nConnection: close\r\n\r\n" - local bytes, err = sock:send(req) - if not bytes then - ngx.say("failed to send http request: ", err) - return - end - - ngx.say("sent http request: ", bytes, " bytes.") - - while true do - local line, err = sock:receive() - if not line then - -- ngx.say("failed to receive response status line: ", err) - break - end - - ngx.say("received: ", line) - end - - local ok, err = sock:close() - ngx.say("close: ", ok, " ", err) - end -- do - -- collectgarbage() - } -} ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body eval -qr{connected: 1 -ssl handshake: userdata -sent http request: 62 bytes. -received: HTTP/1.1 200 OK -received: Content-Type: text/plain -received: Connection: close -received: Server: openresty -received: \nreceived: hello world -close: 1 nil} ---- error_log -lua ssl server name: "www.test.com" ---- no_error_log -[error] -[alert] - - - -=== TEST 4: client request(no cert domain) ---- config -location /t { - content_by_lua_block { - -- etcd sync - ngx.sleep(0.2) - - do - local sock = ngx.socket.tcp() - - sock:settimeout(2000) - - local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock") - if not ok then - ngx.say("failed to connect: ", err) - return - end - - ngx.say("connected: ", ok) - - local sess, err = sock:sslhandshake(nil, "no-cert.com", true) - if not sess then - ngx.say("failed to do SSL handshake: ", err) - return - end - end - } -} ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body -connected: 1 -failed to do SSL handshake: certificate host mismatch ---- error_log -not found any valid sni configuration - - - -=== TEST 5: set ssl(sni: wildcard) ---- config -location /t { - content_by_lua_block { - local core = require("apisix.core") - local t = require("lib.test_admin") - - local ssl_cert = t.read_file("conf/cert/apisix.crt") - local ssl_key = t.read_file("conf/cert/apisix.key") - local data = {cert = ssl_cert, key = ssl_key, sni = "*.test.com"} - - local code, body = t.test('/apisix/admin/ssl/1', - ngx.HTTP_PUT, - core.json.encode(data), - [[{ - "node": { - "value": { - "sni": "*.test.com" - }, - "key": "/apisix/ssl/1" - }, - "action": "set" - }]] - ) - - ngx.status = code - ngx.say(body) - } -} ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 6: client request ---- config -location /t { - content_by_lua_block { - -- etcd sync - ngx.sleep(0.2) - - do - local sock = ngx.socket.tcp() - - sock:settimeout(2000) - - local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock") - if not ok then - ngx.say("failed to connect: ", err) - return - end - - ngx.say("connected: ", ok) - - local sess, err = sock:sslhandshake(nil, "www.test.com", false) - if not sess then - ngx.say("failed to do SSL handshake: ", err) - return - end - - ngx.say("ssl handshake: ", type(sess)) - - local req = "GET /hello HTTP/1.0\r\nHost: www.test.com\r\nConnection: close\r\n\r\n" - local bytes, err = sock:send(req) - if not bytes then - ngx.say("failed to send http request: ", err) - return - end - - ngx.say("sent http request: ", bytes, " bytes.") - - while true do - local line, err = sock:receive() - if not line then - -- ngx.say("failed to receive response status line: ", err) - break - end - - ngx.say("received: ", line) - end - - local ok, err = sock:close() - ngx.say("close: ", ok, " ", err) - end -- do - -- collectgarbage() - } -} ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body eval -qr{connected: 1 -ssl handshake: userdata -sent http request: 62 bytes. -received: HTTP/1.1 200 OK -received: Content-Type: text/plain -received: Connection: close -received: Server: openresty -received: \nreceived: hello world -close: 1 nil} ---- error_log -lua ssl server name: "www.test.com" ---- no_error_log -[error] -[alert] - - - -=== TEST 7: set ssl(sni: test.com) ---- config -location /t { - content_by_lua_block { - local core = require("apisix.core") - local t = require("lib.test_admin") - - local ssl_cert = t.read_file("conf/cert/apisix.crt") - local ssl_key = t.read_file("conf/cert/apisix.key") - local data = {cert = ssl_cert, key = ssl_key, sni = "test.com"} - - local code, body = t.test('/apisix/admin/ssl/1', - ngx.HTTP_PUT, - core.json.encode(data), - [[{ - "node": { - "value": { - "sni": "test.com" - }, - "key": "/apisix/ssl/1" - }, - "action": "set" - }]] - ) - - ngx.status = code - ngx.say(body) - } -} ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 8: client request ---- config -location /t { - content_by_lua_block { - -- etcd sync - ngx.sleep(0.2) - - do - local sock = ngx.socket.tcp() - - sock:settimeout(2000) - - local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock") - if not ok then - ngx.say("failed to connect: ", err) - return - end - - ngx.say("connected: ", ok) - - local sess, err = sock:sslhandshake(nil, "test.com", false) - if not sess then - ngx.say("failed to do SSL handshake: ", err) - return - end - - ngx.say("ssl handshake: ", type(sess)) - - local req = "GET /hello HTTP/1.0\r\nHost: test.com\r\nConnection: close\r\n\r\n" - local bytes, err = sock:send(req) - if not bytes then - ngx.say("failed to send http request: ", err) - return - end - - ngx.say("sent http request: ", bytes, " bytes.") - - while true do - local line, err = sock:receive() - if not line then - -- ngx.say("failed to receive response status line: ", err) - break - end - - ngx.say("received: ", line) - end - - local ok, err = sock:close() - ngx.say("close: ", ok, " ", err) - end -- do - -- collectgarbage() - } -} ---- request -GET /t ---- yaml_config eval: $::yaml_config ---- response_body eval -qr{connected: 1 -ssl handshake: userdata -sent http request: 58 bytes. -received: HTTP/1.1 200 OK -received: Content-Type: text/plain -received: Connection: close -received: Server: openresty -received: \nreceived: hello world -close: 1 nil} ---- error_log -lua ssl server name: "test.com" ---- no_error_log -[error] -[alert] diff --git a/t/router/radixtree-uri-host.t b/t/router/radixtree-uri-host.t index 95f4943f8c61..eddfa7f2874e 100644 --- a/t/router/radixtree-uri-host.t +++ b/t/router/radixtree-uri-host.t @@ -18,7 +18,6 @@ sub read_file($) { our $yaml_config = read_file("conf/config.yaml"); $yaml_config =~ s/node_listen: 9080/node_listen: 1984/; $yaml_config =~ s/enable_heartbeat: true/enable_heartbeat: false/; -$yaml_config =~ s/http: 'r3_uri'/http: 'radixtree_uri'/; run_tests(); diff --git a/t/router/radixtree-uri-multiple.t b/t/router/radixtree-uri-multiple.t index b150056b6b42..a45ec32fff18 100644 --- a/t/router/radixtree-uri-multiple.t +++ b/t/router/radixtree-uri-multiple.t @@ -18,7 +18,6 @@ sub read_file($) { our $yaml_config = read_file("conf/config.yaml"); $yaml_config =~ s/node_listen: 9080/node_listen: 1984/; $yaml_config =~ s/enable_heartbeat: true/enable_heartbeat: false/; -$yaml_config =~ s/http: 'r3_uri'/http: 'radixtree_uri'/; run_tests(); diff --git a/t/router/radixtree-uri-sanity.t b/t/router/radixtree-uri-sanity.t index 004d55435786..e2ffe2fe22f4 100644 --- a/t/router/radixtree-uri-sanity.t +++ b/t/router/radixtree-uri-sanity.t @@ -18,7 +18,6 @@ sub read_file($) { our $yaml_config = read_file("conf/config.yaml"); $yaml_config =~ s/node_listen: 9080/node_listen: 1984/; $yaml_config =~ s/enable_heartbeat: true/enable_heartbeat: false/; -$yaml_config =~ s/http: 'r3_uri'/http: 'radixtree_uri'/; run_tests();