use complex values in add_header, auth_basic_user_file,

sub_filter, and xslt_stylesheet parameters
This commit is contained in:
Igor Sysoev 2009-03-22 09:39:19 +00:00
parent 8508c10bb8
commit 0c2fd4a5de
4 changed files with 102 additions and 195 deletions

View File

@ -13,15 +13,13 @@
typedef struct { typedef struct {
ngx_str_t passwd; ngx_str_t passwd;
} ngx_http_auth_basic_ctx_t; } ngx_http_auth_basic_ctx_t;
typedef struct { typedef struct {
ngx_str_t realm; ngx_str_t realm;
ngx_str_t user_file; ngx_http_complex_value_t user_file;
ngx_array_t *user_file_lengths;
ngx_array_t *user_file_values;
} ngx_http_auth_basic_loc_conf_t; } ngx_http_auth_basic_loc_conf_t;
@ -117,7 +115,7 @@ ngx_http_auth_basic_handler(ngx_http_request_t *r)
alcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_basic_module); alcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_basic_module);
if (alcf->realm.len == 0 || alcf->user_file.len == 0) { if (alcf->realm.len == 0 || alcf->user_file.value.len == 0) {
return NGX_DECLINED; return NGX_DECLINED;
} }
@ -142,18 +140,8 @@ ngx_http_auth_basic_handler(ngx_http_request_t *r)
return NGX_HTTP_INTERNAL_SERVER_ERROR; return NGX_HTTP_INTERNAL_SERVER_ERROR;
} }
if (alcf->user_file_lengths) { if (ngx_http_complex_value(r, &alcf->user_file, &user_file) != NGX_OK) {
if (ngx_http_script_run(r, &user_file, alcf->user_file_lengths->elts, 1, return NGX_ERROR;
alcf->user_file_values->elts)
== NULL)
{
return NGX_ERROR;
}
user_file.data[--user_file.len] = '\0';
} else {
user_file = alcf->user_file;
} }
fd = ngx_open_file(user_file.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); fd = ngx_open_file(user_file.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
@ -401,10 +389,8 @@ ngx_http_auth_basic_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
conf->realm = prev->realm; conf->realm = prev->realm;
} }
if (conf->user_file.data == NULL) { if (conf->user_file.value.len == 0) {
conf->user_file = prev->user_file; conf->user_file = prev->user_file;
conf->user_file_lengths = prev->user_file_lengths;
conf->user_file_values = prev->user_file_values;
} }
return NGX_CONF_OK; return NGX_CONF_OK;
@ -468,47 +454,24 @@ ngx_http_auth_basic_user_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{ {
ngx_http_auth_basic_loc_conf_t *alcf = conf; ngx_http_auth_basic_loc_conf_t *alcf = conf;
ngx_str_t *value; ngx_str_t *value;
ngx_uint_t n; ngx_http_compile_complex_value_t ccv;
ngx_http_script_compile_t sc;
if (alcf->user_file.data) { if (alcf->user_file.value.len) {
return "is duplicate"; return "is duplicate";
} }
value = cf->args->elts; value = cf->args->elts;
alcf->user_file = value[1]; ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
if (alcf->user_file.len == 0) { ccv.cf = cf;
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, ccv.value = &value[1];
"invalid parameter \"%V\"", &alcf->user_file); ccv.complex_value = &alcf->user_file;
return NGX_CONF_ERROR; ccv.zero = 1;
} ccv.conf_prefix = 1;
if (alcf->user_file.data[0] != '$') { if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
if (ngx_conf_full_name(cf->cycle, &alcf->user_file, 1) != NGX_OK) {
return NGX_CONF_ERROR;
}
}
n = ngx_http_script_variables_count(&alcf->user_file);
if (n == 0) {
return NGX_CONF_OK;
}
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
sc.cf = cf;
sc.source = &alcf->user_file;
sc.lengths = &alcf->user_file_lengths;
sc.values = &alcf->user_file_values;
sc.variables = n;
sc.complete_lengths = 1;
sc.complete_values = 1;
if (ngx_http_script_compile(&sc) != NGX_OK) {
return NGX_CONF_ERROR; return NGX_CONF_ERROR;
} }

View File

