2014-11-25 163 views
0

我写了一个用于解析GET,POST,Cookie,Header请求的自定义模块。这工作正常,但是当我通过POST请求,然后第一个请求工作正常,在第二个请求没有响应之后。正在请求时间。我不明白是什么问题。我流:Nginx模块Post分析请求超时

this module.

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



    static char *ngx_http_ab_router_post_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 
    static ngx_int_t ngx_http_ab_router_prefix_init(ngx_conf_t *cf); 

    ngx_flag_t ngx_http_ab_router_post_read_used = 0; 

    static ngx_int_t ngx_http_ab_router_request_handler(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) { 
     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "CALLED HANDLER"); 

     static char *prefix = NULL; 
     char *header_params_name = "access_token"; 
     ngx_http_ab_router_post_read_used = 1; 
     /*Parse Cookie Portion*/ 
     ngx_int_t response; 
     ngx_str_t cookie_key = (ngx_str_t) ngx_string("ckisession"); 
     ngx_str_t cookie_value; 
     response = ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &cookie_key, &cookie_value); 
     if (response != NGX_DECLINED) { 
      ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, (char *) cookie_value.data); 
      prefix = (char *) cookie_value.data; 
     } else { 
      ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Cookie to nai"); 
     } 

     /*Parse Header Portion*/ 
     if (ngx_http_ab_router_headers_value(r, header_params_name, &prefix) == NGX_OK) { 
      ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, prefix); 
     } else if (r->method == NGX_HTTP_GET) { 
      /*Parse GET Request Parameter Portion*/ 
      if (ngx_http_ab_router_get_params(r, &prefix) == NGX_OK) { 
       ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, prefix); 

      } 
     } else if (r->method == NGX_HTTP_POST) { 
      if (ngx_http_ab_router_parse_post_json(r, &prefix) == NGX_OK) { 


      } 
     } 

     if (prefix != NULL) { 
      char *buffer; 
      if (ngx_http_ab_router_decrypt_prifix(prefix, &buffer) == NGX_OK) { 
       ngx_http_variable_value_t *vv = v; 
       vv->data = (u_char *) buffer; 
       if (vv->data == NULL) { 
        vv->valid = 0; 
        vv->not_found = 1; 
       } else { 
        vv->len = ngx_strlen(vv->data); 
        vv->valid = 1; 
        vv->no_cacheable = 0; 
        vv->not_found = 0; 
       } 
      } 
      return NGX_OK; 
     } else { 
      return NGX_OK; 
      ; 
     } 
    } 


    static ngx_command_t ngx_http_ab_router_commands[] = { 
     { ngx_string("ab_server_type"), 
      NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS, 
      ngx_http_ab_router_post_init, 
      0, 
      0, 
      NULL}, 
     ngx_null_command 
    }; 

    static ngx_http_module_t ngx_http_ab_router_module_ctx = { 
     NULL, /* preconfiguration */ 
     ngx_http_ab_router_prefix_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_ab_router_module = { 
     NGX_MODULE_V1, 
     &ngx_http_ab_router_module_ctx, /* module context */ 
     ngx_http_ab_router_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 
    }; 

    typedef struct { 
     unsigned done : 1; 
     unsigned waiting_more_body : 1; 
    } ngx_http_ab_router_ctx_t; 

    static void 
    ngx_http_ab_router_post_read(ngx_http_request_t *r) { 
     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Request Body Here"); 
     ngx_http_ab_router_ctx_t *ctx; 
     ctx = ngx_http_get_module_ctx(r, ngx_http_ab_router_module); 
     ctx->done = 1; 

    #if defined(nginx_version) && nginx_version >= 8011 
     dd("count--"); 
     r->main->count--; 
    #endif 
     /* waiting_more_body my rewrite phase handler */ 
     if (ctx->waiting_more_body) { 
      ctx->waiting_more_body = 0; 

      ngx_http_core_run_phases(r); 
     } 
    } 

    static ngx_int_t 
    ngx_http_ab_router_post_parser(ngx_http_request_t *r) { 
       ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "CALLED POST REQUEST"); 
     ngx_http_ab_router_ctx_t *ctx; 
     ngx_int_t rc; 
     ctx = ngx_http_get_module_ctx(r, ngx_http_ab_router_module); 

     if (ctx != NULL) { 
      if (ctx->done) { 
       return NGX_DECLINED; 
      } 
      return NGX_DONE; 
     } 

     if (r->method != NGX_HTTP_POST) { 
      return NGX_DECLINED; 
     } 

     ctx = ngx_pcalloc(r->pool, sizeof (ngx_http_ab_router_ctx_t)); 
     if (ctx == NULL) { 
      return NGX_ERROR; 
     } 
     ngx_http_set_ctx(r, ctx, ngx_http_ab_router_module); 
     rc = ngx_http_read_client_request_body(r, ngx_http_ab_router_post_read); 

     if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) { 
    #if (nginx_version < 1002006) ||            \ 
      (nginx_version >= 1003000 && nginx_version < 1003009) 
      r->main->count--; 
    #endif 

      return rc; 
     } 

     if (rc == NGX_AGAIN) { 
      ctx->waiting_more_body = 1; 
      return NGX_DONE; 
     } 
     return NGX_DECLINED; 
    } 

    static ngx_int_t ngx_http_ab_router_prefix_init(ngx_conf_t *cf) { 
     ngx_http_variable_t *ab_prefix_var; 
     ngx_str_t ab_prefix_var_name = ngx_string("ab_prefix"); 
     ab_prefix_var = ngx_http_add_variable(cf, &ab_prefix_var_name, NGX_HTTP_VAR_NOCACHEABLE); 
     if (ab_prefix_var == NULL) { 
      return NGX_ERROR; 
     } 
     ab_prefix_var->get_handler = ngx_http_ab_router_request_handler; 


     ngx_http_handler_pt *h; 
     ngx_http_core_main_conf_t *cmcf; 
     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); 
     h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers); 
     if (h == NULL) { 
      return NGX_ERROR; 
     } 
     *h = ngx_http_ab_router_post_parser; 
     return NGX_OK; 
    } 

    static char *ngx_http_ab_router_post_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { 
     return NGX_CONF_OK; 
    } 

回答

1

最后我得到了解决。当任何一个注册一个新的重写阶段处理程序等作为

ngx_http_read_client_request_body(r, ngx_http_ab_router_post_read); 

在nginx的后所做的工作需要从主删除重写阶段的处理程序。像

r->main->count--; 

这是我没有删除处理程序的问题。