27.Nginx HTTP之监听套接字连接处理函数ngx_http_init_connection

前一篇讲过,对于HTTP指令块下的server,Nginx会为其创建相应的监听套接字,并且注册连接处理函数ngx_http_init_connection。

我们在ngx_event_accept中看到过,该函数会在accept后进行调用。

/* http/ngx_http_request.h */

typedef struct {                                 // HTTP连接结构体定义
    ngx_http_request_t   *request;               // HTTP请求

    ngx_buf_t           **busy;
    ngx_int_t             nbusy;

    ngx_buf_t           **free;
    ngx_int_t             nfree;

    ngx_uint_t            pipeline;
} ngx_http_connection_t;


/* http/ngx_http_request.c */

/* 初始化HTTP请求
 * param rev: HTTP连接的读事件
 */
static void ngx_http_init_request(ngx_event_t *rev)
{
    ngx_uint_t                 i;
    socklen_t                  len;
    struct sockaddr_in         addr_in;
    ngx_connection_t          *c;
    ngx_http_request_t        *r;
    ngx_http_in_port_t        *in_port;
    ngx_http_in_addr_t        *in_addr;
    ngx_http_connection_t     *hc;
    ngx_http_server_name_t    *server_name;
    ngx_http_core_srv_conf_t  *cscf;
    ngx_http_core_loc_conf_t  *clcf;
#if (NGX_HTTP_SSL)
    ngx_http_ssl_srv_conf_t   *sscf;
#endif

    c = rev->data;

    if (rev->timedout) {
        // 如果timedout为1, 表明客户端超时
        
        ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");

#if (NGX_STAT_STUB)
        (*ngx_stat_reading)--;
#endif
        // 关闭HTTP连接
        ngx_http_close_connection(c);
        return;
    }

    // 从连接c获取其custom data, 在这里也就是HTTP连接
    hc = c->data;

    if (hc) {

#if (NGX_STAT_STUB)
        (*ngx_stat_reading)++;
#endif

    } else {
        // 如果hc为空
        
        // 从内存池c->pool申请一个ngx_http_connection_t结构体大小的内存空间hc
        if (!(hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t)))) {

#if (NGX_STAT_STUB)
            (*ngx_stat_reading)--;
#endif

            ngx_http_close_connection(c);
            return;
        }
    }

    // 获取HTTP连接hc的请求r
    r = hc->request;

    if (r) {
        // 如果r不为空
        
        // 对r置全0
        ngx_memzero(r, sizeof(ngx_http_request_t));

        r->pipeline = hc->pipeline;

        if (hc->nbusy) {
       

你可能感兴趣的:(Nginx-0.1.0源码学习)