Skip to content

Commit c9ef59d

Browse files
authored
Merge pull request #2 from ysugimoto/support-grpcwebtext-content-type
Provide grpc-web-text mode polyfill
2 parents 29df2bd + 75d9b23 commit c9ef59d

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,25 @@ local cors = require("grpc-gateway.cors")
232232
cors("http://localhost:8080") -- or cors() to set "*"
233233
```
234234

235+
## Polyfill grpc-web-text mode
236+
237+
In default, nginx can proxy only `application/grpc-web+proto` which means request body will come as binary,
238+
So nginx cannot support `application/grpc-web-text` mode because rquest body will come as base64-encoded string.
239+
240+
So this package also provide a tiny polyfill:
241+
242+
```lua
243+
location / {
244+
access_by_lua_block {
245+
local polyfill = require("grpc-gateway.polyfill")
246+
polyfill()
247+
}
248+
249+
grpc_pass localhost:9000;
250+
}
251+
```
252+
253+
235254
## License
236255

237256
MIT

grpc-gateway/polyfill.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
return function()
2+
-- nginx's gRPC proxy supports only grpcweb protocol whose request body accepts only binary proto format.
3+
-- So if user requests with 'Content-Type: application/grpc-web-text', we need to decode request body as binary.
4+
if ngx.req.get_headers()["Content-Type"] == "application/grpc-web-text" then
5+
ngx.req.read_body()
6+
local body = ngx.req.get_body_data()
7+
local decoded, err = ngx.decode_base64(body)
8+
if err then
9+
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
10+
ngx.say("Failed to decode base64 body for grpc-web-text protocol")
11+
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
12+
return
13+
end
14+
nex.req.set_body_data(decoded)
15+
end
16+
end

lua-resty-grpc-gateway.rockspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ build = {
2121
["grpc-gateway.request"] = "grpc-gateway/request.lua",
2222
["grpc-gateway.response"] = "grpc-gateway/response.lua",
2323
["grpc-gateway.util"] = "grpc-gateway/util.lua",
24-
["grpc-gateway.cors"] = "grpc-gateway/cors.lua"
24+
["grpc-gateway.cors"] = "grpc-gateway/cors.lua",
25+
["grpc-gateway.polyfill"] = "grpc-gateway/polyfill.lua"
2526
}
2627
}

0 commit comments

Comments
 (0)