Skip to content

Commit 817f68c

Browse files
feat: allow grpc web in non prefix based routes
Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
1 parent 614c44c commit 817f68c

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

apisix/plugins/grpc-web.lua

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ local schema = {
4343
properties = {
4444
cors_allow_headers = {
4545
description =
46-
"multiple header use ',' to split. default: content-type,x-grpc-web,x-user-agent.",
46+
"multiple header use ',' to split. default: content-type,x-grpc-web,x-user-agent.",
4747
type = "string",
4848
default = DEFAULT_CORS_ALLOW_HEADERS
4949
}
@@ -134,20 +134,14 @@ function _M.access(conf, ctx)
134134
-- set context variable encoding method
135135
ctx.grpc_web_encoding = encoding
136136

137-
-- set grpc path
138-
if not (ctx.curr_req_matched and ctx.curr_req_matched[":ext"]) then
139-
core.log.error("routing configuration error, grpc-web plugin only supports ",
140-
"`prefix matching` pattern routing")
141-
return exit(ctx, 400)
142-
end
143-
144137
local path = ctx.curr_req_matched[":ext"]
145-
if path:byte(1) ~= core.string.byte("/") then
146-
path = "/" .. path
138+
if path and path ~= "" then
139+
if path:byte(1) ~= core.string.byte("/") then
140+
path = "/" .. path
141+
end
142+
req_set_uri(path)
147143
end
148144

149-
req_set_uri(path)
150-
151145
-- set grpc body
152146
local body, err = core.request.get_body()
153147
if err or not body then

t/plugin/grpc-web.t

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ request Content-Type: `application/json` invalid
137137
content_by_lua_block {
138138
139139
local config = {
140-
uri = "/grpc/web2/a6.RouteService/GetRoute",
140+
uri = "/grpc/web/a6.RouteService/GetRoute",
141141
upstream = {
142142
scheme = "grpc",
143143
type = "roundrobin",
@@ -168,16 +168,14 @@ passed
168168
169169
=== TEST 8: test route (absolute match)
170170
--- request
171-
POST /grpc/web2/a6.RouteService/GetRoute
171+
POST /grpc/web/a6.RouteService/GetRoute
172+
hello
172173
--- more_headers
173174
Content-Type: application/grpc-web
174-
--- error_code: 400
175+
--- error_code: 200
175176
--- response_headers
176177
Access-Control-Allow-Origin: *
177-
Content-Type: text/html
178-
--- error_log
179-
routing configuration error, grpc-web plugin only supports `prefix matching` pattern routing
180-
178+
Content-Type: application/grpc-web
181179
182180
183181
=== TEST 9: set route (with cors plugin)

0 commit comments

Comments
 (0)