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

fix: graphql post request route matching exception #10198

Merged
merged 6 commits into from
Sep 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions apisix/http/route.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,16 @@ 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
match_opts.vars = api_ctx.var
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

Expand Down
6 changes: 3 additions & 3 deletions apisix/http/router/radixtree_host_uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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

Expand Down
4 changes: 1 addition & 3 deletions apisix/http/router/radixtree_uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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


Expand Down
4 changes: 1 addition & 3 deletions apisix/http/router/radixtree_uri_with_parameter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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


Expand Down
98 changes: 98 additions & 0 deletions t/cli/test_route_match_with_graphql.sh
Original file line number Diff line number Diff line change
@@ -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"