nginx處理http(http處理結構篇)
阿新 • • 發佈:2019-01-22
http頭部結構定義
頭部處理函式指標定義
/*
@param r http請求結構
@param h http頭部的hash表
@param offset 位於結構體中的偏移量
*/
typedef ngx_int_t (*ngx_http_header_handler_pt)(ngx_http_request_t *r,
ngx_table_elt_t *h, ngx_uint_t offset)
typedef struct {
ngx_str_t name; //頭鍵值名稱
ngx_uint_t offset; //位於結構體中的偏移量
ngx_http_header_handler_pt handler; //處理函式指標
} ngx_http_header_t
響應頭定義
typedef struct {
ngx_str_t name; //鍵值名稱
ngx_uint_t offset; //位於結構體中的偏移
} ngx_http_header_out_t
請求頭部結構
typedef struct {
ngx_list_t headers; //頭部表
ngx_table_elt_t *host; //host頭
ngx_table_elt_t *connection; //connection頭
ngx_table_elt_t *if_modified_since; //if_modified_since頭
ngx_table_elt_t *if_unmodified_since; //if_unmodified_since頭
ngx_table_elt_t *if_match; //if_match頭
ngx_table_elt_t *if_none_match; //if_none_match頭
ngx_table_elt_t *user_agent; //user_agent頭
ngx_table_elt_t *referer; //referer頭
ngx_table_elt_t *content_length; //content_length頭(內容長度)
ngx_table_elt_t *content_range; //content_range頭(起始終止偏移量)
ngx_table_elt_t *content_type; //資源型別
ngx_table_elt_t *range; //range頭
ngx_table_elt_t *if_range; //if_range頭(用於斷點)
ngx_table_elt_t *transfer_encoding; //內容傳輸編碼
ngx_table_elt_t *expect; //expect頭
ngx_table_elt_t *upgrade; //upgrade 協議升級頭
#if (NGX_HTTP_GZIP)
ngx_table_elt_t *accept_encoding; //可接受的編碼格式頭
ngx_table_elt_t *via; //via頭
#endif
ngx_table_elt_t *authorization; //認證頭
ngx_table_elt_t *keep_alive; //keep_alive頭(http1.1)
#if (NGX_HTTP_X_FORWARDED_FOR)
ngx_array_t x_forwarded_for;//識別代理或者負載均衡
#endif
#if (NGX_HTTP_REALIP)
ngx_table_elt_t *x_real_ip; //真實請求IP頭
#endif
#if (NGX_HTTP_HEADERS)
ngx_table_elt_t *accept; //accept頭
ngx_table_elt_t *accept_language; //accept_language頭
#endif
#if (NGX_HTTP_DAV)
ngx_table_elt_t *depth; //depth頭
ngx_table_elt_t *destination; //dest頭
ngx_table_elt_t *overwrite; //overwrite頭
ngx_table_elt_t *date; //date日期頭
#endif
ngx_str_t user; //user 使用者名稱
ngx_str_t passwd; //passwd 密碼
ngx_array_t cookies; //cookies資訊
ngx_str_t server; //server資訊
off_t content_length_n; //請求內容長
time_t keep_alive_n; //keep_alive時長
unsigned connection_type:2; //連線型別(keepalive或者請求完成即關閉連線)
unsigned chunked:1;//是否是chunked傳輸
unsigned msie:1; //ie瀏覽器
unsigned msie6:1; //ie6
unsigned opera:1; //opera瀏覽器
unsigned gecko:1; //gecko瀏覽器
unsigned chrome:1; //chrome瀏覽器
unsigned safari:1; //safari瀏覽器
unsigned konqueror:1; //KDE開發的瀏覽器
} ngx_http_headers_in_t
響應頭結構
typedef struct {
ngx_list_t headers; //頭部列表
ngx_list_t trailers; //chunked傳輸方式使用
ngx_uint_t status; //響應狀態碼
ngx_str_t status_line; //狀態行(包含狀態碼和狀態資訊)
ngx_table_elt_t *server; //server頭
ngx_table_elt_t *date; //date日期頭
ngx_table_elt_t *content_length; //內容長度頭
ngx_table_elt_t *content_encoding;//內容編碼頭
ngx_table_elt_t *location; //地址頭
ngx_table_elt_t *refresh; //refresh頭(重新整理頭 可用於請求跳轉)
ngx_table_elt_t *last_modified; //last_modified頭
ngx_table_elt_t *content_range; //content_range頭
ngx_table_elt_t *accept_ranges; //range的型別(bytes...)
ngx_table_elt_t *www_authenticate; //www_authenticate頭
ngx_table_elt_t *expires; //expires內容過期時間頭
ngx_table_elt_t *etag; //內容etag頭
ngx_str_t *override_charset; //override_charset
size_t content_type_len; //內容型別長度
ngx_str_t content_type; //內容型別
ngx_str_t charset; //字符集資訊
u_char *content_type_lowcase; //小寫
ngx_uint_t content_type_hash; //內容型別的hash值
ngx_array_t cache_control; //cache_control頭
off_t content_length_n; //內容長度
off_t content_offset; //內容偏移量
time_t date_time; //date時間資訊
time_t last_modified_time; //last_modified時間
} ngx_http_headers_out_t
請求包體結構
typedef struct {
ngx_temp_file_t *temp_file; //臨時檔案
ngx_chain_t *bufs; //鏈式的buffer集合
ngx_buf_t *buf; //單一的緩衝區
off_t rest; //剩餘長度
off_t received; //已經接收的長度
ngx_chain_t *free; //空閒的buffer集合
ngx_chain_t *busy; //處於"忙碌"狀態的buffer集合
ngx_http_chunked_t *chunked; //chunked型別內容
ngx_http_client_body_handler_pt post_handler; //對請求包體的處理函式指標
} ngx_http_request_body_t
對於tcp連線的結構
typedef struct {
ngx_http_addr_conf_t *addr_conf; //地址的配置資訊
ngx_http_conf_ctx_t *conf_ctx; //配置資訊的上下文
#if (NGX_HTTP_SSL || NGX_COMPAT)
ngx_str_t *ssl_servername; //ssl server的名稱
#if (NGX_PCRE)
ngx_http_regex_t *ssl_servername_regex; //正則形式的server名
#endif
#endif
ngx_chain_t *busy; //處於"忙碌"狀態的buffer集合
ngx_int_t nbusy; //處於"忙碌"狀態的數量
ngx_chain_t *free; //空閒的buffer集合
unsigned ssl:1; //是否使用ssl協議
unsigned proxy_protocol:1; //是否使用代理協議
} ngx_http_connection_t
http請求結構
struct ngx_http_request_s {
uint32_t signature; /* "HTTP" */ //簽名
ngx_connection_t *connection; //tcp連線
void **ctx; //http模組上下文指標
void **main_conf; //main配置指標
void **srv_conf; //server配置指標
void **loc_conf; //location配置指標
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; //http快取
#endif
ngx_http_upstream_t *upstream; //upstream指標
ngx_array_t *upstream_states; //upstream狀態陣列
/* of ngx_http_upstream_state_t */
ngx_pool_t *pool; //記憶體池
ngx_buf_t *header_in; //處理請求頭的buffer
ngx_http_headers_in_t headers_in; //請求頭
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; //開始事件(單位毫秒)
ngx_uint_t method; //請求的method
ngx_uint_t http_version; //http協議版本
/*
請求行形如 GET /url/params HTTP/1.1
*/
ngx_str_t request_line;
ngx_str_t uri; //請求的uri
ngx_str_t args; //請求引數
ngx_str_t exten; //擴充套件引數
ngx_str_t unparsed_uri; //未解析的uri(原始uri)
ngx_str_t method_name; //請求方法名稱
ngx_str_t http_protocol; //http協議名稱
ngx_chain_t *out; //響應連結串列
ngx_http_request_t *main; //主請求
ngx_http_request_t *parent; //父親請求
ngx_http_postponed_request_t *postponed; //postponed 子請求專用
ngx_http_post_subrequest_t *post_subrequest; //投遞的子請求處理函式 finalize時呼叫
ngx_http_posted_request_t *posted_requests; //已經投遞的請求
ngx_int_t phase_handler; //phase處理函式的索引
ngx_http_handler_pt content_handler; //內容處理函式指標
ngx_uint_t access_code; //訪問碼
ngx_http_variable_value_t *variables; //http變數集合
#if (NGX_PCRE)
/*正則相關*/
ngx_uint_t ncaptures;
int *captures;
u_char *captures_data;
#endif
size_t limit_rate; //限速值
size_t limit_rate_after; //限速發生閾值
/* used to learn the Apache compatible response length without a header */
size_t header_size; //http頭部的大小
off_t request_length; //請求的長度
ngx_uint_t err_status; //錯誤碼
ngx_http_connection_t *http_connection; //http連線處理結構
ngx_http_v2_stream_t *stream; //http2.0 stream
ngx_http_log_handler_pt log_handler; //生成日誌處理函式
ngx_http_cleanup_t *cleanup; //清理函式
unsigned count:16; //請求引用計數
unsigned subrequests:8; //子請求的遞迴深度
unsigned blocked:8; //blocked阻塞計數
unsigned aio:1; //非同步IO標記
unsigned http_state:4; //http請求處理狀態
/* URI with "/." and on Win32 with "//" */
unsigned complex_uri:1; //複雜的uri
/* URI with "%" */
unsigned quoted_uri:1; //攜帶分號的uri
/* URI with "+" */
unsigned plus_in_uri:1; //uri中包含+號
/* URI with " " */
unsigned space_in_uri:1; //uri中包含空格
unsigned invalid_header:1; //是否為無效頭部
unsigned add_uri_to_alias:1;
unsigned valid_location:1; //是否是有效的路徑
unsigned valid_unparsed_uri:1; //是否是有效的未解析的uri
unsigned uri_changed:1; //uri是否發生了變化
unsigned uri_changes:4; //剩餘的uri可改變次數
unsigned request_body_in_single_buf:1; //請求包體在單一的buffer中
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; //設定請求包體檔案的group許可權
unsigned request_body_file_log_level:3; //設定日誌級別
unsigned request_body_no_buffering:1; //請求包體是否進行緩衝處理
unsigned subrequest_in_memory:1; //子請求是否在記憶體buffer中處理
unsigned waited:1; //是否等待
#if (NGX_HTTP_CACHE)
unsigned cached:1; //請求的資料是否快取
#endif
#if (NGX_HTTP_GZIP)
/*gzip壓縮相關*/
unsigned gzip_tested:1;
unsigned gzip_ok:1;
unsigned gzip_vary:1;
#endif
unsigned proxy:1; //unused
unsigned bypass_cache:1; //unused
unsigned no_cache:1; //unused
/*
* 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; //unused
#endif
unsigned pipeline:1; //流水線
unsigned chunked:1; //chunked傳輸
unsigned header_only:1; //只有頭部
unsigned expect_trailers:1; //trailers與chunked傳輸對應
unsigned keepalive:1; //是否使用keepalive
unsigned lingering_close:1; //延遲關閉連線
unsigned discard_body:1; //對齊包體
unsigned reading_body:1; //是否正在讀取包體
unsigned internal:1; //內部跳轉
unsigned error_page:1; //error_page 形如404.html
unsigned filter_finalize:1; //filter終止
unsigned post_action:1; //終止請求或者錯誤的處理post_action可以配置
unsigned request_complete:1; //請求完成標記
unsigned request_output:1; //請求output標記
unsigned header_sent:1; //頭部是否傳送標記
unsigned expect_tested:1; //expect_tested標記(對應expect請求頭處理)
unsigned root_tested:1; //root_test標記
unsigned done:1; //處理完成標記
unsigned logged:1; //日誌處理是否進行過設定
unsigned buffered:4; //請求buffered標記
unsigned main_filter_need_in_memory:1; //主請求的filter需要在記憶體中處理
unsigned filter_need_in_memory:1; //filter需要在記憶體中處理
unsigned filter_need_temporary:1; //需要在臨時檔案中處理
unsigned preserve_body:1; //是否保留請求包體
unsigned allow_ranges:1; //是否允許使用range
unsigned subrequest_ranges:1; //子請求是否允許range
unsigned single_range:1; //只有一個range
unsigned disable_not_modified:1; //是否禁用not_modified_filter模組處理
unsigned stat_reading:1; //開始讀(stat_stub專用)
unsigned stat_writing:1; //開始寫(stat_stub專用)
unsigned stat_processing:1; //unused
unsigned background:1; //後臺處理
unsigned health_check:1; //unused
/* used to parse HTTP headers */
ngx_uint_t state; //狀態碼
ngx_uint_t header_hash; //頭部hash值
ngx_uint_t lowcase_index; //小寫索引
u_char lowcase_header[NGX_HTTP_LC_HEADER_LEN]; //小寫http頭部陣列
/*解析http頭部專用*/
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
*/
/*request_line請求行解析專用*/
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; //http副版本號
unsigned http_major:16; //http主版本號
}