This is an automated email from the ASF dual-hosted git repository.
monkeydluffy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new ca808732f fix: graphql post request route matching exception (#10198)
ca808732f is described below
commit ca808732ff3c1746fd55e4b8e0fb2bd8f11915af
Author: Xin Rong <[email protected]>
AuthorDate: Thu Sep 14 13:50:17 2023 +0800
fix: graphql post request route matching exception (#10198)
---
apisix/http/route.lua | 5 +-
apisix/http/router/radixtree_host_uri.lua | 6 +-
apisix/http/router/radixtree_uri.lua | 4 +-
.../http/router/radixtree_uri_with_parameter.lua | 4 +-
t/cli/test_route_match_with_graphql.sh | 98 ++++++++++++++++++++++
5 files changed, 106 insertions(+), 11 deletions(-)
diff --git a/apisix/http/route.lua b/apisix/http/route.lua
index d475646b5..dbf11abf5 100644
--- a/apisix/http/route.lua
+++ b/apisix/http/route.lua
@@ -103,8 +103,8 @@ function _M.create_radixtree_uri_router(routes, uri_routes,
with_parameter)
end
-function _M.match_uri(uri_router, match_opts, api_ctx)
- core.table.clear(match_opts)
+function _M.match_uri(uri_router, api_ctx)
+ local match_opts = core.tablepool.fetch("route_match_opts", 0, 4)
match_opts.method = api_ctx.var.request_method
match_opts.host = api_ctx.var.host
match_opts.remote_addr = api_ctx.var.remote_addr
@@ -112,6 +112,7 @@ function _M.match_uri(uri_router, match_opts, api_ctx)
match_opts.matched = core.tablepool.fetch("matched_route_record", 0, 4)
local ok = uri_router:dispatch(api_ctx.var.uri, match_opts, api_ctx,
match_opts)
+ core.tablepool.release("route_match_opts", match_opts)
return ok
end
diff --git a/apisix/http/router/radixtree_host_uri.lua
b/apisix/http/router/radixtree_host_uri.lua
index 532576e53..680a04fbe 100644
--- a/apisix/http/router/radixtree_host_uri.lua
+++ b/apisix/http/router/radixtree_host_uri.lua
@@ -142,8 +142,6 @@ local function create_radixtree_router(routes)
return true
end
-
- local match_opts = {}
function _M.match(api_ctx)
local user_routes = _M.user_routes
local _, service_version = get_services()
@@ -162,7 +160,7 @@ end
function _M.matching(api_ctx)
core.log.info("route match mode: radixtree_host_uri")
- core.table.clear(match_opts)
+ local match_opts = core.tablepool.fetch("route_match_opts", 0, 16)
match_opts.method = api_ctx.var.request_method
match_opts.remote_addr = api_ctx.var.remote_addr
match_opts.vars = api_ctx.var
@@ -181,11 +179,13 @@ function _M.matching(api_ctx)
api_ctx.curr_req_matched._host =
api_ctx.real_curr_req_matched_host:reverse()
api_ctx.real_curr_req_matched_host = nil
end
+ core.tablepool.release("route_match_opts", match_opts)
return true
end
end
local ok = only_uri_router:dispatch(api_ctx.var.uri, match_opts, api_ctx,
match_opts)
+ core.tablepool.release("route_match_opts", match_opts)
return ok
end
diff --git a/apisix/http/router/radixtree_uri.lua
b/apisix/http/router/radixtree_uri.lua
index 6e546364a..7c1b5c0c1 100644
--- a/apisix/http/router/radixtree_uri.lua
+++ b/apisix/http/router/radixtree_uri.lua
@@ -27,7 +27,6 @@ local _M = {version = 0.2}
local uri_routes = {}
local uri_router
- local match_opts = {}
function _M.match(api_ctx)
local user_routes = _M.user_routes
local _, service_version = get_services()
@@ -51,8 +50,7 @@ end
function _M.matching(api_ctx)
core.log.info("route match mode: radixtree_uri")
-
- return base_router.match_uri(uri_router, match_opts, api_ctx)
+ return base_router.match_uri(uri_router, api_ctx)
end
diff --git a/apisix/http/router/radixtree_uri_with_parameter.lua
b/apisix/http/router/radixtree_uri_with_parameter.lua
index 4bf7f3ebe..3f10f4fca 100644
--- a/apisix/http/router/radixtree_uri_with_parameter.lua
+++ b/apisix/http/router/radixtree_uri_with_parameter.lua
@@ -27,7 +27,6 @@ local _M = {}
local uri_routes = {}
local uri_router
- local match_opts = {}
function _M.match(api_ctx)
local user_routes = _M.user_routes
local _, service_version = get_services()
@@ -51,8 +50,7 @@ end
function _M.matching(api_ctx)
core.log.info("route match mode: radixtree_uri_with_parameter")
-
- return base_router.match_uri(uri_router, match_opts, api_ctx)
+ return base_router.match_uri(uri_router, api_ctx)
end
diff --git a/t/cli/test_route_match_with_graphql.sh
b/t/cli/test_route_match_with_graphql.sh
new file mode 100755
index 000000000..c67027748
--- /dev/null
+++ b/t/cli/test_route_match_with_graphql.sh
@@ -0,0 +1,98 @@
+#!/usr/bin/env bash
+
+#
+# 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.
+#
+
+. ./t/cli/common.sh
+
+echo '
+deployment:
+ role: data_plane
+ role_data_plane:
+ config_provider: yaml
+
+apisix:
+ router:
+ http: radixtree_uri
+
+nginx_config:
+ worker_processes: 1
+
+' > conf/config.yaml
+
+echo '
+routes:
+ - uri: "/hello"
+ hosts:
+ - test.com
+ vars:
+ - - "graphql_name"
+ - "=="
+ - "createAccount"
+ priority: 30
+ id: "graphql1"
+ upstream_id: "invalid"
+
+ - uri: "/hello"
+ hosts:
+ - test.com
+ plugins:
+ echo:
+ body: "test server"
+ priority: 20
+ id: "graphql2"
+ upstream_id: "invalid"
+
+ - uri: "/hello"
+ hosts:
+ - test2.com
+ plugins:
+ echo:
+ body: "test2"
+ priority: 20
+ id: "graphql3"
+ upstream_id: "invalid"
+
+upstreams:
+ - nodes:
+ 127.0.0.1:1999: 1
+ id: "invalid"
+#END
+' > conf/apisix.yaml
+
+make run
+
+dd if=/dev/urandom of=tmp_data.json bs=300K count=1
+
+for i in {1..100}; do
+ curl -s http://127.0.0.1:9080/hello -H "Host: test.com" -H "Content-Type:
application/json" -X POST -d @tmp_data.json > /tmp/graphql_request1.txt &
+ curl -s http://127.0.0.1:9080/hello -H "Host: test2.com" -H "Content-Type:
application/json" -X POST -d @tmp_data.json > /tmp/graphql_request2.txt &
+
+ wait
+
+ if diff /tmp/graphql_request1.txt /tmp/graphql_request2.txt > /dev/null;
then
+ make stop
+ echo "failed: route match error in GraphQL requests, route should not
be the same"
+ exit 1
+ fi
+done
+
+make stop
+
+rm tmp_data.json /tmp/graphql_request1.txt /tmp/graphql_request2.txt
+
+echo "passed: GraphQL requests can be correctly matched to the route"