Nginx源码阅读(ngx_http_process_request)

ngx_http_process_request()执行流程

ngx_http_process_request() {
    ...
    ngx_http_handler() {
        ...
        ngx_http_core_run_phases()
    }
    ngx_http_run_posted_requests() // 处理post请求
}

ngx_http_process_request()

void
ngx_http_process_request(ngx_http_request_t *r)
{
    ngx_connection_t  *c;

    c = r->connection;

#if (NGX_HTTP_SSL)

    if (r->http_connection->ssl) {
        long                      rc;
        X509                     *cert;
        ngx_http_ssl_srv_conf_t  *sscf;

        if (c->ssl == NULL) {
            ngx_log_error(NGX_LOG_INFO, c->log, 0,
                          "client sent plain HTTP request to HTTPS port");
            ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
            return;
        }

        sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);

        if (sscf->verify) {
            rc = SSL_get_verify_result(c->ssl->connection);

            if (rc != X509_V_OK
                && (sscf->verify != 3 || !ngx_ssl_verify_error_optional(rc)))
            {
                ngx_log_error(NGX_LOG_INFO, c->log, 0,
                              "client SSL certificate verify error: (%l:%s)",
                              rc, X509_verify_cert_error_string(rc));

                ngx_ssl_remove_cached_session(sscf->ssl.ctx,
                                       (SSL_get0_session(c->ssl->connection)));

                ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
                return;
            }

            if (sscf->verify == 1) {
                cert = SSL_get_peer_certificate(c->ssl->connection);

                if (cert == NULL) {
                    ngx_log_error(NGX_LOG_INFO, c->log, 0,
                                  "client sent no required SSL certificate");

                    ngx_ssl_remove_cached_session(sscf->ssl.ctx,
                                       (SSL_get0_session(c->ssl->connection)));

                    ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
                    return;
                }

                X509_free(cert);
            }
        }
    }

#endif

    // 若读事件在定时器中
    if (c->read->timer_set) {
        // 将读事件从定时器中删除
        ngx_del_timer(c->read);
    }

#if (NGX_STAT_STUB)
    (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
    r->stat_reading = 0;
    (void) ngx_atomic_fetch_add(ngx_stat_writing, 1);
    r->stat_writing = 1;
#endif

    // 修改读写事件的回调函数为ngx_http_request_handler()
    c->read->handler = ngx_http_request_handler;
    c->write->handler = ngx_http_request_handler;
    // 设置r->read_event_handler为ngx_http_block_reading()
    r->read_event_handler = ngx_http_block_reading;

    ngx_http_handler(r);

    ngx_http_run_posted_requests(c);
}


// 调用r->read_event_handler或r->write_event_handler
static void
ngx_http_request_handler(ngx_event_t *ev)
{
    ngx_connection_t    *c;
    ngx_http_request_t  *r;

    c = ev->data;
    r = c->data;

    ngx_http_set_log_request(c->log, r);

    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
                   "http run request: \"%V?%V\"", &r->uri, &r->args);

    if (ev->write) {
        r->write_event_handler(r);

    } else {
        r->read_event_handler(r);
    }

    ngx_http_run_posted_requests(c);
}


void
ngx_http_block_reading(ngx_http_request_t *r)
{
    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "http reading blocked");

    /* aio does not call this handler */

    if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
        && r->connection->read->active)
    {
        if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
            ngx_http_close_request(r, 0);
        }
    }
}

ngx_http_handler()

/* ngx_http_phase_engine_t结构体的handlers数组保存了请求需要经历的所有回调方法,
   ngx_http_request_t结构体的phase_handler指定着请求将要执行的handlers数组中的方法位置 */
typedef struct {
    ...
    ngx_http_phase_engine_t    phase_engine;
    ...
} ngx_http_core_main_conf_t;


typedef struct {
    ngx_http_phase_handler_t  *handlers;
    ...
} ngx_http_phase_engine_t;


struct ngx_http_phase_handler_s {
    ngx_http_phase_handler_pt  checker; // 同一阶段的所有handler的checker相同
    ngx_http_handler_pt        handler;
    ngx_uint_t                 next;
};
void
ngx_http_handler(ngx_http_request_t *r)
{
    ngx_http_core_main_conf_t  *cmcf;

    r->connection->log->action = NULL;

    r->connection->unexpected_eof = 0;

    // 若不需要重定向
    if (!r->internal) {
        switch (r->headers_in.connection_type) {
        case 0:
            r->keepalive = (r->http_version > NGX_HTTP_VERSION_10);
            break;

        case NGX_HTTP_CONNECTION_CLOSE:
            r->keepalive = 0;
            break;

        case NGX_HTTP_CONNECTION_KEEP_ALIVE:
            r->keepalive = 1;
            break;
        }

        r->lingering_close = (r->headers_in.content_length_n > 0
                              || r->headers_in.chunked);
        // phase_handler为0表示从ngx_http_phase_engine_t结构体的handlers数组的第一个回调方法开始执行
        r->phase_handler = 0;

    } else {
        cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
        // phase_handler为server_rewrite_index表示从NGX_HTTP_SERVER_REWRITE_PHASE阶段的第一个回调方法开始执行
        r->phase_handler = cmcf->phase_engine.server_rewrite_index;
    }

    r->valid_location = 1;
#if (NGX_HTTP_GZIP)
    r->gzip_tested = 0;
    r->gzip_ok = 0;
    r->gzip_vary = 0;
#endif

    // 设置r->write_event_handler为ngx_http_core_run_phases()并执行该函数
    r->write_event_handler = ngx_http_core_run_phases;
    ngx_http_core_run_phases(r);
}

ngx_http_core_run_phase()

void
ngx_http_core_run_phases(ngx_http_request_t *r)
{
    ngx_int_t                   rc;
    ngx_http_phase_handler_t   *ph;
    ngx_http_core_main_conf_t  *cmcf;

    cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

    ph = cmcf->phase_engine.handlers;

    while (ph[r->phase_handler].checker) {

        // checker方法会修改r->phase_handler
        rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);

        // 若checker方法返回NGX_OK,意味着把控制权交还给事件模块;否则意味着向下执行handlers数组中的回调方法
        if (rc == NGX_OK) {
            return;
        }
    }
}

ngx_http_request_handler()

static void
ngx_http_request_handler(ngx_event_t *ev)
{
    ngx_connection_t    *c;
    ngx_http_request_t  *r;
  
    c = ev->data;
    r = c->data;
  
    ngx_http_set_log_request(c->log, r);
  
    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
                   "http run request: \"%V?%V\"", &r->uri, &r->args);
  
    if (ev->write) {
        r->write_event_handler(r); // 调用ngx_http_core_run_phases()
  
    } else {
        r->read_event_handler(r); // 调用ngx_http_block_reading()
    }
  
    ngx_http_run_posted_requests(c); // 处理post请求
}

你可能感兴趣的:(Nginx源码阅读(ngx_http_process_request))