From 40ebaeebf5e7f36c890ff2ce04b480e8c98c4adf Mon Sep 17 00:00:00 2001 From: Kirill A. Korinskiy Date: Thu, 28 May 2009 17:10:37 +0400 Subject: [PATCH] Add '>', '!>', '<' and '<' in config if statment Cc: catap@catap.ru Now nginx try convert string to int and try compare int. --- src/http/modules/ngx_http_rewrite_module.c | 66 ++++++++++ src/http/ngx_http_script.c | 184 ++++++++++++++++++++++++++++ src/http/ngx_http_script.h | 4 + 3 files changed, 254 insertions(+), 0 deletions(-) diff --git a/src/http/modules/ngx_http_rewrite_module.c b/src/http/modules/ngx_http_rewrite_module.c index a848443..cb15c54 100644 --- a/src/http/modules/ngx_http_rewrite_module.c +++ b/src/http/modules/ngx_http_rewrite_module.c @@ -720,6 +720,72 @@ ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf) return NGX_CONF_OK; } + if (len == 1 && p[0] == '>') { + + if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { + return NGX_CONF_ERROR; + } + + code = ngx_http_script_start_code(cf->pool, &lcf->codes, + sizeof(uintptr_t)); + if (code == NULL) { + return NGX_CONF_ERROR; + } + + *code = ngx_http_script_more_code; + + return NGX_CONF_OK; + } + + if (len == 2 && p[0] == '!' && p[1] == '>') { + + if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { + return NGX_CONF_ERROR; + } + + code = ngx_http_script_start_code(cf->pool, &lcf->codes, + sizeof(uintptr_t)); + if (code == NULL) { + return NGX_CONF_ERROR; + } + + *code = ngx_http_script_not_more_code; + return NGX_CONF_OK; + } + + if (len == 1 && p[0] == '<') { + + if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { + return NGX_CONF_ERROR; + } + + code = ngx_http_script_start_code(cf->pool, &lcf->codes, + sizeof(uintptr_t)); + if (code == NULL) { + return NGX_CONF_ERROR; + } + + *code = ngx_http_script_less_code; + + return NGX_CONF_OK; + } + + if (len == 2 && p[0] == '!' && p[1] == '<') { + + if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { + return NGX_CONF_ERROR; + } + + code = ngx_http_script_start_code(cf->pool, &lcf->codes, + sizeof(uintptr_t)); + if (code == NULL) { + return NGX_CONF_ERROR; + } + + *code = ngx_http_script_not_less_code; + return NGX_CONF_OK; + } + if ((len == 1 && p[0] == '~') || (len == 2 && p[0] == '~' && p[1] == '*') || (len == 2 && p[0] == '!' && p[1] == '~') diff --git a/src/http/ngx_http_script.c b/src/http/ngx_http_script.c index b8e998a..4aeb049 100644 --- a/src/http/ngx_http_script.c +++ b/src/http/ngx_http_script.c @@ -1385,6 +1385,190 @@ ngx_http_script_not_equal_code(ngx_http_script_engine_t *e) void +ngx_http_script_more_code(ngx_http_script_engine_t *e) +{ + ngx_int_t v, r; + + ngx_http_variable_value_t *val, *res; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script more"); + + e->sp--; + val = e->sp; + res = e->sp - 1; + + e->ip += sizeof(uintptr_t); + + v = ngx_atoi(val->data, val->len); + if (v == NGX_ERROR) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script more: val isn't number"); + + *res = ngx_http_variable_null_value; + return; + } + + r = ngx_atoi(res->data, res->len); + if (r == NGX_ERROR) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script more: res isn't number"); + + *res = ngx_http_variable_null_value; + return; + } + + if (r > v) { + *res = ngx_http_variable_true_value; + return; + } + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script more: no"); + + *res = ngx_http_variable_null_value; +} + + +void +ngx_http_script_not_more_code(ngx_http_script_engine_t *e) +{ + ngx_int_t v, r; + + ngx_http_variable_value_t *val, *res; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script not more"); + + e->sp--; + val = e->sp; + res = e->sp - 1; + + e->ip += sizeof(uintptr_t); + + v = ngx_atoi(val->data, val->len); + if (v == NGX_ERROR) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script not more: val isn't number"); + + *res = ngx_http_variable_null_value; + return; + } + + r = ngx_atoi(res->data, res->len); + if (r == NGX_ERROR) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script more: res isn't number"); + + *res = ngx_http_variable_null_value; + return; + } + + if (!(r > v)) { + *res = ngx_http_variable_true_value; + return; + } + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script not more: no"); + + *res = ngx_http_variable_true_value; +} + + +void +ngx_http_script_less_code(ngx_http_script_engine_t *e) +{ + ngx_int_t v, r; + + ngx_http_variable_value_t *val, *res; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script less"); + + e->sp--; + val = e->sp; + res = e->sp - 1; + + e->ip += sizeof(uintptr_t); + + v = ngx_atoi(val->data, val->len); + if (v == NGX_ERROR) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script less: val isn't number"); + + *res = ngx_http_variable_null_value; + return; + } + + r = ngx_atoi(res->data, res->len); + if (r == NGX_ERROR) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script less: res isn't number"); + + *res = ngx_http_variable_null_value; + return; + } + + if (r < v) { + *res = ngx_http_variable_true_value; + return; + } + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script less: no"); + + *res = ngx_http_variable_null_value; +} + + +void +ngx_http_script_not_less_code(ngx_http_script_engine_t *e) +{ + ngx_int_t v, r; + + ngx_http_variable_value_t *val, *res; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script not less"); + + e->sp--; + val = e->sp; + res = e->sp - 1; + + e->ip += sizeof(uintptr_t); + + v = ngx_atoi(val->data, val->len); + if (v == NGX_ERROR) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script not less: val isn't number"); + + *res = ngx_http_variable_null_value; + return; + } + + r = ngx_atoi(res->data, res->len); + if (r == NGX_ERROR) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script less: res isn't number"); + + *res = ngx_http_variable_null_value; + return; + } + + if (!(r < v)) { + *res = ngx_http_variable_true_value; + return; + } + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, + "http script not less: no"); + + *res = ngx_http_variable_null_value; +} + + +void ngx_http_script_file_code(ngx_http_script_engine_t *e) { ngx_str_t path; diff --git a/src/http/ngx_http_script.h b/src/http/ngx_http_script.h index ea97069..3ce72aa 100644 --- a/src/http/ngx_http_script.h +++ b/src/http/ngx_http_script.h @@ -237,6 +237,10 @@ void ngx_http_script_break_code(ngx_http_script_engine_t *e); void ngx_http_script_if_code(ngx_http_script_engine_t *e); void ngx_http_script_equal_code(ngx_http_script_engine_t *e); void ngx_http_script_not_equal_code(ngx_http_script_engine_t *e); +void ngx_http_script_more_code(ngx_http_script_engine_t *e); +void ngx_http_script_not_more_code(ngx_http_script_engine_t *e); +void ngx_http_script_less_code(ngx_http_script_engine_t *e); +void ngx_http_script_not_less_code(ngx_http_script_engine_t *e); void ngx_http_script_file_code(ngx_http_script_engine_t *e); void ngx_http_script_complex_value_code(ngx_http_script_engine_t *e); void ngx_http_script_value_code(ngx_http_script_engine_t *e); -- 1.6.2