Chinaunix首页 | 论坛 | 博客
  • 博客访问: 144378
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 218
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:02
文章分类
文章存档

2017年(1)

2016年(20)

我的朋友

分类: LINUX

2016-08-30 16:55:14

本代码是在原书的基础上修改的,修改的返回的状态码(200->302)增加了location字段 使之自动跳转。

点击(此处)折叠或打开

  1. #include <ngx_core.h>
  2. #include <ngx_http.h>
  3. #include <nginx.h>

  4. typedef struct {
  5.     ngx_flag_t enable;
  6. } ngx_http_myfilter_conf_t;

  7. typedef struct {
  8.     ngx_int_t add_prefix;
  9. } ngx_http_myfilter_ctx_t;

  10. static void* ngx_http_myfilter_create_conf(ngx_conf_t *cf);
  11. static char* ngx_http_myfilter_merge_conf(ngx_conf_t *cf,void *parent, void *child);
  12. static ngx_int_t ngx_http_myfilter_init(ngx_conf_t *cf);
  13. static ngx_int_t ngx_http_myfilter_header_filter(ngx_http_request_t *r);
  14. static ngx_int_t ngx_http_myfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in);

  15. static ngx_str_t filter_prefix = ngx_string("[my filter prefix]");
  16. static ngx_command_t ngx_http_mytest_commands[] = {
  17.     {
  18.         ngx_string("add_prefix"),
  19.         NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_FLAG,
  20.         ngx_conf_set_flag_slot,
  21.         NGX_HTTP_LOC_CONF_OFFSET,
  22.         offsetof(ngx_http_myfilter_conf_t,enable),
  23.         NULL },
  24.     ngx_null_command
  25. };

  26. static ngx_http_module_t ngx_http_myfilter_module_ctx = {
  27.     NULL,
  28.     ngx_http_myfilter_init,
  29.     NULL,
  30.     NULL,

  31.     NULL,
  32.     NULL,
  33.     ngx_http_myfilter_create_conf,
  34.     ngx_http_myfilter_merge_conf
  35. };

  36. ngx_module_t ngx_http_myfilter_module = {
  37.     NGX_MODULE_V1,
  38.     &ngx_http_myfilter_module_ctx,
  39.     ngx_http_mytest_commands,
  40.     NGX_HTTP_MODULE,
  41.     NULL,
  42.     NULL,
  43.     NULL,
  44.     NULL,
  45.     NULL,
  46.     NULL,
  47.     NULL,
  48.     NGX_MODULE_V1_PADDING
  49. };

  50. static void* ngx_http_myfilter_create_conf(ngx_conf_t *cf)
  51. {
  52.     ngx_http_myfilter_conf_t *mycf;
  53.     mycf = (ngx_http_myfilter_conf_t *)ngx_pcalloc(cf->pool,sizeof(ngx_http_myfilter_conf_t));
  54.     if(mycf == NULL) {
  55.         return NULL;
  56.     }
  57.     mycf->enable = NGX_CONF_UNSET;
  58.     return mycf;
  59. }

  60. static char* ngx_http_myfilter_merge_conf(ngx_conf_t *cf,void *parent, void *child)
  61. {
  62.     ngx_http_myfilter_conf_t *prev = (ngx_http_myfilter_conf_t *)parent;
  63.     ngx_http_myfilter_conf_t *conf = (ngx_http_myfilter_conf_t *)child;

  64.     ngx_conf_merge_value(conf->enable,prev->enable,0);
  65.     return NGX_CONF_OK;
  66. }

  67. static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
  68. static ngx_http_output_body_filter_pt ngx_http_next_body_filter;

  69. static ngx_int_t ngx_http_myfilter_init(ngx_conf_t *cf)
  70. {
  71.     ngx_http_next_header_filter = ngx_http_top_header_filter;
  72.     ngx_http_top_header_filter = ngx_http_myfilter_header_filter;


  73.     ngx_http_next_body_filter = ngx_http_top_body_filter;
  74.     ngx_http_top_body_filter = ngx_http_myfilter_body_filter;
  75.     return NGX_OK;
  76. }

  77. static ngx_int_t
  78. ngx_http_myfilter_header_filter(ngx_http_request_t *r)
  79. {
  80.     ngx_http_myfilter_ctx_t *ctx;
  81.     ngx_http_myfilter_conf_t *conf;

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

  86.     ctx = ngx_http_get_module_ctx(r,ngx_http_myfilter_module);
  87.     if(ctx) {
  88.         return ngx_http_next_header_filter(r);
  89.     }
  90.     conf = ngx_http_get_module_loc_conf(r,ngx_http_myfilter_module);
  91.     if(conf->enable == 0)
  92.     {
  93.         return ngx_http_next_header_filter(r);
  94.     }
  95.     ctx = ngx_pcalloc(r->pool,sizeof(ngx_http_myfilter_ctx_t));
  96.     if(ctx == NULL)
  97.     {
  98.         return NGX_ERROR;
  99.     }
  100.     ctx->add_prefix = 0;

  101.     ngx_http_set_ctx(r,ctx,ngx_http_myfilter_module);

  102.     r->headers_out.status = NGX_HTTP_MOVED_TEMPORARILY;
  103.     ngx_str_t keys = ngx_string("Location");
  104.     ngx_str_t datas = ngx_string("");
  105.     ngx_table_elt_t *headers = ngx_list_push(&r->headers_out.headers);
  106.     if(headers){
  107.         headers->key.data = keys.data;
  108.         headers->key.len = keys.len;
  109.         headers->value.data = datas.data;
  110.         headers->value.len = datas.len;
  111.         headers->hash = 1;
  112.     }
  113.     
  114.     if(r->headers_out.content_type.len >= sizeof("text/plain")-1
  115.             && ngx_strncasecmp(r->headers_out.content_type.data,(u_char *)"text/plain",
  116.                 sizeof("text/plain")-1) == 0)
  117.     {
  118.         ctx->add_prefix = 1;
  119.         if(r->headers_out.content_length_n > 0) {
  120.             r->headers_out.content_length_n += filter_prefix.len;
  121.         }
  122.     }
  123.     return ngx_http_myfilter_header_filter(r);
  124. }

  125.     static ngx_int_t
  126. ngx_http_myfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
  127. {
  128.     ngx_http_myfilter_ctx_t *ctx;
  129.     ctx = ngx_http_get_module_ctx(r,ngx_http_myfilter_module);
  130.     if(ctx==NULL||ctx->add_prefix != 1) {
  131.         return ngx_http_next_body_filter(r,in);
  132.     }

  133.     ctx->add_prefix = 2;

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

  137.     ngx_chain_t *c1 = ngx_alloc_chain_link(r->pool);
  138.     c1->buf = b;
  139.     c1->next = in;
  140.     return ngx_http_next_body_filter(r,c1);
  141. }

阅读(2640) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~