Skip to content

Commit 989d0a7

Browse files
committed
feat(balancer) recreate request to update Host header
1 parent 521a88e commit 989d0a7

File tree

3 files changed

+780
-0
lines changed

3 files changed

+780
-0
lines changed

lualib/resty/kong/balancer.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
local _M = {}
2+
3+
4+
local ffi = require("ffi")
5+
6+
7+
ffi.cdef([[
8+
int ngx_http_lua_kong_ffi_recreate_request(ngx_http_request_t *r);
9+
]])
10+
11+
12+
local get_phase = ngx.get_phase
13+
local C = ffi.C
14+
15+
16+
local get_request
17+
do
18+
local ok, exdata = pcall(require, "thread.exdata")
19+
if ok and exdata then
20+
function get_request()
21+
local r = exdata()
22+
if r ~= nil then
23+
return r
24+
end
25+
end
26+
27+
else
28+
local getfenv = getfenv
29+
30+
function get_request()
31+
return getfenv(0).__ngx_req
32+
end
33+
end
34+
end
35+
36+
37+
function _M.update_host_header()
38+
if get_phase() ~= "balancer" then
39+
error("API disabled in the current context", 2)
40+
end
41+
42+
local r = get_request()
43+
44+
local ret = C.ngx_http_lua_kong_ffi_recreate_request(r)
45+
if ret == ngx.NGX_OK then
46+
return true
47+
end
48+
49+
return nil, "could not recreate request"
50+
51+
end
52+
53+
54+
return _M

0 commit comments

Comments
 (0)