Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 44 additions & 3 deletions src/ngx_stream_lua_directive.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,12 +1209,29 @@ ngx_stream_lua_undefined_var(ngx_stream_session_t *s,
}


static ngx_int_t
ngx_stream_lua_var(ngx_stream_session_t *s,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function name is confusing. I think it should be ngx_stream_lua_var_default_value instead.

Also, better declare this at the beginning of this .c file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not better that the declarations under ngx_stream_lua_undefined_var ?

ngx_stream_variable_value_t *v, uintptr_t data)
{
ngx_str_t *str = (ngx_str_t *) data;

v->not_found = 0;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two v->not_found = 0;?

v->len = str->len;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just write *v = *str; to make it shorter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can not, v is type ngx_variable_value_t, str is ngx_str_t

v->data = str->data;

return NGX_OK;
}


char *
ngx_stream_lua_add_variable(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
ngx_stream_variable_t *var;
ngx_str_t *value;
ngx_str_t *value, *data;
ngx_int_t ret;

value = cf->args->elts;
Expand All @@ -1234,8 +1251,32 @@ ngx_stream_lua_add_variable(ngx_conf_t *cf, ngx_command_t *cmd,
return NGX_CONF_ERROR;
}

if (var->get_handler == NULL) {
var->get_handler = ngx_stream_lua_undefined_var;
if (cf->args->nelts == 2) {
if (var->get_handler == NULL) {
var->get_handler = ngx_stream_lua_undefined_var;
}
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: needs a blank line before this line.

if (var->get_handler == NULL) {
var->get_handler = ngx_stream_lua_var;
}

if (var->data) {
data = (ngx_str_t *) var->data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this code path is reacheable. Will you explain?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called when the variable is redefined.
lua_add_variable $ldap_user 'foo';
lua_add_variable $ldap_user 'bar';

} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

data = ngx_pnalloc(cf->pool, sizeof(ngx_str_t));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use ngx_palloc instead since there's only one data chunk to be allocated.

if (data == NULL) {
return NGX_CONF_ERROR;
}
}
data->len = value[2].len;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a newline before this.


data->data = ngx_pnalloc(cf->pool, value[2].len);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. Also, I wonder if the value[2].data memory is also allocated in cf->pool. If yes, then this new allocation and data copying can be saved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this can be saved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right

if (data->data == NULL) {
return NGX_CONF_ERROR;
}
ngx_memcpy(data->data, value[2].data, value[2].len);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a newline before this.


var->data = (uintptr_t) data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type casting looks weird. I don't think var->data is of the uintptr_t type at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, my bad, it's indeed declared as uintptr_t. Never mind then.

}

ret = ngx_stream_get_variable_index(cf, value + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/ngx_stream_lua_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ static ngx_command_t ngx_stream_lua_cmds[] = {
NULL },

{ ngx_string("lua_add_variable"),
NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1,
NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE12,
ngx_stream_lua_add_variable,
0,
0,
Expand Down
55 changes: 55 additions & 0 deletions t/139-add-variable.t
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,58 @@ bar
[error]
--- error_log
access log: bar



=== TEST 5: sanity
--- stream_config
lua_add_variable $foo "foo";
--- stream_server_config
content_by_lua_block {
ngx.say(ngx.var.foo)
ngx.var.foo = "bar"
ngx.say(ngx.var.foo)
}
--- stream_response
foo
bar
--- no_error_log
[warn]
[error]



=== TEST 6: works with C code
--- stream_config
lua_add_variable $foo "foo";
--- stream_server_config
preread_by_lua_block {
ngx.var.foo = "bar"
}

return $foo\n;
--- stream_response
bar
--- no_error_log
[warn]
[error]



=== TEST 7: multiple add with same name works
--- stream_config
lua_add_variable $foo "bar";
lua_add_variable $foo "foo";
--- stream_server_config
preread_by_lua_block {
ngx.say(ngx.var.foo)
ngx.var.foo = "bar"
}

return $foo\n;
--- stream_response
foo
bar
--- no_error_log
[warn]
[error]