【问题标题】:Nginx Module Post parse Request timeoutNginx 模块后解析请求超时
【发布时间】:2014-11-25 05:00:15
【问题描述】:

我为解析 GET、POST、Cookie、Header 请求编写了一个自定义模块。这工作正常,但是当我通过 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;
    }

【问题讨论】:

    标签: nginx module server


    【解决方案1】:

    最后我得到了解决方案。当任何人注册一个新的重写阶段处理程序时,比如 as

    ngx_http_read_client_request_body(r, ngx_http_ab_router_post_read);
    

    在 nginx 中完成工作后需要从 main 中删除该重写阶段处理程序。喜欢

    r->main->count--;
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      相关资源
      最近更新 更多