Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions apisix/plugins/grpc-web.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ local schema = {
properties = {
cors_allow_headers = {
description =
"multiple header use ',' to split. default: content-type,x-grpc-web,x-user-agent.",
"multiple header use ',' to split. default: content-type,x-grpc-web,x-user-agent.",
type = "string",
default = DEFAULT_CORS_ALLOW_HEADERS
}
Expand Down Expand Up @@ -134,20 +134,14 @@ function _M.access(conf, ctx)
-- set context variable encoding method
ctx.grpc_web_encoding = encoding

-- set grpc path
if not (ctx.curr_req_matched and ctx.curr_req_matched[":ext"]) then
core.log.error("routing configuration error, grpc-web plugin only supports ",
"`prefix matching` pattern routing")
return exit(ctx, 400)
end

local path = ctx.curr_req_matched[":ext"]
if path:byte(1) ~= core.string.byte("/") then
path = "/" .. path
if path and path ~= "" then
if path:byte(1) ~= core.string.byte("/") then
path = "/" .. path
end
req_set_uri(path)
end

req_set_uri(path)

-- set grpc body
local body, err = core.request.get_body()
if err or not body then
Expand Down
8 changes: 0 additions & 8 deletions docs/en/latest/plugins/grpc-web.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X P
}'
```

:::info IMPORTANT

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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove this note from zh doc as well


So, when absolute matching is used, the Plugin would not be hit and the information from the proto would not be extracted.

:::

## Example usage

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.
Expand Down
11 changes: 5 additions & 6 deletions t/plugin/grpc-web.t
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ request Content-Type: `application/json` invalid
content_by_lua_block {

local config = {
uri = "/grpc/web2/a6.RouteService/GetRoute",
uri = "/grpc/web/a6.RouteService/GetRoute",
upstream = {
scheme = "grpc",
type = "roundrobin",
Expand Down Expand Up @@ -168,15 +168,14 @@ passed

=== TEST 8: test route (absolute match)
--- request
POST /grpc/web2/a6.RouteService/GetRoute
POST /grpc/web/a6.RouteService/GetRoute
hello
--- more_headers
Content-Type: application/grpc-web
--- error_code: 400
--- error_code: 200
--- response_headers
Access-Control-Allow-Origin: *
Content-Type: text/html
--- error_log
routing configuration error, grpc-web plugin only supports `prefix matching` pattern routing
Content-Type: application/grpc-web



Expand Down
Loading