Skip to content

Commit

Permalink
Added decode base64 variable
Browse files Browse the repository at this point in the history
  • Loading branch information
yzprofile committed Sep 5, 2013
1 parent 31d056d commit b5b2507
Showing 1 changed file with 86 additions and 1 deletion.
87 changes: 86 additions & 1 deletion src/http/ngx_http_variables.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
Expand Down Expand Up @@ -36,6 +35,9 @@ static ngx_int_t ngx_http_variable_sent_cookie(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_argument(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_decode_base64(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);

#if (NGX_HAVE_TCP_INFO)
static ngx_int_t ngx_http_variable_tcpinfo(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
Expand Down Expand Up @@ -376,6 +378,9 @@ static ngx_http_variable_t ngx_http_core_variables[] = {
{ ngx_string("time_local"), NULL, ngx_http_variable_time_local,
0, NGX_HTTP_VAR_NOCACHEABLE, 0 },

{ ngx_string("decode_base64_"), NULL, ngx_http_variable_decode_base64,
0, NGX_HTTP_VAR_NOCACHEABLE, 0 },

#if (NGX_HAVE_TCP_INFO)
{ ngx_string("tcpinfo_rtt"), NULL, ngx_http_variable_tcpinfo,
0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
Expand Down Expand Up @@ -680,6 +685,16 @@ ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key)
return NULL;
}

if (ngx_strncmp(name->data, "decode_base64_", 14) == 0) {

if (ngx_http_variable_decode_base64(r, vv, (uintptr_t) name) == NGX_OK)
{
return vv;
}

return NULL;
}

vv->not_found = 1;

return vv;
Expand Down Expand Up @@ -1109,6 +1124,69 @@ ngx_http_variable_argument(ngx_http_request_t *r, ngx_http_variable_value_t *v,
}


static ngx_int_t
ngx_http_variable_decode_base64(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_str_t *name = (ngx_str_t *) data;

size_t len;
u_char *low, *p;
ngx_str_t var, src;
ngx_uint_t hash;
ngx_http_variable_value_t *vv;

len = name->len - (sizeof("decode_base64_") - 1);

low = ngx_pnalloc(r->pool, len);
if (low == NULL) {
return NGX_ERROR;
}

p = name->data + sizeof("decode_base64_") - 1;

hash = ngx_hash_strlow(low, p, len);

var.len = len;
var.data = low;

vv = ngx_http_get_variable(r, &var, hash);

if (vv == NULL || vv->not_found) {
v->not_found = 1;
return NGX_OK;
}

src.data = vv->data;
src.len = vv->len;

var.len = ngx_base64_decoded_length(src.len);
var.data = ngx_palloc(r->pool, var.len);
if (var.data == NULL) {
return NGX_ERROR;
}

if (ngx_decode_base64(&var, &src) != NGX_OK) {

v->data = src.data;
v->len = src.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;

return NGX_OK;
}

v->data = var.data;
v->len = var.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;

return NGX_OK;
}


#if (NGX_HAVE_TCP_INFO)

static ngx_int_t
Expand Down Expand Up @@ -2764,6 +2842,13 @@ ngx_http_variables_init_vars(ngx_conf_t *cf)
continue;
}

if (ngx_strncmp(v[i].name.data, "decode_base64_", 14) == 0) {
v[i].get_handler = ngx_http_variable_decode_base64;
v[i].data = (uintptr_t) &v[i].name;

continue;
}

ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"unknown \"%V\" variable", &v[i].name);

Expand Down

0 comments on commit b5b2507

Please sign in to comment.