spacewander commented on a change in pull request #5964:
URL: https://github.com/apache/apisix/pull/5964#discussion_r779240279



##########
File path: apisix/plugins/grpc-web.lua
##########
@@ -0,0 +1,146 @@
+--
+-- 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.
+
+local ngx = ngx
+local ngx_arg = ngx.arg
+local core = require("apisix.core")
+local req_set_uri = ngx.req.set_uri
+local decode_base64 = ngx.decode_base64
+local encode_base64 = ngx.encode_base64
+
+
+local ALLOW_METHOD_OPTIONS = "OPTIONS"
+local ALLOW_METHOD_POST = "POST"
+local CONTENT_ENCODING_BASE64 = "base64"
+local CONTENT_ENCODING_BINARY = "binary"
+local DEFAULT_CORS_CONTENT_TYPE = "application/grpc-web-text+proto"
+local DEFAULT_CORS_ALLOW_ORIGIN = "*"
+local DEFAULT_CORS_ALLOW_METHODS = ALLOW_METHOD_POST
+local DEFAULT_CORS_ALLOW_HEADERS = "content-type,x-grpc-web,x-user-agent"
+local DEFAULT_PROXY_CONTENT_TYPE = "application/grpc"
+
+
+local plugin_name = "grpc-web"
+
+local schema = {
+    type = "object",
+    properties = {},
+}
+
+local grpc_web_content_encoding = {
+    ["application/grpc-web"] = CONTENT_ENCODING_BINARY,
+    ["application/grpc-web-text"] = CONTENT_ENCODING_BASE64,
+    ["application/grpc-web+proto"] = CONTENT_ENCODING_BINARY,
+    ["application/grpc-web-text+proto"] = CONTENT_ENCODING_BASE64,
+}
+
+local _M = {
+    version = 0.1,
+    priority = 505,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+function _M.access(conf, ctx)
+    local method = core.request.get_method()
+    if method == ALLOW_METHOD_OPTIONS then
+        return 204
+    end
+
+    if method ~= ALLOW_METHOD_POST then
+        -- 
https://github.com/grpc/grpc-web/blob/master/doc/browser-features.md#cors-support
+        core.log.error("request method: `", method, "` invalid")
+        return 400
+    end
+
+    local mimetype = core.request.header(ctx, "Content-Type")
+    local encoding = grpc_web_content_encoding[mimetype]
+    if not encoding then
+        core.log.error("request Content-Type: `", mimetype, "` invalid")
+        return 400
+    end
+
+    -- set grpc path
+    if not (ctx.curr_req_matched and ctx.curr_req_matched[":ext"]) then
+        core.log.error("please use matching pattern for routing URI, for 
example: /* or /grpc/*")
+        return 400
+    end
+
+    local path = ctx.curr_req_matched[":ext"]
+    if path:byte(1) ~= core.string.byte("/") then
+        path = "/" .. path
+    end
+
+    req_set_uri(path)
+
+    -- set grpc body
+    local body, err = core.request.get_body()
+    if err then
+        core.log.error("reading body err, ", err)
+        return 400
+    end
+
+    if body and encoding == CONTENT_ENCODING_BASE64 then
+        body = decode_base64(body)
+    end
+
+    local ok
+    ok, err = core.request.set_raw_body(body)

Review comment:
       We don't need to make things so complex. We just read the body in 
core.request.get_body

##########
File path: apisix/plugins/grpc-web.lua
##########
@@ -0,0 +1,146 @@
+--
+-- 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.
+
+local ngx = ngx
+local ngx_arg = ngx.arg
+local core = require("apisix.core")
+local req_set_uri = ngx.req.set_uri
+local decode_base64 = ngx.decode_base64
+local encode_base64 = ngx.encode_base64
+
+
+local ALLOW_METHOD_OPTIONS = "OPTIONS"
+local ALLOW_METHOD_POST = "POST"
+local CONTENT_ENCODING_BASE64 = "base64"
+local CONTENT_ENCODING_BINARY = "binary"
+local DEFAULT_CORS_CONTENT_TYPE = "application/grpc-web-text+proto"
+local DEFAULT_CORS_ALLOW_ORIGIN = "*"
+local DEFAULT_CORS_ALLOW_METHODS = ALLOW_METHOD_POST
+local DEFAULT_CORS_ALLOW_HEADERS = "content-type,x-grpc-web,x-user-agent"
+local DEFAULT_PROXY_CONTENT_TYPE = "application/grpc"
+
+
+local plugin_name = "grpc-web"
+
+local schema = {
+    type = "object",
+    properties = {},
+}
+
+local grpc_web_content_encoding = {
+    ["application/grpc-web"] = CONTENT_ENCODING_BINARY,
+    ["application/grpc-web-text"] = CONTENT_ENCODING_BASE64,
+    ["application/grpc-web+proto"] = CONTENT_ENCODING_BINARY,
+    ["application/grpc-web-text+proto"] = CONTENT_ENCODING_BASE64,
+}
+
+local _M = {
+    version = 0.1,
+    priority = 505,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+function _M.access(conf, ctx)
+    local method = core.request.get_method()
+    if method == ALLOW_METHOD_OPTIONS then
+        return 204
+    end
+
+    if method ~= ALLOW_METHOD_POST then
+        -- 
https://github.com/grpc/grpc-web/blob/master/doc/browser-features.md#cors-support
+        core.log.error("request method: `", method, "` invalid")
+        return 400
+    end
+
+    local mimetype = core.request.header(ctx, "Content-Type")
+    local encoding = grpc_web_content_encoding[mimetype]
+    if not encoding then
+        core.log.error("request Content-Type: `", mimetype, "` invalid")
+        return 400
+    end
+
+    -- set grpc path
+    if not (ctx.curr_req_matched and ctx.curr_req_matched[":ext"]) then
+        core.log.error("please use matching pattern for routing URI, for 
example: /* or /grpc/*")
+        return 400
+    end
+
+    local path = ctx.curr_req_matched[":ext"]
+    if path:byte(1) ~= core.string.byte("/") then
+        path = "/" .. path
+    end
+
+    req_set_uri(path)
+
+    -- set grpc body
+    local body, err = core.request.get_body()
+    if err then
+        core.log.error("reading body err, ", err)
+        return 400
+    end
+
+    if body and encoding == CONTENT_ENCODING_BASE64 then
+        body = decode_base64(body)
+    end
+
+    local ok
+    ok, err = core.request.set_raw_body(body)
+    if not ok then
+        core.log.error("setting body err, ", err)
+        return 400
+    end
+
+    -- set grpc content-type
+    core.request.set_header(ctx, "Content-Type", DEFAULT_PROXY_CONTENT_TYPE)
+
+    -- set context variable
+    ctx.grpc_web_mime = mimetype
+    ctx.grpc_web_encoding = encoding
+end
+
+function _M.header_filter(conf, ctx)
+    local method = core.request.get_method()
+    if method == ALLOW_METHOD_OPTIONS then
+        core.response.set_header("Access-Control-Allow-Methods", 
DEFAULT_CORS_ALLOW_METHODS)
+        core.response.set_header("Access-Control-Allow-Headers", 
DEFAULT_CORS_ALLOW_HEADERS)
+    end
+    core.response.set_header("Access-Control-Allow-Origin", 
DEFAULT_CORS_ALLOW_ORIGIN)
+    core.response.set_header("Content-Type", ctx.grpc_web_mime or 
DEFAULT_CORS_CONTENT_TYPE)
+end
+
+function _M.body_filter(conf, ctx)
+    -- If the gRPC-Web standard MIME extension type is not obtained or
+    -- the `POST` method is not used for interaction according to the gRPC-Web 
specification,
+    -- the request body processing will be ignored
+    -- 
https://github.com/grpc/grpc-web/blob/master/doc/browser-features.md#cors-support
+    local method = core.request.get_method()
+    if not ctx.grpc_web_mime or method ~= ALLOW_METHOD_POST then

Review comment:
       If the grpc_web_mime is set, the method must be POST?

##########
File path: docs/en/latest/plugins/grpc-web.md
##########
@@ -0,0 +1,85 @@
+---
+title: grpc-web
+---
+
+<!--
+#
+# 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.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+The `grpc-web` plugin is a proxy plugin used to process [gRPC 
Web](https://github.com/grpc/grpc-web) client requests to `gRPC Server`.
+
+gRPC Web Client -> APISIX -> gRPC server
+
+## How To Enable
+
+To enable the `gRPC Web` proxy plugin, routing must use the `Prefix matching` 
pattern (for example: `/*` or `/grpc/example/*`),
+Because the `gRPC-Web` client will pass the `package name`, `service interface 
name`, `method name` and other information declared in the `proto` in the URI 
(for example: `/path/a6.RouteService/Insert`) ,

Review comment:
       ```suggestion
   Because the `gRPC Web` client will pass the `package name`, `service 
interface name`, `method name` and other information declared in the `proto` in 
the URI (for example: `/path/a6.RouteService/Insert`) ,
   ```

##########
File path: t/plugin/grpc-web.t
##########
@@ -0,0 +1,135 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route (default grpc web proxy route)
+--- config
+    location /t {
+        content_by_lua_block {
+
+            local config = {
+                uri = "/grpc/*",
+                upstream = {
+                    scheme = "grpc",
+                    type = "roundrobin",
+                    nodes = {
+                        ["127.0.0.1:50001"] = 1
+                    }
+                },
+                plugins = {
+                    ["grpc-web"] = {}
+                }
+            }
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, 
config)
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 2: Flush all data through APISIX gRPC-Web Proxy
+--- exec
+node ./t/plugin/grpc-web/client.js FLUSH
+--- response_body
+[]
+
+
+
+=== TEST 3: Insert first data through APISIX gRPC-Web Proxy
+--- exec
+node ./t/plugin/grpc-web/client.js POST 1 route01 path01
+--- response_body
+[["1",{"name":"route01","path":"path01"}]]
+
+
+
+=== TEST 4: Update data through APISIX gRPC-Web Proxy
+--- exec
+node ./t/plugin/grpc-web/client.js PUT 1 route01 hello
+--- response_body
+[["1",{"name":"route01","path":"hello"}]]
+
+
+
+=== TEST 5: Insert second data through APISIX gRPC-Web Proxy
+--- exec
+node ./t/plugin/grpc-web/client.js POST 2 route02 path02
+--- response_body
+[["1",{"name":"route01","path":"hello"}],["2",{"name":"route02","path":"path02"}]]
+
+
+
+=== TEST 6: Insert third data through APISIX gRPC-Web Proxy
+--- exec
+node ./t/plugin/grpc-web/client.js POST 3 route03 path03
+--- response_body
+[["1",{"name":"route01","path":"hello"}],["2",{"name":"route02","path":"path02"}],["3",{"name":"route03","path":"path03"}]]
+
+
+
+=== TEST 7: Delete first data through APISIX gRPC-Web Proxy
+--- exec
+node ./t/plugin/grpc-web/client.js DEL 1
+--- response_body
+[["2",{"name":"route02","path":"path02"}],["3",{"name":"route03","path":"path03"}]]
+
+
+
+=== TEST 8: Get second data through APISIX gRPC-Web Proxy
+--- exec
+node ./t/plugin/grpc-web/client.js GET 2
+--- response_body
+{"name":"route02","path":"path02"}
+
+
+
+=== TEST 9: Get all data through APISIX gRPC-Web Proxy
+--- exec
+node ./t/plugin/grpc-web/client.js all
+--- response_body
+[["2",{"name":"route02","path":"path02"}],["3",{"name":"route03","path":"path03"}]]

Review comment:
       Do we need two more cases:
   1. check the CORS header for OPTION/non-OPTION requests
   2. configure a route with absolute match




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to