Skip to content

Commit

Permalink
[proxy_unbuffered] Export context in rewrite phase
Browse files Browse the repository at this point in the history
  • Loading branch information
tkan145 committed Jul 11, 2024
1 parent 889f5d7 commit a580700
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ function _M.new(config)
return self
end

function _M:export()
return {
request_unbuffered = true,
}
function _M:rewrite(context)
context.request_unbuffered = true
end

return _M
1 change: 1 addition & 0 deletions gateway/src/apicast/upstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ local function get_upstream_location_name(context)
return context.upstream_location_name
end
if context.request_unbuffered then
ngx.log(ngx.INFO, "\n------\n HERE \n------\n")
return "@upstream_request_unbuffered"
end
end
Expand Down
148 changes: 148 additions & 0 deletions t/apicast-policy-request-unbuffered.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ run_tests();
__DATA__
=== TEST 1: request_unbuffered policy with big file
For this test, we send a request with a large body, APIcast sends the request as soon as
it receives it. However, since upstream buffered the request body and sends it back to the
client, we will see exactly one occurrence of the line "client request body cached to file
temporary" in the log.
--- configuration
{
"services": [
Expand Down Expand Up @@ -213,3 +217,147 @@ qr/a client request body is buffered to a temporary file/
a client request body is buffered to a temporary file
--- no_error_log
[error]
=== TEST 4: request_unbuffered policy with big file and inside a conditional policy
--- configuration
{
"services": [
{
"backend_version": 1,
"proxy": {
"api_backend": "http://test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "POST", "metric_system_name": "hits", "delta": 2 }
],
"policy_chain": [
{
"name": "apicast.policy.conditional",
"configuration": {
"condition": {
"operations": [
{
"left": "{{ uri }}",
"left_type": "liquid",
"op": "==",
"right": "/test",
"right_type": "plain"
}
]
},
"policy_chain": [
{
"name": "request_unbuffered",
"version": "builtin",
"configuration": {}
}
]
}
},
{
"name": "apicast",
"version": "builtin",
"configuration": {}
}
]
}
}
]
}
--- backend
location /transactions/authrep.xml {
content_by_lua_block {
ngx.exit(200)
}
}
--- upstream
server_name test-upstream.lvh.me;
location /test {
echo_read_request_body;
echo_request_body;
}
--- request eval
"POST /test?user_key= \n" . $ENV{LARGE_BODY}
--- response_body eval chomp
$ENV{LARGE_BODY}
--- error_code: 200
--- grep_error_log eval
qr/a client request body is buffered to a temporary file/
--- grep_error_log_out
a client request body is buffered to a temporary file
--- no_error_log
[error]
=== TEST 5: request_unbuffered policy with conditional policy and false condition
--- ONLY
--- configuration
{
"services": [
{
"backend_version": 1,
"proxy": {
"api_backend": "http://test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "POST", "metric_system_name": "hits", "delta": 2 }
],
"policy_chain": [
{
"name": "apicast.policy.conditional",
"configuration": {
"condition": {
"operations": [
{
"left": "{{ uri }}",
"left_type": "liquid",
"op": "==",
"right": "/invalid",
"right_type": "plain"
}
]
},
"policy_chain": [
{
"name": "request_unbuffered",
"version": "builtin",
"configuration": {}
}
]
}
},
{
"name": "apicast",
"version": "builtin",
"configuration": {}
}
]
}
}
]
}
--- backend
location /transactions/authrep.xml {
content_by_lua_block {
ngx.exit(200)
}
}
--- upstream
server_name test-upstream.lvh.me;
location /test {
echo_read_request_body;
echo_request_body;
}
--- request eval
"POST /test?user_key= \n" . $ENV{LARGE_BODY}
--- response_body eval chomp
$ENV{LARGE_BODY}
--- error_code: 200
--- grep_error_log eval
qr/a client request body is buffered to a temporary file/
--- grep_error_log_out
a client request body is buffered to a temporary file
a client request body is buffered to a temporary file
--- no_error_log
[error]

0 comments on commit a580700

Please sign in to comment.