Skip to content
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

lua_add_variable - initialize value (add optional arguments) #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

krzysin
Copy link

@krzysin krzysin commented Apr 25, 2018

No description provided.

@krzysin krzysin changed the title lua_add_variable - initialize value (add optional optional arguments) lua_add_variable - initialize value (add optional arguments) Apr 25, 2018
@dndx dndx self-requested a review April 25, 2018 18:37
@dndx
Copy link
Member

dndx commented Apr 25, 2018

Hi,

Thanks for your PR. Could you please port those changes to https://github.com/openresty/meta-lua-nginx-module as well? The source code in this repository are automatically generated from https://github.com/openresty/meta-lua-nginx-module and editing them here will not persist through next update.

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->data) {
data = (ngx_str_t *) var->data;
} 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.

if (var->data) {
data = (ngx_str_t *) var->data;
} else {
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.

}
data->len = value[2].len;

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

}
ngx_memcpy(data->data, value[2].data, value[2].len);

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.

}

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';

@@ -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 ?

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.

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->valid = 1;
v->no_cacheable = 0;
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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants