[PATCH] export variables from the log module

Vladimir Shebordaev vshebordaev at mail.ru
Sun Sep 30 05:11:15 UTC 2012


On 30.09.2012 07:55, Vladimir Shebordaev wrote:
> Index: src/http/ngx_http_variables.c
> ===================================================================
> --- src/http/ngx_http_variables.c    (revision 4872)
> +++ src/http/ngx_http_variables.c    (working copy)
> @@ -487,12 +487,14 @@
>           } else {
>
>               vv = ngx_palloc(r->pool,
> sizeof(ngx_http_variable_value_t));
> -
> -            if (vv && v->get_handler(r, vv, v->data) == NGX_OK) {
> -                return vv;
> +            if (vv) {
> +
> +                vv->len = 0;

The idea behind this is to indicate unallocated value data to the 
getter. Probably, zero vv-data field would be cleaner solution, 
since the getter can just do nothing that gives zero value length 
while the space has been actually allocated.

> +                if (v->get_handler(r, vv, v->data) != NGX_OK) {
> +                    vv = NULL;
> +                }
>               }
> -
> -            return NULL;
> +            return vv;
>           }
>       }
>
> Index: src/http/modules/ngx_http_log_module.c
> ===================================================================
> --- src/http/modules/ngx_http_log_module.c    (revision 4872)
> +++ src/http/modules/ngx_http_log_module.c    (working copy)
> @@ -114,6 +114,7 @@
>   static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
>   static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void
> *parent,
>       void *child);
> +static ngx_int_t ngx_http_log_add_variables(ngx_conf_t *cf);
>   static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t
> *cmd,
>       void *conf);
>   static char *ngx_http_log_set_format(ngx_conf_t *cf,
> ngx_command_t *cmd,
> @@ -154,7 +155,7 @@
>
>
>   static ngx_http_module_t  ngx_http_log_module_ctx = {
> -    NULL,                                  /* preconfiguration */
> +    ngx_http_log_add_variables,            /* preconfiguration */
>       ngx_http_log_init,                     /* postconfiguration */
>
>       ngx_http_log_create_main_conf,         /* create main
> configuration */
> @@ -1338,6 +1339,70 @@
>
>
>   static ngx_int_t
> +ngx_http_log_get_variable(ngx_http_request_t *r,
> ngx_variable_value_t *value, uintptr_t data)
> +{
> +    ngx_http_log_var_t *v;
> +
> +    v = (ngx_http_log_var_t *)data;
> +
> +    if (!value->len) {

The same field should be checked here.

Regards,
Vladimir

> +        value->data = ngx_pnalloc(r->pool, v->len);
> +        if (value->data == NULL) {
> +            return NGX_ERROR;
> +        }
> +    }
> +
> +    value->len = v->run(r, value->data, NULL) - value->data;
> +
> +    value->not_found = 0;
> +    value->valid = 1;
> +
> +    return NGX_OK;
> +}
> +
> +
> +static ngx_int_t
> +ngx_http_log_add_variables(ngx_conf_t *cf)
> +{
> +    ngx_http_log_var_t *v;
> +    ngx_http_variable_t *var;
> +    ngx_http_core_main_conf_t *cmcf;
> +
> +    ngx_int_t rc;
> +
> +    cmcf = ngx_http_conf_get_module_main_conf(cf,
> ngx_http_core_module);
> +
> +    var = NULL;
> +
> +    for (v = ngx_http_log_vars; v->name.len; v++) {
> +
> +        if (var == NULL) {
> +            var = ngx_pcalloc(cf->pool,
> sizeof(ngx_http_variable_t));
> +            if (var == NULL) {
> +                return NGX_ERROR;
> +            }
> +        }
> +
> +        var->name = v->name;
> +
> +        rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, var,
> +                              NGX_HASH_READONLY_KEY);
> +
> +        if (rc == NGX_BUSY) {
> +            continue;
> +        }
> +
> +        var->get_handler = ngx_http_log_get_variable;
> +        var->data = (uintptr_t)v;
> +        var->flags =
> NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE;
> +
> +        var = NULL;
> +    }
> +
> +    return NGX_OK;
> +}
> +
> +static ngx_int_t
>   ngx_http_log_init(ngx_conf_t *cf)
>   {
>       ngx_str_t                  *value;
>
>
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel
>



More information about the nginx-devel mailing list