Skip to content

Commit 4abe43c

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.
1 parent 55165c8 commit 4abe43c

File tree

8 files changed

+783
-85
lines changed

8 files changed

+783
-85
lines changed

nginx/ngx_http_js_module.c

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

16451645
if (rc == NGX_DECLINED) {
16461646
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1647-
"no \"js_import\" directives found for \"js_set\" handler"
1648-
" \"%V\" in the current scope", fname);
1647+
"no \"js_import\" or inline expression found"
1648+
" for \"js_set\" handler \"%V\" at %s:%ui",
1649+
fname, vdata->file_name, vdata->line);
16491650
v->not_found = 1;
16501651
return NGX_OK;
16511652
}
@@ -1732,6 +1733,7 @@ ngx_http_js_init_vm(ngx_http_request_t *r, njs_int_t proto_id)
17321733
}
17331734

17341735
ngx_js_ctx_init((ngx_js_ctx_t *) ctx, r->connection->log);
1736+
ctx->conf = (ngx_js_loc_conf_t *) jlcf;
17351737

17361738
ngx_http_set_ctx(r, ctx, ngx_http_js_module);
17371739
}
@@ -8040,9 +8042,12 @@ ngx_http_js_periodic(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
80408042
static char *
80418043
ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
80428044
{
8043-
ngx_str_t *value;
8044-
ngx_js_set_t *data, *prev;
8045-
ngx_http_variable_t *v;
8045+
ngx_str_t *value;
8046+
ngx_js_set_t *data, *prev;
8047+
ngx_http_variable_t *v;
8048+
ngx_http_js_loc_conf_t *jlcf;
8049+
8050+
static ngx_uint_t ngx_http_js_inline_index;
80468051

80478052
value = cf->args->elts;
80488053

@@ -8065,20 +8070,25 @@ ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
80658070
return NGX_CONF_ERROR;
80668071
}
80678072

8068-
data->fname = value[2];
8069-
data->flags = 0;
8070-
data->file_name = cf->conf_file->file.name.data;
8071-
data->line = cf->conf_file->line;
8073+
jlcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_js_module);
8074+
8075+
if (ngx_js_set_init(cf, &jlcf->inlines, &ngx_http_js_inline_index,
8076+
&value[2], "r", data)
8077+
!= NGX_OK)
8078+
{
8079+
return NGX_CONF_ERROR;
8080+
}
80728081

80738082
if (v->get_handler == ngx_http_js_variable_set) {
80748083
prev = (ngx_js_set_t *) v->data;
80758084

80768085
if (data->fname.len != prev->fname.len
8077-
|| ngx_strncmp(data->fname.data, prev->fname.data, data->fname.len) != 0)
8086+
|| ngx_strncmp(data->fname.data, prev->fname.data,
8087+
data->fname.len) != 0)
80788088
{
80798089
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
80808090
"variable \"%V\" is redeclared with "
8081-
"different function name", &value[1]);
8091+
"different handler", &value[1]);
80828092
return NGX_CONF_ERROR;
80838093
}
80848094
}

0 commit comments

Comments
 (0)