text: 
  /*
   * Copyright (C) Igor Sysoev
   * Copyright (C) Nginx, Inc.
   */
  
  
  #include <ngx_config.h>
  #include <ngx_core.h>
  #include <ngx_http.h>
  
  
  static void ngx_http_init_request(ngx_event_t *ev);
  static void ngx_http_process_request_line(ngx_event_t *rev);
  static void ngx_http_process_request_headers(ngx_event_t *rev);
  static ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
  static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
      ngx_uint_t request_line);
  
  static ngx_int_t ngx_http_process_header_line(ngx_http_request_t *r,
      ngx_table_elt_t *h, ngx_uint_t offset);
  static ngx_int_t ngx_http_process_unique_header_line(ngx_http_request_t *r,
      ngx_table_elt_t *h, ngx_uint_t offset);
  static ngx_int_t ngx_http_process_host(ngx_http_request_t *r,
      ngx_table_elt_t *h, ngx_uint_t offset);
  static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r,
      ngx_table_elt_t *h, ngx_uint_t offset);
  static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r,
      ngx_table_elt_t *h, ngx_uint_t offset);
  static ngx_int_t ngx_http_process_cookie(ngx_http_request_t *r,
      ngx_table_elt_t *h, ngx_uint_t offset);
  
  static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
  static void ngx_http_process_request(ngx_http_request_t *r);
  static ssize_t ngx_http_validate_host(ngx_http_request_t *r, u_char **host,
      size_t len, ngx_uint_t alloc);
  static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r,
      u_char *host, size_t len);
  
  static void ngx_http_request_handler(ngx_event_t *ev);
  static void ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc);
  static void ngx_http_terminate_handler(ngx_http_request_t *r);
  static void ngx_http_finalize_connection(ngx_http_request_t *r);
  static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
  static void ngx_http_writer(ngx_http_request_t *r);
  static void ngx_http_request_finalizer(ngx_http_request_t *r);
  
  static void ngx_http_set_keepalive(ngx_http_request_t *r);
  static void ngx_http_keepalive_handler(ngx_event_t *ev);
  static void ngx_http_set_lingering_close(ngx_http_request_t *r);
  static void ngx_http_lingering_close_handler(ngx_event_t *ev);
  static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
  static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
  static void ngx_http_free_request(ngx_http_request_t *r, ngx_int_t error);
  static void ngx_http_log_request(ngx_http_request_t *r);
  static void ngx_http_close_connection(ngx_connection_t *c);
  
  static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
  static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
      ngx_http_request_t *sr, u_char *buf, size_t len);
  
  #if (NGX_HTTP_SSL)
  static void ngx_http_ssl_handshake(ngx_event_t *rev);
  static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
  #endif
  
  
  static char *ngx_http_client_errors[] = {
  
      /* NGX_HTTP_PARSE_INVALID_METHOD */
      "client sent invalid method",
  
      /* NGX_HTTP_PARSE_INVALID_REQUEST */
      "client sent invalid request",
  
      /* NGX_HTTP_PARSE_INVALID_09_METHOD */
      "client sent invalid method in HTTP/0.9 request"
  };
  
  
  ngx_http_header_t  ngx_http_headers_in[] = {
      { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host),
                   ngx_http_process_host },
  
      { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
                   ngx_http_process_connection },
  
      { ngx_string("If-Modified-Since"),
                   offsetof(ngx_http_headers_in_t, if_modified_since),
                   ngx_http_process_unique_header_line },
  
      { ngx_string("If-Unmodified-Since"),
                   offsetof(ngx_http_headers_in_t, if_unmodified_since),
                   ngx_http_process_unique_header_line },
  
      { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
                   ngx_http_process_user_agent },
  
      { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
                   ngx_http_process_header_line },
  
      { ngx_string("Content-Length"),
                   offsetof(ngx_http_headers_in_t, content_length),
                   ngx_http_process_unique_header_line },
  
      { ngx_string("Content-Type"),
                   offsetof(ngx_http_headers_in_t, content_type),
                   ngx_http_process_header_line },
  
      { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
                   ngx_http_process_header_line },
  
      { ngx_string("If-Range"),
                   offsetof(ngx_http_headers_in_t, if_range),
                   ngx_http_process_unique_header_line },
  
      { ngx_string("Transfer-Encoding"),
                   offsetof(ngx_http_headers_in_t, transfer_encoding),
                   ngx_http_process_header_line },
  
      { ngx_string("Expect"),
                   offsetof(ngx_http_headers_in_t, expect),
                   ngx_http_process_unique_header_line },
  
  #if (NGX_HTTP_GZIP)
      { ngx_string("Accept-Encoding"),
                   offsetof(ngx_http_headers_in_t, accept_encoding),
                   ngx_http_process_header_line },
  
      { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
                   ngx_http_process_header_line },
  #endif
  
      { ngx_string("Authorization"),
                   offsetof(ngx_http_headers_in_t, authorization),
                   ngx_http_process_unique_header_line },
  
      { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
                   ngx_http_process_header_line },
  
  #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP || NGX_HTTP_GEO)
      { ngx_string("X-Forwarded-For"),
                   offsetof(ngx_http_headers_in_t, x_forwarded_for),
                   ngx_http_process_header_line },
  #endif
  
  #if (NGX_HTTP_REALIP)
      { ngx_string("X-Real-IP"),
                   offsetof(ngx_http_headers_in_t, x_real_ip),
                   ngx_http_process_header_line },
  #endif
  
  #if (NGX_HTTP_HEADERS)
      { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
                   ngx_http_process_header_line },
  
      { ngx_string("Accept-Language"),
                   offsetof(ngx_http_headers_in_t, accept_language),
                   ngx_http_process_header_line },
  #endif
  
  #if (NGX_HTTP_DAV)
      { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
                   ngx_http_process_header_line },
  
      { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
                   ngx_http_process_header_line },
  
      { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite),
                   ngx_http_process_header_line },
  
      { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date),
                   ngx_http_process_header_line },
  #endif
  
      { ngx_string("Cookie"), 0, ngx_http_process_cookie },
  
      { ngx_null_string, 0, NULL }
  };
  
  //HTTP框架处理请求的第一步 add by luguifang
  void
  ngx_http_init_connection(ngx_connection_t *c)
  {
      ngx_event_t         *rev;
      ngx_http_log_ctx_t  *ctx;
  
      ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
      if (ctx == NULL) {
          ngx_http_close_connection(c);
          return;
      }
  
      ctx->connection = c;
      ctx->request = NULL;
      ctx->current_request = NULL;
  
      c->log->connection = c->number;
      c->log->handler = ngx_http_log_error;
      c->log->data = ctx;
      c->log->action = "reading client request line";
  
      c->log_error = NGX_ERROR_INFO;
  	//可读事件处理方法设置为ngx_http_init_request 它意味着当用户在这个TCP连接上发送的数据到达服务器后，ngx_http_init_request方法将会被调用add by luguifang
      rev = c->read;
      rev->handler = ngx_http_init_request;
  	/*可写事件，也会设置它的handler回调方法为ngx_http_empty_handler，这个方法不会做任何工作 
  	 这个方法仅有一个用途：当业务上不需要处理可写事件时，就把ngx_http_empty_handler
  	 方法设置到连接的可写事件的handler中，这样可写事件被定时器或者epoll触发后是不做任何工作的--luguifang*/
      c->write->handler = ngx_http_empty_handler;
  
  #if (NGX_STAT_STUB)
      (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
  #endif
  	// 连接上有用户请求的数据
      if (rev->ready) {
          /* the deferred accept(), rtsig, aio, iocp */
  
          if (ngx_use_accept_mutex) {
              ngx_post_event(rev, &ngx_posted_events);
              return;
          }
  		//处理请求数据
          ngx_http_init_request(rev);
          return;
      }
  	/*调用ngx_add_timer方法把读事件添加到定时器中，设置的超时时间则是nginx.conf中client_header_timeout配置项指
  	定的参数。也就是说，如果经过client_header_timeout时间后这个连接上还没有用户数据到达，则会由定时器触发调用读事件的ngx_http_init_request处理方法---luguifang*/
      ngx_add_timer(rev, c->listening->post_accept_timeout);
  	/*调用ngx_handle_read_event方法把连接c的可读事件添加到epoll中 这里并没有把可写事件添加到epoll中，因为现在不需要向客户端发送任何数据--luguifang*/
      if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  #if (NGX_STAT_STUB)
          (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
  #endif
          ngx_http_close_connection(c);
          return;
      }
  }
  
  
  static void
  ngx_http_init_request(ngx_event_t *rev)
  {
      ngx_time_t                 *tp;
      ngx_uint_t                  i;
      ngx_connection_t           *c;
      ngx_http_request_t         *r;
      struct sockaddr_in         *sin;
      ngx_http_port_t            *port;
      ngx_http_in_addr_t         *addr;
      ngx_http_log_ctx_t         *ctx;
      ngx_http_addr_conf_t       *addr_conf;
      ngx_http_connection_t      *hc;
      ngx_http_core_srv_conf_t   *cscf;
      ngx_http_core_loc_conf_t   *clcf;
      ngx_http_core_main_conf_t  *cmcf;
  #if (NGX_HAVE_INET6)
      struct sockaddr_in6        *sin6;
      ngx_http_in6_addr_t        *addr6;
  #endif
  
  #if (NGX_STAT_STUB)
      (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
  #endif
  
      c = rev->data;
  	// step1: 检验请求是否超时 如果超时关闭连接
      if (rev->timedout) {
          ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  
          ngx_http_close_connection(c);
          return;
      }
  
  	/*step2:构造ngx_http_request_t 结构体并设置到连接的data成员，每个请求都会有一个ngx_http_request_t结构
  体，所有的HTTP模块都以此作为核心结构体来处理请求*/
  	
      c->requests++;
  
      hc = c->data;
  
      if (hc == NULL) {
          hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
          if (hc == NULL) {
              ngx_http_close_connection(c);
              return;
          }
      }
  
      r = hc->request;
  
      if (r) {
          ngx_memzero(r, sizeof(ngx_http_request_t));
  
          r->pipeline = hc->pipeline;
  
          if (hc->nbusy) {
              r->header_in = hc->busy[0];
          }
  
      } else {
          r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t)); //创建ngx_http_request_t 结构
          if (r == NULL) {
              ngx_http_close_connection(c);
              return;
          }
  
          hc->request = r;
      }
  
      c->data = r;// 设置到 ngx_connetcion_s 的结构体data变量中
      r->http_connection = hc;
  
      c->sent = 0;
      r->signature = NGX_HTTP_MODULE;
  
      /* find the server configuration for the address:port */
  	/* step3:找到监听地址对应的默认server add luguifang*/
  
      port = c->listening->servers;
  
      r->connection = c;
  
      if (port->naddrs > 1) {
  
          /*
           * there are several addresses on this port and one of them
           * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
           * is required to determine a server address
           */
  
          if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
              ngx_http_close_connection(c);
              return;
          }
  
          switch (c->local_sockaddr->sa_family) {
  
  #if (NGX_HAVE_INET6)
          case AF_INET6:
              sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
  
              addr6 = port->addrs;
  
              /* the last address is "*" */
  
              for (i = 0; i < port->naddrs - 1; i++) {
                  if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
                      break;
                  }
              }
  
              addr_conf = &addr6[i].conf;
  
              break;
  #endif
  
          default: /* AF_INET */
              sin = (struct sockaddr_in *) c->local_sockaddr;
  
              addr = port->addrs;
  
              /* the last address is "*" */
  
              for (i = 0; i < port->naddrs - 1; i++) {
                  if (addr[i].addr == sin->sin_addr.s_addr) {
                      break;
                  }
              }
  
              addr_conf = &addr[i].conf;
  
              break;
          }
  
      } else {
  
          switch (c->local_sockaddr->sa_family) {
  
  #if (NGX_HAVE_INET6)
          case AF_INET6:
              addr6 = port->addrs;
              addr_conf = &addr6[0].conf;
              break;
  #endif
  
          default: /* AF_INET */
              addr = port->addrs;
              addr_conf = &addr[0].conf;
              break;
          }
      }
  
      r->virtual_names = addr_conf->virtual_names;
  
      /* the default server configuration for the address:port */
  	/*step4:设置请求对应的main、src、loc配置项*/
      cscf = addr_conf->default_server;
  
      r->main_conf = cscf->ctx->main_conf;
      r->srv_conf = cscf->ctx->srv_conf;
      r->loc_conf = cscf->ctx->loc_conf;
  
  	/*step5：重新设置连接读事件的回调方法为ngx_http_process_request_line -----luguifang*/
      rev->handler = ngx_http_process_request_line;
      r->read_event_handler = ngx_http_block_reading;
  
  #if (NGX_HTTP_SSL)
  
      {
      ngx_http_ssl_srv_conf_t  *sscf;
  
      sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
      if (sscf->enable || addr_conf->ssl) {
  
          if (c->ssl == NULL) {
  
              c->log->action = "SSL handshaking";
  
              if (addr_conf->ssl && sscf->ssl.ctx == NULL) {
                  ngx_log_error(NGX_LOG_ERR, c->log, 0,
                                "no \"ssl_certificate\" is defined "
                                "in server listening on SSL port");
                  ngx_http_close_connection(c);
                  return;
              }
  
              if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
                  != NGX_OK)
              {
                  ngx_http_close_connection(c);
                  return;
              }
  
              rev->handler = ngx_http_ssl_handshake;
          }
  
          r->main_filter_need_in_memory = 1;
      }
      }
  
  #endif
  
      clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
      c->log->file = clcf->error_log->file;
      if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
          c->log->log_level = clcf->error_log->log_level;
      }
  	/*ste6： 创建接受缓冲区 -----luguifang*/
      if (c->buffer == NULL) {
          c->buffer = ngx_create_temp_buf(c->pool,
                                          cscf->client_header_buffer_size);
          if (c->buffer == NULL) {
              ngx_http_close_connection(c);
              return;
          }
      }
  
      if (r->header_in == NULL) {
          r->header_in = c->buffer; /*ngx_connection_t结构体的buffer指针以及ngx_http_request_t结构体的header_in指针共
  									同指向这块内存缓冲区。这个header_in缓冲区将负责接收用户发送来的请求内容*/
      }
  	/*step7 :创建ngx_http_request_t 结构体内存池 -----luguifang*/
      r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
      if (r->pool == NULL) {
          ngx_http_close_connection(c);
          return;
      }
  
  	/*step8：初始化ngx_http_request_t 结构体中的容器*/
      if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
                        sizeof(ngx_table_elt_t))
          != NGX_OK)
      {
          ngx_destroy_pool(r->pool);
          ngx_http_close_connection(c);
          return;
      }
  	/*step9: 创建指针数组以存放所有 http模块对该请求可能创建的上下文结构体----luguifang*/
      r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
      if (r->ctx == NULL) {
          ngx_destroy_pool(r->pool);
          ngx_http_close_connection(c);
          return;
      }
  
      cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
  
      r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
                                          * sizeof(ngx_http_variable_value_t));
      if (r->variables == NULL) {
          ngx_destroy_pool(r->pool);
          ngx_http_close_connection(c);
          return;
      }
  
      c->single_connection = 1;
      c->destroyed = 0;
  
      r->main = r;
      r->count = 1;
  	/*step10：更新请求开始的事件*/
      tp = ngx_timeofday();
      r->start_sec = tp->sec;
      r->start_msec = tp->msec;
  
      r->method = NGX_HTTP_UNKNOWN;
  
      r->headers_in.content_length_n = -1;
      r->headers_in.keep_alive_n = -1;
      r->headers_out.content_length_n = -1;
      r->headers_out.last_modified_time = -1;
  
      r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
      r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
  
      r->http_state = NGX_HTTP_READING_REQUEST_STATE;
  
      ctx = c->log->data;
      ctx->request = r;
      ctx->current_request = r;
      r->log_handler = ngx_http_log_error_handler;
  
  #if (NGX_STAT_STUB)
      (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
      r->stat_reading = 1;
      (void) ngx_atomic_fetch_add(ngx_stat_requests, 1);
  #endif
  	/*step11:调用ngx_http_process_request_line 接受http请求行------luguifang*/
      rev->handler(rev);
  }
  
  
  #if (NGX_HTTP_SSL)
  
  static void
  ngx_http_ssl_handshake(ngx_event_t *rev)
  {
      u_char               buf[1];
      ssize_t              n;
      ngx_int_t            rc;
      ngx_connection_t    *c;
      ngx_http_request_t  *r;
  
      c = rev->data;
      r = c->data;
  
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
                     "http check ssl handshake");
  
      if (rev->timedout) {
          ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
          c->timedout = 1;
          ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
          return;
      }
  
      n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
  
      if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
  
          if (!rev->timer_set) {
              ngx_add_timer(rev, c->listening->post_accept_timeout);
          }
  
          if (ngx_handle_read_event(rev, 0) != NGX_OK) {
              ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
          }
  
          return;
      }
  
      if (n == 1) {
          if (buf[0] & 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
              ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
                             "https ssl handshake: 0x%02Xd", buf[0]);
  
              rc = ngx_ssl_handshake(c);
  
              if (rc == NGX_AGAIN) {
  
                  if (!rev->timer_set) {
                      ngx_add_timer(rev, c->listening->post_accept_timeout);
                  }
  
                  c->ssl->handler = ngx_http_ssl_handshake_handler;
                  return;
              }
  
              ngx_http_ssl_handshake_handler(c);
  
              return;
  
          } else {
              ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
                             "plain http");
  
              r->plain_http = 1;
          }
      }
  
      c->log->action = "reading client request line";
  
      rev->handler = ngx_http_process_request_line;
      ngx_http_process_request_line(rev);
  }
  
  
  static void
  ngx_http_ssl_handshake_handler(ngx_connection_t *c)
  {
      ngx_http_request_t  *r;
  
      if (c->ssl->handshaked) {
  
          /*
           * The majority of browsers do not send the "close notify" alert.
           * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
           * and Links.  And what is more, MSIE ignores the server's alert.
           *
           * Opera and recent Mozilla send the alert.
           */
  
          c->ssl->no_wait_shutdown = 1;
  
          c->read->handler = ngx_http_process_request_line;
          /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
  
          ngx_http_process_request_line(c->read);
  
          return;
      }
  
      r = c->data;
  
      ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
  
      return;
  }
  
  #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
  
  int
  ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
  {
      size_t                    len;
      u_char                   *host;
      const char               *servername;
      ngx_connection_t         *c;
      ngx_http_request_t       *r;
      ngx_http_ssl_srv_conf_t  *sscf;
  
      servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
  
      if (servername == NULL) {
          return SSL_TLSEXT_ERR_NOACK;
      }
  
      c = ngx_ssl_get_connection(ssl_conn);
  
      ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
                     "SSL server name: \"%s\"", servername);
  
      len = ngx_strlen(servername);
  
      if (len == 0) {
          return SSL_TLSEXT_ERR_NOACK;
      }
  
      r = c->data;
  
      host = (u_char *) servername;
  
      len = ngx_http_validate_host(r, &host, len, 1);
  
      if (len <= 0) {
          return SSL_TLSEXT_ERR_NOACK;
      }
  
      if (ngx_http_find_virtual_server(r, host, len) != NGX_OK) {
          return SSL_TLSEXT_ERR_NOACK;
      }
  
      sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
  
      if (sscf->ssl.ctx) {
          SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
  
          /*
           * SSL_set_SSL_CTX() only changes certs as of 1.0.0d
           * adjust other things we care about
           */
  
          SSL_set_verify(ssl_conn, SSL_CTX_get_verify_mode(sscf->ssl.ctx),
                         SSL_CTX_get_verify_callback(sscf->ssl.ctx));
  
          SSL_set_verify_depth(ssl_conn, SSL_CTX_get_verify_depth(sscf->ssl.ctx));
  
  #ifdef SSL_CTRL_CLEAR_OPTIONS
          /* only in 0.9.8m+ */
          SSL_clear_options(ssl_conn, SSL_get_options(ssl_conn) &
                                      ~SSL_CTX_get_options(sscf->ssl.ctx));
  #endif
  
          SSL_set_options(ssl_conn, SSL_CTX_get_options(sscf->ssl.ctx));
      }
  
      return SSL_TLSEXT_ERR_OK;
  }
  
  #endif
  
  #endif
  
  
  static void
  ngx_http_process_request_line(ngx_event_t *rev)
  {
      u_char                    *host;
      ssize_t                    n;
      ngx_int_t                  rc, rv;
      ngx_connection_t          *c;
      ngx_http_request_t        *r;
      ngx_http_core_srv_conf_t  *cscf;
  
      c = rev->data;
      r = c->data;
  
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
                     "http process request line");
  	/*检查读事件是否超时，超时事件是nginx.conf 中配置的client_header_timeout的值 lgf-conf*/
      if (rev->timedout) {
          ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
          c->timedout = 1;
          ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
          return;
      }
  
      rc = NGX_AGAIN;
  
      for ( ;; ) {
  
          if (rc == NGX_AGAIN) {
              n = ngx_http_read_request_header(r);//将数据从socket接受后 copy 到header_in中
  
              if (n == NGX_AGAIN || n == NGX_ERROR) {
                  return;
              }
          }
  		//用状态机解析已经接受到的tcp字符流，确认其是否构成完整的http请求行
          rc = ngx_http_parse_request_line(r, r->header_in);
  		/*接收到完整的HTTP请求行后，首先要把请求行中的信息如方法名、URI及其参
  			数、HTTP版本等信息设置到ngx_http_request_t结构体的相应成员中*/
          if (rc == NGX_OK) {
  
              /* the request line has been parsed successfully */
  
              r->request_line.len = r->request_end - r->request_start;
              r->request_line.data = r->request_start;
  
  
              if (r->args_start) {
                  r->uri.len = r->args_start - 1 - r->uri_start;
              } else {
                  r->uri.len = r->uri_end - r->uri_start;
              }
  
  
              if (r->complex_uri || r->quoted_uri) {
  
                  r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
                  if (r->uri.data == NULL) {
                      ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                      return;
                  }
  
                  cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  
                  rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
  
                  if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
                      ngx_log_error(NGX_LOG_INFO, c->log, 0,
                                    "client sent invalid request");
                      ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
                      return;
                  }
  
              } else {
                  r->uri.data = r->uri_start;
              }
  
  
              r->unparsed_uri.len = r->uri_end - r->uri_start;
              r->unparsed_uri.data = r->uri_start;
  
              r->valid_unparsed_uri = r->space_in_uri ? 0 : 1;
  
              r->method_name.len = r->method_end - r->request_start + 1;
              r->method_name.data = r->request_line.data;
  
  
              if (r->http_protocol.data) {
                  r->http_protocol.len = r->request_end - r->http_protocol.data;
              }
  
  
              if (r->uri_ext) {
                  if (r->args_start) {
                      r->exten.len = r->args_start - 1 - r->uri_ext;
                  } else {
                      r->exten.len = r->uri_end - r->uri_ext;
                  }
  
                  r->exten.data = r->uri_ext;
              }
  
  
              if (r->args_start && r->uri_end > r->args_start) {
                  r->args.len = r->uri_end - r->args_start;
                  r->args.data = r->args_start;
              }
  
  #if (NGX_WIN32)
              {
              u_char  *p;
  
              p = r->uri.data + r->uri.len - 1;
  
              while (p > r->uri.data) {
  
                  if (*p == ' ') {
                      p--;
                      continue;
                  }
  
                  if (*p == '.') {
                      p--;
                      continue;
                  }
  
                  if (ngx_strncasecmp(p - 6, (u_char *) "::$data", 7) == 0) {
                      p -= 7;
                      continue;
                  }
  
                  break;
              }
  
              if (p != r->uri.data + r->uri.len - 1) {
                  r->uri.len = p + 1 - r->uri.data;
                  ngx_http_set_exten(r);
              }
  
              }
  #endif
  
              ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
                             "http request line: \"%V\"", &r->request_line);
  
              ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
                             "http uri: \"%V\"", &r->uri);
  
              ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
                             "http args: \"%V\"", &r->args);
  
              ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
                             "http exten: \"%V\"", &r->exten);
  
              if (r->host_start && r->host_end) {
  
                  host = r->host_start;
                  n = ngx_http_validate_host(r, &host,
                                             r->host_end - r->host_start, 0);
  
                  if (n == 0) {
                      ngx_log_error(NGX_LOG_INFO, c->log, 0,
                                    "client sent invalid host in request line");
                      ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
                      return;
                  }
  
                  if (n < 0) {
                      ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                      return;
                  }
  
                  r->headers_in.server.len = n;
                  r->headers_in.server.data = host;
              }
  			//HTTP版本小于1.0
              if (r->http_version < NGX_HTTP_VERSION_10) {
  
                  if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
                                                   r->headers_in.server.len)
                      == NGX_ERROR)
                  {
                      ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                      return;
                  }
  				// 开始处理请求---------lgf-key
                  ngx_http_process_request(r);
                  return;
              }
  
  			//初始化ngx_http_request_t结构体中存放HTTP头部的一些容器 为下一步接受http头部做好准备
              if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
                                sizeof(ngx_table_elt_t))
                  != NGX_OK)
              {
                  ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                  return;
              }
  
  
              if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
                                 sizeof(ngx_table_elt_t *))
                  != NGX_OK)
              {
                  ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                  return;
              }
  
              c->log->action = "reading client request headers";
  			//接受完请求行，将回调方法由ngx_http_process_request_line改为ngx_http_process_request_headers准备接收HTTP头部
              rev->handler = ngx_http_process_request_headers;
  			//http请求头的处理------lgf-key
              ngx_http_process_request_headers(rev);
  
              return;
          }
  
          if (rc != NGX_AGAIN) {
  
              /* there was error while a request line parsing */
  
              ngx_log_error(NGX_LOG_INFO, c->log, 0,
                            ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
              ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
              return;
          }
  
          /* NGX_AGAIN: a request line parsing is still incomplete */
  		/*需要接收更多的字符流，header_in缓冲区做判断，检查是否还有空闲的内存*/
  
          if (r->header_in->pos == r->header_in->end) {
  			/*如果没有空间则分配更大的接受缓冲区，分配大小有nginx.conf 文件中的large_client_header_buffer配置项指定 lgf-conf*/
              rv = ngx_http_alloc_large_header_buffer(r, 1);
  
              if (rv == NGX_ERROR) {
                  ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                  return;
              }
  
              if (rv == NGX_DECLINED) {
                  r->request_line.len = r->header_in->end - r->request_start;
                  r->request_line.data = r->request_start;
  
                  ngx_log_error(NGX_LOG_INFO, c->log, 0,
                                "client sent too long URI");
                  ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
                  return;
              }
          }
      }
  }
  
  //处理http请求头
  static void
  ngx_http_process_request_headers(ngx_event_t *rev)
  {
      u_char                     *p;
      size_t                      len;
      ssize_t                     n;
      ngx_int_t                   rc, rv;
      ngx_table_elt_t            *h;
      ngx_connection_t           *c;
      ngx_http_header_t          *hh;
      ngx_http_request_t         *r;
      ngx_http_core_srv_conf_t   *cscf;
      ngx_http_core_main_conf_t  *cmcf;
  
      c = rev->data;
      r = c->data;
  
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
                     "http process request header line");
  
  	// 检查当前的读事件是否已经超时
      if (rev->timedout) {
          ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
          c->timedout = 1;
          ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
          return;
      }
  
      cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
      cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  
      rc = NGX_AGAIN;
  
      for ( ;; ) {
  
          if (rc == NGX_AGAIN) {
  			/*检查接收HTTP请求头部的header_in缓冲区是否用尽，当header_in缓冲区的pos成员指
  			向了end成员时，表示已经用尽，这时需要调用ngx_http_alloc_large_header_buffer方法分配更
  			大、更多的缓冲区*/
              if (r->header_in->pos == r->header_in->end) {
  
                  rv = ngx_http_alloc_large_header_buffer(r, 0);
  				/*rv 三种值：NGX_OK表示成功分配到更大的缓冲区，可以继续接收客户端发来的字符流；NGX_DECLINED表示已经
  				达到缓冲区大小的上限，无法分配更大的缓冲区；NGX_ERROR表示出现错误*/
  
                  if (rv == NGX_ERROR) {
                      ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                      return;
                  }
  
                  if (rv == NGX_DECLINED) {
                      p = r->header_name_start;
  
                      r->lingering_close = 1;
  
                      if (p == NULL) {
                          ngx_log_error(NGX_LOG_INFO, c->log, 0,
                                        "client sent too large request");
                          ngx_http_finalize_request(r,
                                              NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
                          return;
                      }
  
                      len = r->header_in->end - p;
  
                      if (len > NGX_MAX_ERROR_STR - 300) {
                          len = NGX_MAX_ERROR_STR - 300;
                          p[len++] = '.'; p[len++] = '.'; p[len++] = '.';
                      }
  
                      ngx_log_error(NGX_LOG_INFO, c->log, 0,
                                    "client sent too long header line: \"%*s\"",
                                    len, r->header_name_start);
  
                      ngx_http_finalize_request(r,
                                              NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
                      return;
                  }
              }
  
              n = ngx_http_read_request_header(r);
  
              if (n == NGX_AGAIN || n == NGX_ERROR) {
                  return;
              }
          }
  		/*调用ngx_http_parse_header_line方法解析缓冲区中的字符流。这种方法有3个返回值：
  		返回NGX_OK时，表示解析出一行HTTP头部，返回NGX_HTTP_PARSE_HEADER_DONE时，表示已经解析出了完整的HTTP头
  		部，这时可以准备开始处理HTTP请求了返回NGX_AGAIN时，表示还需要
  		接收到更多的字符流才能继续解析，这时需要去接收更多的字符流；除此之外
  		的错误情况发送400错误给客户端*/
          rc = ngx_http_parse_header_line(r, r->header_in,
                                          cscf->underscores_in_headers);
  
          if (rc == NGX_OK) {
  
              if (r->invalid_header && cscf->ignore_invalid_headers) {
  
                  /* there was error while a header line parsing */
  
                  ngx_log_error(NGX_LOG_INFO, c->log, 0,
                                "client sent invalid header line: \"%*s\"",
                                r->header_end - r->header_name_start,
                                r->header_name_start);
                  continue;
              }
  
              /* a header line has been parsed successfully */
  			// 将解析到的http头部数据设置到ngx_http_request_t结构体headers_in成员的headers链表中
  
              h = ngx_list_push(&r->headers_in.headers);
              if (h == NULL) {
                  ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                  return;
              }
  
              h->hash = r->header_hash;
  
              h->key.len = r->header_name_end - r->header_name_start;
              h->key.data = r->header_name_start;
              h->key.data[h->key.len] = '\0';
  
              h->value.len = r->header_end - r->header_start;
              h->value.data = r->header_start;
              h->value.data[h->value.len] = '\0';
  
              h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
              if (h->lowcase_key == NULL) {
                  ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                  return;
              }
  
              if (h->key.len == r->lowcase_index) {
                  ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
  
              } else {
                  ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
              }
  
              hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
                                 h->lowcase_key, h->key.len);
  
              if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
                  return;
              }
  
              ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                             "http header: \"%V: %V\"",
                             &h->key, &h->value);
  
              continue;
          }
  
          if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
  
              /* a whole header has been parsed successfully */
  
              ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                             "http header done");
  
              r->request_length += r->header_in->pos - r->header_in->start;
  
              r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
  			/*根据HTTP头部中的host字段情况，调用ngx_http_find_virtual_server方法找到对应的虚拟主机配置块
  			检查以上步骤中接收解析出的HTTP头部是否合法，主要包括以下几
  			项：如果HTTP版本为1.1，则host头部不可以为空，否则返回400 Bad Request错误响应给客户
  			端；如果传递了Content-Length头部，那么它必须是合法的数字，否则会返回400 Length
  			Required错误响应给客户端；如果请求使用了PUT方法，那么必须传递Content-Length头部，
  			否则会返回400 Length Required错误响应给客户端*/
              rc = ngx_http_process_request_header(r);
  
              if (rc != NGX_OK) {
                  return;
              }
  			//调用ngx_http_process_request方法开始使用各HTTP模块正式地在业务上处理HTTP请求-----lgf-key
              ngx_http_process_request(r);
  
              return;
          }
  
          if (rc == NGX_AGAIN) {
  
              /* a header line parsing is still not complete */
  
              continue;
          }
  
          /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
  
          ngx_log_error(NGX_LOG_INFO, c->log, 0,
                        "client sent invalid header line: \"%*s\\r...\"",
                        r->header_end - r->header_name_start,
                        r->header_name_start);
          ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
          return;
      }
  }
  
  
  static ssize_t
  ngx_http_read_request_header(ngx_http_request_t *r)
  {
      ssize_t                    n;
      ngx_event_t               *rev;
      ngx_connection_t          *c;
      ngx_http_core_srv_conf_t  *cscf;
  
      c = r->connection;
      rev = c->read;
  
      n = r->header_in->last - r->header_in->pos;
  	//接收到的还未解析的字符流
      if (n > 0) {
          return n;
      }
  	/*调用封装的recv方法把Linux内核套接字缓冲区中的TCP流复制到header_in*/
      if (rev->ready) {
          n = c->recv(c, r->header_in->last,
                      r->header_in->end - r->header_in->last);
      } else { 
          n = NGX_AGAIN;
      }
  	/*没有接受到tcp 流则检查读事件中是否存在定时器 如果不存在定时器则调用ngx_add_timer 向读事件中添加定时器*/
      if (n == NGX_AGAIN) {
          if (!rev->timer_set) {
              cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
              ngx_add_timer(rev, cscf->client_header_timeout);
          }
  		/*调用ngx_handle_read_event 把该读事件添加到epoll 中*/
          if (ngx_handle_read_event(rev, 0) != NGX_OK) {
              ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
              return NGX_ERROR;
          }
  
          return NGX_AGAIN;
      }
  
      if (n == 0) {
          ngx_log_error(NGX_LOG_INFO, c->log, 0,
                        "client closed prematurely connection");
      }
  
      if (n == 0 || n == NGX_ERROR) {
          c->error = 1;
          c->log->action = "reading client request headers";
  
          ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
          return NGX_ERROR;
      }
  
      r->header_in->last += n;
  
      return n;
  }
  
  
  static ngx_int_t
  ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
      ngx_uint_t request_line)
  {
      u_char                    *old, *new;
      ngx_buf_t                 *b;
      ngx_http_connection_t     *hc;
      ngx_http_core_srv_conf_t  *cscf;
  
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                     "http alloc large header buffer");
  
      if (request_line && r->state == 0) {
  
          /* the client fills up the buffer with "\r\n" */
  
          r->request_length += r->header_in->end - r->header_in->start;
  
          r->header_in->pos = r->header_in->start;
          r->header_in->last = r->header_in->start;
  
          return NGX_OK;
      }
  
      old = request_line ? r->request_start : r->header_name_start;
  
      cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  
      if (r->state != 0
          && (size_t) (r->header_in->pos - old)
                                       >= cscf->large_client_header_buffers.size)
      {
          return NGX_DECLINED;
      }
  
      hc = r->http_connection;
  
      if (hc->nfree) {
          b = hc->free[--hc->nfree];
  
          ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                         "http large header free: %p %uz",
                         b->pos, b->end - b->last);
  
      } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
  
          if (hc->busy == NULL) {
              hc->busy = ngx_palloc(r->connection->pool,
                    cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
              if (hc->busy == NULL) {
                  return NGX_ERROR;
              }
          }
  
          b = ngx_create_temp_buf(r->connection->pool,
                                  cscf->large_client_header_buffers.size);
          if (b == NULL) {
              return NGX_ERROR;
          }
  
          ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                         "http large header alloc: %p %uz",
                         b->pos, b->end - b->last);
  
      } else {
          return NGX_DECLINED;
      }
  
      hc->busy[hc->nbusy++] = b;
  
      if (r->state == 0) {
          /*
           * r->state == 0 means that a header line was parsed successfully
           * and we do not need to copy incomplete header line and
           * to relocate the parser header pointers
           */
  
          r->request_length += r->header_in->end - r->header_in->start;
  
          r->header_in = b;
  
          return NGX_OK;
      }
  
      ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                     "http large header copy: %d", r->header_in->pos - old);
  
      r->request_length += old - r->header_in->start;
  
      new = b->start;
  
      ngx_memcpy(new, old, r->header_in->pos - old);
  
      b->pos = new + (r->header_in->pos - old);
      b->last = new + (r->header_in->pos - old);
  
      if (request_line) {
          r->request_start = new;
  
          if (r->request_end) {
              r->request_end = new + (r->request_end - old);
          }
  
          r->method_end = new + (r->method_end - old);
  
          r->uri_start = new + (r->uri_start - old);
          r->uri_end = new + (r->uri_end - old);
  
          if (r->schema_start) {
              r->schema_start = new + (r->schema_start - old);
              r->schema_end = new + (r->schema_end - old);
          }
  
          if (r->host_start) {
              r->host_start = new + (r->host_start - old);
              if (r->host_end) {
                  r->host_end = new + (r->host_end - old);
              }
          }
  
          if (r->port_start) {
              r->port_start = new + (r->port_start - old);
              r->port_end = new + (r->port_end - old);
          }
  
          if (r->uri_ext) {
              r->uri_ext = new + (r->uri_ext - old);
          }
  
          if (r->args_start) {
              r->args_start = new + (r->args_start - old);
          }
  
          if (r->http_protocol.data) {
              r->http_protocol.data = new + (r->http_protocol.data - old);
          }
  
      } else {
          r->header_name_start = new;
          r->header_name_end = new + (r->header_name_end - old);
          r->header_start = new + (r->header_start - old);
          r->header_end = new + (r->header_end - old);
      }
  
      r->header_in = b;
  
      return NGX_OK;
  }
  
  
  static ngx_int_t
  ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
      ngx_uint_t offset)
  {
      ngx_table_elt_t  **ph;
  
      ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
  
      if (*ph == NULL) {
          *ph = h;
      }
  
      return NGX_OK;
  }
  
  
  static ngx_int_t
  ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
      ngx_uint_t offset)
  {
      ngx_table_elt_t  **ph;
  
      ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
  
      if (*ph == NULL) {
          *ph = h;
          return NGX_OK;
      }
  
      ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
                    "client sent duplicate header line: \"%V: %V\", "
                    "previous value: \"%V: %V\"",
                    &h->key, &h->value, &(*ph)->key, &(*ph)->value);
  
      ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  
      return NGX_ERROR;
  }
  
  
  static ngx_int_t
  ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
      ngx_uint_t offset)
  {
      u_char   *host;
      ssize_t   len;
  
      if (r->headers_in.host == NULL) {
          r->headers_in.host = h;
      }
  
      host = h->value.data;
      len = ngx_http_validate_host(r, &host, h->value.len, 0);
  
      if (len == 0) {
          ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
                        "client sent invalid host header");
          ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
          return NGX_ERROR;
      }
  
      if (len < 0) {
          ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
          return NGX_ERROR;
      }
  
      if (r->headers_in.server.len) {
          return NGX_OK;
      }
  
      r->headers_in.server.len = len;
      r->headers_in.server.data = host;
  
      return NGX_OK;
  }
  
  
  static ngx_int_t
  ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
      ngx_uint_t offset)
  {
      if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
          r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
  
      } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
          r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
      }
  
      return NGX_OK;
  }
  
  
  static ngx_int_t
  ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
      ngx_uint_t offset)
  {
      u_char  *user_agent, *msie;
  
      if (r->headers_in.user_agent) {
          return NGX_OK;
      }
  
      r->headers_in.user_agent = h;
  
      /* check some widespread browsers while the header is in CPU cache */
  
      user_agent = h->value.data;
  
      msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
  
      if (msie && msie + 7 < user_agent + h->value.len) {
  
          r->headers_in.msie = 1;
  
          if (msie[6] == '.') {
  
              switch (msie[5]) {
              case '4':
              case '5':
                  r->headers_in.msie6 = 1;
                  break;
              case '6':
                  if (ngx_strstrn(msie + 8, "SV1", 3 - 1) == NULL) {
                      r->headers_in.msie6 = 1;
                  }
                  break;
              }
          }
  
  #if 0
          /* MSIE ignores the SSL "close notify" alert */
          if (c->ssl) {
              c->ssl->no_send_shutdown = 1;
          }
  #endif
      }
  
      if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
          r->headers_in.opera = 1;
          r->headers_in.msie = 0;
          r->headers_in.msie6 = 0;
      }
  
      if (!r->headers_in.msie && !r->headers_in.opera) {
  
          if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
              r->headers_in.gecko = 1;
  
          } else if (ngx_strstrn(user_agent, "Chrome/", 7 - 1)) {
              r->headers_in.chrome = 1;
  
          } else if (ngx_strstrn(user_agent, "Safari/", 7 - 1)) {
              r->headers_in.safari = 1;
  
          } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
              r->headers_in.konqueror = 1;
          }
      }
  
      return NGX_OK;
  }
  
  
  static ngx_int_t
  ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
      ngx_uint_t offset)
  {
      ngx_table_elt_t  **cookie;
  
      cookie = ngx_array_push(&r->headers_in.cookies);
      if (cookie) {
          *cookie = h;
          return NGX_OK;
      }
  
      ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  
      return NGX_ERROR;
  }
  
  
  static ngx_int_t
  ngx_http_process_request_header(ngx_http_request_t *r)
  {
      if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
                                       r->headers_in.server.len)
          == NGX_ERROR)
      {
          ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
          return NGX_ERROR;
      }
  
      if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
          ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
                     "client sent HTTP/1.1 request without \"Host\" header");
          ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
          return NGX_ERROR;
      }
  
      if (r->headers_in.content_length) {
          r->headers_in.content_length_n =
                              ngx_atoof(r->headers_in.content_length->value.data,
                                        r->headers_in.content_length->value.len);
  
          if (r->headers_in.content_length_n == NGX_ERROR) {
              ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
                            "client sent invalid \"Content-Length\" header");
              ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
              return NGX_ERROR;
          }
      }
  
      if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
          ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
                    "client sent %V method without \"Content-Length\" header",
                    &r->method_name);
          ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
          return NGX_ERROR;
      }
  
      if (r->method & NGX_HTTP_TRACE) {
          ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
                        "client sent TRACE method");
          ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
          return NGX_ERROR;
      }
  
      if (r->headers_in.transfer_encoding
          && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
                             "chunked", 7 - 1))
      {
          ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
                        "client sent \"Transfer-Encoding: chunked\" header");
          ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
          return NGX_ERROR;
      }
  
      if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
          if (r->headers_in.keep_alive) {
              r->headers_in.keep_alive_n =
                              ngx_atotm(r->headers_in.keep_alive->value.data,
                                        r->headers_in.keep_alive->value.len);
          }
      }
  
      return NGX_OK;
  }
  
  //处理http 请求
  static void
  ngx_http_process_request(ngx_http_request_t *r)
  {
      ngx_connection_t  *c;
  
      c = r->connection;
  
      if (r->plain_http) {
          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;
      }
  
  #if (NGX_HTTP_SSL)
  
      if (c->ssl) {
          long                      rc;
          X509                     *cert;
          ngx_http_ssl_srv_conf_t  *sscf;
  
          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) {
                  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
  	/*开始处理请求了，不存在读取超时问题如果timer_set 标志为1 则从定时器中移除该读事件---luguifang*/
      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
  	/*现在开始不会再需要接收HTTP请求行或者头部，所以需要重新设置当前连接读/写事件的回调方法*/
      c->read->handler = ngx_http_request_handler;
      c->write->handler = ngx_http_request_handler;
  	/*设置ngx_http_request_t结构体的read_event_handler方法为ngx_http_block_reading,当再次有读事件到来时，将会调用
  	read_event_handler方法处理请求。而这里将它设置为ngx_http_block_reading方法，这个方法
  	可认为不做任何事，它的意义在于，目前已经开始处理HTTP请求，除非某个HTTP模块重新
  	设置了read_event_handler方法，否则任何读事件都将得不到处理，也可以认为读事件被阻塞了--lgf*/
      r->read_event_handler = ngx_http_block_reading;
  
      ngx_http_handler(r);
  	/*调用ngx_http_run_posted_requests方法执行post请求----lgf*/
      ngx_http_run_posted_requests(c);
  }
  
  
  static ssize_t
  ngx_http_validate_host(ngx_http_request_t *r, u_char **host, size_t len,
      ngx_uint_t alloc)
  {
      u_char      *h, ch;
      size_t       i, last;
      ngx_uint_t   dot;
  
      last = len;
      h = *host;
      dot = 0;
  
      for (i = 0; i < len; i++) {
          ch = h[i];
  
          if (ch == '.') {
              if (dot) {
                  return 0;
              }
  
              dot = 1;
              continue;
          }
  
          dot = 0;
  
          if (ch == ':') {
              last = i;
              continue;
          }
  
          if (ngx_path_separator(ch) || ch == '\0') {
              return 0;
          }
  
          if (ch >= 'A' || ch < 'Z') {
              alloc = 1;
          }
      }
  
      if (dot) {
          last--;
      }
  
      if (alloc) {
          *host = ngx_pnalloc(r->pool, last) ;
          if (*host == NULL) {
              return -1;
          }
  
          ngx_strlow(*host, h, last);
      }
  
      return last;
  }
  
  
  static ngx_int_t
  ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
  {
      ngx_http_core_loc_conf_t  *clcf;
      ngx_http_core_srv_conf_t  *cscf;
  
      if (r->virtual_names == NULL) {
          return NGX_DECLINED;
      }
  
      cscf = ngx_hash_find_combined(&r->virtual_names->names,
                                    ngx_hash_key(host, len), host, len);
  
      if (cscf) {
          goto found;
      }
  
  #if (NGX_PCRE)
  
      if (len && r->virtual_names->nregex) {
          ngx_int_t                n;
          ngx_uint_t               i;
          ngx_str_t                name;
          ngx_http_server_name_t  *sn;
  
          name.len = len;
          name.data = host;
  
          sn = r->virtual_names->regex;
  
          for (i = 0; i < r->virtual_names->nregex; i++) {
  
              n = ngx_http_regex_exec(r, sn[i].regex, &name);
  
              if (n == NGX_OK) {
                  cscf = sn[i].server;
                  goto found;
              }
  
              if (n == NGX_DECLINED) {
                  continue;
              }
  
              return NGX_ERROR;
          }
      }
  
  #endif
  
      return NGX_OK;
  
  found:
  
      r->srv_conf = cscf->ctx->srv_conf;
      r->loc_conf = cscf->ctx->loc_conf;
  
      clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
      r->connection->log->file = clcf->error_log->file;
  
      if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
          r->connection->log->log_level = clcf->error_log->log_level;
      }
  
      return NGX_OK;
  }
  
  /*一个请求多半需要nginx 事件多次地调用http模块处理，通过ngx_http_request_handler函数来设置读写事件的回调方法*/
  static void
  ngx_http_request_handler(ngx_event_t *ev)
  {
      ngx_connection_t    *c;
      ngx_http_request_t  *r;
      ngx_http_log_ctx_t  *ctx;
  
  	/*从事件中取出ngx_http_request_t 结构体。
  	  在ngx_event_t结构体表示的事件中，data成员指向了这个事件对应的ngx_connection_t连接，
  	  在HTTP框架的ngx_connection_t结构体中的data成员则指向了ngx_http_request_t结
  		构体。毫无疑问，只有拥有了ngx_http_request_t结构体才可以处理HTTP请求*/
  
      c = ev->data;
      r = c->data;
  
      ctx = c->log->data;
      ctx->current_request = r;
  
      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
                     "http run request: \"%V?%V\"", &r->uri, &r->args);
  	/*检查wirte事件是否可读。之前我们已经在hgx_http_handler 方法中已经将write_event_handler设置为ngx_http_core_run_phases方法，
  	而一般我们开发的不太复杂的HTTP模块是不会重新设置write_event_handler方法的，因此，一旦有可写事件时，就会继续
  	执行ngx_http_core_run_phases方法，并继续按阶段调用各个HTTP模块实现的方法处理请求*/
      if (ev->write) {
          r->write_event_handler(r);
  
      } else {
      	//当读写事件同时为1是 只会调用write事件处理方法，ngx 会优先处理写事件来保证性能
          r->read_event_handler(r);
      }
  
      ngx_http_run_posted_requests(c);
  }
  
  /*处理post请求*/
  void
  ngx_http_run_posted_requests(ngx_connection_t *c)
  {
      ngx_http_request_t         *r;
      ngx_http_log_ctx_t         *ctx;
      ngx_http_posted_request_t  *pr;
  
      for ( ;; ) {
  
          if (c->destroyed) {
  			/*连接被销毁，就结束ngx_http_run_posted_requests方法*/
              return;
          }
  		/*找到原始请求*/
          r = c->data;
          pr = r->main->posted_requests;
  
          if (pr == NULL) {
              return;
          }
  		/*将原始请求的posted_requests指针指向链表中下一个post请求（通过第1个post请求的
  		next指针可以获得），当然，下一个post请求有可能不存在，这在下一次循环中就会检测到*/
          r->main->posted_requests = pr->next;
  
          r = pr->request;
  
          ctx = c->log->data;
          ctx->current_request = r;
  
          ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
                         "http posted request: \"%V?%V\"", &r->uri, &r->args);
  		/*调用这个post请求ngx_http_request_t结构体中的write_event_handler方法。为什么不是
  		执行read_event_handler方法呢？原因很简单，子请求不是被网络事件驱动的，因此，执行
  		post请求时就相当于有可写事件，由Nginx主动做出动作*/
          r->write_event_handler(r);
      }
  }
  
  
  ngx_int_t
  ngx_http_post_request(ngx_http_request_t *r, ngx_http_posted_request_t *pr)
  {
      ngx_http_posted_request_t  **p;
  
      if (pr == NULL) {
          pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
          if (pr == NULL) {
              return NGX_ERROR;
          }
      }
  
      pr->request = r;
      pr->next = NULL;
  
      for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
  
      *p = pr;
  
      return NGX_OK;
  }
  
  
  void
  ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
  {
      ngx_connection_t          *c;
      ngx_http_request_t        *pr;
      ngx_http_core_loc_conf_t  *clcf;
  
      c = r->connection;
  
      ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,
                     "http finalize request: %d, \"%V?%V\" a:%d, c:%d",
                     rc, &r->uri, &r->args, r == c->data, r->main->count);
  	/*NGX_DONE参数表示不需要做任何事，直接调用ngx_http_finalize_connection方法，
  之后ngx_http_finalize_request方法结束。当某一种动作（如接收HTTP请求包体）正常结束而
  请求还有业务要继续处理时，多半都是传递NGX_DONE参数。这个
  ngx_http_finalize_connection方法还会去检查引用计数情况，并不一定会销毁请求-----luguifang*/
      if (rc == NGX_DONE) {
          ngx_http_finalize_connection(r);
          return;
      }
  
      if (rc == NGX_OK && r->filter_finalize) {
          c->error = 1;
          return;
      }
  	/*首先检查rc参数。如果rc为NGX_DECLINED，
  	NGX_DECLINED参数表示请求还需要按照11个HTTP阶段继续处理下去*/
      if (rc == NGX_DECLINED) {
  		/*首先会把ngx_http_request_t结构体的write_event_handler设为ngx_http_core_run_phases方法。同时，
  		将请求的content_handler成员置为NULL空指针，这个成员是一种用于在NGX_HTTP_CONTENT_PHASE阶段处理请求的方式，将其设置为NULL是为了让
  		ngx_http_core_content_phase方法，可以继续调用NGX_HTTP_CONTENT_PHASE阶段的其他处理方法-----luguifang*/
          r->content_handler = NULL;
          r->write_event_handler = ngx_http_core_run_phases;
          ngx_http_core_run_phases(r);
          return;
      }
  	
      if (r != r->main && r->post_subrequest) {
  		/*如果是子请求那么调用post_subrequest下的handler回调方法-----luguifang*/
          rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
      }
  
      if (rc == NGX_ERROR
          || rc == NGX_HTTP_REQUEST_TIME_OUT
          || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
          || c->error)
      {
          if (ngx_http_post_action(r) == NGX_OK) {
              return;
          }
  
          if (r->main->blocked) {
              r->write_event_handler = ngx_http_request_finalizer;
          }
  		/*直接调用ngx_http_terminate_request方法强制结束请求，同时，
  ngx_http_finalize_request方法结束*/
          ngx_http_terminate_request(r, rc);
          return;
      }
  /*如果rc为NGX_HTTP_CREATED、NGX_HTTP_NO_CONTENT或者大于或等于
  NGX_HTTP_SPECIAL_RESPONSE，则表示请求的动作是上传文件，或者HTTP模块需要
  HTTP框架构造并发送响应码大于或等于300以上的特殊响应------luguifang*/
      if (rc >= NGX_HTTP_SPECIAL_RESPONSE
          || rc == NGX_HTTP_CREATED
          || rc == NGX_HTTP_NO_CONTENT)
      {
          if (rc == NGX_HTTP_CLOSE) {
              ngx_http_terminate_request(r, rc);
              return;
          }
  		/*检查当前请求的main是否指向自己，如果是，这个请求就是来自客户端的原始请求
  （非子请求），这时检查读/写事件的timer_set标志位，如果timer_set为1，则表明事件在定时
  器中，需要调用ngx_del_timer方法把读/写事件从定时器中移除----luguifang*/
          if (r == r->main) {
              if (c->read->timer_set) {
                  ngx_del_timer(c->read);
              }
  
              if (c->write->timer_set) {
                  ngx_del_timer(c->write);
              }
          }
  		/*设置读/写事件的回调方法为ngx_http_request_handler方法*/
          c->read->handler = ngx_http_request_handler;
          c->write->handler = ngx_http_request_handler;
  		/*调用ngx_http_special_response_handler方法，该方法负责根据rc参数构造完整的
  HTTP响应。rc要么是表示上传成功的201或者204，要么就是表示异步的300以上的响应码，对于这些情况，都是可以
  让HTTP框架独立构造响应包的------luguifang*/
          ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
          return;
      }
  
      if (r != r->main) {
  		/*由于当前请求是子请求，那么正常情况下需要跳到它的父请求上，激活父请求继续
  向下执行，所以这一步首先根据ngx_http_request_t结构体的parent成员找到父请求，再构造一
  个ngx_http_posted_request_t结构体把父请求放置其中，最后把该结构体添加到原始请求的
  posted_requests链表中-----luguifang*/
          if (r->buffered || r->postponed) {
  
              if (ngx_http_set_write_handler(r) != NGX_OK) {
                  ngx_http_terminate_request(r, 0);
              }
  
              return;
          }
  
  #if (NGX_DEBUG)
          if (r != c->data) {
              ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
                             "http finalize non-active request: \"%V?%V\"",
                             &r->uri, &r->args);
          }
  #endif
  
          pr = r->parent;
  
          if (r == c->data) {
  
              r->main->count--;
  
              if (!r->logged) {
  
                  clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  
                  if (clcf->log_subrequest) {
                      ngx_http_log_request(r);
                  }
  
                  r->logged = 1;
  
              } else {
                  ngx_log_error(NGX_LOG_ALERT, c->log, 0,
                                "subrequest: \"%V?%V\" logged again",
                                &r->uri, &r->args);
              }
  
              r->done = 1;
  
              if (pr->postponed && pr->postponed->request == r) {
                  pr->postponed = pr->postponed->next;
              }
  
              c->data = pr;
  
          } else {
  
              r->write_event_handler = ngx_http_request_finalizer;
  
              if (r->waited) {
                  r->done = 1;
              }
          }
  
          if (ngx_http_post_request(pr, NULL) != NGX_OK) {
              r->main->count++;
              ngx_http_terminate_request(r, 0);
              return;
          }
  
          ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
                         "http wake parent request: \"%V?%V\"",
                         &pr->uri, &pr->args);
  
          return;
      }
  	/*如果是原始请求，那么还需要检查out缓冲区内是否还有没发送完的响应
  	当HTTP响应过大，无法一次性发送给客户端时，需要调用ngx_http_finalize_request方法结束请求，
  	而该方法会ngx_http_writer方法注册给epoll和定时器，当连接再次可写时就会继续发送剩余的响应。
  	*/
      if (r->buffered || c->buffered || r->postponed || r->blocked) {
  
          if (ngx_http_set_write_handler(r) != NGX_OK) {
              ngx_http_terminate_request(r, 0);
          }
  
          return;
      }
  
      if (r != c->data) {
          ngx_log_error(NGX_LOG_ALERT, c->log, 0,
                        "http finalize non-active request: \"%V?%V\"",
                        &r->uri, &r->args);
          return;
      }
  
      r->done = 1;
      r->write_event_handler = ngx_http_request_empty_handler;
  
      if (!r->post_action) {
          r->request_complete = 1;
      }
  
      if (ngx_http_post_action(r) == NGX_OK) {
          return;
      }
  	/*真的要结束请求了。首先判断读/写事件的timer_set标志位，如果timer_set
  为1，则需要把相应的读/写事件从定时器中移除*/
      if (c->read->timer_set) {
          ngx_del_timer(c->read);
      }
  
      if (c->write->timer_set) {
          c->write->delayed = 0;
          ngx_del_timer(c->write);
      }
  
      if (c->read->eof) {
          ngx_http_close_request(r, 0);
          return;
      }
  
      ngx_http_finalize_connection(r);
  }
  
  
  static void
  ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc)
  {
      ngx_http_cleanup_t    *cln;
      ngx_http_request_t    *mr;
      ngx_http_ephemeral_t  *e;
  
      mr = r->main;
  
      ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                     "http terminate request count:%d", mr->count);
  
      if (rc > 0 && (mr->headers_out.status == 0 || mr->connection->sent == 0)) {
          mr->headers_out.status = rc;
      }
  
      cln = mr->cleanup;
      mr->cleanup = NULL;
  
      while (cln) {
          if (cln->handler) {
              cln->handler(cln->data);
          }
  
          cln = cln->next;
      }
  
      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                     "http terminate cleanup count:%d blk:%d",
                     mr->count, mr->blocked);
  
      if (mr->write_event_handler) {
  
          if (mr->blocked) {
              return;
          }
  
          e = ngx_http_ephemeral(mr);
          mr->posted_requests = NULL;
          mr->write_event_handler = ngx_http_terminate_handler;
          (void) ngx_http_post_request(mr, &e->terminal_posted_request);
          return;
      }
  
      ngx_http_close_request(mr, rc);
  }
  
  
  static void
  ngx_http_terminate_handler(ngx_http_request_t *r)
  {
      ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                     "http terminate handler count:%d", r->count);
  
      r->count = 1;
  
      ngx_http_close_request(r, 0);
  }
  
  
  static void
  ngx_http_finalize_connection(ngx_http_request_t *r)
  {
      ngx_http_core_loc_conf_t  *clcf;
  
      clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  	/*查看原始请求引用计数，如果不等于1 则表示还有多个动作操作着请求---luguifang*/
      if (r->main->count != 1) {
  		/*discard_body 为1则表示正在丢弃包体这时会再一次把请求的read_event_handler成员设为
  ngx_http_discarded_request_body_handler方法---luguifang*/
          if (r->discard_body) {
              r->read_event_handler = ngx_http_discarded_request_body_handler;
  			/*将读事件添加到定时器中*/
              ngx_add_timer(r->connection->read, clcf->lingering_timeout);
  
              if (r->lingering_time == 0) {
                  r->lingering_time = ngx_time()
                                        + (time_t) (clcf->lingering_time / 1000);
              }
          }
  		/*ngx_http_close_request方法结束请求---luguifang*/
          ngx_http_close_request(r, 0);
          return;
      }
  	/*检查keepalive成员，如果keepalive 成员为1 则说明这个请求需要释放但tcp链接还是需要复用----luguifang*/
      if (!ngx_terminate
           && !ngx_exiting
           && r->keepalive
           && clcf->keepalive_timeout > 0)
      {	
      /*调用ngx_http_set_keepalive方法将当前连接设为keepalive状态。它实际上会把表示请
  	求的ngx_http_request_t结构体释放，却又不会调用ngx_http_close_connection方法关闭连接，同
  	时也在检测keepalive连接是否超时----luguifang*/
          ngx_http_set_keepalive(r);
          return;
      }
  	/*检测请求的lingering_close成员，如果lingering_close为1，则说明需要延迟关闭请求，这时也不能真的去
  结束请求------luguifang*/
      if (clcf->lingering_close == NGX_HTTP_LINGERING_ALWAYS
          || (clcf->lingering_close == NGX_HTTP_LINGERING_ON
              && (r->lingering_close
                  || r->header_in->pos < r->header_in->last
                  || r->connection->read->ready)))
      {
      	/*调用ngx_http_set_lingering_close方法延迟关闭请求。实际上，这个方法的意义就在于
  把一些必须做的事情做完（如接收用户端发来的字符流）再关闭连接-----luguifang*/
          ngx_http_set_lingering_close(r);
          return;
      }
  
      ngx_http_close_request(r, 0);
  }
  
  
  static ngx_int_t
  ngx_http_set_write_handler(ngx_http_request_t *r)
  {
      ngx_event_t               *wev;
      ngx_http_core_loc_conf_t  *clcf;
  
      r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
  
      r->read_event_handler = r->discard_body ?
                                  ngx_http_discarded_request_body_handler:
                                  ngx_http_test_reading;
      r->write_event_handler = ngx_http_writer;
  
      wev = r->connection->write;
  
      if (wev->ready && wev->delayed) {
          return NGX_OK;
      }
  
      clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  	/*如果写事件的delayed标志位为0，就把写事件添加到定时器中，超时时间就是
  nginx.conf文件中的send_timeout配置项；当然，如果delayed为1，则表示限制发送速度，
  在需要限速时，根据计算得到的超时时间已经把写事件添加到定时器中
  了。再调用ngx_handle_write_event方法把写事件添加到epoll中*/
      if (!wev->delayed) {
          ngx_add_timer(wev, clcf->send_timeout);
      }
  
      if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
          ngx_http_close_request(r, 0);
          return NGX_ERROR;
      }
  
      return NGX_OK;
  }
  
  
  static void
  ngx_http_writer(ngx_http_request_t *r)
  {
      int                        rc;
      ngx_event_t               *wev;
      ngx_connection_t          *c;
      ngx_http_core_loc_conf_t  *clcf;
  
      c = r->connection;
      wev = c->write;
  
      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
                     "http writer handler: \"%V?%V\"", &r->uri, &r->args);
  
      clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
  	
  	/*检查连接上的写事件是否超时-----luguifang*/
      if (wev->timedout) {
  		/*如果delayed为0 这表示不是因为限速导致的超时，应该网络异常导致客户端长时间不响应导致的超时
  		此时将使用ngx_http_finalize_request结束请求-----luguifang*/
          if (!wev->delayed) {
              ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
                            "client timed out");
              c->timedout = 1;
  
              ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
              return;
          }
  		/*限速引起的超时，那么此时可以把写事件的timedout标志位和delayed标志位都重置为0----luguifang*/
          wev->timedout = 0;
          wev->delayed = 0;
  		/*检查TCP连接上是否可以发送数据------luguifang*/
          if (!wev->ready) {
  			/*如果没有准备好则将写事件添加到定时器中，这里的超时时间就是配置文件中的send_timeout参数----luguifang*/
              ngx_add_timer(wev, clcf->send_timeout);
  			/*调用ngx_handle_write_event方法将写事件添加到epoll等事件收集器中---luguifang*/
              if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
                  ngx_http_close_request(r, 0);
              }
  
              return;
          }
  
      }
  
      if (wev->delayed || r->aio) {
          ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
                         "http writer delayed");
  
          if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
              ngx_http_close_request(r, 0);
          }
  
          return;
      }
  	/*调用ngx_http_output_filter方法发送响应，其中第2个参数（也就是表示需要发送的缓
  	冲区）为NULL指针。这意味着，需要调用各包体过滤模块处理out缓冲区中的剩余内容，最
  	后调用ngx_http_write_filter方法把响应发送出去------luguifang*/
      rc = ngx_http_output_filter(r, NULL);
  
      ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
                     "http writer output filter: %d, \"%V?%V\"",
                     rc, &r->uri, &r->args);
  
      if (rc == NGX_ERROR) {
          ngx_http_finalize_request(r, rc);
          return;
      }
  	/*
  	发送响应后，查看ngx_http_request_t结构体中的buffered和postponed标志位，如果任一个
  不为0，则意味着没有发送完out中的全部响应，请求main指针指向请求自身，表示这个请求是原始请求，再检查与客户端间的连接ngx_connection_t结构体中的
  buffered标志位，如果buffered不为0，同样表示没有发送完out中的全部响应，这时需要把写事件加入到定时器和epoll中
  
  	*/
      if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
  
          if (!wev->delayed) {
              ngx_add_timer(wev, clcf->send_timeout);
          }
  
          if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
              ngx_http_close_request(r, 0);
          }
  
          return;
      }
  	/*走到此处时表明 out中的响应数据已经发送完毕-----luguifang*/
      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
                     "http writer done: \"%V?%V\"", &r->uri, &r->args);
  	/*将请求的write_event_handler方法置为ngx_http_request_empty_handler，也就是说，如
  果这个请求的连接上再有可写事件，将不做任何处理*/
      r->write_event_handler = ngx_http_request_empty_handler;
  	/*调用ngx_http_finalize_request方法结束请求，其中第2个参数传入的是
  ngx_http_output_filter方法的返回值-----luguifang*/
      ngx_http_finalize_request(r, rc);
  }
  
  
  static void
  ngx_http_request_finalizer(ngx_http_request_t *r)
  {
      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                     "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
  
      ngx_http_finalize_request(r, 0);
  }
  
  
  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);
          }
      }
  }
  
  
  void
  ngx_http_test_reading(ngx_http_request_t *r)
  {
      int                n;
      char               buf[1];
      ngx_err_t          err;
      ngx_event_t       *rev;
      ngx_connection_t  *c;
  
      c = r->connection;
      rev = c->read;
  
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
  
  #if (NGX_HAVE_KQUEUE)
  
      if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
  
          if (!rev->pending_eof) {
              return;
          }
  
          rev->eof = 1;
          c->error = 1;
          err = rev->kq_errno;
  
          goto closed;
      }
  
  #endif
  
      n = recv(c->fd, buf, 1, MSG_PEEK);
  
      if (n == 0) {
          rev->eof = 1;
          c->error = 1;
          err = 0;
  
          goto closed;
  
      } else if (n == -1) {
          err = ngx_socket_errno;
  
          if (err != NGX_EAGAIN) {
              rev->eof = 1;
              c->error = 1;
  
              goto closed;
          }
      }
  
      /* aio does not call this handler */
  
      if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
  
          if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
              ngx_http_close_request(r, 0);
          }
      }
  
      return;
  
  closed:
  
      if (err) {
          rev->error = 1;
      }
  
      ngx_log_error(NGX_LOG_INFO, c->log, err,
                    "client closed prematurely connection");
  
      ngx_http_finalize_request(r, 0);
  }
  
  
  static void
  ngx_http_set_keepalive(ngx_http_request_t *r)
  {
      int                        tcp_nodelay;
      ngx_int_t                  i;
      ngx_buf_t                 *b, *f;
      ngx_event_t               *rev, *wev;
      ngx_connection_t          *c;
      ngx_http_connection_t     *hc;
      ngx_http_core_srv_conf_t  *cscf;
      ngx_http_core_loc_conf_t  *clcf;
  
      c = r->connection;
      rev = c->read;
  
      clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
  
      if (r->discard_body) {
          r->write_event_handler = ngx_http_request_empty_handler;
          r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
          ngx_add_timer(rev, clcf->lingering_timeout);
          return;
      }
  
      c->log->action = "closing request";
  
      hc = r->http_connection;
      b = r->header_in;
  
      if (b->pos < b->last) {
  
          /* the pipelined request */
  
          if (b != c->buffer) {
  
              /*
               * If the large header buffers were allocated while the previous
               * request processing then we do not use c->buffer for
               * the pipelined request (see ngx_http_init_request()).
               *
               * Now we would move the large header buffers to the free list.
               */
  
              cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  
              if (hc->free == NULL) {
                  hc->free = ngx_palloc(c->pool,
                    cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
  
                  if (hc->free == NULL) {
                      ngx_http_close_request(r, 0);
                      return;
                  }
              }
  
              for (i = 0; i < hc->nbusy - 1; i++) {
                  f = hc->busy[i];
                  hc->free[hc->nfree++] = f;
                  f->pos = f->start;
                  f->last = f->start;
              }
  
              hc->busy[0] = b;
              hc->nbusy = 1;
          }
      }
  
      r->keepalive = 0;
  
      ngx_http_free_request(r, 0);
  
      c->data = hc;
  
      ngx_add_timer(rev, clcf->keepalive_timeout);
  
      if (ngx_handle_read_event(rev, 0) != NGX_OK) {
          ngx_http_close_connection(c);
          return;
      }
  
      wev = c->write;
      wev->handler = ngx_http_empty_handler;
  
      if (b->pos < b->last) {
  
          ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
  
  #if (NGX_STAT_STUB)
          (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
  #endif
  
          hc->pipeline = 1;
          c->log->action = "reading client pipelined request line";
  
          rev->handler = ngx_http_init_request;
          ngx_post_event(rev, &ngx_posted_events);
          return;
      }
  
      hc->pipeline = 0;
  
      /*
       * To keep a memory footprint as small as possible for an idle
       * keepalive connection we try to free the ngx_http_request_t and
       * c->buffer's memory if they were allocated outside the c->pool.
       * The large header buffers are always allocated outside the c->pool and
       * are freed too.
       */
  
      if (ngx_pfree(c->pool, r) == NGX_OK) {
          hc->request = NULL;
      }
  
      b = c->buffer;
  
      if (ngx_pfree(c->pool, b->start) == NGX_OK) {
  
          /*
           * the special note for ngx_http_keepalive_handler() that
           * c->buffer's memory was freed
           */
  
          b->pos = NULL;
  
      } else {
          b->pos = b->start;
          b->last = b->start;
      }
  
      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
                     hc->free, hc->nfree);
  
      if (hc->free) {
          for (i = 0; i < hc->nfree; i++) {
              ngx_pfree(c->pool, hc->free[i]->start);
              hc->free[i] = NULL;
          }
  
          hc->nfree = 0;
      }
  
      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
                     hc->busy, hc->nbusy);
  
      if (hc->busy) {
          for (i = 0; i < hc->nbusy; i++) {
              ngx_pfree(c->pool, hc->busy[i]->start);
              hc->busy[i] = NULL;
          }
  
          hc->nbusy = 0;
      }
  
  #if (NGX_HTTP_SSL)
      if (c->ssl) {
          ngx_ssl_free_buffer(c);
      }
  #endif
  
      rev->handler = ngx_http_keepalive_handler;
  
      if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
          if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
              ngx_http_close_connection(c);
              return;
          }
      }
  
      c->log->action = "keepalive";
  
      if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
          if (ngx_tcp_push(c->fd) == -1) {
              ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
              ngx_http_close_connection(c);
              return;
          }
  
          c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
          tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
  
      } else {
          tcp_nodelay = 1;
      }
  
      if (tcp_nodelay
          && clcf->tcp_nodelay
          && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
      {
          ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
  
          if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
                         (const void *) &tcp_nodelay, sizeof(int))
              == -1)
          {
  #if (NGX_SOLARIS)
              /* Solaris returns EINVAL if a socket has been shut down */
              c->log_error = NGX_ERROR_IGNORE_EINVAL;
  #endif
  
              ngx_connection_error(c, ngx_socket_errno,
                                   "setsockopt(TCP_NODELAY) failed");
  
              c->log_error = NGX_ERROR_INFO;
              ngx_http_close_connection(c);
              return;
          }
  
          c->tcp_nodelay = NGX_TCP_NODELAY_SET;
      }
  
  #if 0
      /* if ngx_http_request_t was freed then we need some other place */
      r->http_state = NGX_HTTP_KEEPALIVE_STATE;
  #endif
  
      c->idle = 1;
      ngx_reusable_connection(c, 1);
  
      if (rev->ready) {
          ngx_post_event(rev, &ngx_posted_events);
      }
  }
  
  
  static void
  ngx_http_keepalive_handler(ngx_event_t *rev)
  {
      size_t             size;
      ssize_t            n;
      ngx_buf_t         *b;
      ngx_connection_t  *c;
  
      c = rev->data;
  
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
  
      if (rev->timedout || c->close) {
          ngx_http_close_connection(c);
          return;
      }
  
  #if (NGX_HAVE_KQUEUE)
  
      if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
          if (rev->pending_eof) {
              c->log->handler = NULL;
              ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
                            "kevent() reported that client %V closed "
                            "keepalive connection", &c->addr_text);
  #if (NGX_HTTP_SSL)
              if (c->ssl) {
                  c->ssl->no_send_shutdown = 1;
              }
  #endif
              ngx_http_close_connection(c);
              return;
          }
      }
  
  #endif
  
      b = c->buffer;
      size = b->end - b->start;
  
      if (b->pos == NULL) {
  
          /*
           * The c->buffer's memory was freed by ngx_http_set_keepalive().
           * However, the c->buffer->start and c->buffer->end were not changed
           * to keep the buffer size.
           */
  
          b->pos = ngx_palloc(c->pool, size);
          if (b->pos == NULL) {
              ngx_http_close_connection(c);
              return;
          }
  
          b->start = b->pos;
          b->last = b->pos;
          b->end = b->pos + size;
      }
  
      /*
       * MSIE closes a keepalive connection with RST flag
       * so we ignore ECONNRESET here.
       */
  
      c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
      ngx_set_socket_errno(0);
  
      n = c->recv(c, b->last, size);
      c->log_error = NGX_ERROR_INFO;
  
      if (n == NGX_AGAIN) {
          if (ngx_handle_read_event(rev, 0) != NGX_OK) {
              ngx_http_close_connection(c);
          }
  
          return;
      }
  
      if (n == NGX_ERROR) {
          ngx_http_close_connection(c);
          return;
      }
  
      c->log->handler = NULL;
  
      if (n == 0) {
          ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
                        "client %V closed keepalive connection", &c->addr_text);
          ngx_http_close_connection(c);
          return;
      }
  
      b->last += n;
  
  #if (NGX_STAT_STUB)
      (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
  #endif
  
      c->log->handler = ngx_http_log_error;
      c->log->action = "reading client request line";
  
      c->idle = 0;
      ngx_reusable_connection(c, 0);
  
      ngx_http_init_request(rev);
  }
  
  
  static void
  ngx_http_set_lingering_close(ngx_http_request_t *r)
  {
      ngx_event_t               *rev, *wev;
      ngx_connection_t          *c;
      ngx_http_core_loc_conf_t  *clcf;
  
      c = r->connection;
  
      clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  
      rev = c->read;
      rev->handler = ngx_http_lingering_close_handler;
  
      r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
      ngx_add_timer(rev, clcf->lingering_timeout);
  
      if (ngx_handle_read_event(rev, 0) != NGX_OK) {
          ngx_http_close_request(r, 0);
          return;
      }
  
      wev = c->write;
      wev->handler = ngx_http_empty_handler;
  
      if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
          if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
              ngx_http_close_request(r, 0);
              return;
          }
      }
  
      if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
          ngx_connection_error(c, ngx_socket_errno,
                               ngx_shutdown_socket_n " failed");
          ngx_http_close_request(r, 0);
          return;
      }
  
      if (rev->ready) {
          ngx_http_lingering_close_handler(rev);
      }
  }
  
  
  static void
  ngx_http_lingering_close_handler(ngx_event_t *rev)
  {
      ssize_t                    n;
      ngx_msec_t                 timer;
      ngx_connection_t          *c;
      ngx_http_request_t        *r;
      ngx_http_core_loc_conf_t  *clcf;
      u_char                     buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
  
      c = rev->data;
      r = c->data;
  
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
                     "http lingering close handler");
  
      if (rev->timedout) {
          ngx_http_close_request(r, 0);
          return;
      }
  
      timer = (ngx_msec_t) (r->lingering_time - ngx_time());
      if (timer <= 0) {
          ngx_http_close_request(r, 0);
          return;
      }
  
      do {
          n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
  
          ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
  
          if (n == NGX_ERROR || n == 0) {
              ngx_http_close_request(r, 0);
              return;
          }
  
      } while (rev->ready);
  
      if (ngx_handle_read_event(rev, 0) != NGX_OK) {
          ngx_http_close_request(r, 0);
          return;
      }
  
      clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  
      timer *= 1000;
  
      if (timer > clcf->lingering_timeout) {
          timer = clcf->lingering_timeout;
      }
  
      ngx_add_timer(rev, timer);
  }
  
  
  void
  ngx_http_empty_handler(ngx_event_t *wev)
  {
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
  
      return;
  }
  
  
  void
  ngx_http_request_empty_handler(ngx_http_request_t *r)
  {
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                     "http request empty handler");
  
      return;
  }
  
  
  ngx_int_t
  ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
  {
      ngx_buf_t    *b;
      ngx_chain_t   out;
  
      b = ngx_calloc_buf(r->pool);
      if (b == NULL) {
          return NGX_ERROR;
      }
  
      if (flags & NGX_HTTP_LAST) {
  
          if (r == r->main && !r->post_action) {
              b->last_buf = 1;
  
          } else {
              b->sync = 1;
              b->last_in_chain = 1;
          }
      }
  
      if (flags & NGX_HTTP_FLUSH) {
          b->flush = 1;
      }
  
      out.buf = b;
      out.next = NULL;
  
      return ngx_http_output_filter(r, &out);
  }
  
  
  static ngx_int_t
  ngx_http_post_action(ngx_http_request_t *r)
  {
      ngx_http_core_loc_conf_t  *clcf;
  
      clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  
      if (clcf->post_action.data == NULL) {
          return NGX_DECLINED;
      }
  
      if (r->post_action && r->uri_changes == 0) {
          return NGX_DECLINED;
      }
  
      ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                     "post action: \"%V\"", &clcf->post_action);
  
      r->main->count--;
  
      r->http_version = NGX_HTTP_VERSION_9;
      r->header_only = 1;
      r->post_action = 1;
  
      r->read_event_handler = ngx_http_block_reading;
  
      if (clcf->post_action.data[0] == '/') {
          ngx_http_internal_redirect(r, &clcf->post_action, NULL);
  
      } else {
          ngx_http_named_location(r, &clcf->post_action);
      }
  
      return NGX_OK;
  }
  
  
  static void
  ngx_http_close_request(ngx_http_request_t *r, ngx_int_t rc)
  {
  	/*由ngx_http_request_t结构体的main成员中取出对应的原始请求（当然，可能就
  是这个请求本身），再取出count引用计数并减1。然后，检查count引用计数是否已经为0，
  以及blocked标志位是否为0。如果count已经为0，则证明请求没有其他动作要使用了，同时
  blocked标志位也为0，表示没有HTTP模块还需要处理请求，所以此时请求可以真正释放
  如果count引用计数大于0，或者blocked大于0，这样都不可以结束请求，
  ngx_http_close_request方法直接结束。-----luguifang*/
      ngx_connection_t  *c;
  
      r = r->main;
      c = r->connection;
  
      ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
                     "http request count:%d blk:%d", r->count, r->blocked);
  
      if (r->count == 0) {
          ngx_log_error(NGX_LOG_ALERT, c->log, 0, "http request count is zero");
      }
  
      r->count--;
  	/*ngx_http_request_t结构体中的blocked标志位主要
  由异步I/O使用，ngx_http_close_request方法正是通过blocked配合着异步I/O工作，如果AIO
  上下文中还在处理这个请求，blocked必然是大于0的，这时ngx_http_close_request方法不能结
  束请求*/
      if (r->count || r->blocked) {
          return;
      }
  	/*释放请求关闭链接*/
      ngx_http_free_request(r, rc);
      ngx_http_close_connection(c);
  }
  
  
  static void
  ngx_http_free_request(ngx_http_request_t *r, ngx_int_t rc)
  {
      ngx_log_t                 *log;
      struct linger              linger;
      ngx_http_cleanup_t        *cln;
      ngx_http_log_ctx_t        *ctx;
      ngx_http_core_loc_conf_t  *clcf;
  
      log = r->connection->log;
  
      ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
  
      if (r->pool == NULL) {
          ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
          return;
      }
  	/*循环地遍历请求ngx_http_request_t结构体中的cleanup链表，依次调用每一个
  	ngx_http_cleanup_pt方法释放资源-----luguifang*/
      for (cln = r->cleanup; cln; cln = cln->next) {
          if (cln->handler) {
              cln->handler(cln->data);
          }
      }
  
  #if (NGX_STAT_STUB)
  
      if (r->stat_reading) {
          (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
      }
  
      if (r->stat_writing) {
          (void) ngx_atomic_fetch_add(ngx_stat_writing, -1);
      }
  
  #endif
  
      if (rc > 0 && (r->headers_out.status == 0 || r->connection->sent == 0)) {
          r->headers_out.status = rc;
      }
  	/*在11个ngx_http_phases阶段中，最后一个阶段叫做NGX_HTTP_LOG_PHASE，它是用
  来记录客户端的访问日志的。在这一步骤中，将会依次调用NGX_HTTP_LOG_PHASE阶段的
  所有回调方法记录日志。官方的ngx_http_log_module模块就是在这里记录access log的-------luguifang*/
      log->action = "logging request";
  
      ngx_http_log_request(r);
  
      log->action = "closing request";
  
      if (r->connection->timedout) {
          clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  
          if (clcf->reset_timedout_connection) {
              linger.l_onoff = 1;
              linger.l_linger = 0;
  
              if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
                             (const void *) &linger, sizeof(struct linger)) == -1)
              {
                  ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
                                "setsockopt(SO_LINGER) failed");
              }
          }
      }
  
      /* the various request strings were allocated from r->pool */
      ctx = log->data;
      ctx->request = NULL;
  
      r->request_line.len = 0;
  
      r->connection->destroyed = 1;
  	/*销毁请求ngx_http_request_t结构体中的pool内存池。在销毁内存池时，挂在该内存池
  下的由各Nginx模块实现的ngx_pool_cleanup_t方法也会被调用------luguifang*/
      ngx_destroy_pool(r->pool);
  }
  
  
  static void
  ngx_http_log_request(ngx_http_request_t *r)
  {
      ngx_uint_t                  i, n;
      ngx_http_handler_pt        *log_handler;
      ngx_http_core_main_conf_t  *cmcf;
  
      cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
  
      log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
      n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
  
      for (i = 0; i < n; i++) {
          log_handler[i](r);
      }
  }
  
  
  static void
  ngx_http_close_connection(ngx_connection_t *c)
  {
      ngx_pool_t  *pool;
  
      ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
                     "close http connection: %d", c->fd);
  
  #if (NGX_HTTP_SSL)
  
      if (c->ssl) {
          if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
              c->ssl->handler = ngx_http_close_connection;
              return;
          }
      }
  
  #endif
  
  #if (NGX_STAT_STUB)
      (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
  #endif
  
      c->destroyed = 1;
  
      pool = c->pool;
  
      ngx_close_connection(c);
  
      ngx_destroy_pool(pool);
  }
  
  
  static u_char *
  ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
  {
      u_char              *p;
      ngx_http_request_t  *r;
      ngx_http_log_ctx_t  *ctx;
  
      if (log->action) {
          p = ngx_snprintf(buf, len, " while %s", log->action);
          len -= p - buf;
          buf = p;
      }
  
      ctx = log->data;
  
      p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
      len -= p - buf;
  
      r = ctx->request;
  
      if (r) {
          return r->log_handler(r, ctx->current_request, p, len);
  
      } else {
          p = ngx_snprintf(p, len, ", server: %V",
                           &ctx->connection->listening->addr_text);
      }
  
      return p;
  }
  
  
  static u_char *
  ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
      u_char *buf, size_t len)
  {
      char                      *uri_separator;
      u_char                    *p;
      ngx_http_upstream_t       *u;
      ngx_http_core_srv_conf_t  *cscf;
  
      cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  
      p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
      len -= p - buf;
      buf = p;
  
      if (r->request_line.data == NULL && r->request_start) {
          for (p = r->request_start; p < r->header_in->last; p++) {
              if (*p == CR || *p == LF) {
                  break;
              }
          }
  
          r->request_line.len = p - r->request_start;
          r->request_line.data = r->request_start;
      }
  
      if (r->request_line.len) {
          p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
          len -= p - buf;
          buf = p;
      }
  
      if (r != sr) {
          p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
          len -= p - buf;
          buf = p;
      }
  
      u = sr->upstream;
  
      if (u && u->peer.name) {
  
          uri_separator = "";
  
  #if (NGX_HAVE_UNIX_DOMAIN)
          if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
              uri_separator = ":";
          }
  #endif
  
          p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
                           &u->schema, u->peer.name,
                           uri_separator, &u->uri);
          len -= p - buf;
          buf = p;
      }
  
      if (r->headers_in.host) {
          p = ngx_snprintf(buf, len, ", host: \"%V\"",
                           &r->headers_in.host->value);
          len -= p - buf;
          buf = p;
      }
  
      if (r->headers_in.referer) {
          p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
                           &r->headers_in.referer->value);
          buf = p;
      }
  
      return buf;
  }
  