Ryan Burn
Ryan Burn

Reputation: 2316

What's the best way for nginx modules to expand variables in directives?

Suppose I'm writing an nginx module that declares some directive mymod_opt available in location blocks and a user writes something like the following:

location / {
  mymod_opt $uri;
  ...
}

What's the best way for the module to expand any variables so that it gets an ngx_str_t with the actual uri instead of "$uri"?

Upvotes: 1

Views: 1182

Answers (1)

Ryan Burn
Ryan Burn

Reputation: 2316

It looks like the best way to do this is to treat the variable as a script, using the function ngx_http_script_compile when the configuration is first processed for compilation and ngx_http_script_run when the module is invoked to expand the variables. Here's an example of how it can be set up:

Add variables in the module's local configuration to store the comiled script.

struct ngx_http_mymod_loc_conf_t {
  ngx_array_t* opt_lengths;
  ngx_array_t* opt_values;
};

Specify in the command array to run a custom function when the directive is encountered.

static ngx_command_t ngx_http_mymod_commands[] = {
  {
    ngx_string("mymod_opt"),
    NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1, 
    ngx_http_mymod_opt,
    NGX_HTTP_LOC_CONF_OFFSET, 
    0, 
    NULL
  },
  ...
}

Add a function to compile the script

static char *ngx_http_mymod_opt(ngx_conf_t *cf, ngx_command_t *command,
                                void *conf) {
  ngx_http_mymod_loc_conf_t *loc_conf;
  ngx_str_t *value;
  ngx_str_t *mymod_opt;
  ngx_http_script_compile_t script_compile;

  loc_conf = conf;
  value = cf->args->elts;
  mymod_opt = &value[1];

  ngx_memzero(&script_compile, sizeof(ngx_http_script_compile_t));
  script_compile.cf = cf;
  script_compile.source = mymod_opt;
  script_compile.lengths = &loc_conf->mymod_opt_lengths;
  script_compile.values = &loc_conf->mymod_opt_values;
  script_compile.variables = ngx_http_script_variables_count(mymod_opt);
  script_compile.complete_lengths = 1;
  script_compile.complete_values = 1;

  if (ngx_http_script_compile(&script_compile) != NGX_OK) return NGX_CONF_ERROR;

  return NGX_CONF_OK;
}

And then the variables can later expanded with this function call:

ngx_str_t result;
ngx_http_script_run(request, &result, loc_conf->lengths->elts, 0, 
                    loc_conf->values->elts)

Upvotes: 3

Related Questions