天天看點

Nginx學習之三-ngx_http_request_t結構體

ngx_http_request_s是nginx中非常重要的一個結構體,貫穿于htpp請求處理的整個過程中。

下面解釋了ngx_http_request_s結構體中與HTTP架構相關的重要的成員變量。

struct ngx_http_request_s {  

    uint32_t                          signature;         /* "HTTP" */  

    //請求對應的用戶端連接配接  

    ngx_connection_t                 *connection;  

    //指向存放所有HTTP子產品的上下文結構體的指針數組  

    void                            **ctx;  

    //指向請求對應的存放main級别配置結構體的指針數組  

    void                            **main_conf;  

    //指向請求對應的存放srv級别配置結構體的指針數組  

    void                            **srv_conf;  

    //指向請求對應的存放loc級别配置結構體的指針數組  

    void                            **loc_conf;  

    /* 

     * 在接收完http頭部,第一次在業務上處理http請求時,http架構提供的處理方法是ngx_http_process_request。 

     但如果該方法無法一次處理完該請求的全部業務,在歸還控制權到epoll時間子產品後,該請求再次被回調時, 

     将通過Ngx_http_request_handler方法來處理,而這個方法中對于可讀事件的處理就是調用read_event_handler處理請求。 

     也就是說,http子產品希望在底層處理請求的讀事件時,重新實作read_event_handler方法 

    */  

    ngx_http_event_handler_pt         read_event_handler;  

    //與上面的方法類似  

    ngx_http_event_handler_pt         write_event_handler;  

#if (NGX_HTTP_CACHE)  

    ngx_http_cache_t                 *cache;  

#endif  

    //upstream機制用到的結構體  

    ngx_http_upstream_t              *upstream;  

    ngx_array_t                      *upstream_states;  

                                         /* of ngx_http_upstream_state_t */  

    //這個請求的記憶體池  

    ngx_pool_t                       *pool;  

    //用于接收http請求内容的緩沖區,主要接收http頭部  

    ngx_buf_t                        *header_in;  

    //ngx_http_process_request_headers在接收、解析完http請求的頭部後,會把解析完的每一個http頭部加入到headers_in的headers連結清單中,同時會構造headers_in中的其他成員  

    ngx_http_headers_in_t             headers_in;  

    //http子產品會把想要發送的http相應資訊放到headers_out中,期望http架構将headers_out中的成員序列化為http響應包發送給使用者  

    ngx_http_headers_out_t            headers_out;  

    //接收請求中包體的資料結構  

    ngx_http_request_body_t          *request_body;  

    //延遲關閉連接配接的時間  

    time_t                            lingering_time;  

    //目前請求初始化時的時間  

    time_t                            start_sec;  

    ngx_msec_t                        start_msec;  

    //下面的9個成員是函數ngx_http_process_request_line方法在接收、解析http請求行時解析出的資訊  

    ngx_uint_t                        method;//方法名  

    ngx_uint_t                        http_version;//協定版本  

    ngx_str_t                         request_line;  

    ngx_str_t                         uri;//使用者請求中的uri  

    ngx_str_t                         args;//使用者請求中的url參數  

    ngx_str_t                         exten;//使用者請求的檔案擴充名  

    ngx_str_t                         unparsed_uri;//沒有進行URL解碼的原始請求  

    ngx_str_t                         method_name;//使用者請求中的方法名字元串  

    ngx_str_t                         http_protocol;//其data成員指向請求中http起始位址  

    /*表示需要發送給用戶端的http響應。out中儲存着由headers_out中序列化後的表示http頭部的TCP流。 

     * 在調用ngx_http_output_filter方法後,out中還會儲存着待發送的http包體,它是實作異步發送http響應的關鍵。*/  

    ngx_chain_t                      *out;  

    /*目前請求既有可能是使用者發來的請求,也可能是派生出的子請求。 

     * 而main辨別一系列相關的派生子請求的原始請求。 

     * 一般可通過main和目前請求的位址是否相等來判斷目前請求是否為使用者發來的原始請求。*/  

    ngx_http_request_t               *main;  

    //目前請求的父請求(不一定是原始請求)  

    ngx_http_request_t               *parent;  

    //與subrequest子請求相關的功能  

    ngx_http_postponed_request_t     *postponed;  

    ngx_http_post_subrequest_t       *post_subrequest;  

    //所有的子請求都是通過這個單連結清單連結起來的  

    ngx_http_posted_request_t        *posted_requests;  

    /*全局的ngx_http_phase_engine_t結構體中定義了一個ngx_http_phase_handler_t回答方法組成的數組。 

     * 而phase_handler成員則與該數組配合使用。表示請求下次應當執行phase_handler作為序列号指定的數組中的回調方法*/  

    ngx_int_t                         phase_handler;  

    //表示NGX_HTTP_CONTENT_PHASE階段提供給http子產品處理請求的一種方式,它指向http子產品實作的請求處理方法  

    ngx_http_handler_pt               content_handler;  

    //在NGX_HTTP_ACCESS_PHASE節點需要判斷請求是否具有通路權限時,通過access_code來傳遞http子產品的handler回調方法的傳回值,如果為0表示具備權限。否則不具備。  

    ngx_uint_t                        access_code;  

    ngx_http_variable_value_t        *variables;  

#if (NGX_PCRE)  

    ngx_uint_t                        ncaptures;  

    int                              *captures;  

    u_char                           *captures_data;  

    size_t                            limit_rate;  