@ -16,18 +16,18 @@ typedef ngx_int_t (*ngx_http_set_header_pt)(ngx_http_request_t *r,
typedef struct { typedef struct {
ngx_str_t name; ngx_str_t name;
ngx_uint_t offset; ngx_uint_t offset;
ngx_http_set_header_pt handler; ngx_http_set_header_pt handler;
} ngx_http_set_header_t; } ngx_http_set_header_t;
struct ngx_http_header_val_s { struct ngx_http_header_val_s {
ngx_table_elt_t value; ngx_http_complex_value_t value;
ngx_uint_t offset; ngx_uint_t hash;
ngx_http_set_header_pt handler; ngx_str_t key;
ngx_array_t *lengths; ngx_http_set_header_pt handler;
ngx_array_t *values; ngx_uint_t offset;
}; };
@ -162,16 +162,8 @@ ngx_http_headers_filter(ngx_http_request_t *r)
h = conf->headers->elts; h = conf->headers->elts;
for (i = 0; i < conf->headers->nelts; i++) { for (i = 0; i < conf->headers->nelts; i++) {
if (h[i].lengths == NULL) { if (ngx_http_complex_value(r, &h[i].value, &value) != NGX_OK) {
value = h[i].value.value; return NGX_ERROR;
} else {
if (ngx_http_script_run(r, &value, h[i].lengths->elts, 0,
h[i].values->elts)
== NULL)
{
return NGX_ERROR;
}
} }
if (h[i].handler(r, &h[i], &value) != NGX_OK) { if (h[i].handler(r, &h[i], &value) != NGX_OK) {
@ -331,8 +323,8 @@ ngx_http_add_header(ngx_http_request_t *r, ngx_http_header_val_t *hv,
return NGX_ERROR; return NGX_ERROR;
} }
h->hash = hv->value.hash; h->hash = hv->hash;
h->key = hv->value.key; h->key = hv->key;
h->value = *value; h->value = *value;
} }
@ -414,8 +406,8 @@ ngx_http_set_last_modified(ngx_http_request_t *r, ngx_http_header_val_t *hv,
} }
} }
h->hash = hv->value.hash; h->hash = hv->hash;
h->key = hv->value.key; h->key = hv->key;
h->value = *value; h->value = *value;
return NGX_OK; return NGX_OK;
@ -578,12 +570,11 @@ ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{ {
ngx_http_headers_conf_t *hcf = conf; ngx_http_headers_conf_t *hcf = conf;
ngx_int_t n; ngx_str_t *value;
ngx_str_t *value; ngx_uint_t i;
ngx_uint_t i; ngx_http_header_val_t *hv;
ngx_http_header_val_t *h; ngx_http_set_header_t *set;
ngx_http_set_header_t *sh; ngx_http_compile_complex_value_t ccv;
ngx_http_script_compile_t sc;
value = cf->args->elts; value = cf->args->elts;
@ -595,47 +586,35 @@ ngx_http_headers_add(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
} }
} }
h = ngx_array_push(hcf->headers); hv = ngx_array_push(hcf->headers);
if (h == NULL) { if (hv == NULL) {
return NGX_CONF_ERROR; return NGX_CONF_ERROR;
} }
h->value.hash = 1; hv->hash = 1;
h->value.key = value[1]; hv->key = value[1];
h->value.value = value[2]; hv->handler = ngx_http_add_header;
h->offset = 0; hv->offset = 0;
h->handler = ngx_http_add_header;
h->lengths = NULL;
h->values = NULL;
sh = ngx_http_set_headers; set = ngx_http_set_headers;
for (i = 0; sh[i].name.len; i++) { for (i = 0; set[i].name.len; i++) {
if (ngx_strcasecmp(value[1].data, sh[i].name.data) != 0) { if (ngx_strcasecmp(value[1].data, set[i].name.data) != 0) {
continue; continue;
} }
h->offset = sh[i].offset; hv->offset = set[i].offset;
h->handler = sh[i].handler; hv->handler = set[i].handler;
break; break;
} }
n = ngx_http_script_variables_count(&value[2]); ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
if (n == 0) { ccv.cf = cf;
return NGX_CONF_OK; ccv.value = &value[2];
} ccv.complex_value = &hv->value;
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
sc.cf = cf;
sc.source = &value[2];
sc.lengths = &h->lengths;
sc.values = &h->values;
sc.variables = n;
sc.complete_lengths = 1;
sc.complete_values = 1;
if (ngx_http_script_compile(&sc) != NGX_OK) {
return NGX_CONF_ERROR; return NGX_CONF_ERROR;
} }

