Skip to content

Commit

Permalink
refactor(plugins/grpc-web): use string buffer to concat strings (#11562)
Browse files Browse the repository at this point in the history
string.buffer is better than table.concat, we have already done the same thing in other place.

use string.buffer to replace table.concat
use localized ngx.var and ngx.encode_base64
  • Loading branch information
chronolaw authored Sep 19, 2023
1 parent 7a32972 commit e657234
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions kong/plugins/grpc-web/deco.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- Copyright (c) Kong Inc. 2020

local cjson = require "cjson"
local buffer = require "string.buffer"
local pb = require "pb"
local grpc_tools = require "kong.tools.grpc"
local grpc_frame = grpc_tools.frame
Expand Down Expand Up @@ -139,7 +140,7 @@ function deco:downstream(chunk)
if self.msg_encoding ~= "proto" then
local body = (self.downstream_body or "") .. chunk

local out, n = {}, 1
local out = buffer.new()
local msg, body = grpc_unframe(body)

while msg do
Expand All @@ -148,13 +149,12 @@ function deco:downstream(chunk)
msg = grpc_frame(0x0, msg)
end

out[n] = msg
n = n + 1
out:put(msg)
msg, body = grpc_unframe(body)
end

self.downstream_body = body
chunk = table.concat(out)
chunk = out:get()
end

if self.text_encoding == "base64" then
Expand All @@ -169,7 +169,7 @@ function deco:frame(ftype, msg)
local f = grpc_frame(ftype, msg)

if self.text_encoding == "base64" then
f = ngx.encode_base64(f)
f = encode_base64(f)
end

return f
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/grpc-web/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function grpc_web:access(conf)

local uri
if conf.pass_stripped_path then
uri = ngx.var.upstream_uri
uri = ngx_var.upstream_uri
ngx.req.set_uri(uri)
else
uri = kong_request_get_path()
Expand Down

1 comment on commit e657234

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:e657234b46cf7308fda5ac99a2118a634adf93d6
Artifacts available https://github.com/Kong/kong/actions/runs/6230933022

Please sign in to comment.