Skip to content

Commit 4bc3951

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

File tree

3 files changed

+785
-0
lines changed

3 files changed

+785
-0
lines changed

lualib/resty/kong/balancer.lua

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

0 commit comments

Comments
 (0)