Skip to content

Commit 23d2541

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

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

lualib/resty/kong/balancer.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
local _M = {}
2+
3+
4+
local ffi = require("ffi")
5+
local get_request = require("resty.core.base").get_request
6+
7+
8+
if ngx.config.subsystem == "http" then
9+
ffi.cdef([[
10+
int ngx_http_lua_kong_ffi_recreate_request(ngx_http_request_t *r);
11+
]])
12+
end
13+
14+
15+
local get_phase = ngx.get_phase
16+
local C = ffi.C
17+
18+
19+
function _M.update_proxy_request()
20+
if ngx.config.subsystem == "http" then
21+
if get_phase() ~= "balancer" then
22+
error("API disabled in the current context", 2)
23+
end
24+
25+
local r = get_request()
26+
27+
local ret = C.ngx_http_lua_kong_ffi_recreate_request(r)
28+
if ret == ngx.OK then
29+
return true
30+
end
31+
32+
error("could not recreate request", 2)
33+
end
34+
35+
return true
36+
end
37+
38+
39+
return _M

src/ngx_http_lua_kong_module.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,13 @@ ngx_http_lua_kong_get_upstream_ssl_verify(ngx_http_request_t *r,
624624
}
625625

626626
#endif
627+
628+
629+
extern ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r);
630+
631+
632+
ngx_int_t
633+
ngx_http_lua_kong_ffi_recreate_request(ngx_http_request_t *r)
634+
{
635+
return ngx_http_proxy_create_request(r);
636+
}

t/003-balancer.t

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# vim:set ft= ts=4 sw=4 et:
2+
3+
use Test::Nginx::Socket::Lua;
4+
use Cwd qw(cwd);
5+
6+
repeat_each(3);
7+
8+
plan tests => repeat_each() * (blocks() * 2);
9+
10+
my $pwd = cwd();
11+
12+
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
13+
14+
no_long_string();
15+
#no_diff();
16+
17+
run_tests();
18+
19+
__DATA__
20+
21+
=== TEST 1: overwrite Host header on retry
22+
--- http_config
23+
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;";
24+
25+
server {
26+
listen 20666;
27+
server_name "pittsburgh.test" default_server;
28+
return 500;
29+
}
30+
31+
server {
32+
listen 20666;
33+
server_name "punxsutawney.test";
34+
location / {
35+
echo "OK, campers, rise and shine";
36+
}
37+
}
38+
39+
proxy_next_upstream_tries 10;
40+
proxy_next_upstream error http_500;
41+
upstream backend {
42+
server 0.0.0.1;
43+
balancer_by_lua_block {
44+
local b = require "ngx.balancer"
45+
46+
if not ngx.ctx.tries then
47+
ngx.ctx.tries = 0
48+
end
49+
50+
if ngx.ctx.tries > 0 then
51+
local kb = require("resty.kong.balancer")
52+
ngx.var.upstream_host = "punxsutawney.test"
53+
kb.update_proxy_request()
54+
end
55+
56+
ngx.ctx.tries = ngx.ctx.tries + 1
57+
b.set_more_tries(1)
58+
assert(b.set_current_peer("127.0.0.1", 20666))
59+
}
60+
}
61+
62+
63+
--- config
64+
65+
location /t {
66+
proxy_pass http://backend/;
67+
set $upstream_host '';
68+
proxy_set_header Host $upstream_host;
69+
access_by_lua_block {
70+
ngx.var.upstream_host = "strike"
71+
}
72+
}
73+
74+
--- request
75+
GET /t
76+
77+
--- more_headers
78+
Host: thereandback.test
79+
80+
--- response_body_like
81+
OK, campers, rise and shine
82+
83+
--- error_code: 200

0 commit comments

Comments
 (0)