All pastes #2062420 Raw Edit

Something

public text v1 · immutable
#2062420 ·published 2011-05-17 12:50 UTC
rendered paste body
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <stdio.h>

typedef struct {
    ngx_flag_t enable;
} prs_ads_conf_t;

static char * prs_ads_filter_command(ngx_conf_t *cf, ngx_command_t*, void*);
static ngx_int_t prs_ads_handler(ngx_http_request_t*);
static char * prs_ads_merge_loc_conf(ngx_conf_t*, void*, void*);
static void * prs_ads_create_loc_conf(ngx_conf_t*);

static ngx_command_t prs_ads_filter_commands[] = {
    { ngx_string("proserver_ads_filter"),
      NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
      prs_ads_filter_command,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(prs_ads_conf_t, enable),
      NULL },

      ngx_null_command
};

static ngx_http_module_t  prs_ads_filter_ctx = {
    NULL,                       /* preconfiguration */
    NULL,/*prs_ads_filter_init,*/        /* postconfiguration */

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

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

    prs_ads_create_loc_conf,    /* create location configuration */
    prs_ads_merge_loc_conf      /* merge location configuration */
};

ngx_module_t  prs_ads_filter_module = {
    NGX_MODULE_V1,
    &prs_ads_filter_ctx,        /* module context */
    prs_ads_filter_commands,    /* 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 char * prs_ads_filter_command(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "Init");

    ngx_http_core_loc_conf_t *clcf;

    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    clcf->handler = prs_ads_handler;

    return NGX_CONF_OK;
}

static void * prs_ads_create_loc_conf(ngx_conf_t *cf) {
    prs_ads_conf_t *conf;

    conf = ngx_pcalloc(cf->pool, sizeof(prs_ads_conf_t));
    if (conf == NULL) {
        return NGX_CONF_ERROR;
    }

    conf->enable = NGX_CONF_UNSET;

    return conf;
}

static char * prs_ads_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "Merge");

    prs_ads_conf_t *prev = parent;
    prs_ads_conf_t *conf = child;

    ngx_conf_merge_value(conf->enable, prev->enable, 0);

    return NGX_CONF_OK;
}

static ngx_int_t prs_ads_handler(ngx_http_request_t *r) {
    ngx_log_t                 *log;

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    log = r->connection->log;
    ngx_log_error(NGX_LOG_ERR, log, 0, "Test!");

    r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
    if (r->headers_out.location == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    r->headers_out.location->key.len = sizeof("Location") - 1;
    r->headers_out.location->key.data = (u_char *) "Location";
    r->headers_out.location->value.len = sizeof("http://google.com") - 1;
    r->headers_out.location->value.data = (u_char *) "http://google.com";

    return NGX_HTTP_MOVED_PERMANENTLY;
}