Skip to content

Commit 1bb62b7

Browse files
committed
Modules: js_set inline JavaScript expressions.
Added support for inline JavaScript expressions in the js_set directive. Previously, js_set only accepted function references: js_set $var main.handler; Now it also accepts inline expressions: js_set $var '(r.uri)'; js_set $var 'r.headersIn["Host"] || "none"'; Additionally, nginx-style $variable references are expanded to the corresponding JavaScript variable access. For example: js_set $var '$uri.toUpperCase()'; is equivalent to: js_set $var 'r.variables.uri.toUpperCase()'; In stream context, $var expands to s.variables.var. The expansion is a textual substitution at configuration parse time for $name patterns where name starts with [A-Za-z_].
1 parent 1dbce7d commit 1bb62b7

File tree

8 files changed

+703
-76
lines changed

8 files changed

+703
-76
lines changed

nginx/ngx_http_js_module.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,8 +1620,9 @@ ngx_http_js_variable_set(ngx_http_request_t *r, ngx_http_variable_value_t *v,
16201620

16211621
if (rc == NGX_DECLINED) {
16221622
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1623-
"no \"js_import\" directives found for \"js_set\" handler"
1624-
" \"%V\" in the current scope", fname);
1623+
"no \"js_import\" or inline expression found"
1624+
" for \"js_set\" handler \"%V\" at %s:%ui",
1625+
fname, vdata->file_name, vdata->line);
16251626
v->not_found = 1;
16261627
return NGX_OK;
16271628
}
@@ -8016,9 +8017,12 @@ ngx_http_js_periodic(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
80168017
static char *
80178018
ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
80188019
{
8019-
ngx_str_t *value;
8020-
ngx_js_set_t *data, *prev;
8021-
ngx_http_variable_t *v;
8020+
ngx_str_t *value;
8021+
ngx_js_set_t *data, *prev;
8022+
ngx_http_variable_t *v;
8023+
ngx_http_js_loc_conf_t *jlcf;
8024+
8025+
static ngx_uint_t ngx_http_js_inline_index;
80228026

80238027
value = cf->args->elts;
80248028

@@ -8041,20 +8045,25 @@ ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
80418045
return NGX_CONF_ERROR;
80428046
}
80438047

8044-
data->fname = value[2];
8045-
data->flags = 0;
8046-
data->file_name = cf->conf_file->file.name.data;
8047-
data->line = cf->conf_file->line;
8048+
jlcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_js_module);
8049+
8050+
if (ngx_js_set_init(cf, &jlcf->inlines, &ngx_http_js_inline_index,
8051+
&value[2], "r", data)
8052+
!= NGX_OK)
8053+
{
8054+
return NGX_CONF_ERROR;
8055+
}
80488056

80498057
if (v->get_handler == ngx_http_js_variable_set) {
80508058
prev = (ngx_js_set_t *) v->data;
80518059

80528060
if (data->fname.len != prev->fname.len
8053-
|| ngx_strncmp(data->fname.data, prev->fname.data, data->fname.len) != 0)
8061+
|| ngx_strncmp(data->fname.data, prev->fname.data,
8062+
data->fname.len) != 0)
80548063
{
80558064
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
80568065
"variable \"%V\" is redeclared with "
8057-
"different function name", &value[1]);
8066+
"different handler", &value[1]);
80588067
return NGX_CONF_ERROR;
80598068
}
80608069
}

0 commit comments

Comments
 (0)