Skip to content

Commit

Permalink
Merge pull request alibaba#315 from flygoast/master
Browse files Browse the repository at this point in the history
add numeric comparison for 'if' directive
  • Loading branch information
Chuanwen Chen committed Sep 6, 2013
2 parents 31d056d + 4d68b3f commit 8dc9cfb
Show file tree
Hide file tree
Showing 4 changed files with 507 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/http/modules/ngx_http_rewrite_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,74 @@ 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_greater_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_greater_or_equal_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_less_or_equal_code;
return NGX_CONF_OK;

}

if (len == 2 && p[0] == '!' && p[1] == '=') {

if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
Expand Down
156 changes: 156 additions & 0 deletions src/http/ngx_http_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,162 @@ ngx_http_script_equal_code(ngx_http_script_engine_t *e)
}


void
ngx_http_script_greater_code(ngx_http_script_engine_t *e)
{
ngx_http_variable_value_t *val, *res;
ngx_int_t val_n, res_n;

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
"http script greater");

e->sp--;
val = e->sp;
res = e->sp - 1;

e->ip += sizeof(uintptr_t);

val_n = ngx_atoi(val->data, val->len);
if (val_n == NGX_ERROR) {
*res = ngx_http_variable_null_value;
return;
}

res_n = ngx_atoi(res->data, res->len);
if (res_n == NGX_ERROR) {
*res = ngx_http_variable_null_value;
return;
}

if (res_n > val_n) {
*res = ngx_http_variable_true_value;
return;
}

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
"http script greater: no");

*res = ngx_http_variable_null_value;
}


void
ngx_http_script_less_code(ngx_http_script_engine_t *e)
{
ngx_http_variable_value_t *val, *res;
ngx_int_t val_n, res_n;

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);

val_n = ngx_atoi(val->data, val->len);
if (val_n == NGX_ERROR) {
*res = ngx_http_variable_null_value;
return;
}

res_n = ngx_atoi(res->data, res->len);
if (res_n == NGX_ERROR) {
*res = ngx_http_variable_null_value;
return;
}

if (res_n < val_n) {
*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_greater_or_equal_code(ngx_http_script_engine_t *e)
{
ngx_http_variable_value_t *val, *res;
ngx_int_t val_n, res_n;

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
"http script greater or equal");

e->sp--;
val = e->sp;
res = e->sp - 1;

e->ip += sizeof(uintptr_t);

val_n = ngx_atoi(val->data, val->len);
if (val_n == NGX_ERROR) {
*res = ngx_http_variable_null_value;
return;
}

res_n = ngx_atoi(res->data, res->len);
if (res_n == NGX_ERROR) {
*res = ngx_http_variable_null_value;
return;
}

if (res_n >= val_n) {
*res = ngx_http_variable_true_value;
return;
}

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
"http script greater or equal: no");

*res = ngx_http_variable_null_value;
}


void
ngx_http_script_less_or_equal_code(ngx_http_script_engine_t *e)
{
ngx_http_variable_value_t *val, *res;
ngx_int_t val_n, res_n;

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
"http script less or equal");

e->sp--;
val = e->sp;
res = e->sp - 1;

e->ip += sizeof(uintptr_t);

val_n = ngx_atoi(val->data, val->len);
if (val_n == NGX_ERROR) {
*res = ngx_http_variable_null_value;
return;
}

res_n = ngx_atoi(res->data, res->len);
if (res_n == NGX_ERROR) {
*res = ngx_http_variable_null_value;
return;
}

if (res_n <= val_n) {
*res = ngx_http_variable_true_value;
return;
}

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
"http script less or equal: no");

*res = ngx_http_variable_null_value;
}


void
ngx_http_script_not_equal_code(ngx_http_script_engine_t *e)
{
Expand Down
4 changes: 4 additions & 0 deletions src/http/ngx_http_script.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ void ngx_http_script_return_code(ngx_http_script_engine_t *e);
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_greater_code(ngx_http_script_engine_t *e);
void ngx_http_script_less_code(ngx_http_script_engine_t *e);
void ngx_http_script_greater_or_equal_code(ngx_http_script_engine_t *e);
void ngx_http_script_less_or_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_file_code(ngx_http_script_engine_t *e);
void ngx_http_script_complex_value_code(ngx_http_script_engine_t *e);
Expand Down
Loading

0 comments on commit 8dc9cfb

Please sign in to comment.