From 454ea23ca6ed991848c5d229568915d03d62203d Mon Sep 17 00:00:00 2001 From: Kirill A. Korinskiy Date: Mon, 8 Mar 2010 15:35:29 +0300 Subject: [PATCH] Implement support complex value for `secure_link_secret' Cc: catap@catap.ru --- src/http/modules/ngx_http_secure_link_module.c | 56 +++++++++++++++++++----- 1 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/http/modules/ngx_http_secure_link_module.c b/src/http/modules/ngx_http_secure_link_module.c index 2f9351d..0eee63c 100644 --- a/src/http/modules/ngx_http_secure_link_module.c +++ b/src/http/modules/ngx_http_secure_link_module.c @@ -11,7 +11,7 @@ typedef struct { - ngx_str_t secret; + ngx_http_complex_value_t secret; } ngx_http_secure_link_conf_t; @@ -19,13 +19,15 @@ static void *ngx_http_secure_link_create_conf(ngx_conf_t *cf); static char *ngx_http_secure_link_merge_conf(ngx_conf_t *cf, void *parent, void *child); static ngx_int_t ngx_http_secure_link_add_variables(ngx_conf_t *cf); +static char *ngx_http_secure_link_secret(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf); static ngx_command_t ngx_http_secure_link_commands[] = { { ngx_string("secure_link_secret"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, - ngx_conf_set_str_slot, + ngx_http_secure_link_secret, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_secure_link_conf_t, secret), NULL }, @@ -74,6 +76,7 @@ ngx_http_secure_link_variable(ngx_http_request_t *r, { u_char *p, *start, *end, *last; size_t len; + ngx_str_t secret; ngx_int_t n; ngx_uint_t i; ngx_md5_t md5; @@ -82,10 +85,17 @@ ngx_http_secure_link_variable(ngx_http_request_t *r, conf = ngx_http_get_module_loc_conf(r, ngx_http_secure_link_module); - if (conf->secret.len == 0) { + if (conf->secret.value.len == 0) { goto not_found; } + if (ngx_http_complex_value(r, &conf->secret, &secret) != NGX_OK) { + goto not_found; + } + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "secure_link secret: \"%V\"", &secret); + p = &r->unparsed_uri.data[1]; last = r->unparsed_uri.data + r->unparsed_uri.len; @@ -119,7 +129,7 @@ url_start: ngx_md5_init(&md5); ngx_md5_update(&md5, p, len); - ngx_md5_update(&md5, conf->secret.data, conf->secret.len); + ngx_md5_update(&md5, secret.data, secret.len); ngx_md5_final(hash, &md5); for (i = 0; i < 16; i++) { @@ -155,12 +165,6 @@ ngx_http_secure_link_create_conf(ngx_conf_t *cf) return NULL; } - /* - * set by ngx_pcalloc(): - * - * conf->secret = { 0, NULL } - */ - return conf; } @@ -171,7 +175,9 @@ ngx_http_secure_link_merge_conf(ngx_conf_t *cf, void *parent, void *child) ngx_http_secure_link_conf_t *prev = parent; ngx_http_secure_link_conf_t *conf = child; - ngx_conf_merge_str_value(conf->secret, prev->secret, ""); + if (conf->secret.value.len == 0) { + conf->secret = prev->secret; + } return NGX_CONF_OK; } @@ -191,3 +197,31 @@ ngx_http_secure_link_add_variables(ngx_conf_t *cf) return NGX_OK; } + + +static char * +ngx_http_secure_link_secret(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + ngx_http_secure_link_conf_t *slcf = conf; + + ngx_str_t *value; + ngx_http_compile_complex_value_t ccv; + + if (slcf->secret.value.len) { + return "is duplicate"; + } + + value = cf->args->elts; + + ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); + + ccv.cf = cf; + ccv.value = &value[1]; + ccv.complex_value = &slcf->secret; + + if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { + return NGX_CONF_ERROR; + } + + return NGX_CONF_OK; +} -- 1.6.6.1