View File

@ -10,17 +10,14 @@
typedef struct { typedef struct {
ngx_str_t match; ngx_str_t match;
ngx_str_t sub; ngx_http_complex_value_t value;
ngx_hash_t types; ngx_hash_t types;
ngx_array_t *sub_lengths; ngx_flag_t once;
ngx_array_t *sub_values;
ngx_flag_t once; ngx_array_t *types_keys;
ngx_array_t *types_keys;
} ngx_http_sub_loc_conf_t; } ngx_http_sub_loc_conf_t;
@ -31,27 +28,27 @@ typedef enum {
typedef struct { typedef struct {
ngx_str_t match; ngx_str_t match;
ngx_uint_t once; /* unsigned once:1 */ ngx_uint_t once; /* unsigned once:1 */
ngx_buf_t *buf; ngx_buf_t *buf;
u_char *pos; u_char *pos;
u_char *copy_start; u_char *copy_start;
u_char *copy_end; u_char *copy_end;
ngx_chain_t *in; ngx_chain_t *in;
ngx_chain_t *out; ngx_chain_t *out;
ngx_chain_t **last_out; ngx_chain_t **last_out;
ngx_chain_t *busy; ngx_chain_t *busy;
ngx_chain_t *free; ngx_chain_t *free;
ngx_str_t sub; ngx_str_t sub;
ngx_uint_t state; ngx_uint_t state;
size_t saved; size_t saved;
size_t looked; size_t looked;
} ngx_http_sub_ctx_t; } ngx_http_sub_ctx_t;
@ -154,7 +151,6 @@ ngx_http_sub_header_filter(ngx_http_request_t *r)
ctx->match = slcf->match; ctx->match = slcf->match;
ctx->last_out = &ctx->out; ctx->last_out = &ctx->out;
ctx->sub = slcf->sub;
r->filter_need_in_memory = 1; r->filter_need_in_memory = 1;
@ -346,9 +342,8 @@ ngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
if (ctx->sub.data == NULL) { if (ctx->sub.data == NULL) {
if (ngx_http_script_run(r, &ctx->sub, slcf->sub_lengths->elts, if (ngx_http_complex_value(r, &slcf->value, &ctx->sub)
0, slcf->sub_values->elts) != NGX_OK)
== NULL)
{ {
return NGX_ERROR; return NGX_ERROR;
} }
@ -609,9 +604,8 @@ ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{ {
ngx_http_sub_loc_conf_t *slcf = conf; ngx_http_sub_loc_conf_t *slcf = conf;
ngx_str_t *value; ngx_str_t *value;
ngx_int_t n; ngx_http_compile_complex_value_t ccv;
ngx_http_script_compile_t sc;
if (slcf->match.len) { if (slcf->match.len) {
return "is duplicate"; return "is duplicate";
@ -623,24 +617,13 @@ ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
slcf->match = value[1]; slcf->match = value[1];
n = ngx_http_script_variables_count(&value[2]); ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
if (n == 0) { ccv.cf = cf;
slcf->sub = value[2]; ccv.value = &value[2];
return NGX_CONF_OK; ccv.complex_value = &slcf->value;
}
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
sc.cf = cf;
sc.source = &value[2];
sc.lengths = &slcf->sub_lengths;
sc.values = &slcf->sub_values;
sc.variables = n;
sc.complete_lengths = 1;
sc.complete_values = 1;
if (ngx_http_script_compile(&sc) != NGX_OK) {
return NGX_CONF_ERROR; return NGX_CONF_ERROR;
} }
@ -684,10 +667,8 @@ ngx_http_sub_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->once, prev->once, 1); ngx_conf_merge_value(conf->once, prev->once, 1);
ngx_conf_merge_str_value(conf->match, prev->match, ""); ngx_conf_merge_str_value(conf->match, prev->match, "");
if (conf->sub.data == NULL && conf->sub_lengths == NULL) { if (conf->value.value.len == 0) {
conf->sub = prev->sub; conf->value = prev->value;
conf->sub_lengths = prev->sub_lengths;
conf->sub_values = prev->sub_values;
} }
if (ngx_http_merge_types(cf, conf->types_keys, &conf->types, if (ngx_http_merge_types(cf, conf->types_keys, &conf->types,

View File

@ -37,15 +37,9 @@ typedef struct {
} ngx_http_xslt_filter_main_conf_t; } ngx_http_xslt_filter_main_conf_t;
typedef struct {
ngx_array_t *lengths;
ngx_array_t *values;
} ngx_http_xslt_param_t;
typedef struct { typedef struct {
xsltStylesheetPtr stylesheet; xsltStylesheetPtr stylesheet;
ngx_array_t params; /* ngx_http_xslt_param_t */ ngx_array_t params; /* ngx_http_complex_value_t */
} ngx_http_xslt_sheet_t; } ngx_http_xslt_sheet_t;
@ -867,30 +861,25 @@ static ngx_int_t
ngx_http_xslt_params(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx, ngx_http_xslt_params(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx,
ngx_array_t *params) ngx_array_t *params)
{ {
u_char *p, *last, *value, *dst, *src, **s; u_char *p, *last, *value, *dst, *src, **s;
size_t len; size_t len;
ngx_uint_t i; ngx_uint_t i;
ngx_str_t string; ngx_str_t string;
ngx_http_xslt_param_t *param; ngx_http_complex_value_t *param;
param = params->elts; param = params->elts;
for (i = 0; i < params->nelts; i++) { for (i = 0; i < params->nelts; i++) {
if (ngx_http_script_run(r, &string, param[i].lengths->elts, 1, if (ngx_http_complex_value(r, &param[i], &string) != NGX_OK) {
param[i].values->elts)
== NULL)
{
return NGX_ERROR; return NGX_ERROR;
} }
last = string.data + string.len - 1;
*last = '\0';
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"xslt filter param: \"%s\"", string.data); "xslt filter param: \"%s\"", string.data);
p = string.data; p = string.data;
last = string.data + string.len - 1;
while (p && *p) { while (p && *p) {
@ -1073,8 +1062,8 @@ ngx_http_xslt_stylesheet(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_pool_cleanup_t *cln; ngx_pool_cleanup_t *cln;
ngx_http_xslt_file_t *file; ngx_http_xslt_file_t *file;
ngx_http_xslt_sheet_t *sheet; ngx_http_xslt_sheet_t *sheet;
ngx_http_xslt_param_t *param; ngx_http_complex_value_t *param;
ngx_http_script_compile_t sc; ngx_http_compile_complex_value_t ccv;
ngx_http_xslt_filter_main_conf_t *xmcf; ngx_http_xslt_filter_main_conf_t *xmcf;
value = cf->args->elts; value = cf->args->elts;
@ -1142,7 +1131,7 @@ found:
} }
if (ngx_array_init(&sheet->params, cf->pool, n - 2, if (ngx_array_init(&sheet->params, cf->pool, n - 2,
sizeof(ngx_http_xslt_param_t)) sizeof(ngx_http_complex_value_t))
!= NGX_OK) != NGX_OK)
{ {
return NGX_CONF_ERROR; return NGX_CONF_ERROR;
@ -1155,22 +1144,17 @@ found:
return NGX_CONF_ERROR; return NGX_CONF_ERROR;
} }
param->lengths = NULL; ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
param->values = NULL;
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); ccv.cf = cf;
ccv.value = &value[i];
ccv.complex_value = param;
ccv.zero = 1;
sc.cf = cf; if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
sc.source = &value[i];
sc.lengths = &param->lengths;
sc.values = &param->values;
sc.variables = ngx_http_script_variables_count(&value[i]);
sc.complete_lengths = 1;
sc.complete_values = 1;
if (ngx_http_script_compile(&sc) != NGX_OK) {
return NGX_CONF_ERROR; return NGX_CONF_ERROR;
} }
} }
return NGX_CONF_OK; return NGX_CONF_OK;