ngx_http_core_module.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */
  5. #ifndef _NGX_HTTP_CORE_H_INCLUDED_
  6. #define _NGX_HTTP_CORE_H_INCLUDED_
  7. #include <ngx_config.h>
  8. #include <ngx_core.h>
  9. #include <ngx_http.h>
  10. #if (NGX_THREADS)
  11. #include <ngx_thread_pool.h>
  12. #elif (NGX_COMPAT)
  13. typedef struct ngx_thread_pool_s ngx_thread_pool_t;
  14. #endif
  15. #define NGX_HTTP_GZIP_PROXIED_OFF 0x0002
  16. #define NGX_HTTP_GZIP_PROXIED_EXPIRED 0x0004
  17. #define NGX_HTTP_GZIP_PROXIED_NO_CACHE 0x0008
  18. #define NGX_HTTP_GZIP_PROXIED_NO_STORE 0x0010
  19. #define NGX_HTTP_GZIP_PROXIED_PRIVATE 0x0020
  20. #define NGX_HTTP_GZIP_PROXIED_NO_LM 0x0040
  21. #define NGX_HTTP_GZIP_PROXIED_NO_ETAG 0x0080
  22. #define NGX_HTTP_GZIP_PROXIED_AUTH 0x0100
  23. #define NGX_HTTP_GZIP_PROXIED_ANY 0x0200
  24. #define NGX_HTTP_AIO_OFF 0
  25. #define NGX_HTTP_AIO_ON 1
  26. #define NGX_HTTP_AIO_THREADS 2
  27. #define NGX_HTTP_SATISFY_ALL 0
  28. #define NGX_HTTP_SATISFY_ANY 1
  29. #define NGX_HTTP_LINGERING_OFF 0
  30. #define NGX_HTTP_LINGERING_ON 1
  31. #define NGX_HTTP_LINGERING_ALWAYS 2
  32. #define NGX_HTTP_IMS_OFF 0
  33. #define NGX_HTTP_IMS_EXACT 1
  34. #define NGX_HTTP_IMS_BEFORE 2
  35. #define NGX_HTTP_KEEPALIVE_DISABLE_NONE 0x0002
  36. #define NGX_HTTP_KEEPALIVE_DISABLE_MSIE6 0x0004
  37. #define NGX_HTTP_KEEPALIVE_DISABLE_SAFARI 0x0008
  38. #define NGX_HTTP_SERVER_TOKENS_OFF 0
  39. #define NGX_HTTP_SERVER_TOKENS_ON 1
  40. #define NGX_HTTP_SERVER_TOKENS_BUILD 2
  41. typedef struct ngx_http_location_tree_node_s ngx_http_location_tree_node_t;
  42. typedef struct ngx_http_core_loc_conf_s ngx_http_core_loc_conf_t;
  43. typedef struct {
  44. struct sockaddr *sockaddr;
  45. socklen_t socklen;
  46. ngx_str_t addr_text;
  47. unsigned set:1;
  48. unsigned default_server:1;
  49. unsigned bind:1;
  50. unsigned wildcard:1;
  51. unsigned ssl:1;
  52. unsigned http2:1;
  53. unsigned quic:1;
  54. #if (NGX_HAVE_INET6)
  55. unsigned ipv6only:1;
  56. #endif
  57. unsigned deferred_accept:1;
  58. unsigned reuseport:1;
  59. unsigned so_keepalive:2;
  60. unsigned proxy_protocol:1;
  61. int backlog;
  62. int rcvbuf;
  63. int sndbuf;
  64. int type;
  65. #if (NGX_HAVE_SETFIB)
  66. int setfib;
  67. #endif
  68. #if (NGX_HAVE_TCP_FASTOPEN)
  69. int fastopen;
  70. #endif
  71. #if (NGX_HAVE_KEEPALIVE_TUNABLE)
  72. int tcp_keepidle;
  73. int tcp_keepintvl;
  74. int tcp_keepcnt;
  75. #endif
  76. #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
  77. char *accept_filter;
  78. #endif
  79. } ngx_http_listen_opt_t;
  80. typedef enum {
  81. NGX_HTTP_POST_READ_PHASE = 0,
  82. NGX_HTTP_SERVER_REWRITE_PHASE,
  83. NGX_HTTP_FIND_CONFIG_PHASE,
  84. NGX_HTTP_REWRITE_PHASE,
  85. NGX_HTTP_POST_REWRITE_PHASE,
  86. NGX_HTTP_PREACCESS_PHASE,
  87. NGX_HTTP_ACCESS_PHASE,
  88. NGX_HTTP_POST_ACCESS_PHASE,
  89. NGX_HTTP_PRECONTENT_PHASE,
  90. NGX_HTTP_CONTENT_PHASE,
  91. NGX_HTTP_LOG_PHASE
  92. } ngx_http_phases;
  93. typedef struct ngx_http_phase_handler_s ngx_http_phase_handler_t;
  94. typedef ngx_int_t (*ngx_http_phase_handler_pt)(ngx_http_request_t *r,
  95. ngx_http_phase_handler_t *ph);
  96. struct ngx_http_phase_handler_s {
  97. ngx_http_phase_handler_pt checker;
  98. ngx_http_handler_pt handler;
  99. ngx_uint_t next;
  100. };
  101. typedef struct {
  102. ngx_http_phase_handler_t *handlers;
  103. ngx_uint_t server_rewrite_index;
  104. ngx_uint_t location_rewrite_index;
  105. } ngx_http_phase_engine_t;
  106. typedef struct {
  107. ngx_array_t handlers;
  108. } ngx_http_phase_t;
  109. typedef struct {
  110. ngx_array_t servers; /* ngx_http_core_srv_conf_t */
  111. ngx_http_phase_engine_t phase_engine;
  112. ngx_hash_t headers_in_hash;
  113. ngx_hash_t variables_hash;
  114. ngx_array_t variables; /* ngx_http_variable_t */
  115. ngx_array_t prefix_variables; /* ngx_http_variable_t */
  116. ngx_uint_t ncaptures;
  117. ngx_uint_t server_names_hash_max_size;
  118. ngx_uint_t server_names_hash_bucket_size;
  119. ngx_uint_t variables_hash_max_size;
  120. ngx_uint_t variables_hash_bucket_size;
  121. ngx_hash_keys_arrays_t *variables_keys;
  122. ngx_array_t *ports;
  123. ngx_http_phase_t phases[NGX_HTTP_LOG_PHASE + 1];
  124. } ngx_http_core_main_conf_t;
  125. typedef struct {
  126. /* array of the ngx_http_server_name_t, "server_name" directive */
  127. ngx_array_t server_names;
  128. /* server ctx */
  129. ngx_http_conf_ctx_t *ctx;
  130. u_char *file_name;
  131. ngx_uint_t line;
  132. ngx_str_t server_name;
  133. size_t connection_pool_size;
  134. size_t request_pool_size;
  135. size_t client_header_buffer_size;
  136. ngx_bufs_t large_client_header_buffers;
  137. ngx_msec_t client_header_timeout;
  138. ngx_flag_t ignore_invalid_headers;
  139. ngx_flag_t merge_slashes;
  140. ngx_flag_t underscores_in_headers;
  141. unsigned listen:1;
  142. #if (NGX_PCRE)
  143. unsigned captures:1;
  144. #endif
  145. ngx_http_core_loc_conf_t **named_locations;
  146. } ngx_http_core_srv_conf_t;
  147. /* list of structures to find core_srv_conf quickly at run time */
  148. typedef struct {
  149. #if (NGX_PCRE)
  150. ngx_http_regex_t *regex;
  151. #endif
  152. ngx_http_core_srv_conf_t *server; /* virtual name server conf */
  153. ngx_str_t name;
  154. } ngx_http_server_name_t;
  155. typedef struct {
  156. ngx_hash_combined_t names;
  157. ngx_uint_t nregex;
  158. ngx_http_server_name_t *regex;
  159. } ngx_http_virtual_names_t;
  160. struct ngx_http_addr_conf_s {
  161. /* the default server configuration for this address:port */
  162. ngx_http_core_srv_conf_t *default_server;
  163. ngx_http_virtual_names_t *virtual_names;
  164. unsigned ssl:1;
  165. unsigned http2:1;
  166. unsigned quic:1;
  167. unsigned proxy_protocol:1;
  168. };
  169. typedef struct {
  170. in_addr_t addr;
  171. ngx_http_addr_conf_t conf;
  172. } ngx_http_in_addr_t;
  173. #if (NGX_HAVE_INET6)
  174. typedef struct {
  175. struct in6_addr addr6;
  176. ngx_http_addr_conf_t conf;
  177. } ngx_http_in6_addr_t;
  178. #endif
  179. typedef struct {
  180. /* ngx_http_in_addr_t or ngx_http_in6_addr_t */
  181. void *addrs;
  182. ngx_uint_t naddrs;
  183. } ngx_http_port_t;
  184. typedef struct {
  185. ngx_int_t family;
  186. ngx_int_t type;
  187. in_port_t port;
  188. ngx_array_t addrs; /* array of ngx_http_conf_addr_t */
  189. } ngx_http_conf_port_t;
  190. typedef struct {
  191. ngx_http_listen_opt_t opt;
  192. unsigned protocols:3;
  193. unsigned protocols_set:1;
  194. unsigned protocols_changed:1;
  195. ngx_hash_t hash;
  196. ngx_hash_wildcard_t *wc_head;
  197. ngx_hash_wildcard_t *wc_tail;
  198. #if (NGX_PCRE)
  199. ngx_uint_t nregex;
  200. ngx_http_server_name_t *regex;
  201. #endif
  202. /* the default server configuration for this address:port */
  203. ngx_http_core_srv_conf_t *default_server;
  204. ngx_array_t servers; /* array of ngx_http_core_srv_conf_t */
  205. } ngx_http_conf_addr_t;
  206. typedef struct {
  207. ngx_int_t status;
  208. ngx_int_t overwrite;
  209. ngx_http_complex_value_t value;
  210. ngx_str_t args;
  211. } ngx_http_err_page_t;
  212. struct ngx_http_core_loc_conf_s {
  213. ngx_str_t name; /* location name */
  214. ngx_str_t escaped_name;
  215. #if (NGX_PCRE)
  216. ngx_http_regex_t *regex;
  217. #endif
  218. unsigned noname:1; /* "if () {}" block or limit_except */
  219. unsigned lmt_excpt:1;
  220. unsigned named:1;
  221. unsigned exact_match:1;
  222. unsigned noregex:1;
  223. unsigned auto_redirect:1;
  224. #if (NGX_HTTP_GZIP)
  225. unsigned gzip_disable_msie6:2;
  226. unsigned gzip_disable_degradation:2;
  227. #endif
  228. ngx_http_location_tree_node_t *static_locations;
  229. #if (NGX_PCRE)
  230. ngx_http_core_loc_conf_t **regex_locations;
  231. #endif
  232. /* pointer to the modules' loc_conf */
  233. void **loc_conf;
  234. uint32_t limit_except;
  235. void **limit_except_loc_conf;
  236. ngx_http_handler_pt handler;
  237. /* location name length for inclusive location with inherited alias */
  238. size_t alias;
  239. ngx_str_t root; /* root, alias */
  240. ngx_str_t post_action;
  241. ngx_array_t *root_lengths;
  242. ngx_array_t *root_values;
  243. ngx_array_t *types;
  244. ngx_hash_t types_hash;
  245. ngx_str_t default_type;
  246. off_t client_max_body_size; /* client_max_body_size */
  247. off_t directio; /* directio */
  248. off_t directio_alignment; /* directio_alignment */
  249. size_t client_body_buffer_size; /* client_body_buffer_size */
  250. size_t send_lowat; /* send_lowat */
  251. size_t postpone_output; /* postpone_output */
  252. size_t sendfile_max_chunk; /* sendfile_max_chunk */
  253. size_t read_ahead; /* read_ahead */
  254. size_t subrequest_output_buffer_size;
  255. /* subrequest_output_buffer_size */
  256. ngx_http_complex_value_t *limit_rate; /* limit_rate */
  257. ngx_http_complex_value_t *limit_rate_after; /* limit_rate_after */
  258. ngx_msec_t client_body_timeout; /* client_body_timeout */
  259. ngx_msec_t send_timeout; /* send_timeout */
  260. ngx_msec_t keepalive_time; /* keepalive_time */
  261. ngx_msec_t keepalive_timeout; /* keepalive_timeout */
  262. ngx_msec_t lingering_time; /* lingering_time */
  263. ngx_msec_t lingering_timeout; /* lingering_timeout */
  264. ngx_msec_t resolver_timeout; /* resolver_timeout */
  265. ngx_msec_t auth_delay; /* auth_delay */
  266. ngx_resolver_t *resolver; /* resolver */
  267. time_t keepalive_header; /* keepalive_timeout */
  268. ngx_uint_t keepalive_requests; /* keepalive_requests */
  269. ngx_uint_t keepalive_disable; /* keepalive_disable */
  270. ngx_uint_t satisfy; /* satisfy */
  271. ngx_uint_t lingering_close; /* lingering_close */
  272. ngx_uint_t if_modified_since; /* if_modified_since */
  273. ngx_uint_t max_ranges; /* max_ranges */
  274. ngx_uint_t client_body_in_file_only; /* client_body_in_file_only */
  275. ngx_flag_t client_body_in_single_buffer;
  276. /* client_body_in_singe_buffer */
  277. ngx_flag_t internal; /* internal */
  278. ngx_flag_t sendfile; /* sendfile */
  279. ngx_flag_t aio; /* aio */
  280. ngx_flag_t aio_write; /* aio_write */
  281. ngx_flag_t tcp_nopush; /* tcp_nopush */
  282. ngx_flag_t tcp_nodelay; /* tcp_nodelay */
  283. ngx_flag_t reset_timedout_connection; /* reset_timedout_connection */
  284. ngx_flag_t absolute_redirect; /* absolute_redirect */
  285. ngx_flag_t server_name_in_redirect; /* server_name_in_redirect */
  286. ngx_flag_t port_in_redirect; /* port_in_redirect */
  287. ngx_flag_t msie_padding; /* msie_padding */
  288. ngx_flag_t msie_refresh; /* msie_refresh */
  289. ngx_flag_t log_not_found; /* log_not_found */
  290. ngx_flag_t log_subrequest; /* log_subrequest */
  291. ngx_flag_t recursive_error_pages; /* recursive_error_pages */
  292. ngx_uint_t server_tokens; /* server_tokens */
  293. ngx_flag_t chunked_transfer_encoding; /* chunked_transfer_encoding */
  294. ngx_flag_t etag; /* etag */
  295. #if (NGX_HTTP_GZIP)
  296. ngx_flag_t gzip_vary; /* gzip_vary */
  297. ngx_uint_t gzip_http_version; /* gzip_http_version */
  298. ngx_uint_t gzip_proxied; /* gzip_proxied */
  299. #if (NGX_PCRE)
  300. ngx_array_t *gzip_disable; /* gzip_disable */
  301. #endif
  302. #endif
  303. #if (NGX_THREADS || NGX_COMPAT)
  304. ngx_thread_pool_t *thread_pool;
  305. ngx_http_complex_value_t *thread_pool_value;
  306. #endif
  307. #if (NGX_HAVE_OPENAT)
  308. ngx_uint_t disable_symlinks; /* disable_symlinks */
  309. ngx_http_complex_value_t *disable_symlinks_from;
  310. #endif
  311. ngx_array_t *error_pages; /* error_page */
  312. ngx_path_t *client_body_temp_path; /* client_body_temp_path */
  313. ngx_open_file_cache_t *open_file_cache;
  314. time_t open_file_cache_valid;
  315. ngx_uint_t open_file_cache_min_uses;
  316. ngx_flag_t open_file_cache_errors;
  317. ngx_flag_t open_file_cache_events;
  318. ngx_log_t *error_log;
  319. ngx_uint_t types_hash_max_size;
  320. ngx_uint_t types_hash_bucket_size;
  321. ngx_queue_t *locations;
  322. #if 0
  323. ngx_http_core_loc_conf_t *prev_location;
  324. #endif
  325. };
  326. typedef struct {
  327. ngx_queue_t queue;
  328. ngx_http_core_loc_conf_t *exact;
  329. ngx_http_core_loc_conf_t *inclusive;
  330. ngx_str_t *name;
  331. u_char *file_name;
  332. ngx_uint_t line;
  333. ngx_queue_t list;
  334. } ngx_http_location_queue_t;
  335. struct ngx_http_location_tree_node_s {
  336. ngx_http_location_tree_node_t *left;
  337. ngx_http_location_tree_node_t *right;
  338. ngx_http_location_tree_node_t *tree;
  339. ngx_http_core_loc_conf_t *exact;
  340. ngx_http_core_loc_conf_t *inclusive;
  341. u_short len;
  342. u_char auto_redirect;
  343. u_char name[1];
  344. };
  345. void ngx_http_core_run_phases(ngx_http_request_t *r);
  346. ngx_int_t ngx_http_core_generic_phase(ngx_http_request_t *r,
  347. ngx_http_phase_handler_t *ph);
  348. ngx_int_t ngx_http_core_rewrite_phase(ngx_http_request_t *r,
  349. ngx_http_phase_handler_t *ph);
  350. ngx_int_t ngx_http_core_find_config_phase(ngx_http_request_t *r,
  351. ngx_http_phase_handler_t *ph);
  352. ngx_int_t ngx_http_core_post_rewrite_phase(ngx_http_request_t *r,
  353. ngx_http_phase_handler_t *ph);
  354. ngx_int_t ngx_http_core_access_phase(ngx_http_request_t *r,
  355. ngx_http_phase_handler_t *ph);
  356. ngx_int_t ngx_http_core_post_access_phase(ngx_http_request_t *r,
  357. ngx_http_phase_handler_t *ph);
  358. ngx_int_t ngx_http_core_content_phase(ngx_http_request_t *r,
  359. ngx_http_phase_handler_t *ph);
  360. void *ngx_http_test_content_type(ngx_http_request_t *r, ngx_hash_t *types_hash);
  361. ngx_int_t ngx_http_set_content_type(ngx_http_request_t *r);
  362. void ngx_http_set_exten(ngx_http_request_t *r);
  363. ngx_int_t ngx_http_set_etag(ngx_http_request_t *r);
  364. void ngx_http_weak_etag(ngx_http_request_t *r);
  365. ngx_int_t ngx_http_send_response(ngx_http_request_t *r, ngx_uint_t status,
  366. ngx_str_t *ct, ngx_http_complex_value_t *cv);
  367. u_char *ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *name,
  368. size_t *root_length, size_t reserved);
  369. ngx_int_t ngx_http_auth_basic_user(ngx_http_request_t *r);
  370. #if (NGX_HTTP_GZIP)
  371. ngx_int_t ngx_http_gzip_ok(ngx_http_request_t *r);
  372. #endif
  373. ngx_int_t ngx_http_subrequest(ngx_http_request_t *r,
  374. ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,
  375. ngx_http_post_subrequest_t *ps, ngx_uint_t flags);
  376. ngx_int_t ngx_http_internal_redirect(ngx_http_request_t *r,
  377. ngx_str_t *uri, ngx_str_t *args);
  378. ngx_int_t ngx_http_named_location(ngx_http_request_t *r, ngx_str_t *name);
  379. ngx_http_cleanup_t *ngx_http_cleanup_add(ngx_http_request_t *r, size_t size);
  380. typedef ngx_int_t (*ngx_http_output_header_filter_pt)(ngx_http_request_t *r);
  381. typedef ngx_int_t (*ngx_http_output_body_filter_pt)
  382. (ngx_http_request_t *r, ngx_chain_t *chain);
  383. typedef ngx_int_t (*ngx_http_request_body_filter_pt)
  384. (ngx_http_request_t *r, ngx_chain_t *chain);
  385. ngx_int_t ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *chain);
  386. ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *chain);
  387. ngx_int_t ngx_http_request_body_save_filter(ngx_http_request_t *r,
  388. ngx_chain_t *chain);
  389. ngx_int_t ngx_http_set_disable_symlinks(ngx_http_request_t *r,
  390. ngx_http_core_loc_conf_t *clcf, ngx_str_t *path, ngx_open_file_info_t *of);
  391. ngx_int_t ngx_http_get_forwarded_addr(ngx_http_request_t *r, ngx_addr_t *addr,
  392. ngx_table_elt_t *headers, ngx_str_t *value, ngx_array_t *proxies,
  393. int recursive);
  394. ngx_int_t ngx_http_link_multi_headers(ngx_http_request_t *r);
  395. extern ngx_module_t ngx_http_core_module;
  396. extern ngx_uint_t ngx_http_max_module;
  397. extern ngx_str_t ngx_http_core_get_method;
  398. #define ngx_http_clear_content_length(r) \
  399. \
  400. r->headers_out.content_length_n = -1; \
  401. if (r->headers_out.content_length) { \
  402. r->headers_out.content_length->hash = 0; \
  403. r->headers_out.content_length = NULL; \
  404. }
  405. #define ngx_http_clear_accept_ranges(r) \
  406. \
  407. r->allow_ranges = 0; \
  408. if (r->headers_out.accept_ranges) { \
  409. r->headers_out.accept_ranges->hash = 0; \
  410. r->headers_out.accept_ranges = NULL; \
  411. }
  412. #define ngx_http_clear_last_modified(r) \
  413. \
  414. r->headers_out.last_modified_time = -1; \
  415. if (r->headers_out.last_modified) { \
  416. r->headers_out.last_modified->hash = 0; \
  417. r->headers_out.last_modified = NULL; \
  418. }
  419. #define ngx_http_clear_location(r) \
  420. \
  421. if (r->headers_out.location) { \
  422. r->headers_out.location->hash = 0; \
  423. r->headers_out.location = NULL; \
  424. }
  425. #define ngx_http_clear_etag(r) \
  426. \
  427. if (r->headers_out.etag) { \
  428. r->headers_out.etag->hash = 0; \
  429. r->headers_out.etag = NULL; \
  430. }
  431. #endif /* _NGX_HTTP_CORE_H_INCLUDED_ */