Nginx学习之三-ngx_http_request_t结构体

时间:2023-03-09 02:47:14
Nginx学习之三-ngx_http_request_t结构体

ngx_http_request_s是nginx中非常重要的一个结构体,贯穿于htpp请求处理的整个过程中。

下面解释了ngx_http_request_s结构体中与HTTP框架相关的重要的成员变量。

  1. struct ngx_http_request_s {
  2. uint32_t                          signature;         /* "HTTP" */
  3. //请求对应的客户端连接
  4. ngx_connection_t                 *connection;
  5. //指向存放所有HTTP模块的上下文结构体的指针数组
  6. void                            **ctx;
  7. //指向请求对应的存放main级别配置结构体的指针数组
  8. void                            **main_conf;
  9. //指向请求对应的存放srv级别配置结构体的指针数组
  10. void                            **srv_conf;
  11. //指向请求对应的存放loc级别配置结构体的指针数组
  12. void                            **loc_conf;
  13. /*
  14. * 在接收完http头部,第一次在业务上处理http请求时,http框架提供的处理方法是ngx_http_process_request。
  15. 但如果该方法无法一次处理完该请求的全部业务,在归还控制权到epoll时间模块后,该请求再次被回调时,
  16. 将通过Ngx_http_request_handler方法来处理,而这个方法中对于可读事件的处理就是调用read_event_handler处理请求。
  17. 也就是说,http模块希望在底层处理请求的读事件时,重新实现read_event_handler方法
  18. */
  19. ngx_http_event_handler_pt         read_event_handler;
  20. //与上面的方法类似
  21. ngx_http_event_handler_pt         write_event_handler;
  22. #if (NGX_HTTP_CACHE)
  23. ngx_http_cache_t                 *cache;
  24. #endif
  25. //upstream机制用到的结构体
  26. ngx_http_upstream_t              *upstream;
  27. ngx_array_t                      *upstream_states;
  28. /* of ngx_http_upstream_state_t */
  29. //这个请求的内存池
  30. ngx_pool_t                       *pool;
  31. //用于接收http请求内容的缓冲区,主要接收http头部
  32. ngx_buf_t                        *header_in;
  33. //ngx_http_process_request_headers在接收、解析完http请求的头部后,会把解析完的每一个http头部加入到headers_in的headers链表中,同时会构造headers_in中的其他成员
  34. ngx_http_headers_in_t             headers_in;
  35. //http模块会把想要发送的http相应信息放到headers_out中,期望http框架将headers_out中的成员序列化为http响应包发送给用户
  36. ngx_http_headers_out_t            headers_out;
  37. //接收请求中包体的数据结构
  38. ngx_http_request_body_t          *request_body;
  39. //延迟关闭连接的时间
  40. time_t                            lingering_time;
  41. //当前请求初始化时的时间
  42. time_t                            start_sec;
  43. ngx_msec_t                        start_msec;
  44. //下面的9个成员是函数ngx_http_process_request_line方法在接收、解析http请求行时解析出的信息
  45. ngx_uint_t                        method;//方法名
  46. ngx_uint_t                        http_version;//协议版本
  47. ngx_str_t                         request_line;
  48. ngx_str_t                         uri;//用户请求中的uri
  49. ngx_str_t                         args;//用户请求中的url参数
  50. ngx_str_t                         exten;//用户请求的文件扩展名
  51. ngx_str_t                         unparsed_uri;//没有进行URL解码的原始请求
  52. ngx_str_t                         method_name;//用户请求中的方法名字符串
  53. ngx_str_t                         http_protocol;//其data成员指向请求中http起始地址
  54. /*表示需要发送给客户端的http响应。out中保存着由headers_out中序列化后的表示http头部的TCP流。
  55. * 在调用ngx_http_output_filter方法后,out中还会保存着待发送的http包体,它是实现异步发送http响应的关键。*/
  56. ngx_chain_t                      *out;
  57. /*当前请求既有可能是用户发来的请求,也可能是派生出的子请求。
  58. * 而main标识一系列相关的派生子请求的原始请求。
  59. * 一般可通过main和当前请求的地址是否相等来判断当前请求是否为用户发来的原始请求。*/
  60. ngx_http_request_t               *main;
  61. //当前请求的父请求(不一定是原始请求)
  62. ngx_http_request_t               *parent;
  63. //与subrequest子请求相关的功能
  64. ngx_http_postponed_request_t     *postponed;
  65. ngx_http_post_subrequest_t       *post_subrequest;
  66. //所有的子请求都是通过这个单链表链接起来的
  67. ngx_http_posted_request_t        *posted_requests;
  68. /*全局的ngx_http_phase_engine_t结构体中定义了一个ngx_http_phase_handler_t回答方法组成的数组。
  69. * 而phase_handler成员则与该数组配合使用。表示请求下次应当执行phase_handler作为序列号指定的数组中的回调方法*/
  70. ngx_int_t                         phase_handler;
  71. //表示NGX_HTTP_CONTENT_PHASE阶段提供给http模块处理请求的一种方式,它指向http模块实现的请求处理方法
  72. ngx_http_handler_pt               content_handler;
  73. //在NGX_HTTP_ACCESS_PHASE节点需要判断请求是否具有访问权限时,通过access_code来传递http模块的handler回调方法的返回值,如果为0表示具备权限。否则不具备。
  74. ngx_uint_t                        access_code;
  75. ngx_http_variable_value_t        *variables;
  76. #if (NGX_PCRE)
  77. ngx_uint_t                        ncaptures;
  78. int                              *captures;
  79. u_char                           *captures_data;
  80. #endif
  81. size_t                            limit_rate;
  82. /* used to learn the Apache compatible response length without a header */
  83. size_t                            header_size;
  84. //http请求的全部长度,包括http包体
  85. off_t                             request_length;
  86. ngx_uint_t                        err_status;
  87. ngx_http_connection_t            *http_connection;
  88. #if (NGX_HTTP_SPDY)
  89. ngx_http_spdy_stream_t           *spdy_stream;
  90. #endif
  91. ngx_http_log_handler_pt           log_handler;
  92. //在这个请求中如果打开了某些资源,并需要在请求结束时释放,那么需要把定义的释放资源的方法添加到这个成员
  93. ngx_http_cleanup_t               *cleanup;
  94. unsigned                          subrequests:8;
  95. //引用计数一般都作用于这个请求的原始请求上
  96. //引用计数,每当派生出子请求时,原始请求的count成员都会加一
  97. unsigned                          count:8;
  98. //阻塞标志位,目前仅由aio使用
  99. unsigned                          blocked:8;
  100. //标志位:为1表示蛋清请求正在使用异步IO
  101. unsigned                          aio:1;
  102. unsigned                          http_state:4;
  103. /* URI with "/." and on Win32 with "//" */
  104. unsigned                          complex_uri:1;
  105. /* URI with "%" */
  106. unsigned                          quoted_uri:1;
  107. /* URI with "+" */
  108. unsigned                          plus_in_uri:1;
  109. /* URI with " " */
  110. unsigned                          space_in_uri:1;
  111. unsigned                          invalid_header:1;
  112. unsigned                          add_uri_to_alias:1;
  113. unsigned                          valid_location:1;
  114. unsigned                          valid_unparsed_uri:1;
  115. //标志位:为1时表示URL发生过rewrite重写
  116. unsigned                          uri_changed:1;
  117. //表示使用rewrite重写URL的次数
  118. unsigned                          uri_changes:4;
  119. unsigned                          request_body_in_single_buf:1;
  120. unsigned                          request_body_in_file_only:1;
  121. unsigned                          request_body_in_persistent_file:1;
  122. unsigned                          request_body_in_clean_file:1;
  123. unsigned                          request_body_file_group_access:1;
  124. unsigned                          request_body_file_log_level:3;
  125. unsigned                          subrequest_in_memory:1;
  126. unsigned                          waited:1;
  127. #if (NGX_HTTP_CACHE)
  128. unsigned                          cached:1;
  129. #endif
  130. #if (NGX_HTTP_GZIP)
  131. unsigned                          gzip_tested:1;
  132. unsigned                          gzip_ok:1;
  133. unsigned                          gzip_vary:1;
  134. #endif
  135. unsigned                          proxy:1;
  136. unsigned                          bypass_cache:1;
  137. unsigned                          no_cache:1;
  138. /*
  139. * instead of using the request context data in
  140. * ngx_http_limit_conn_module and ngx_http_limit_req_module
  141. * we use the single bits in the request structure
  142. */
  143. unsigned                          limit_conn_set:1;
  144. unsigned                          limit_req_set:1;
  145. #if 0
  146. unsigned                          cacheable:1;
  147. #endif
  148. unsigned                          pipeline:1;
  149. unsigned                          chunked:1;
  150. unsigned                          header_only:1;
  151. //标志位,为1表示当前请求时keepalive请求
  152. unsigned                          keepalive:1;
  153. //延迟关闭标志位
  154. unsigned                          lingering_close:1;
  155. //标志位:为1表示正在丢弃http请求中的包体
  156. unsigned                          discard_body:1;
  157. //标志位:为1表示请求的当前状态是在做内部跳转
  158. unsigned                          internal:1;
  159. unsigned                          error_page:1;
  160. unsigned                          ignore_content_encoding:1;
  161. unsigned                          filter_finalize:1;
  162. unsigned                          post_action:1;
  163. unsigned                          request_complete:1;
  164. unsigned                          request_output:1;
  165. //标志位:为1表示发生给客户端的http响应头已经发送
  166. unsigned                          header_sent:1;
  167. unsigned                          expect_tested:1;
  168. unsigned                          root_tested:1;
  169. unsigned                          done:1;
  170. unsigned                          logged:1;
  171. //标志位,表示缓冲中是否有待发送内容
  172. unsigned                          buffered:4;
  173. unsigned                          main_filter_need_in_memory:1;
  174. unsigned                          filter_need_in_memory:1;
  175. unsigned                          filter_need_temporary:1;
  176. unsigned                          allow_ranges:1;
  177. #if (NGX_STAT_STUB)
  178. unsigned                          stat_reading:1;
  179. unsigned                          stat_writing:1;
  180. #endif
  181. /* used to parse HTTP headers */
  182. //状态机解析http时使用state来表示当前的解析状态,需要检查是否构成完成的http请求行
  183. ngx_uint_t                        state;
  184. ngx_uint_t                        header_hash;
  185. ngx_uint_t                        lowcase_index;
  186. u_char                            lowcase_header[NGX_HTTP_LC_HEADER_LEN];
  187. u_char                           *header_name_start;
  188. u_char                           *header_name_end;
  189. u_char                           *header_start;
  190. u_char                           *header_end;
  191. /*
  192. * a memory that can be reused after parsing a request line
  193. * via ngx_http_ephemeral_t
  194. */
  195. u_char                           *uri_start;
  196. u_char                           *uri_end;
  197. u_char                           *uri_ext;
  198. u_char                           *args_start;
  199. u_char                           *request_start;
  200. u_char                           *request_end;
  201. u_char                           *method_end;
  202. u_char                           *schema_start;
  203. u_char                           *schema_end;
  204. u_char                           *host_start;
  205. u_char                           *host_end;
  206. u_char                           *port_start;
  207. u_char                           *port_end;
  208. unsigned                          http_minor:16;
  209. unsigned                          http_major:16;
  210. };