Skip to content

Commit d68c243

Browse files
authored
feat: allow configuring allow-headers in grpc-web plugin (#10904)
1 parent 3faeff6 commit d68c243

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

apisix/plugins/grpc-web.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,22 @@ local CONTENT_ENCODING_BINARY = "binary"
3232
local DEFAULT_CORS_ALLOW_ORIGIN = "*"
3333
local DEFAULT_CORS_ALLOW_METHODS = ALLOW_METHOD_POST
3434
local DEFAULT_CORS_ALLOW_HEADERS = "content-type,x-grpc-web,x-user-agent"
35+
local DEFAULT_CORS_EXPOSE_HEADERS = "grpc-message,grpc-status"
3536
local DEFAULT_PROXY_CONTENT_TYPE = "application/grpc"
3637

3738

3839
local plugin_name = "grpc-web"
3940

4041
local schema = {
4142
type = "object",
42-
properties = {},
43+
properties = {
44+
cors_allow_headers = {
45+
description =
46+
"multiple header use ',' to split. default: content-type,x-grpc-web,x-user-agent.",
47+
type = "string",
48+
default = DEFAULT_CORS_ALLOW_HEADERS
49+
}
50+
}
4351
}
4452

4553
local grpc_web_content_encoding = {
@@ -125,14 +133,14 @@ function _M.header_filter(conf, ctx)
125133
local method = core.request.get_method()
126134
if method == ALLOW_METHOD_OPTIONS then
127135
core.response.set_header("Access-Control-Allow-Methods", DEFAULT_CORS_ALLOW_METHODS)
128-
core.response.set_header("Access-Control-Allow-Headers", DEFAULT_CORS_ALLOW_HEADERS)
136+
core.response.set_header("Access-Control-Allow-Headers", conf.cors_allow_headers)
129137
end
130138

131139
if not ctx.cors_allow_origins then
132140
core.response.set_header("Access-Control-Allow-Origin", DEFAULT_CORS_ALLOW_ORIGIN)
133141
end
134142
core.response.set_header("Content-Type", ctx.grpc_web_mime)
135-
core.response.set_header("Access-Control-Expose-Headers", "grpc-message,grpc-status")
143+
core.response.set_header("Access-Control-Expose-Headers", DEFAULT_CORS_EXPOSE_HEADERS)
136144
end
137145

138146
function _M.body_filter(conf, ctx)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ description: This document contains information about the Apache APISIX grpc-web
3232

3333
The `grpc-web` Plugin is a proxy Plugin that can process [gRPC Web](https://github.com/grpc/grpc-web) requests from JavaScript clients to a gRPC service.
3434

35+
## Attributes
36+
37+
| Name | Type | Required | Default | Description |
38+
|-------------------------|---------|----------|-----------------------------------------|----------------------------------------------------------------------------------------------------------|
39+
| cors_allow_headers | string | False | "content-type,x-grpc-web,x-user-agent" | Headers in the request allowed when accessing a cross-origin resource. Use `,` to add multiple headers. |
40+
3541
## Enable Plugin
3642

3743
You can enable the `grpc-web` Plugin on a specific Route as shown below:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ description: 本文介绍了关于 Apache APISIX `grpc-web` 插件的基本信
3232

3333
`grpc-web` 插件是一个代理插件,可以处理从 JavaScript 客户端到 gRPC Service 的 [gRPC Web](https://github.com/grpc/grpc-web) 请求。
3434

35+
## 属性
36+
37+
| 名称 | 类型 | 必选项 | 默认值 | 描述 |
38+
|---------------------| ------- |----|-----------------------------------------|----------------------------------------------------------------|
39+
| cors_allow_headers | string || "content-type,x-grpc-web,x-user-agent" | 允许跨域访问时请求方携带哪些非 `CORS 规范` 以外的 Header。如果你有多个 Header,请使用 `,` 分割。 |
40+
3541
## 启用插件
3642

3743
你可以通过如下命令在指定路由上启用 `gRPC-web` 插件:

t/plugin/grpc-web.t

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,97 @@ curl -iv --location 'http://127.0.0.1:1984/grpc/web/a6.RouteService/GetRoute' \
260260
--data-binary '@./t/plugin/grpc-web/req.bin'
261261
--- response_body eval
262262
qr/grpc-status:0\x0d\x0agrpc-message:/
263+
264+
265+
266+
=== TEST 13: confg default response route
267+
--- config
268+
location /t {
269+
content_by_lua_block {
270+
local config = {
271+
uri = "/grpc/web/*",
272+
upstream = {
273+
scheme = "grpc",
274+
type = "roundrobin",
275+
nodes = {
276+
["127.0.0.1:50001"] = 1
277+
}
278+
},
279+
plugins = {
280+
["grpc-web"] = {}
281+
}
282+
}
283+
local t = require("lib.test_admin").test
284+
local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, config)
285+
286+
if code >= 300 then
287+
ngx.status = code
288+
ngx.say(body)
289+
return
290+
end
291+
292+
ngx.say(body)
293+
}
294+
}
295+
--- response_body
296+
passed
297+
298+
299+
300+
=== TEST 14: check header in default response
301+
--- request
302+
OPTIONS /grpc/web/a6.RouteService/GetRoute
303+
--- error_code: 204
304+
--- response_headers
305+
Access-Control-Allow-Methods: POST
306+
Access-Control-Allow-Headers: content-type,x-grpc-web,x-user-agent
307+
Access-Control-Allow-Origin: *
308+
Access-Control-Expose-Headers: grpc-message,grpc-status
309+
310+
311+
312+
=== TEST 15: Custom configuration routing
313+
--- config
314+
location /t {
315+
content_by_lua_block {
316+
local config = {
317+
uri = "/grpc/web/*",
318+
upstream = {
319+
scheme = "grpc",
320+
type = "roundrobin",
321+
nodes = {
322+
["127.0.0.1:50001"] = 1
323+
}
324+
},
325+
plugins = {
326+
["grpc-web"] = {
327+
cors_allow_headers = "grpc-accept-encoding"
328+
}
329+
}
330+
}
331+
local t = require("lib.test_admin").test
332+
local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, config)
333+
334+
if code >= 300 then
335+
ngx.status = code
336+
ngx.say(body)
337+
return
338+
end
339+
340+
ngx.say(body)
341+
}
342+
}
343+
--- response_body
344+
passed
345+
346+
347+
348+
=== TEST 16: check header in default response
349+
--- request
350+
OPTIONS /grpc/web/a6.RouteService/GetRoute
351+
--- error_code: 204
352+
--- response_headers
353+
Access-Control-Allow-Methods: POST
354+
Access-Control-Allow-Headers: grpc-accept-encoding
355+
Access-Control-Allow-Origin: *
356+
Access-Control-Expose-Headers: grpc-message,grpc-status

0 commit comments

Comments
 (0)