Skip to content

Commit 7b665be

Browse files
feat: allow grpc web in non prefix based routes (#12830)
1 parent c394310 commit 7b665be

File tree

3 files changed

+11
-26
lines changed

3 files changed

+11
-26
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

docs/en/latest/plugins/grpc-web.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X P
6868
}'
6969
```
7070

71-
:::info IMPORTANT
72-
73-
While using the `grpc-web` Plugin, always use a prefix matching pattern (`/*`, `/grpc/example/*`) for matching Routes. This is because the gRPC Web client passes the package name, the service interface name, the method name and other information in the proto in the URI. For example, `/path/a6.RouteService/Insert`.
74-
75-
So, when absolute matching is used, the Plugin would not be hit and the information from the proto would not be extracted.
76-
77-
:::
78-
7971
## Example usage
8072

8173
Refer to [gRPC-Web Client Runtime Library](https://www.npmjs.com/package/grpc-web) or [Apache APISIX gRPC Web Test Framework](https://github.com/apache/apisix/tree/master/t/plugin/grpc-web) to learn how to setup your web client.

t/plugin/grpc-web.t

Lines changed: 5 additions & 6 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,15 +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
178+
Content-Type: application/grpc-web
180179
181180
182181

0 commit comments

Comments
 (0)