Nginx_开发一个简单的HTTP过滤模块

原创:https://blog.csdn.net/ndzjx/article/details/89503058

 

过滤模块仅处理服务器发往客户端的HTTP响应。

config文件:

ngx_addon_name=ngx_http_myfilter_module
HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES ngx_http_myfilter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_myfilter_module.c"

ngx_http_myfilter_module.c文件内容:

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

/* 过滤模块是在链表头插入,所有执行顺序与ngx_modules[]中的顺序相反,
 * 大多数官方HTTP过滤模块都会把初始化方法放到ngx_http_module_t的postconfiguration中
 *
 */

typedef struct {
    ngx_flag_t enable;
} ngx_http_myfilter_conf_t;

/* 0:不加前缀  1:加前缀  2:已经加过前缀
 * 定义三个值是因为HTTP头部处理方法在1个请求中只会被调用1次,但包体处理方法在1个请求中是有可能被多次调用的,
 * 而前缀我们只希望加一次。
*/
typedef struct {
    ngx_int_t add_prefix_flag;
}ngx_http_myfilter_ctx_t;

static ngx_int_t ngx_http_myfilter_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_myfilter_header_filter(ngx_http_request_t *r);
static ngx_int_t ngx_http_myfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in);

/* 两个静态指针只在文件范围内有效 
 * 发送头部ngx_http_send_header与发送包体ngx_http_output_filter会调用这两个方法
*/
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
static ngx_str_t filter_prefix = ngx_string("\r\n[--------my filter prefix--------]\r\n");

static void *
ngx_http_myfilter_create_conf(ngx_conf_t *cf)
{
    ngx_http_myfilter_conf_t *mycf;
    mycf = (ngx_http_myfilter_conf_t *)ngx_pcalloc(cf->pool, sizeof (ngx_http_myfilter_conf_t));
    if (mycf == NULL) {
        return NULL;
    }

    mycf->enable = NGX_CONF_UNSET;

    return mycf;
}

static char *
ngx_http_myfilter_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{
    ngx_http_myfilter_conf_t *prev = (ngx_http_myfilter_conf_t *)parent;
    ngx_http_myfilter_conf_t *conf = (ngx_http_myfilter_conf_t *)child;

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

    return NGX_CONF_OK;
}

static ngx_command_t ngx_http_myfilter_commands[] = {
    { ngx_string("add_prefix"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_myfilter_conf_t, enable),
      NULL },
    ngx_null_command
};

static ngx_http_module_t ngx_http_myfilter_module_ctx = {
    NULL,
    ngx_http_myfilter_init, /* postconfiguration */
    NULL,
    NULL,
    NULL,
    NULL,
    ngx_http_myfilter_create_conf,
    ngx_http_myfilter_merge_conf
};

ngx_module_t ngx_http_myfilter_module = {
    NGX_MODULE_V1,
    &ngx_http_myfilter_module_ctx,
    ngx_http_myfilter_commands,
    NGX_HTTP_MODULE,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NGX_MODULE_V1_PADDING
};

static ngx_int_t
ngx_http_myfilter_init(ngx_conf_t *cf)
{
    // 每个HTTP过滤模块都有这4个指针用来指向自己和链表
    ngx_http_next_header_filter = ngx_http_top_header_filter;
    ngx_http_top_header_filter = ngx_http_myfilter_header_filter;

    ngx_http_next_body_filter = ngx_http_top_body_filter;
    ngx_http_top_body_filter = ngx_http_myfilter_body_filter;

    return NGX_OK;
}

static ngx_int_t
ngx_http_myfilter_header_filter(ngx_http_request_t *r)
{
    ngx_http_myfilter_ctx_t     *ctx;
    ngx_http_myfilter_conf_t    *conf;

    if (r->headers_out.status != NGX_HTTP_OK) {
        return ngx_http_next_header_filter(r);
    }

    /* 获取HTTP上下文 */
    ctx = ngx_http_get_module_ctx(r, ngx_http_myfilter_module);
    if (ctx) {
        return ngx_http_next_header_filter(r);
    }

    /* 获取存储配置项ngx_http_myfilter_conf_t结构体 */
    conf = ngx_http_get_module_loc_conf(r, ngx_http_myfilter_module);
    /* 如果enable为0,就是配置文件中没有配置add_prefix或者add_prefix值为off,则交给下一个过滤模块处理 */
    if (conf->enable == 0) {
        return ngx_http_next_header_filter(r);
    }

    ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_myfilter_ctx_t));
    if (ctx == NULL) {
        return NGX_ERROR;
    }

    ctx->add_prefix_flag = 0;

    ngx_http_set_ctx(r, ctx, ngx_http_myfilter_module);
    if (r->headers_out.content_type.len >= sizeof("text/plain") - 1
            && ngx_strncasecmp(r->headers_out.content_type.data, (u_char *)"text/plain", sizeof("text/plain") - 1) == 0) {
        ctx->add_prefix_flag = 1;

        if (r->headers_out.content_length_n > 0) {
            r->headers_out.content_length_n += filter_prefix.len;
        }
    }

    return ngx_http_next_header_filter(r);
}

static ngx_int_t
ngx_http_myfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
    ngx_http_myfilter_ctx_t *ctx;
    ctx = ngx_http_get_module_ctx(r, ngx_http_myfilter_module);

    if (ctx == NULL || ctx->add_prefix_flag != 1) {
        return ngx_http_next_body_filter(r, in);
    }

    ctx->add_prefix_flag = 2;

    ngx_buf_t *b = ngx_create_temp_buf(r->pool, filter_prefix.len);
    b->start = b->pos = filter_prefix.data;
    b->last = b->pos + filter_prefix.len;

    ngx_chain_t *cl = ngx_alloc_chain_link(r->pool);
    cl->buf = b;
    cl->next = in;

    return ngx_http_next_body_filter(r, cl);
}

如果配合之前那个简单的HTTP模块需要这样执行:多个模块加多个--add-module即可

./configure --add-module=PATH1/myfiltermodules --add-module=PATH2/mytestmodules

make

sudo make install

 

nginx.conf配置如下:

location /foo {
            mytest;
            add_prefix on;
        }

curl localhost:80/foo

结果为:

[--------my filter prefix--------]
Hello World!

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值