    /* used to learn the Apache compatible response length without a header */  

    size_t                            header_size;  

    //http請求的全部長度,包括http包體  

    off_t                             request_length;  

    ngx_uint_t                        err_status;  

    ngx_http_connection_t            *http_connection;  

#if (NGX_HTTP_SPDY)  

    ngx_http_spdy_stream_t           *spdy_stream;  

    ngx_http_log_handler_pt           log_handler;  

    //在這個請求中如果打開了某些資源,并需要在請求結束時釋放,那麼需要把定義的釋放資源的方法添加到這個成員  

    ngx_http_cleanup_t               *cleanup;  

    unsigned                          subrequests:8;  

    //引用計數一般都作用于這個請求的原始請求上  

    //引用計數,每當派生出子請求時,原始請求的count成員都會加一  

    unsigned                          count:8;  

    //阻塞标志位,目前僅由aio使用  

    unsigned                          blocked:8;  

    //标志位:為1表示蛋清請求正在使用異步IO  

    unsigned                          aio:1;  

    unsigned                          http_state:4;  

    /* URI with "/." and on Win32 with "//" */  

    unsigned                          complex_uri:1;  

    /* URI with "%" */  

    unsigned                          quoted_uri:1;  

    /* URI with "+" */  

    unsigned                          plus_in_uri:1;  

    /* URI with " " */  

    unsigned                          space_in_uri:1;  

    unsigned                          invalid_header:1;  

    unsigned                          add_uri_to_alias:1;  

    unsigned                          valid_location:1;  

    unsigned                          valid_unparsed_uri:1;  

    //标志位:為1時表示URL發生過rewrite重寫  

    unsigned                          uri_changed:1;  

    //表示使用rewrite重寫URL的次數  

    unsigned                          uri_changes:4;  

    unsigned                          request_body_in_single_buf:1;  

    unsigned                          request_body_in_file_only:1;  

    unsigned                          request_body_in_persistent_file:1;  

    unsigned                          request_body_in_clean_file:1;  

    unsigned                          request_body_file_group_access:1;  

    unsigned                          request_body_file_log_level:3;  

    unsigned                          subrequest_in_memory:1;  

    unsigned                          waited:1;  

    unsigned                          cached:1;  

#if (NGX_HTTP_GZIP)  

    unsigned                          gzip_tested:1;  

    unsigned                          gzip_ok:1;  

    unsigned                          gzip_vary:1;  

    unsigned                          proxy:1;  

    unsigned                          bypass_cache:1;  

    unsigned                          no_cache:1;  

     * instead of using the request context data in 

     * ngx_http_limit_conn_module and ngx_http_limit_req_module 

     * we use the single bits in the request structure 

     */  

    unsigned                          limit_conn_set:1;  

    unsigned                          limit_req_set:1;  

#if 0  

    unsigned                          cacheable:1;  

    unsigned                          pipeline:1;  

    unsigned                          chunked:1;  

    unsigned                          header_only:1;  

    //标志位,為1表示目前請求時keepalive請求  

    unsigned                          keepalive:1;  

    //延遲關閉标志位  

    unsigned                          lingering_close:1;  

    //标志位:為1表示正在丢棄http請求中的包體  

    unsigned                          discard_body:1;  

    //标志位:為1表示請求的目前狀态是在做内部跳轉  

    unsigned                          internal:1;  

    unsigned                          error_page:1;  

    unsigned                          ignore_content_encoding:1;  

    unsigned                          filter_finalize:1;  

    unsigned                          post_action:1;  

    unsigned                          request_complete:1;  

    unsigned                          request_output:1;  

    //标志位:為1表示發生給用戶端的http響應頭已經發送  

    unsigned                          header_sent:1;  

    unsigned                          expect_tested:1;  

    unsigned                          root_tested:1;  

    unsigned                          done:1;  

    unsigned                          logged:1;  

    //标志位,表示緩沖中是否有待發送内容  

    unsigned                          buffered:4;  

    unsigned                          main_filter_need_in_memory:1;  

    unsigned                          filter_need_in_memory:1;  

    unsigned                          filter_need_temporary:1;  

    unsigned                          allow_ranges:1;  

#if (NGX_STAT_STUB)  

    unsigned                          stat_reading:1;  

    unsigned                          stat_writing:1;  

    /* used to parse HTTP headers */  

    //狀态機解析http時使用state來表示目前的解析狀态,需要檢查是否構成完成的http請求行  

    ngx_uint_t                        state;  

    ngx_uint_t                        header_hash;  

    ngx_uint_t                        lowcase_index;  

    u_char                            lowcase_header[NGX_HTTP_LC_HEADER_LEN];  

    u_char                           *header_name_start;  

    u_char                           *header_name_end;  

    u_char                           *header_start;  

    u_char                           *header_end;  

     * a memory that can be reused after parsing a request line 

     * via ngx_http_ephemeral_t 

    u_char                           *uri_start;  

    u_char                           *uri_end;  

    u_char                           *uri_ext;  

    u_char                           *args_start;  

    u_char                           *request_start;  

    u_char                           *request_end;  

    u_char                           *method_end;  

    u_char                           *schema_start;  

    u_char                           *schema_end;  

    u_char                           *host_start;  

    u_char                           *host_end;  

    u_char                           *port_start;  

    u_char                           *port_end;  

    unsigned                          http_minor:16;  

    unsigned                          http_major:16;  

};  

繼續閱讀