-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
Description
When calling a GRPC service from a lua filter the returned body is always empty. Digging into this I think its related to the response handling code that copies the c++ string to a c string as the body of the http request starts with a null char because the grpc wire format is uncompressed. This doesn't happen though on the request sending, the lua string handles having a null as the first char.
local bit = require("bit")
function codeToProto(code)
return "\n\n" .. code
end
function protoToGRPC(proto)
local protoLength = string.len(proto)
local protoLengthBytes = {}
for i = 0, 3 do
protoLengthBytes[i] = bit.band(bit.rshift(protoLength,i * 8), 0xff)
end
return string.char(0, protoLengthBytes[3], protoLengthBytes[2], protoLengthBytes[1], protoLengthBytes[0]) .. proto
end
function envoy_on_request(request_handle)
local code = request_handle:headers():get("code")
print("Making request for cluster of " .. code)
local headers, response = request_handle:httpCall(
"servicediscovery",
{
[":method"] = "POST",
[":path"] = "/envoy.Discovery/Lookup",
[":authority"] = "routing",
["Content-Type"] = "application/grpc+proto",
["TE"] = "trailers"
},
protoToGRPC(codeToProto(code)),
5000
)
for key,value in pairs(headers) do print(key,value) end
print(string.byte(response, 1))
cluster = string.sub(response, 6)
print(string.len(response))
request_handle:headers():add("cluster", cluster)
endends up with the following debug print
Making request for cluster of 1234567
:status 200
grpc-accept-encoding gzip
x-envoy-upstream-service-time 221
grpc-encoding identity
content-type application/grpc
0
Reactions are currently unavailable