-
Notifications
You must be signed in to change notification settings - Fork 21
feat(balancer) recreate request to update Host header #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
local _M = {} | ||
|
||
|
||
local ffi = require("ffi") | ||
local get_request = require("resty.core.base").get_request | ||
|
||
|
||
if ngx.config.subsystem == "http" then | ||
ffi.cdef([[ | ||
int ngx_http_lua_kong_ffi_recreate_request(ngx_http_request_t *r); | ||
]]) | ||
end | ||
|
||
|
||
local get_phase = ngx.get_phase | ||
local C = ffi.C | ||
|
||
|
||
function _M.update_proxy_request() | ||
if ngx.config.subsystem == "http" then | ||
if get_phase() ~= "balancer" then | ||
error("API disabled in the current context", 2) | ||
end | ||
|
||
local r = get_request() | ||
|
||
local ret = C.ngx_http_lua_kong_ffi_recreate_request(r) | ||
if ret == ngx.OK then | ||
return true | ||
end | ||
|
||
error("could not recreate request", 2) | ||
end | ||
|
||
return true | ||
end | ||
|
||
|
||
return _M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# vim:set ft= ts=4 sw=4 et: | ||
|
||
use Test::Nginx::Socket::Lua; | ||
use Cwd qw(cwd); | ||
|
||
repeat_each(3); | ||
|
||
plan tests => repeat_each() * (blocks() * 2); | ||
|
||
my $pwd = cwd(); | ||
|
||
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir(); | ||
|
||
no_long_string(); | ||
#no_diff(); | ||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: overwrite Host header on retry | ||
--- http_config | ||
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;"; | ||
|
||
server { | ||
listen 20666; | ||
server_name "pittsburgh.test" default_server; | ||
return 500; | ||
} | ||
|
||
server { | ||
listen 20666; | ||
server_name "punxsutawney.test"; | ||
location / { | ||
echo "OK, campers, rise and shine"; | ||
} | ||
} | ||
|
||
proxy_next_upstream_tries 10; | ||
proxy_next_upstream error http_500; | ||
upstream backend { | ||
server 0.0.0.1; | ||
balancer_by_lua_block { | ||
local b = require "ngx.balancer" | ||
|
||
if not ngx.ctx.tries then | ||
ngx.ctx.tries = 0 | ||
end | ||
|
||
if ngx.ctx.tries > 0 then | ||
local kb = require("resty.kong.balancer") | ||
ngx.var.upstream_host = "punxsutawney.test" | ||
kb.update_proxy_request() | ||
end | ||
|
||
ngx.ctx.tries = ngx.ctx.tries + 1 | ||
b.set_more_tries(1) | ||
assert(b.set_current_peer("127.0.0.1", 20666)) | ||
} | ||
} | ||
|
||
|
||
--- config | ||
|
||
location /t { | ||
proxy_pass http://backend/; | ||
set $upstream_host ''; | ||
proxy_set_header Host $upstream_host; | ||
access_by_lua_block { | ||
ngx.var.upstream_host = "strike" | ||
} | ||
} | ||
|
||
--- request | ||
GET /t | ||
|
||
--- more_headers | ||
Host: thereandback.test | ||
|
||
--- response_body_like | ||
OK, campers, rise and shine | ||
|
||
--- error_code: 200 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to make this function disappear in
stream
module instead of a no-op to make sure the caller errors out. Basically move theif
one line higher.