Skip to content

Commit

Permalink
feat(var) add special option default to lua_kong_load_var_index f…
Browse files Browse the repository at this point in the history
…or automatic loading of commonly used variables
  • Loading branch information
chronolaw authored Feb 22, 2022
1 parent 4ccf6e0 commit a2829b1
Show file tree
Hide file tree
Showing 5 changed files with 532 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/setup_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ kong-ngx-build \
--work $DOWNLOAD_ROOT \
--prefix $INSTALL_ROOT \
--openresty $OPENRESTY \
--kong-nginx-module ${GITHUB_BASE_REF:-$GITHUB_REF_NAME} \
--kong-nginx-module ${GITHUB_HEAD_REF:-$GITHUB_REF_NAME} \
--luarocks $LUAROCKS \
--openssl $OPENSSL \
--debug \
Expand Down
103 changes: 103 additions & 0 deletions src/ngx_http_lua_kong_var.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,99 @@
#include "ngx_http_lua_kong_common.h"


/* default variable indexes will be loaded */
static ngx_str_t default_vars[] = {
ngx_string("args"),
ngx_string("is_args"),
ngx_string("bytes_sent"),
ngx_string("content_type"),
/* ngx_string("host"), */

/* http request headers */
ngx_string("http_authorization"),
ngx_string("http_connection"),
ngx_string("http_host"),
ngx_string("http_kong_debug"),
ngx_string("http_proxy"),
ngx_string("http_proxy_connection"),
ngx_string("http_te"),
ngx_string("http_upgrade"),

/* http request headers */
ngx_string("http_x_forwarded_for"),
ngx_string("http_x_forwarded_host"),
ngx_string("http_x_forwarded_path"),
ngx_string("http_x_forwarded_port"),
ngx_string("http_x_forwarded_prefix"),
ngx_string("http_x_forwarded_proto"),

/* --with-http_ssl_module */
#if (NGX_HTTP_SSL)
ngx_string("https"),
#endif

/* --with-http_v2_module */
#if (NGX_HTTP_V2)
ngx_string("http2"),
#endif

/* --with-http_realip_module */
#if (NGX_HTTP_REALIP)
ngx_string("realip_remote_addr"),
ngx_string("realip_remote_port"),
#endif

/* ngx_string("remote_addr"), */
ngx_string("remote_port"),

/* ngx_string("request"), */
ngx_string("request_length"),
ngx_string("request_method"),
ngx_string("request_time"),
ngx_string("request_uri"),
ngx_string("scheme"),
ngx_string("server_addr"),
ngx_string("server_port"),

/* --with-http_ssl_module */
#if (NGX_SSL)
ngx_string("ssl_cipher"),
ngx_string("ssl_client_raw_cert"),
ngx_string("ssl_client_verify"),
ngx_string("ssl_protocol"),
ngx_string("ssl_server_name"),
#endif

ngx_string("upstream_http_connection"),
ngx_string("upstream_http_trailer"),
ngx_string("upstream_http_upgrade"),
ngx_string("upstream_status"),

ngx_null_string
};


static char *
ngx_http_lua_kong_load_default_var_indexes(ngx_conf_t *cf)
{
ngx_str_t *var;
ngx_int_t index;

for (var = default_vars; var->len; var++) {
index = ngx_http_get_variable_index(cf, var);

if (index == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"unable to mark variable \"%V\" as indexed: no memory",
var);
return NGX_CONF_ERROR;
}
}

return NGX_CONF_OK;
}


char *
ngx_http_lua_kong_load_var_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
Expand All @@ -26,6 +119,10 @@ ngx_http_lua_kong_load_var_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

value = cf->args->elts;

if (ngx_strcmp(value[1].data, "default") == 0) {
return ngx_http_lua_kong_load_default_var_indexes(cf);
}

if (value[1].data[0] != '$') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"%V\"", &value[1]);
Expand Down Expand Up @@ -93,12 +190,18 @@ ngx_http_lua_kong_ffi_var_get_by_index(ngx_http_request_t *r, ngx_uint_t index,

vv = ngx_http_get_indexed_variable(r, index);
if (vv == NULL || vv->not_found) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"variable value is not found by index %d", index);

return NGX_DECLINED;
}

*value = vv->data;
*value_len = vv->len;

ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"get variable value '%v' by index %d", vv, index);

return NGX_OK;
}

Expand Down
6 changes: 5 additions & 1 deletion t/004-indexed-var.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

plan tests => repeat_each() * (blocks() * 5);
plan tests => repeat_each() * (blocks() * 5) + 2;

my $pwd = cwd();

Expand Down Expand Up @@ -106,6 +106,8 @@ GET /t
value3
--- error_code: 200
--- error_log
get variable value 'value3' by index
--- no_error_log
[error]
[crit]
Expand Down Expand Up @@ -145,6 +147,8 @@ GET /t
value4_2
--- error_code: 200
--- error_log
get variable value 'value4_2' by index
--- no_error_log
[error]
[crit]
Expand Down
12 changes: 10 additions & 2 deletions t/005-indexed-var-openresty-suites.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 2 + 9);
plan tests => repeat_each() * (blocks() * 2 + 9 + 4);

#no_diff();
#no_long_string();
Expand Down Expand Up @@ -40,6 +40,8 @@ GET /test
--- response_body
old: 32
new: nil
--- error_log
get variable value '32' by index



Expand All @@ -64,6 +66,8 @@ GET /test?hello=world
--- response_body
old: hello=world
new: nil
--- error_log
get variable value 'hello=world' by index



Expand Down Expand Up @@ -164,6 +168,8 @@ invalid referer:
--- no_error_log
[error]

--- error_log
get variable value '' by index


=== TEST 7: false $invalid_referer variable value in Lua
Expand Down Expand Up @@ -195,6 +201,8 @@ invalid referer: 1
--- no_error_log
[error]

--- error_log
get variable value '1' by index


=== TEST 8: $proxy_host & $proxy_port & $proxy_add_x_forwarded_for
Expand Down Expand Up @@ -350,4 +358,4 @@ variable not changeable
--- pipelined_requests eval
["GET /balancer?port=8091", "GET /balancer?port=8092"]
--- response_body eval
["this is backend peer 8091", "this is backend peer 8092"]
["this is backend peer 8091", "this is backend peer 8092"]
Loading

0 comments on commit a2829b1

Please sign in to comment.