[nginx] problem with a sample module

Jérôme Loyet jerome at loyet.net
Tue Jun 1 10:36:05 MSD 2010


Hi,

I'm writing a very simple module which add a header.

In the postconfigure function, I backup up ngx_http_top_header_filter
and set my own filter instead.

static ngx_int_t
ngx_http_xtoken_init(ngx_conf_t *cf)
{
  ngx_http_next_header_filter = ngx_http_top_header_filter;
  ngx_http_top_header_filter = ngx_http_xtoken_clear_cookie_handler;
  return NGX_OK;
}

But the function ngx_http_xtoken_filter seems to be never called.

The module is compile with --add-module=/path/to/module

do you have any clue of what I did wrong ?

Thx
++ jerome

Here is my complete source code:
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

static ngx_int_t  ngx_http_xtoken_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_xtoken_filter(ngx_http_request_t *r);

static ngx_http_module_t  ngx_http_xtoken_module_ctx = {
    NULL,                            /* preconfiguration */
    ngx_http_xtoken_init,            /* postconfiguration */

    NULL,                            /* create main configuration */
    NULL,                            /* init main configuration */

    NULL,                            /* create server configuration */
    NULL,                            /* merge server configuration */

    NULL,                            /* create location configuration */
    NULL                             /* merge location configuration */
};


ngx_module_t  ngx_http_xtoken_module = {
    NGX_MODULE_V1,
    &ngx_http_xtoken_module_ctx, /* module context */
    NULL,   /* module directives */
    NGX_HTTP_MODULE,               /* module type */
    NULL,                          /* init master */
    NULL,                          /* init module */
    NULL,                          /* init process */
    NULL,                          /* init thread */
    NULL,                          /* exit thread */
    NULL,                          /* exit process */
    NULL,                          /* exit master */
    NGX_MODULE_V1_PADDING
};

static ngx_http_output_header_filter_pt  ngx_http_next_header_filter;


static ngx_int_t
ngx_http_xtoken_init(ngx_conf_t *cf)
{
  ngx_http_next_header_filter = ngx_http_top_header_filter;
  ngx_http_top_header_filter = ngx_http_xtoken_clear_cookie_handler;
  return NGX_OK;
}

static ngx_int_t
ngx_http_xtoken_filter(ngx_http_request_t *r)
{
    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "[xtoken] enter filter");
    set_cookie = ngx_list_push(&r->headers_out.headers);
    if (set_cookie == NULL) {
      return NGX_ERROR;
    }

    set_cookie->hash = 1;
    set_cookie->key.len = sizeof("X-test42") - 1;
    set_cookie->key.data = (u_char *) "X-test42";
    set_cookie->value.len = sizeof("foo/bar") - 1;
    set_cookie->value.data = (u_char *)"foo/bar";

    return ngx_http_next_header_filter(r);
}



More information about the nginx-devel mailing list