in apisix/plugins/ext-plugin/init.lua [719:833]
function (conf, ctx, sock, entry)
local lrucache_id = core.lrucache.plugin_ctx_id(ctx, entry)
local token, err = core.lrucache.plugin_ctx(lrucache, ctx, entry, rpc_call,
constants.RPC_PREPARE_CONF, conf, ctx,
lrucache_id)
if not token then
return nil, err
end
builder:Clear()
local var = ctx.var
local res = ctx.runner_ext_response
local textEntries = {}
local hdrs = res.headers
for key, val in pairs(hdrs) do
local ty = type(val)
if ty == "table" then
for _, v in ipairs(val) do
core.table.insert(textEntries, build_headers(var, builder, key, v))
end
else
core.table.insert(textEntries, build_headers(var, builder, key, val))
end
end
local len = #textEntries
http_resp_call_req.StartHeadersVector(builder, len)
for i = len, 1, -1 do
builder:PrependUOffsetTRelative(textEntries[i])
end
local hdrs_vec = builder:EndVector(len)
local id = generate_id()
local status = res.status
http_resp_call_req.Start(builder)
http_resp_call_req.AddId(builder, id)
http_resp_call_req.AddStatus(builder, status)
http_resp_call_req.AddConfToken(builder, token)
http_resp_call_req.AddHeaders(builder, hdrs_vec)
local req = http_resp_call_req.End(builder)
builder:Finish(req)
local ok, err = send(sock, constants.RPC_HTTP_RESP_CALL, builder:Output())
if not ok then
return nil, "failed to send RPC_HTTP_RESP_CALL: " .. err
end
local ty, resp
while true do
ty, resp = receive(sock)
if ty == nil then
return nil, "failed to receive RPC_HTTP_REQ_CALL: " .. resp
end
if ty ~= constants.RPC_EXTRA_INFO then
break
end
local out, err = handle_extra_info(ctx, resp)
if not out then
return nil, "failed to handle RPC_EXTRA_INFO: " .. err
end
local ok, err = send(sock, constants.RPC_EXTRA_INFO, out)
if not ok then
return nil, "failed to reply RPC_EXTRA_INFO: " .. err
end
end
if ty ~= constants.RPC_HTTP_RESP_CALL then
return nil, "failed to receive RPC_HTTP_RESP_CALL: unexpected type " .. ty
end
local buf = flatbuffers.binaryArray.New(resp)
local call_resp = http_resp_call_resp.GetRootAsResp(buf, 0)
local len = call_resp:HeadersLength()
if len > 0 then
local resp_headers = {}
for i = 1, len do
local entry = call_resp:Headers(i)
local name = str_lower(entry:Name())
if resp_headers[name] == nil then
core.response.set_header(name, entry:Value())
resp_headers[name] = true
else
core.response.add_header(name, entry:Value())
end
end
else
for k, v in pairs(res.headers) do
if not exclude_resp_header[str_lower(k)] then
core.response.set_header(k, v)
end
end
end
local body
local len = call_resp:BodyLength()
if len > 0 then
body = call_resp:BodyAsString()
end
local code = call_resp:Status()
core.log.info("recv resp, code: ", code, " body: ", body, " len: ", len)
if code == 0 then
code = body and res.status or nil
end
return true, nil, code, body
end